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 #include "chrome/browser/ui/tabs/existing_window_sub_menu_model.h"
6 
7 #include "chrome/app/vector_icons/vector_icons.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_list.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "ui/base/models/menu_separator_types.h"
14 #include "ui/gfx/paint_vector_icon.h"
15 #include "ui/native_theme/native_theme.h"
16 #include "ui/views/controls/menu/menu_config.h"
17 #include "ui/views/vector_icons.h"
18 
ExistingWindowSubMenuModel(ui::SimpleMenuModel::Delegate * parent_delegate,TabStripModel * model,int context_index)19 ExistingWindowSubMenuModel::ExistingWindowSubMenuModel(
20     ui::SimpleMenuModel::Delegate* parent_delegate,
21     TabStripModel* model,
22     int context_index)
23     : ExistingBaseSubMenuModel(parent_delegate,
24                                model,
25                                context_index,
26                                kMinExistingWindowCommandId) {
27   std::vector<MenuItemInfo> menu_item_infos;
28   auto window_titles = model->GetExistingWindowsForMoveMenu();
29 
30   for (auto& window_title : window_titles) {
31     menu_item_infos.emplace_back(MenuItemInfo{window_title});
32   }
33   Build(IDS_TAB_CXMENU_MOVETOANOTHERNEWWINDOW, menu_item_infos);
34 }
35 
36 ExistingWindowSubMenuModel::~ExistingWindowSubMenuModel() = default;
37 
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator) const38 bool ExistingWindowSubMenuModel::GetAcceleratorForCommandId(
39     int command_id,
40     ui::Accelerator* accelerator) const {
41   if (IsNewCommand(command_id)) {
42     return parent_delegate()->GetAcceleratorForCommandId(
43         TabStripModel::CommandMoveTabsToNewWindow, accelerator);
44   }
45   return false;
46 }
47 
IsCommandIdChecked(int command_id) const48 bool ExistingWindowSubMenuModel::IsCommandIdChecked(int command_id) const {
49   if (IsNewCommand(command_id)) {
50     return parent_delegate()->IsCommandIdChecked(
51         TabStripModel::CommandMoveTabsToNewWindow);
52   }
53   return false;
54 }
55 
IsCommandIdEnabled(int command_id) const56 bool ExistingWindowSubMenuModel::IsCommandIdEnabled(int command_id) const {
57   if (IsNewCommand(command_id)) {
58     return parent_delegate()->IsCommandIdEnabled(
59         TabStripModel::CommandMoveTabsToNewWindow);
60   }
61   return true;
62 }
63 
64 // static
ShouldShowSubmenu(Profile * profile)65 bool ExistingWindowSubMenuModel::ShouldShowSubmenu(Profile* profile) {
66   return chrome::GetTabbedBrowserCount(profile) > 1;
67 }
68 
ExecuteNewCommand(int event_flags)69 void ExistingWindowSubMenuModel::ExecuteNewCommand(int event_flags) {
70   parent_delegate()->ExecuteCommand(TabStripModel::CommandMoveTabsToNewWindow,
71                                     event_flags);
72 }
73 
ExecuteExistingCommand(int command_index)74 void ExistingWindowSubMenuModel::ExecuteExistingCommand(int command_index) {
75   model()->ExecuteAddToExistingWindowCommand(context_index(), command_index);
76 }
77