1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_UI_TABS_EXISTING_BASE_SUB_MENU_MODEL_H_
6 #define CHROME_BROWSER_UI_TABS_EXISTING_BASE_SUB_MENU_MODEL_H_
7 
8 #include <stddef.h>
9 
10 #include "base/macros.h"
11 #include "base/optional.h"
12 #include "ui/base/models/simple_menu_model.h"
13 
14 class TabStripModel;
15 
16 // Base class for creating submenus for the tab context menu. This enforces the
17 // format of the submenu as follows:
18 // - guaranteed unique IDs for different submenus
19 // - the visual layout of the submenu
20 //  - the first item of the submenu should be the option to add a tab to a new
21 //    object model (e.g. group or window)
22 //  - the next item in the menu should be a separator
23 //  - a maximum of 200 items to add a tab to an existing model
24 class ExistingBaseSubMenuModel : public ui::SimpleMenuModel,
25                                  ui::SimpleMenuModel::Delegate {
26  public:
27   ExistingBaseSubMenuModel(ui::SimpleMenuModel::Delegate* parent_delegate,
28                            TabStripModel* model,
29                            int context_index,
30                            int min_command_id);
31 
32   // ui::SimpleMenuModel
33   bool GetAcceleratorForCommandId(int command_id,
34                                   ui::Accelerator* accelerator) const override;
35 
36   // ui::SimpleMenuModel::Delegate
37   bool IsCommandIdChecked(int command_id) const override;
38   bool IsCommandIdEnabled(int command_id) const override;
39   void ExecuteCommand(int command_id, int event_flags) final;
40 
41   // Command IDs for various submenus.
42   static constexpr int kMinExistingWindowCommandId = 1001;
43   static constexpr int kMinExistingTabGroupCommandId = 1301;
44 
45   ~ExistingBaseSubMenuModel() override;
46 
47  protected:
48   struct MenuItemInfo {
49     explicit MenuItemInfo(const base::string16 menu_text);
50     MenuItemInfo(const base::string16& menu_text, ui::ImageModel menu_image);
51     MenuItemInfo(const MenuItemInfo& menu_item_info);
52     ~MenuItemInfo();
53 
54     // The text for an entry in the sub menu.
55     const base::string16 text;
56 
57     // The optional image for an entry in the sub menu.
58     base::Optional<ui::ImageModel> image;
59   };
60 
61   // Helper method to create consistent submenus.|new_text| is the label to add
62   // the tab to a new object model (e.g. group or window). |menu_item_infos| is
63   // a vector of text and optionally images for adding the tab to an existing
64   // object model.
65   void Build(int new_text, std::vector<MenuItemInfo> menu_item_infos);
66 
67   // Helper method for checking if the command is to add a tab to a new object
68   // model.
IsNewCommand(int command_id)69   bool IsNewCommand(int command_id) const {
70     return command_id == min_command_id_;
71   }
72 
73   // Performs the action for adding the tab to a new object model (e.g. group or
74   // window).
75   virtual void ExecuteNewCommand(int event_flags);
76 
77   // Performs the action for adding the tab to an existing object model (e.g.
78   // group or window).
79   virtual void ExecuteExistingCommand(int command_index);
80 
81   // Maximum number of entries for a submenu.
82   static constexpr int max_size = 200;
83 
parent_delegate()84   ui::SimpleMenuModel::Delegate* parent_delegate() const {
85     return parent_delegate_;
86   }
model()87   TabStripModel* model() { return model_; }
context_index()88   int context_index() const { return context_index_; }
89 
90  private:
91   ui::SimpleMenuModel::Delegate* parent_delegate_;
92   TabStripModel* model_;
93   int context_index_;
94   int min_command_id_;
95   DISALLOW_COPY_AND_ASSIGN(ExistingBaseSubMenuModel);
96 };
97 
98 #endif  // CHROME_BROWSER_UI_TABS_EXISTING_BASE_SUB_MENU_MODEL_H_
99