| // Copyright 2019 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/ui/views/frame/webui_tab_strip_container_view.h" |
| |
| #include <utility> |
| |
| #include "base/logging.h" |
| #include "chrome/app/chrome_command_ids.h" |
| #include "chrome/app/vector_icons/vector_icons.h" |
| #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/task_manager/web_contents_tags.h" |
| #include "chrome/browser/ui/browser.h" |
| #include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/layout_constants.h" |
| #include "chrome/browser/ui/view_ids.h" |
| #include "chrome/browser/ui/views/toolbar/toolbar_button.h" |
| #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui.h" |
| #include "chrome/common/webui_url_constants.h" |
| #include "chrome/grit/generated_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #include "ui/gfx/color_palette.h" |
| #include "ui/gfx/paint_vector_icon.h" |
| #include "ui/views/background.h" |
| #include "ui/views/controls/menu/menu_runner.h" |
| #include "ui/views/controls/webview/webview.h" |
| #include "ui/views/layout/fill_layout.h" |
| #include "ui/views/layout/flex_layout.h" |
| #include "ui/views/view_class_properties.h" |
| |
| WebUITabStripContainerView::WebUITabStripContainerView(Browser* browser) |
| : browser_(browser), |
| web_view_( |
| AddChildView(std::make_unique<views::WebView>(browser->profile()))) { |
| SetVisible(false); |
| // TODO(crbug.com/1010589) WebContents are initially assumed to be visible by |
| // default unless explicitly hidden. The WebContents need to be set to hidden |
| // so that the visibility state of the document in JavaScript is correctly |
| // initially set to 'hidden', and the 'visibilitychange' events correctly get |
| // fired. |
| web_view_->GetWebContents()->WasHidden(); |
| |
| SetLayoutManager(std::make_unique<views::FillLayout>()); |
| web_view_->LoadInitialURL(GURL(chrome::kChromeUITabStripURL)); |
| extensions::ChromeExtensionWebContentsObserver::CreateForWebContents( |
| web_view_->web_contents()); |
| task_manager::WebContentsTags::CreateForTabContents( |
| web_view_->web_contents()); |
| |
| TabStripUI* tab_strip_ui = static_cast<TabStripUI*>( |
| web_view_->GetWebContents()->GetWebUI()->GetController()); |
| tab_strip_ui->Initialize(browser_, this); |
| } |
| |
| WebUITabStripContainerView::~WebUITabStripContainerView() = default; |
| |
| views::NativeViewHost* WebUITabStripContainerView::GetNativeViewHost() { |
| return web_view_->holder(); |
| } |
| |
| std::unique_ptr<ToolbarButton> |
| WebUITabStripContainerView::CreateNewTabButton() { |
| auto new_tab_button = std::make_unique<ToolbarButton>(this); |
| new_tab_button->SetID(VIEW_ID_WEBUI_TAB_STRIP_NEW_TAB_BUTTON); |
| new_tab_button->SetTooltipText( |
| l10n_util::GetStringUTF16(IDS_TOOLTIP_NEW_TAB)); |
| return new_tab_button; |
| } |
| |
| // TODO(crbug.com/992972): Replace this button with tab counter. Consider |
| // replacing the "toggle" string with a separate show/hide tooltip string. |
| std::unique_ptr<ToolbarButton> |
| WebUITabStripContainerView::CreateToggleButton() { |
| auto toggle_button = std::make_unique<ToolbarButton>(this); |
| toggle_button->SetID(VIEW_ID_WEBUI_TAB_STRIP_TOGGLE_BUTTON); |
| toggle_button->SetTooltipText( |
| l10n_util::GetStringUTF16(IDS_TOOLTIP_WEBUI_TAB_STRIP_TOGGLE_BUTTON)); |
| return toggle_button; |
| } |
| |
| void WebUITabStripContainerView::ShowContextMenuAtPoint( |
| gfx::Point point, |
| std::unique_ptr<ui::MenuModel> menu_model) { |
| ConvertPointToScreen(this, &point); |
| context_menu_model_ = std::move(menu_model); |
| context_menu_runner_ = std::make_unique<views::MenuRunner>( |
| context_menu_model_.get(), |
| views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU); |
| context_menu_runner_->RunMenuAt( |
| GetWidget(), nullptr, gfx::Rect(point, gfx::Size()), |
| views::MenuAnchorPosition::kTopLeft, ui::MENU_SOURCE_MOUSE); |
| } |
| |
| int WebUITabStripContainerView::GetHeightForWidth(int w) const { |
| constexpr int kWebUITabStripHeightDp = 248; |
| return kWebUITabStripHeightDp; |
| } |
| |
| void WebUITabStripContainerView::ButtonPressed(views::Button* sender, |
| const ui::Event& event) { |
| if (sender->GetID() == VIEW_ID_WEBUI_TAB_STRIP_TOGGLE_BUTTON) { |
| // TODO(pbos): Trigger a slide animation here. |
| SetVisible(!GetVisible()); |
| InvalidateLayout(); |
| } else if (sender->GetID() == VIEW_ID_WEBUI_TAB_STRIP_NEW_TAB_BUTTON) { |
| chrome::ExecuteCommand(browser_, IDC_NEW_TAB); |
| } else { |
| NOTREACHED(); |
| } |
| } |