chrome: Support usage of the Chrome toolbar from Views (see issue #2969)

This commit is contained in:
Marshall Greenblatt
2021-04-11 16:10:11 -04:00
parent 9c82785077
commit a4603c6f1a
55 changed files with 1057 additions and 156 deletions

View File

@ -11,6 +11,7 @@
#include "include/cef_app.h"
#include "include/views/cef_box_layout.h"
#include "include/wrapper/cef_helpers.h"
#include "tests/cefclient/browser/main_context.h"
#include "tests/cefclient/browser/resource.h"
#include "tests/cefclient/browser/views_style.h"
#include "tests/shared/browser/extension_util.h"
@ -235,7 +236,7 @@ void ViewsWindow::SetLoadingState(bool isLoading,
bool canGoBack,
bool canGoForward) {
CEF_REQUIRE_UI_THREAD();
if (!window_ || !with_controls_)
if (!window_ || !with_controls_ || chrome_toolbar_type_ == CEF_CTT_NORMAL)
return;
EnableView(ID_BACK_BUTTON, canGoBack);
@ -274,8 +275,12 @@ void ViewsWindow::TakeFocus(bool next) {
if (!window_ || !with_controls_)
return;
// Give focus to the URL textfield.
window_->GetViewForID(ID_URL_TEXTFIELD)->RequestFocus();
if (chrome_toolbar_type_ == CEF_CTT_NORMAL) {
top_toolbar_->RequestFocus();
} else {
// Give focus to the URL textfield.
location_->RequestFocus();
}
}
void ViewsWindow::OnBeforeContextMenu(CefRefPtr<CefMenuModel> model) {
@ -362,6 +367,10 @@ bool ViewsWindow::OnPopupBrowserViewCreated(
return true;
}
CefBrowserViewDelegate::ChromeToolbarType ViewsWindow::GetChromeToolbarType() {
return chrome_toolbar_type_;
}
void ViewsWindow::OnButtonPressed(CefRefPtr<CefButton> button) {
CEF_REQUIRE_UI_THREAD();
DCHECK(with_controls_);
@ -665,6 +674,120 @@ void ViewsWindow::OnBlur(CefRefPtr<CefView> view) {
}
}
void ViewsWindow::OnWindowChanged(CefRefPtr<CefView> view, bool added) {
if (!with_controls_ || !added)
return;
const int view_id = view->GetID();
if (view_id != ID_BROWSER_VIEW)
return;
// Build the remainder of the UI now that the BrowserView has been added to
// the CefWindow. This is a requirement to use Chrome toolbars.
CefRefPtr<CefPanel> top_menu_panel;
if (top_menu_bar_)
top_menu_panel = top_menu_bar_->GetMenuPanel();
LabelButtons browse_buttons;
CefRefPtr<CefMenuButton> menu_button;
if (chrome_toolbar_type_ == CEF_CTT_NORMAL) {
// Chrome will provide a normal toolbar with location, menu, etc.
top_toolbar_ = browser_view_->GetChromeToolbar();
DCHECK(top_toolbar_);
}
if (!top_toolbar_) {
// Create the browse buttons.
browse_buttons.push_back(CreateBrowseButton("Back", ID_BACK_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Forward", ID_FORWARD_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Reload", ID_RELOAD_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Stop", ID_STOP_BUTTON));
if (chrome_toolbar_type_ == CEF_CTT_LOCATION) {
// Chrome will provide a minimal location bar.
location_ = browser_view_->GetChromeToolbar();
DCHECK(location_);
}
if (!location_) {
// Create the URL textfield.
CefRefPtr<CefTextfield> url_textfield =
CefTextfield::CreateTextfield(this);
url_textfield->SetID(ID_URL_TEXTFIELD);
url_textfield->SetEnabled(false); // Disabled by default.
views_style::ApplyTo(url_textfield);
location_ = url_textfield;
}
// Create the menu button.
menu_button = CefMenuButton::CreateMenuButton(this, CefString());
menu_button->SetID(ID_MENU_BUTTON);
menu_button->SetImage(
CEF_BUTTON_STATE_NORMAL,
delegate_->GetImageCache()->GetCachedImage("menu_icon"));
views_style::ApplyTo(menu_button.get());
menu_button->SetInkDropEnabled(true);
// Override the default minimum size.
menu_button->SetMinimumSize(CefSize(0, 0));
// Create the top panel.
CefRefPtr<CefPanel> top_panel = CefPanel::CreatePanel(nullptr);
// Use a horizontal box layout for |top_panel|.
CefBoxLayoutSettings top_panel_layout_settings;
top_panel_layout_settings.horizontal = true;
CefRefPtr<CefBoxLayout> top_panel_layout =
top_panel->SetToBoxLayout(top_panel_layout_settings);
// Add the buttons and URL textfield to |top_panel|.
for (size_t i = 0U; i < browse_buttons.size(); ++i)
top_panel->AddChildView(browse_buttons[i]);
top_panel->AddChildView(location_);
UpdateExtensionControls();
DCHECK(extensions_panel_);
top_panel->AddChildView(extensions_panel_);
top_panel->AddChildView(menu_button);
views_style::ApplyTo(top_panel);
// Allow |location| to grow and fill any remaining space.
top_panel_layout->SetFlexForView(location_, 1);
top_toolbar_ = top_panel;
}
// Add the top panel and browser view to |window|.
int top_index = 0;
if (top_menu_panel)
window_->AddChildViewAt(top_menu_panel, top_index++);
window_->AddChildViewAt(top_toolbar_, top_index);
// Lay out |window| so we can get the default button sizes.
window_->Layout();
int min_width = 200;
if (!browse_buttons.empty()) {
// Make all browse buttons the same size.
MakeButtonsSameSize(browse_buttons);
// Lay out |window| again with the new button sizes.
window_->Layout();
// Minimum window width is the size of all buttons plus some extra.
min_width = browse_buttons[0]->GetBounds().width * 4 +
menu_button->GetBounds().width + 100;
}
// Minimum window height is the hight of the top toolbar plus some extra.
int min_height = top_toolbar_->GetBounds().height + 100;
if (top_menu_panel)
min_height += top_menu_panel->GetBounds().height;
minimum_window_size_ = CefSize(min_width, min_height);
}
void ViewsWindow::MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
int command_id,
cef_event_flags_t event_flags) {
@ -686,6 +809,20 @@ ViewsWindow::ViewsWindow(Delegate* delegate,
frameless_ = command_line->HasSwitch(switches::kHideFrame) ||
delegate_->WithExtension();
if (MainContext::Get()->UseChromeRuntime()) {
const std::string& toolbar_type =
command_line->GetSwitchValue(switches::kShowChromeToolbar);
if (toolbar_type == "none") {
chrome_toolbar_type_ = CEF_CTT_NONE;
} else if (toolbar_type == "location") {
chrome_toolbar_type_ = CEF_CTT_LOCATION;
} else {
chrome_toolbar_type_ = CEF_CTT_NORMAL;
}
} else {
chrome_toolbar_type_ = CEF_CTT_NONE;
}
#if !defined(OS_MAC)
// On Mac we don't show a top menu on the window. The options are available in
// the app menu instead.
@ -736,59 +873,6 @@ void ViewsWindow::AddControls() {
// Create the MenuModel that will be displayed via the menu button.
CreateMenuModel();
CefRefPtr<CefPanel> top_menu_panel;
if (top_menu_bar_)
top_menu_panel = top_menu_bar_->GetMenuPanel();
// Create the browse buttons.
LabelButtons browse_buttons;
browse_buttons.push_back(CreateBrowseButton("Back", ID_BACK_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Forward", ID_FORWARD_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Reload", ID_RELOAD_BUTTON));
browse_buttons.push_back(CreateBrowseButton("Stop", ID_STOP_BUTTON));
// Create the URL textfield.
CefRefPtr<CefTextfield> url_textfield = CefTextfield::CreateTextfield(this);
url_textfield->SetID(ID_URL_TEXTFIELD);
url_textfield->SetEnabled(false); // Disabled by default.
views_style::ApplyTo(url_textfield);
// Create the menu button.
CefRefPtr<CefMenuButton> menu_button =
CefMenuButton::CreateMenuButton(this, CefString());
menu_button->SetID(ID_MENU_BUTTON);
menu_button->SetImage(
CEF_BUTTON_STATE_NORMAL,
delegate_->GetImageCache()->GetCachedImage("menu_icon"));
views_style::ApplyTo(menu_button.get());
menu_button->SetInkDropEnabled(true);
// Override the default minimum size.
menu_button->SetMinimumSize(CefSize(0, 0));
// Create the top panel.
CefRefPtr<CefPanel> top_panel = CefPanel::CreatePanel(nullptr);
// Use a horizontal box layout for |top_panel|.
CefBoxLayoutSettings top_panel_layout_settings;
top_panel_layout_settings.horizontal = true;
CefRefPtr<CefBoxLayout> top_panel_layout =
top_panel->SetToBoxLayout(top_panel_layout_settings);
// Add the buttons and URL textfield to |top_panel|.
for (size_t i = 0U; i < browse_buttons.size(); ++i)
top_panel->AddChildView(browse_buttons[i]);
top_panel->AddChildView(url_textfield);
UpdateExtensionControls();
DCHECK(extensions_panel_);
top_panel->AddChildView(extensions_panel_);
top_panel->AddChildView(menu_button);
views_style::ApplyTo(top_panel);
// Allow |url_textfield| to grow and fill any remaining space.
top_panel_layout->SetFlexForView(url_textfield, 1);
// Use a vertical box layout for |window|.
CefBoxLayoutSettings window_layout_settings;
window_layout_settings.horizontal = false;
@ -796,33 +880,14 @@ void ViewsWindow::AddControls() {
CefRefPtr<CefBoxLayout> window_layout =
window_->SetToBoxLayout(window_layout_settings);
// Add the top panel and browser view to |window|.
if (top_menu_panel)
window_->AddChildView(top_menu_panel);
window_->AddChildView(top_panel);
window_->AddChildView(browser_view_);
// Allow |browser_view_| to grow and fill any remaining space.
window_layout->SetFlexForView(browser_view_, 1);
// Lay out |window| so we can get the default button sizes.
window_->Layout();
// Make all browse buttons the same size.
MakeButtonsSameSize(browse_buttons);
// Lay out |window| again with the new button sizes.
window_->Layout();
// Minimum window width is the size of all buttons plus some extra.
const int min_width = browse_buttons[0]->GetBounds().width * 4 +
menu_button->GetBounds().width + 100;
// Minimum window height is the hight of the top toolbar plus some extra.
int min_height = top_panel->GetBounds().height + 100;
if (top_menu_panel)
min_height += top_menu_panel->GetBounds().height;
minimum_window_size_ = CefSize(min_width, min_height);
// Remaining setup will be performed in OnWindowChanged after the BrowserView
// is added to the CefWindow. This is necessary because Chrome toolbars are
// only available after the BrowserView is added.
}
void ViewsWindow::AddAccelerators() {
@ -864,12 +929,10 @@ void ViewsWindow::ShowTopControls(bool show) {
if (!window_ || !with_controls_)
return;
// Change the visibility of the panel that contains the buttons.
CefRefPtr<CefView> parent_view =
window_->GetViewForID(ID_BACK_BUTTON)->GetParentView();
if (parent_view->IsVisible() != show) {
parent_view->SetVisible(show);
parent_view->InvalidateLayout();
// Change the visibility of the top toolbar.
if (top_toolbar_->IsVisible() != show) {
top_toolbar_->SetVisible(show);
top_toolbar_->InvalidateLayout();
}
}

View File

@ -128,6 +128,7 @@ class ViewsWindow : public CefBrowserViewDelegate,
bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
CefRefPtr<CefBrowserView> popup_browser_view,
bool is_devtools) OVERRIDE;
ChromeToolbarType GetChromeToolbarType() override;
// CefButtonDelegate methods:
void OnButtonPressed(CefRefPtr<CefButton> button) OVERRIDE;
@ -164,6 +165,7 @@ class ViewsWindow : public CefBrowserViewDelegate,
CefSize GetMinimumSize(CefRefPtr<CefView> view) OVERRIDE;
void OnFocus(CefRefPtr<CefView> view) OVERRIDE;
void OnBlur(CefRefPtr<CefView> view) OVERRIDE;
void OnWindowChanged(CefRefPtr<CefView> view, bool added) OVERRIDE;
// ViewsMenuBar::Delegate methods:
void MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
@ -208,10 +210,13 @@ class ViewsWindow : public CefBrowserViewDelegate,
CefRefPtr<CefBrowserView> browser_view_;
bool frameless_;
bool with_controls_;
ChromeToolbarType chrome_toolbar_type_;
CefRefPtr<CefWindow> window_;
CefRefPtr<CefMenuModel> button_menu_model_;
CefRefPtr<ViewsMenuBar> top_menu_bar_;
CefRefPtr<CefView> top_toolbar_;
CefRefPtr<CefView> location_;
bool menu_has_focus_;
int last_focused_view_;

View File

@ -45,6 +45,7 @@ const char kCRLSetsPath[] = "crl-sets-path";
const char kLoadExtension[] = "load-extension";
const char kNoActivate[] = "no-activate";
const char kEnableChromeRuntime[] = "enable-chrome-runtime";
const char kShowChromeToolbar[] = "show-chrome-toolbar";
} // namespace switches
} // namespace client

View File

@ -39,6 +39,7 @@ extern const char kCRLSetsPath[];
extern const char kLoadExtension[];
extern const char kNoActivate[];
extern const char kEnableChromeRuntime[];
extern const char kShowChromeToolbar[];
} // namespace switches
} // namespace client