File: menu_model_adapter.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,132 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_
#define UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_

#include <map>
#include <memory>

#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/models/menu_model_delegate.h"
#include "ui/views/controls/menu/menu_delegate.h"
#include "ui/views/style/typography.h"

namespace ui {
class MenuModel;
}

namespace views {
class MenuItemView;

// This class wraps an instance of ui::MenuModel with the views::MenuDelegate
// interface required by views::MenuItemView.
class VIEWS_EXPORT MenuModelAdapter : public MenuDelegate,
                                      public ui::MenuModelDelegate {
 public:
  // The caller retains ownership of the ui::MenuModel instance and must ensure
  // it exists for the lifetime of the adapter. |this| will become the new
  // MenuModelDelegate of |menu_model| so that subsequent changes to it get
  // reflected in the created MenuItemView.
  explicit MenuModelAdapter(
      ui::MenuModel* menu_model,
      base::RepeatingClosure on_menu_closed_callback = base::NullCallback());

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

  ~MenuModelAdapter() override;

  // Populate a MenuItemView menu with the ui::MenuModel items
  // (including submenus).
  virtual void BuildMenu(MenuItemView* menu);

  // Creates, populates and returns a menu. Note that a raw pointer it kept
  // internally to be able to update the `MenuItemView` as response to calls to
  // `MenuModelDelegate::OnMenuStructureChanged()`.
  // returned MenuItemView.
  std::unique_ptr<MenuItemView> CreateMenu();

  void set_triggerable_event_flags(int triggerable_event_flags) {
    triggerable_event_flags_ = triggerable_event_flags;
  }
  int triggerable_event_flags() const { return triggerable_event_flags_; }

  // Creates a menu item for the specified entry in the model and adds it as
  // a child to |menu| at the specified |menu_index|.
  static MenuItemView* AddMenuItemFromModelAt(ui::MenuModel* model,
                                              size_t model_index,
                                              MenuItemView* menu,
                                              size_t menu_index,
                                              int item_id);

  // Creates a menu item for the specified entry in the model and appends it as
  // a child to |menu|.
  static MenuItemView* AppendMenuItemFromModel(ui::MenuModel* model,
                                               size_t model_index,
                                               MenuItemView* menu,
                                               int item_id);

  // MenuModelDelegate:
  void OnIconChanged(int command_id) override;
  void OnMenuStructureChanged() override;
  void OnMenuClearingDelegate() override;

 protected:
  // Create and add a menu item to |menu| for the item at index |index| in
  // |model|. Subclasses override this to allow custom items to be added to the
  // menu.
  virtual MenuItemView* AppendMenuItem(MenuItemView* menu,
                                       ui::MenuModel* model,
                                       size_t index);

  // views::MenuDelegate implementation.
  void ExecuteCommand(int id) override;
  void ExecuteCommand(int id, int mouse_event_flags) override;
  bool IsTriggerableEvent(MenuItemView* source, const ui::Event& e) override;
  bool GetAccelerator(int id, ui::Accelerator* accelerator) const override;
  std::u16string GetLabel(int id) const override;
  const gfx::FontList* GetLabelFontList(int id) const override;
  bool IsCommandEnabled(int id) const override;
  bool IsCommandVisible(int id) const override;
  bool IsItemChecked(int id) const override;
  void WillShowMenu(MenuItemView* menu) override;
  void WillHideMenu(MenuItemView* menu) override;
  void OnMenuClosed(MenuItemView* menu) override;
  std::optional<SkColor> GetLabelColor(int command_id) const override;
  bool IsTearingDown() const override;

 private:
  // Implementation of BuildMenu().
  void BuildMenuImpl(MenuItemView* menu, ui::MenuModel* model);

  // Container of ui::MenuModel pointers as encountered by preorder
  // traversal.  The first element is always the top-level model
  // passed to the constructor.
  raw_ptr<ui::MenuModel, DanglingUntriaged> menu_model_;

  // Pointer to the `MenuItemView` created and updated by `this`, but not owned
  // by `this`.
  raw_ptr<MenuItemView, DanglingUntriaged> menu_ = nullptr;

  // Mouse event flags which can trigger menu actions.
  int triggerable_event_flags_;

  // Map MenuItems to MenuModels.  Used to implement WillShowMenu().
  std::map<MenuItemView*, raw_ptr<ui::MenuModel, CtnExperimental>> menu_map_;

  // Optional callback triggered during OnMenuClosed().
  base::RepeatingClosure on_menu_closed_callback_;
};

}  // namespace views

#endif  // UI_VIEWS_CONTROLS_MENU_MENU_MODEL_ADAPTER_H_