File: status_icon_menu_model.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,798 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_
#define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_

#include <map>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "ui/base/models/image_model.h"
#include "ui/menus/simple_menu_model.h"

namespace gfx {
class Image;
}

// StatusIconMenuModel contains the state of the SimpleMenuModel as well as that
// of its delegate. This is done so that we can easily identify when the menu
// model state has changed and can tell the status icon to update the menu. This
// is necessary some platforms which do not notify us before showing the menu
// (like Ubuntu Unity).
class StatusIconMenuModel : public ui::SimpleMenuModel,
                            public ui::SimpleMenuModel::Delegate {
 public:
  class Delegate {
   public:
    // Performs the action associates with the specified command id.
    // The passed |event_flags| are the flags from the event which issued this
    // command and they can be examined to find modifier keys.
    virtual void ExecuteCommand(int command_id, int event_flags) = 0;

   protected:
    virtual ~Delegate() = default;
  };

  class Observer {
   public:
    // Invoked when the menu model has changed.
    virtual void OnMenuStateChanged() {}

   protected:
    virtual ~Observer() = default;
  };

  // The Delegate can be NULL.
  explicit StatusIconMenuModel(Delegate* delegate);

  StatusIconMenuModel(const StatusIconMenuModel&) = delete;
  StatusIconMenuModel& operator=(const StatusIconMenuModel&) = delete;

  ~StatusIconMenuModel() override;

  // Methods for seting the state of specific command ids.
  void SetCommandIdChecked(int command_id, bool checked);
  void SetCommandIdEnabled(int command_id, bool enabled);
  void SetCommandIdVisible(int command_id, bool visible);

  // Sets the accelerator for the specified command id.
  void SetAcceleratorForCommandId(
      int command_id, const ui::Accelerator* accelerator);

  // Calling any of these "change" methods will mark the menu item as "dynamic"
  // (see menu_model.h:IsItemDynamicAt) which many platforms take as a cue to
  // refresh the label and icon of the menu item each time the menu is
  // shown.
  void ChangeLabelForCommandId(int command_id, const std::u16string& label);
  void ChangeIconForCommandId(int command_id, const gfx::Image& icon);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Overridden from ui::SimpleMenuModel::Delegate:
  bool IsCommandIdChecked(int command_id) const override;
  bool IsCommandIdEnabled(int command_id) const override;
  bool IsCommandIdVisible(int command_id) const override;
  bool GetAcceleratorForCommandId(int command_id,
                                  ui::Accelerator* accelerator) const override;
  bool IsItemForCommandIdDynamic(int command_id) const override;
  std::u16string GetLabelForCommandId(int command_id) const override;
  ui::ImageModel GetIconForCommandId(int command_id) const override;
  void ExecuteCommand(int command_id, int event_flags) override;

 protected:
  // Overriden from ui::SimpleMenuModel:
  void MenuItemsChanged() override;

  void NotifyMenuStateChanged();

  void set_delegate(Delegate* delegate) { delegate_ = delegate; }
  Delegate* delegate() { return delegate_; }

 private:
  struct ItemState;

  // Map the properties to the command id (used as key).
  typedef std::map<int, ItemState> ItemStateMap;

  ItemStateMap item_states_;

  base::ObserverList<Observer>::Unchecked observer_list_;

  raw_ptr<Delegate, DanglingUntriaged> delegate_;
};

#endif  // CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_MENU_MODEL_H_