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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/tabs/existing_window_sub_menu_model.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_menu_model_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_delegate.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/accelerators/accelerator.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/tabs/existing_window_sub_menu_model_chromeos.h"
#endif
namespace {
// The max length for a window title.
static constexpr int kWindowTitleForMenuMaxWidth = 400;
} // namespace
// static:
std::unique_ptr<ExistingWindowSubMenuModel> ExistingWindowSubMenuModel::Create(
ui::SimpleMenuModel::Delegate* parent_delegate,
TabMenuModelDelegate* tab_menu_model_delegate,
TabStripModel* model,
int context_index) {
#if BUILDFLAG(IS_CHROMEOS)
return std::make_unique<chromeos::ExistingWindowSubMenuModelChromeOS>(
GetPassKey(), parent_delegate, tab_menu_model_delegate, model,
context_index);
#else
return std::make_unique<ExistingWindowSubMenuModel>(
GetPassKey(), parent_delegate, tab_menu_model_delegate, model,
context_index);
#endif
}
ExistingWindowSubMenuModel::ExistingWindowSubMenuModel(
base::PassKey<ExistingWindowSubMenuModel> passkey,
ui::SimpleMenuModel::Delegate* parent_delegate,
TabMenuModelDelegate* tab_menu_model_delegate,
TabStripModel* model,
int context_index)
: ExistingBaseSubMenuModel(parent_delegate,
model,
context_index,
kMinExistingWindowCommandId,
TabStripModel::CommandMoveTabsToNewWindow) {
Build(IDS_TAB_CXMENU_MOVETOANOTHERNEWWINDOW,
BuildMenuItemInfoVectorForBrowsers(
tab_menu_model_delegate->GetOtherBrowserWindows(
model->delegate()->IsForWebApp())));
}
ExistingWindowSubMenuModel::~ExistingWindowSubMenuModel() = default;
bool ExistingWindowSubMenuModel::GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const {
if (IsNewCommand(command_id)) {
return parent_delegate()->GetAcceleratorForCommandId(
TabStripModel::CommandMoveTabsToNewWindow, accelerator);
}
return false;
}
bool ExistingWindowSubMenuModel::IsCommandIdChecked(int command_id) const {
if (IsNewCommand(command_id)) {
return parent_delegate()->IsCommandIdChecked(
TabStripModel::CommandMoveTabsToNewWindow);
}
return false;
}
bool ExistingWindowSubMenuModel::IsCommandIdEnabled(int command_id) const {
if (IsNewCommand(command_id)) {
return parent_delegate()->IsCommandIdEnabled(
TabStripModel::CommandMoveTabsToNewWindow);
}
return true;
}
// static:
bool ExistingWindowSubMenuModel::ShouldShowSubmenu(Profile* profile) {
return chrome::GetTabbedBrowserCount(profile) > 1;
}
bool ExistingWindowSubMenuModel::ShouldShowSubmenuForApp(
TabMenuModelDelegate* tab_menu_model_delegate) {
return tab_menu_model_delegate->GetOtherBrowserWindows(/*is_app=*/true)
.size() >= 1;
}
// static:
base::PassKey<ExistingWindowSubMenuModel>
ExistingWindowSubMenuModel::GetPassKey() {
return base::PassKey<ExistingWindowSubMenuModel>();
}
// static:
std::vector<ExistingBaseSubMenuModel::MenuItemInfo>
ExistingWindowSubMenuModel::BuildMenuItemInfoVectorForBrowsers(
const std::vector<Browser*>& existing_browsers) {
std::vector<MenuItemInfo> menu_item_infos;
for (size_t i = 0; i < existing_browsers.size(); ++i) {
Browser* browser = existing_browsers[i];
auto window_title =
browser->GetWindowTitleForMaxWidth(kWindowTitleForMenuMaxWidth);
menu_item_infos.emplace_back(window_title);
menu_item_infos.back().may_have_mnemonics = false;
menu_item_infos.back().target_index = i;
}
return menu_item_infos;
}
void ExistingWindowSubMenuModel::ExecuteExistingCommand(size_t target_index) {
model()->ExecuteAddToExistingWindowCommand(GetContextIndex(), target_index);
}
|