#include "tests/cefclient/browser/views_menu_bar.h"
#include "include/cef_i18n_util.h"
#include "include/views/cef_box_layout.h"
#include "include/views/cef_window.h"
#include "tests/cefclient/browser/views_style.h"
namespace client {
namespace {
const int kMenuBarGroupId = 100;
char16 ToLower(char16 c) {
CefStringUTF16 str16;
cef_string_utf16_to_lower(&c, 1, str16.GetWritableStruct());
return str16.length() > 0 ? str16.c_str()[0] : 0;
}
char16 GetMnemonic(const std::u16string& title) {
size_t index = 0;
do {
index = title.find('&', index);
if (index != std::u16string::npos) {
if (index + 1 != title.size() && title[index + 1] != '&')
return ToLower(title[index + 1]);
index++;
}
} while (index != std::u16string::npos);
return 0;
}
}
ViewsMenuBar::ViewsMenuBar(Delegate* delegate, int menu_id_start)
: delegate_(delegate),
id_start_(menu_id_start),
id_next_(menu_id_start),
last_nav_with_keyboard_(false) {
DCHECK(delegate_);
DCHECK_GT(id_start_, 0);
}
bool ViewsMenuBar::HasMenuId(int menu_id) const {
return menu_id >= id_start_ && menu_id < id_next_;
}
CefRefPtr<CefPanel> ViewsMenuBar::GetMenuPanel() {
EnsureMenuPanel();
return panel_;
}
CefRefPtr<CefMenuModel> ViewsMenuBar::CreateMenuModel(const CefString& label,
int* menu_id) {
EnsureMenuPanel();
const int new_menu_id = id_next_++;
if (menu_id)
*menu_id = new_menu_id;
CefRefPtr<CefMenuModel> model = CefMenuModel::CreateMenuModel(this);
views_style::ApplyTo(model);
models_.push_back(model);
CefRefPtr<CefMenuButton> button =
CefMenuButton::CreateMenuButton(this, label);
button->SetID(new_menu_id);
views_style::ApplyTo(button.get());
button->SetInkDropEnabled(true);
button->SetGroupID(kMenuBarGroupId);
panel_->AddChildView(button);
char16 mnemonic = GetMnemonic(label);
if (mnemonic != 0)
mnemonics_.insert(std::make_pair(mnemonic, new_menu_id));
return model;
}
CefRefPtr<CefMenuModel> ViewsMenuBar::GetMenuModel(int menu_id) const {
if (HasMenuId(menu_id))
return models_[menu_id - id_start_];
return nullptr;
}
void ViewsMenuBar::SetMenuFocusable(bool focusable) {
if (!panel_)
return;
for (int id = id_start_; id < id_next_; ++id)
panel_->GetViewForID(id)->SetFocusable(focusable);
if (focusable) {
panel_->GetViewForID(id_start_)->RequestFocus();
}
}
bool ViewsMenuBar::OnKeyEvent(const CefKeyEvent& event) {
if (!panel_)
return false;
if (event.type != KEYEVENT_RAWKEYDOWN)
return false;
if (event.modifiers & (EVENTFLAG_ALT_DOWN | EVENTFLAG_CONTROL_DOWN))
return false;
MnemonicMap::const_iterator it = mnemonics_.find(ToLower(event.character));
if (it == mnemonics_.end())
return false;
last_nav_with_keyboard_ = true;
TriggerMenuButton(panel_->GetViewForID(it->second));
return true;
}
void ViewsMenuBar::Reset() {
panel_ = nullptr;
models_.clear();
mnemonics_.clear();
id_next_ = id_start_;
}
void ViewsMenuBar::OnMenuButtonPressed(
CefRefPtr<CefMenuButton> menu_button,
const CefPoint& screen_point,
CefRefPtr<CefMenuButtonPressedLock> button_pressed_lock) {
CefRefPtr<CefMenuModel> menu_model = GetMenuModel(menu_button->GetID());
CefPoint point = screen_point;
if (CefIsRTL()) {
point.x += menu_button->GetBounds().width - 4;
} else {
point.x -= menu_button->GetBounds().width - 4;
}
bool cur_last_nav_with_keyboard = last_nav_with_keyboard_;
menu_button->ShowMenu(menu_model, point, CEF_MENU_ANCHOR_TOPLEFT);
last_nav_with_keyboard_ = cur_last_nav_with_keyboard;
}
void ViewsMenuBar::ExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
int command_id,
cef_event_flags_t event_flags) {
delegate_->MenuBarExecuteCommand(menu_model, command_id, event_flags);
}
void ViewsMenuBar::MouseOutsideMenu(CefRefPtr<CefMenuModel> menu_model,
const CefPoint& screen_point) {
DCHECK(panel_);
CefRefPtr<CefWindow> window = panel_->GetWindow();
DCHECK(window);
CefPoint window_point = screen_point;
if (!window->ConvertPointFromScreen(window_point))
return;
CefRect panel_bounds = panel_->GetBounds();
if (last_nav_with_keyboard_) {
if (panel_bounds.Contains(window_point))
return;
last_nav_with_keyboard_ = false;
}
if (!panel_bounds.Contains(window_point))
return;
const int active_menu_id = GetActiveMenuId();
for (int id = id_start_; id < id_next_; ++id) {
if (id == active_menu_id)
continue;
CefRefPtr<CefView> button = panel_->GetViewForID(id);
CefRect button_bounds = button->GetBounds();
if (CefIsRTL()) {
button_bounds.x =
panel_bounds.width - button_bounds.x - button_bounds.width;
}
if (button_bounds.Contains(window_point)) {
TriggerMenuButton(button);
break;
}
}
}
void ViewsMenuBar::UnhandledOpenSubmenu(CefRefPtr<CefMenuModel> menu_model,
bool is_rtl) {
TriggerNextMenu(is_rtl ? 1 : -1);
}
void ViewsMenuBar::UnhandledCloseSubmenu(CefRefPtr<CefMenuModel> menu_model,
bool is_rtl) {
TriggerNextMenu(is_rtl ? -1 : 1);
}
void ViewsMenuBar::MenuClosed(CefRefPtr<CefMenuModel> menu_model) {
if (!menu_model->IsSubMenu() && last_nav_with_keyboard_)
last_nav_with_keyboard_ = false;
}
void ViewsMenuBar::EnsureMenuPanel() {
if (panel_)
return;
panel_ = CefPanel::CreatePanel(nullptr);
views_style::ApplyTo(panel_);
CefBoxLayoutSettings top_panel_layout_settings;
top_panel_layout_settings.horizontal = true;
panel_->SetToBoxLayout(top_panel_layout_settings);
}
int ViewsMenuBar::GetActiveMenuId() {
DCHECK(panel_);
for (int id = id_start_; id < id_next_; ++id) {
CefRefPtr<CefButton> button = panel_->GetViewForID(id)->AsButton();
if (button->GetState() == CEF_BUTTON_STATE_PRESSED)
return id;
}
return -1;
}
void ViewsMenuBar::TriggerNextMenu(int offset) {
DCHECK(panel_);
const int active_menu_id = GetActiveMenuId();
const int menu_count = id_next_ - id_start_;
const int active_menu_index = active_menu_id - id_start_;
int next_menu_index = (active_menu_index + offset) % menu_count;
if (next_menu_index < 0)
next_menu_index += menu_count;
panel_->GetWindow()->CancelMenu();
last_nav_with_keyboard_ = true;
TriggerMenuButton(panel_->GetViewForID(id_start_ + next_menu_index));
}
void ViewsMenuBar::TriggerMenuButton(CefRefPtr<CefView> button) {
CefRefPtr<CefMenuButton> menu_button =
button->AsButton()->AsLabelButton()->AsMenuButton();
if (menu_button->IsFocusable())
menu_button->RequestFocus();
menu_button->TriggerMenu();
}
}