views: Support styling of menus (issue #2102)

This commit is contained in:
Marshall Greenblatt
2017-02-25 00:03:31 -05:00
parent b08f0ed713
commit 8fa8af357b
23 changed files with 1124 additions and 65 deletions

View File

@@ -11,8 +11,8 @@
#include "include/views/cef_box_layout.h"
#include "include/wrapper/cef_helpers.h"
#include "include/cef_app.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/resource_util.h"
#include "tests/shared/common/client_switches.h"
@@ -28,6 +28,7 @@ namespace {
// Control IDs for Views in the top-level Window.
enum ControlIds {
ID_WINDOW = 1,
ID_BROWSER_VIEW,
ID_BACK_BUTTON,
ID_FORWARD_BUTTON,
ID_STOP_BUTTON,
@@ -81,11 +82,12 @@ void AddTestMenuItems(CefRefPtr<CefMenuModel> test_menu) {
test_menu->AddItem(ID_TESTS_OTHER_TESTS, "Other Tests");
}
void AddFileMenuItems(CefRefPtr<CefMenuModel> file_menu, int offset) {
void AddFileMenuItems(CefRefPtr<CefMenuModel> file_menu) {
file_menu->AddItem(ID_QUIT, "E&xit");
// Show the accelerator shortcut text in the menu.
file_menu->SetAcceleratorAt(offset, 'X', false, false, true);
file_menu->SetAcceleratorAt(file_menu->GetCount() - 1,
'X', false, false, true);
}
} // namespace
@@ -243,6 +245,12 @@ void ViewsWindow::TakeFocus(bool next) {
window_->GetViewForID(ID_URL_TEXTFIELD)->RequestFocus();
}
void ViewsWindow::OnBeforeContextMenu(CefRefPtr<CefMenuModel> model) {
CEF_REQUIRE_UI_THREAD();
views_style::ApplyTo(model);
}
bool ViewsWindow::OnPopupBrowserViewCreated(
CefRefPtr<CefBrowserView> browser_view,
CefRefPtr<CefBrowserView> popup_browser_view,
@@ -380,7 +388,7 @@ void ViewsWindow::OnWindowCreated(CefRefPtr<CefWindow> window) {
}
// Set the background color for regions that are not obscured by other Views.
window_->SetBackgroundColor(MainContext::Get()->GetBackgroundColor());
views_style::ApplyTo(window_.get());
if (with_controls_) {
// Add the BrowserView and other controls to the Window.
@@ -452,8 +460,18 @@ bool ViewsWindow::OnKeyEvent(CefRefPtr<CefWindow> window,
return false;
if (event.type == KEYEVENT_RAWKEYDOWN && event.windows_key_code == VK_MENU) {
// ALT key is pressed. Make the menu buttons focusable.
SetMenuFocusable(true);
// ALT key is pressed.
int last_focused_view = last_focused_view_;
bool menu_had_focus = menu_has_focus_;
// Toggle menu button focusable.
SetMenuFocusable(!menu_has_focus_);
if (menu_had_focus && last_focused_view != 0) {
// Restore focus to the view that was previously focused.
window_->GetViewForID(last_focused_view)->RequestFocus();
}
return true;
}
@@ -475,9 +493,16 @@ CefSize ViewsWindow::GetMinimumSize(CefRefPtr<CefView> view) {
void ViewsWindow::OnFocus(CefRefPtr<CefView> view) {
CEF_REQUIRE_UI_THREAD();
const int view_id = view->GetID();
// Keep track of the non-menu view that was last focused.
if (last_focused_view_ != view_id &&
(!top_menu_bar_ || !top_menu_bar_->HasMenuId(view_id))) {
last_focused_view_ = view_id;
}
// When focus leaves the menu buttons make them unfocusable.
if (menu_has_focus_) {
const int view_id = view->GetID();
if (top_menu_bar_) {
if (!top_menu_bar_->HasMenuId(view_id))
SetMenuFocusable(false);
@@ -497,7 +522,8 @@ ViewsWindow::ViewsWindow(Delegate* delegate,
CefRefPtr<CefBrowserView> browser_view)
: delegate_(delegate),
with_controls_(false),
menu_has_focus_(false) {
menu_has_focus_(false),
last_focused_view_(false) {
DCHECK(delegate_);
if (browser_view)
SetBrowserView(browser_view);
@@ -506,8 +532,9 @@ ViewsWindow::ViewsWindow(Delegate* delegate,
CefCommandLine::GetGlobalCommandLine();
frameless_ = command_line->HasSwitch(switches::kHideFrame);
if (!command_line->HasSwitch(switches::kHideTopMenu))
if (!command_line->HasSwitch(switches::kHideTopMenu)) {
top_menu_bar_ = new ViewsMenuBar(this, ID_TOP_MENU_FIRST);
}
}
void ViewsWindow::SetBrowserView(CefRefPtr<CefBrowserView> browser_view) {
@@ -516,6 +543,7 @@ void ViewsWindow::SetBrowserView(CefRefPtr<CefBrowserView> browser_view) {
DCHECK(browser_view->IsValid());
DCHECK(!browser_view->IsAttached());
browser_view_ = browser_view;
browser_view_->SetID(ID_BROWSER_VIEW);
}
void ViewsWindow::CreateMenuModel() {
@@ -523,12 +551,13 @@ void ViewsWindow::CreateMenuModel() {
button_menu_model_ = CefMenuModel::CreateMenuModel(this);
CefRefPtr<CefMenuModel> test_menu =
button_menu_model_->AddSubMenu(0, "&Tests");
views_style::ApplyTo(button_menu_model_);
AddTestMenuItems(test_menu);
AddFileMenuItems(button_menu_model_, 1);
AddFileMenuItems(button_menu_model_);
if (top_menu_bar_) {
// Add the menus to the top menu bar.
AddFileMenuItems(top_menu_bar_->CreateMenuModel("&File", NULL), 0);
AddFileMenuItems(top_menu_bar_->CreateMenuModel("&File", NULL));
AddTestMenuItems(top_menu_bar_->CreateMenuModel("&Tests", NULL));
}
}
@@ -536,11 +565,23 @@ void ViewsWindow::CreateMenuModel() {
CefRefPtr<CefLabelButton> ViewsWindow::CreateBrowseButton(
const std::string& label,
int id) {
// The default framed button image resources (IDR_BUTTON_*) look pretty bad
// with non-default background colors, so we'll use frameless buttons with
// ink drop when a background color is specified.
const bool with_frame = !views_style::IsSet();
CefRefPtr<CefLabelButton> button =
CefLabelButton::CreateLabelButton(this, label, true);
CefLabelButton::CreateLabelButton(this, label, with_frame);
button->SetID(id);
button->SetEnabled(false); // Disabled by default.
button->SetFocusable(false); // Don't give focus to the button.
if (!with_frame) {
views_style::ApplyTo(button);
button->SetInkDropEnabled(true);
button->SetHorizontalAlignment(CEF_HORIZONTAL_ALIGNMENT_CENTER);
}
return button;
}
@@ -563,12 +604,14 @@ void ViewsWindow::AddControls() {
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(), false, false);
menu_button->SetID(ID_MENU_BUTTON);
menu_button->SetImage(CEF_BUTTON_STATE_NORMAL, LoadImageIcon("menu_icon"));
views_style::ApplyTo(menu_button.get());
menu_button->SetInkDropEnabled(true);
// Override the default minimum size.
menu_button->SetMinimumSize(CefSize(0, 0));
@@ -587,6 +630,7 @@ void ViewsWindow::AddControls() {
top_panel->AddChildView(browse_buttons[i]);
top_panel->AddChildView(url_textfield);
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);