mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
chrome: Support customization of context menus (see issue #2969)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "libcef/browser/chrome/chrome_browser_main_extra_parts_cef.h"
|
||||
|
||||
#include "libcef/browser/chrome/chrome_context_menu_handler.h"
|
||||
#include "libcef/browser/context.h"
|
||||
#include "libcef/browser/net/chrome_scheme_handler.h"
|
||||
|
||||
@@ -34,4 +35,5 @@ void ChromeBrowserMainExtraPartsCef::PreMainMessageLoopRun() {
|
||||
base::TaskShutdownBehavior::BLOCK_SHUTDOWN, base::MayBlock()});
|
||||
|
||||
scheme::RegisterWebUIControllerFactory();
|
||||
context_menu::RegisterMenuCreatedCallback();
|
||||
}
|
||||
|
207
libcef/browser/chrome/chrome_context_menu_handler.cc
Normal file
207
libcef/browser/chrome/chrome_context_menu_handler.cc
Normal file
@@ -0,0 +1,207 @@
|
||||
// Copyright (c) 2021 The Chromium Embedded Framework 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 "libcef/browser/chrome/chrome_context_menu_handler.h"
|
||||
|
||||
#include "libcef/browser/browser_host_base.h"
|
||||
#include "libcef/browser/context_menu_params_impl.h"
|
||||
#include "libcef/browser/simple_menu_model_impl.h"
|
||||
|
||||
#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
|
||||
|
||||
namespace context_menu {
|
||||
|
||||
namespace {
|
||||
|
||||
// Lifespan is controlled by RenderViewContextMenu.
|
||||
class CefContextMenuObserver : public RenderViewContextMenuObserver,
|
||||
public CefSimpleMenuModelImpl::StateDelegate {
|
||||
public:
|
||||
CefContextMenuObserver(RenderViewContextMenu* context_menu,
|
||||
CefRefPtr<CefBrowserHostBase> browser,
|
||||
CefRefPtr<CefContextMenuHandler> handler)
|
||||
: context_menu_(context_menu), browser_(browser), handler_(handler) {}
|
||||
|
||||
// RenderViewContextMenuObserver methods:
|
||||
|
||||
void InitMenu(const content::ContextMenuParams& params) override {
|
||||
params_ = new CefContextMenuParamsImpl(
|
||||
const_cast<content::ContextMenuParams*>(&context_menu_->params()));
|
||||
model_ = new CefSimpleMenuModelImpl(
|
||||
const_cast<ui::SimpleMenuModel*>(&context_menu_->menu_model()),
|
||||
context_menu_, this, /*is_owned=*/false, /*is_popup=*/false);
|
||||
|
||||
handler_->OnBeforeContextMenu(browser_, GetFrame(), params_, model_);
|
||||
}
|
||||
|
||||
bool IsCommandIdSupported(int command_id) override {
|
||||
// Always claim support for the reserved user ID range.
|
||||
if (command_id >= MENU_ID_USER_FIRST && command_id <= MENU_ID_USER_LAST)
|
||||
return true;
|
||||
|
||||
// Also claim support in specific cases where an ItemInfo exists.
|
||||
return GetItemInfo(command_id) != nullptr;
|
||||
}
|
||||
|
||||
// Only called if IsCommandIdSupported() returns true.
|
||||
bool IsCommandIdEnabled(int command_id) override {
|
||||
// Always return true to use the SimpleMenuModel state.
|
||||
return true;
|
||||
}
|
||||
|
||||
// Only called if IsCommandIdSupported() returns true.
|
||||
bool IsCommandIdChecked(int command_id) override {
|
||||
auto* info = GetItemInfo(command_id);
|
||||
return info ? info->checked : false;
|
||||
}
|
||||
|
||||
// Only called if IsCommandIdSupported() returns true.
|
||||
bool GetAccelerator(int command_id, ui::Accelerator* accel) override {
|
||||
auto* info = GetItemInfo(command_id);
|
||||
if (info && info->accel) {
|
||||
*accel = *info->accel;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CommandWillBeExecuted(int command_id) override {
|
||||
if (handler_->OnContextMenuCommand(browser_, GetFrame(), params_,
|
||||
command_id, EVENTFLAG_NONE)) {
|
||||
// Create an ItemInfo so that we get the ExecuteCommand() callback
|
||||
// instead of the default handler.
|
||||
GetOrCreateItemInfo(command_id);
|
||||
}
|
||||
}
|
||||
|
||||
// Only called if IsCommandIdSupported() returns true.
|
||||
void ExecuteCommand(int command_id) override {
|
||||
auto* info = GetItemInfo(command_id);
|
||||
if (info) {
|
||||
// In case it was added in CommandWillBeExecuted().
|
||||
MaybeDeleteItemInfo(command_id, info);
|
||||
}
|
||||
}
|
||||
|
||||
void OnMenuClosed() override {
|
||||
handler_->OnContextMenuDismissed(browser_, GetFrame());
|
||||
model_->Detach();
|
||||
|
||||
// Clear stored state because this object won't be deleted until a new
|
||||
// context menu is created or the associated browser is destroyed.
|
||||
browser_ = nullptr;
|
||||
handler_ = nullptr;
|
||||
params_ = nullptr;
|
||||
model_ = nullptr;
|
||||
iteminfomap_.clear();
|
||||
}
|
||||
|
||||
// CefSimpleMenuModelImpl::StateDelegate methods:
|
||||
|
||||
void SetChecked(int command_id, bool checked) override {
|
||||
// No-op if already at the default state.
|
||||
if (!checked && !GetItemInfo(command_id))
|
||||
return;
|
||||
|
||||
auto* info = GetOrCreateItemInfo(command_id);
|
||||
info->checked = checked;
|
||||
if (!checked)
|
||||
MaybeDeleteItemInfo(command_id, info);
|
||||
}
|
||||
|
||||
void SetAccelerator(int command_id,
|
||||
base::Optional<ui::Accelerator> accel) override {
|
||||
// No-op if already at the default state.
|
||||
if (!accel && !GetItemInfo(command_id))
|
||||
return;
|
||||
|
||||
auto* info = GetOrCreateItemInfo(command_id);
|
||||
info->accel = accel;
|
||||
if (!accel)
|
||||
MaybeDeleteItemInfo(command_id, info);
|
||||
}
|
||||
|
||||
private:
|
||||
struct ItemInfo {
|
||||
ItemInfo() {}
|
||||
|
||||
bool checked = false;
|
||||
base::Optional<ui::Accelerator> accel;
|
||||
};
|
||||
|
||||
ItemInfo* GetItemInfo(int command_id) {
|
||||
auto it = iteminfomap_.find(command_id);
|
||||
if (it != iteminfomap_.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ItemInfo* GetOrCreateItemInfo(int command_id) {
|
||||
if (auto info = GetItemInfo(command_id))
|
||||
return info;
|
||||
|
||||
auto result = iteminfomap_.insert(std::make_pair(command_id, ItemInfo()));
|
||||
return &result.first->second;
|
||||
}
|
||||
|
||||
void MaybeDeleteItemInfo(int command_id, ItemInfo* info) {
|
||||
// Remove if all info has reverted to the default state.
|
||||
if (!info->checked && !info->accel) {
|
||||
auto it = iteminfomap_.find(command_id);
|
||||
iteminfomap_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
CefRefPtr<CefFrame> GetFrame() const {
|
||||
CefRefPtr<CefFrame> frame;
|
||||
|
||||
// May return nullptr if the frame is destroyed while the menu is pending.
|
||||
auto* rfh = context_menu_->GetRenderFrameHost();
|
||||
if (rfh) {
|
||||
frame = browser_->GetFrameForHost(rfh);
|
||||
}
|
||||
if (!frame) {
|
||||
frame = browser_->GetMainFrame();
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
RenderViewContextMenu* const context_menu_;
|
||||
CefRefPtr<CefBrowserHostBase> browser_;
|
||||
CefRefPtr<CefContextMenuHandler> handler_;
|
||||
CefRefPtr<CefContextMenuParams> params_;
|
||||
CefRefPtr<CefSimpleMenuModelImpl> model_;
|
||||
|
||||
// Map of command_id to ItemInfo.
|
||||
using ItemInfoMap = std::map<int, ItemInfo>;
|
||||
ItemInfoMap iteminfomap_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefContextMenuObserver);
|
||||
};
|
||||
|
||||
std::unique_ptr<RenderViewContextMenuObserver> MenuCreatedCallback(
|
||||
RenderViewContextMenu* context_menu) {
|
||||
auto browser = CefBrowserHostBase::GetBrowserForContents(
|
||||
context_menu->source_web_contents());
|
||||
if (browser) {
|
||||
if (auto client = browser->GetClient()) {
|
||||
if (auto handler = client->GetContextMenuHandler()) {
|
||||
return std::make_unique<CefContextMenuObserver>(context_menu, browser,
|
||||
handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void RegisterMenuCreatedCallback() {
|
||||
RenderViewContextMenu::RegisterMenuCreatedCallback(
|
||||
base::BindRepeating(&MenuCreatedCallback));
|
||||
}
|
||||
|
||||
} // namespace context_menu
|
16
libcef/browser/chrome/chrome_context_menu_handler.h
Normal file
16
libcef/browser/chrome/chrome_context_menu_handler.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2021 The Chromium Embedded Framework Authors. All rights
|
||||
// reserved. Use of this source code is governed by a BSD-style license that can
|
||||
// be found in the LICENSE file.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_CHROME_CHROME_CONTEXT_MENU_HANDLER_H_
|
||||
#define CEF_LIBCEF_BROWSER_CHROME_CHROME_CONTEXT_MENU_HANDLER_H_
|
||||
#pragma once
|
||||
|
||||
namespace context_menu {
|
||||
|
||||
// Register the context menu created callback.
|
||||
void RegisterMenuCreatedCallback();
|
||||
|
||||
} // namespace context_menu
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_CHROME_CHROME_CONTEXT_MENU_HANDLER_H_
|
529
libcef/browser/simple_menu_model_impl.cc
Normal file
529
libcef/browser/simple_menu_model_impl.cc
Normal file
@@ -0,0 +1,529 @@
|
||||
// Copyright (c) 2021 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright (c) 2012 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 "libcef/browser/simple_menu_model_impl.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "libcef/browser/thread_util.h"
|
||||
#include "libcef/common/task_runner_impl.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Value based on the documentation on SimpleMenuModel::GetIndexOfCommandId().
|
||||
const int kInvalidIndex = -1;
|
||||
const int kInvalidCommandId = -1;
|
||||
const int kInvalidGroupId = -1;
|
||||
|
||||
cef_menu_item_type_t GetCefItemType(ui::MenuModel::ItemType type) {
|
||||
switch (type) {
|
||||
case ui::MenuModel::TYPE_COMMAND:
|
||||
return MENUITEMTYPE_COMMAND;
|
||||
case ui::MenuModel::TYPE_CHECK:
|
||||
return MENUITEMTYPE_CHECK;
|
||||
case ui::MenuModel::TYPE_RADIO:
|
||||
return MENUITEMTYPE_RADIO;
|
||||
case ui::MenuModel::TYPE_SEPARATOR:
|
||||
return MENUITEMTYPE_SEPARATOR;
|
||||
case ui::MenuModel::TYPE_SUBMENU:
|
||||
return MENUITEMTYPE_SUBMENU;
|
||||
default:
|
||||
return MENUITEMTYPE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CefSimpleMenuModelImpl::CefSimpleMenuModelImpl(
|
||||
ui::SimpleMenuModel* model,
|
||||
ui::SimpleMenuModel::Delegate* delegate,
|
||||
StateDelegate* state_delegate,
|
||||
bool is_owned,
|
||||
bool is_submenu)
|
||||
: supported_thread_id_(base::PlatformThread::CurrentId()),
|
||||
model_(model),
|
||||
delegate_(delegate),
|
||||
state_delegate_(state_delegate),
|
||||
is_owned_(is_owned),
|
||||
is_submenu_(is_submenu) {
|
||||
DCHECK(model_);
|
||||
DCHECK(delegate_);
|
||||
DCHECK(state_delegate_);
|
||||
}
|
||||
|
||||
CefSimpleMenuModelImpl::~CefSimpleMenuModelImpl() {
|
||||
// Detach() must be called before object destruction.
|
||||
DCHECK(!model_);
|
||||
DCHECK(submenumap_.empty());
|
||||
}
|
||||
|
||||
void CefSimpleMenuModelImpl::Detach() {
|
||||
DCHECK(VerifyContext());
|
||||
|
||||
if (!submenumap_.empty()) {
|
||||
auto it = submenumap_.begin();
|
||||
for (; it != submenumap_.end(); ++it) {
|
||||
it->second->Detach();
|
||||
}
|
||||
submenumap_.clear();
|
||||
}
|
||||
|
||||
if (is_owned_)
|
||||
delete model_;
|
||||
model_ = nullptr;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsSubMenu() {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
return is_submenu_;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::Clear() {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->Clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
int CefSimpleMenuModelImpl::GetCount() {
|
||||
if (!VerifyContext())
|
||||
return 0;
|
||||
|
||||
return model_->GetItemCount();
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::AddSeparator() {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->AddSeparator(ui::NORMAL_SEPARATOR);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::AddItem(int command_id, const CefString& label) {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->AddItem(command_id, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::AddCheckItem(int command_id,
|
||||
const CefString& label) {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->AddCheckItem(command_id, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::AddRadioItem(int command_id,
|
||||
const CefString& label,
|
||||
int group_id) {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->AddRadioItem(command_id, label, group_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
CefRefPtr<CefMenuModel> CefSimpleMenuModelImpl::AddSubMenu(
|
||||
int command_id,
|
||||
const CefString& label) {
|
||||
if (!VerifyContext())
|
||||
return nullptr;
|
||||
|
||||
auto new_menu = CreateNewSubMenu(nullptr);
|
||||
model_->AddSubMenu(command_id, label, new_menu->model());
|
||||
return new_menu;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::InsertSeparatorAt(int index) {
|
||||
if (!VerifyContext())
|
||||
return false;
|
||||
|
||||
model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::InsertItemAt(int index,
|
||||
int command_id,
|
||||
const CefString& label) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->InsertItemAt(index, command_id, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::InsertCheckItemAt(int index,
|
||||
int command_id,
|
||||
const CefString& label) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->InsertCheckItemAt(index, command_id, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::InsertRadioItemAt(int index,
|
||||
int command_id,
|
||||
const CefString& label,
|
||||
int group_id) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->InsertRadioItemAt(index, command_id, label, group_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
CefRefPtr<CefMenuModel> CefSimpleMenuModelImpl::InsertSubMenuAt(
|
||||
int index,
|
||||
int command_id,
|
||||
const CefString& label) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return nullptr;
|
||||
|
||||
auto new_menu = CreateNewSubMenu(nullptr);
|
||||
model_->InsertSubMenuAt(index, command_id, label, new_menu->model());
|
||||
return new_menu;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::Remove(int command_id) {
|
||||
return RemoveAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::RemoveAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
auto* sub_menu =
|
||||
static_cast<ui::SimpleMenuModel*>(model_->GetSubmenuModelAt(index));
|
||||
if (sub_menu) {
|
||||
auto it = submenumap_.find(sub_menu);
|
||||
if (it != submenumap_.end()) {
|
||||
it->second->Detach();
|
||||
submenumap_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
model_->RemoveItemAt(index);
|
||||
return true;
|
||||
}
|
||||
|
||||
int CefSimpleMenuModelImpl::GetIndexOf(int command_id) {
|
||||
if (!VerifyContext())
|
||||
return kInvalidIndex;
|
||||
|
||||
return model_->GetIndexOfCommandId(command_id);
|
||||
}
|
||||
|
||||
int CefSimpleMenuModelImpl::GetCommandIdAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return kInvalidCommandId;
|
||||
|
||||
return model_->GetCommandIdAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetCommandIdAt(int index, int command_id) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
CefString CefSimpleMenuModelImpl::GetLabel(int command_id) {
|
||||
return GetLabelAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
CefString CefSimpleMenuModelImpl::GetLabelAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return CefString();
|
||||
|
||||
return model_->GetLabelAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetLabel(int command_id, const CefString& label) {
|
||||
return SetLabelAt(GetIndexOf(command_id), label);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetLabelAt(int index, const CefString& label) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->SetLabel(index, label);
|
||||
return true;
|
||||
}
|
||||
|
||||
CefSimpleMenuModelImpl::MenuItemType CefSimpleMenuModelImpl::GetType(
|
||||
int command_id) {
|
||||
return GetTypeAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
CefSimpleMenuModelImpl::MenuItemType CefSimpleMenuModelImpl::GetTypeAt(
|
||||
int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return MENUITEMTYPE_NONE;
|
||||
|
||||
return GetCefItemType(model_->GetTypeAt(index));
|
||||
}
|
||||
|
||||
int CefSimpleMenuModelImpl::GetGroupId(int command_id) {
|
||||
return GetGroupIdAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
int CefSimpleMenuModelImpl::GetGroupIdAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return kInvalidGroupId;
|
||||
|
||||
return model_->GetGroupIdAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetGroupId(int command_id, int group_id) {
|
||||
return SetGroupIdAt(GetIndexOf(command_id), group_id);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetGroupIdAt(int index, int group_id) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
CefRefPtr<CefMenuModel> CefSimpleMenuModelImpl::GetSubMenu(int command_id) {
|
||||
return GetSubMenuAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
CefRefPtr<CefMenuModel> CefSimpleMenuModelImpl::GetSubMenuAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return nullptr;
|
||||
|
||||
auto* sub_model =
|
||||
static_cast<ui::SimpleMenuModel*>(model_->GetSubmenuModelAt(index));
|
||||
auto it = submenumap_.find(sub_model);
|
||||
if (it != submenumap_.end())
|
||||
return it->second;
|
||||
return CreateNewSubMenu(sub_model);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsVisible(int command_id) {
|
||||
return IsVisibleAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsVisibleAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
return model_->IsVisibleAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetVisible(int command_id, bool visible) {
|
||||
return SetVisibleAt(GetIndexOf(command_id), visible);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetVisibleAt(int index, bool visible) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->SetVisibleAt(index, visible);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsEnabled(int command_id) {
|
||||
return IsEnabledAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsEnabledAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
return model_->IsEnabledAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetEnabled(int command_id, bool enabled) {
|
||||
return SetEnabledAt(GetIndexOf(command_id), enabled);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetEnabledAt(int index, bool enabled) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
model_->SetEnabledAt(index, enabled);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsChecked(int command_id) {
|
||||
return IsCheckedAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::IsCheckedAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
return model_->IsItemCheckedAt(index);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetChecked(int command_id, bool checked) {
|
||||
if (!VerifyContext() || command_id == kInvalidIndex)
|
||||
return false;
|
||||
|
||||
state_delegate_->SetChecked(command_id, checked);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetCheckedAt(int index, bool checked) {
|
||||
return SetChecked(GetCommandIdAt(index), checked);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::HasAccelerator(int command_id) {
|
||||
return HasAcceleratorAt(GetIndexOf(command_id));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::HasAcceleratorAt(int index) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
ui::Accelerator accelerator;
|
||||
return model_->GetAcceleratorAt(index, &accelerator);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetAccelerator(int command_id,
|
||||
int key_code,
|
||||
bool shift_pressed,
|
||||
bool ctrl_pressed,
|
||||
bool alt_pressed) {
|
||||
if (!VerifyContext() || command_id == kInvalidIndex)
|
||||
return false;
|
||||
|
||||
int modifiers = 0;
|
||||
if (shift_pressed)
|
||||
modifiers |= ui::EF_SHIFT_DOWN;
|
||||
if (ctrl_pressed)
|
||||
modifiers |= ui::EF_CONTROL_DOWN;
|
||||
if (alt_pressed)
|
||||
modifiers |= ui::EF_ALT_DOWN;
|
||||
|
||||
state_delegate_->SetAccelerator(
|
||||
command_id,
|
||||
ui::Accelerator(static_cast<ui::KeyboardCode>(key_code), modifiers));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetAcceleratorAt(int index,
|
||||
int key_code,
|
||||
bool shift_pressed,
|
||||
bool ctrl_pressed,
|
||||
bool alt_pressed) {
|
||||
return SetAccelerator(GetCommandIdAt(index), key_code, shift_pressed,
|
||||
ctrl_pressed, alt_pressed);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::RemoveAccelerator(int command_id) {
|
||||
if (!VerifyContext() || command_id == kInvalidIndex)
|
||||
return false;
|
||||
state_delegate_->SetAccelerator(command_id, base::nullopt);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::RemoveAcceleratorAt(int index) {
|
||||
return RemoveAccelerator(GetCommandIdAt(index));
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::GetAccelerator(int command_id,
|
||||
int& key_code,
|
||||
bool& shift_pressed,
|
||||
bool& ctrl_pressed,
|
||||
bool& alt_pressed) {
|
||||
return GetAcceleratorAt(GetIndexOf(command_id), key_code, shift_pressed,
|
||||
ctrl_pressed, alt_pressed);
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::GetAcceleratorAt(int index,
|
||||
int& key_code,
|
||||
bool& shift_pressed,
|
||||
bool& ctrl_pressed,
|
||||
bool& alt_pressed) {
|
||||
if (!VerifyContext() || !ValidIndex(index))
|
||||
return false;
|
||||
|
||||
ui::Accelerator accel;
|
||||
if (model_->GetAcceleratorAt(index, &accel)) {
|
||||
key_code = accel.key_code();
|
||||
shift_pressed = accel.modifiers() & ui::EF_SHIFT_DOWN;
|
||||
ctrl_pressed = accel.modifiers() & ui::EF_CONTROL_DOWN;
|
||||
alt_pressed = accel.modifiers() & ui::EF_ALT_DOWN;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetColor(int command_id,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t color) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetColorAt(int index,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t color) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::GetColor(int command_id,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t& color) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::GetColorAt(int index,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t& color) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetFontList(int command_id,
|
||||
const CefString& font_list) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::SetFontListAt(int index,
|
||||
const CefString& font_list) {
|
||||
NOTIMPLEMENTED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::VerifyContext() {
|
||||
if (base::PlatformThread::CurrentId() != supported_thread_id_) {
|
||||
// This object should only be accessed from the thread that created it.
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!model_)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CefSimpleMenuModelImpl::ValidIndex(int index) {
|
||||
return index > kInvalidIndex && index < model_->GetItemCount();
|
||||
}
|
||||
|
||||
CefRefPtr<CefSimpleMenuModelImpl> CefSimpleMenuModelImpl::CreateNewSubMenu(
|
||||
ui::SimpleMenuModel* model) {
|
||||
bool is_owned = false;
|
||||
if (!model) {
|
||||
model = new ui::SimpleMenuModel(delegate_);
|
||||
is_owned = true;
|
||||
}
|
||||
|
||||
CefRefPtr<CefSimpleMenuModelImpl> new_impl = new CefSimpleMenuModelImpl(
|
||||
model, delegate_, state_delegate_, is_owned, /*is_submodel=*/true);
|
||||
submenumap_.insert(std::make_pair(model, new_impl));
|
||||
return new_impl;
|
||||
}
|
166
libcef/browser/simple_menu_model_impl.h
Normal file
166
libcef/browser/simple_menu_model_impl.h
Normal file
@@ -0,0 +1,166 @@
|
||||
// Copyright (c) 2021 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright (c) 2012 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_
|
||||
#define CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "include/cef_menu_model.h"
|
||||
|
||||
#include "base/threading/platform_thread.h"
|
||||
#include "ui/base/models/simple_menu_model.h"
|
||||
|
||||
// Implementation of CefMenuModel that wraps an existing ui::SimpleMenuModel.
|
||||
class CefSimpleMenuModelImpl : public CefMenuModel {
|
||||
public:
|
||||
// Interface for setting state using CefMenuModel methods that will later be
|
||||
// retrieved via the ui::SimpleMenuModel::Delegate implementation.
|
||||
class StateDelegate {
|
||||
public:
|
||||
virtual void SetChecked(int command_id, bool checked) = 0;
|
||||
virtual void SetAccelerator(int command_id,
|
||||
base::Optional<ui::Accelerator> accel) = 0;
|
||||
|
||||
protected:
|
||||
virtual ~StateDelegate() {}
|
||||
};
|
||||
|
||||
// |delegate| should be the same that was used to create |model|.
|
||||
// If |is_owned| is true then |model| will be deleted on Detach().
|
||||
CefSimpleMenuModelImpl(ui::SimpleMenuModel* model,
|
||||
ui::SimpleMenuModel::Delegate* delegate,
|
||||
StateDelegate* state_delegate,
|
||||
bool is_owned,
|
||||
bool is_submenu);
|
||||
~CefSimpleMenuModelImpl() override;
|
||||
|
||||
// Must be called before the object is deleted.
|
||||
void Detach();
|
||||
|
||||
// CefMenuModel methods.
|
||||
bool IsSubMenu() override;
|
||||
bool Clear() override;
|
||||
int GetCount() override;
|
||||
bool AddSeparator() override;
|
||||
bool AddItem(int command_id, const CefString& label) override;
|
||||
bool AddCheckItem(int command_id, const CefString& label) override;
|
||||
bool AddRadioItem(int command_id,
|
||||
const CefString& label,
|
||||
int group_id) override;
|
||||
CefRefPtr<CefMenuModel> AddSubMenu(int command_id,
|
||||
const CefString& label) override;
|
||||
bool InsertSeparatorAt(int index) override;
|
||||
bool InsertItemAt(int index, int command_id, const CefString& label) override;
|
||||
bool InsertCheckItemAt(int index,
|
||||
int command_id,
|
||||
const CefString& label) override;
|
||||
bool InsertRadioItemAt(int index,
|
||||
int command_id,
|
||||
const CefString& label,
|
||||
int group_id) override;
|
||||
CefRefPtr<CefMenuModel> InsertSubMenuAt(int index,
|
||||
int command_id,
|
||||
const CefString& label) override;
|
||||
bool Remove(int command_id) override;
|
||||
bool RemoveAt(int index) override;
|
||||
int GetIndexOf(int command_id) override;
|
||||
int GetCommandIdAt(int index) override;
|
||||
bool SetCommandIdAt(int index, int command_id) override;
|
||||
CefString GetLabel(int command_id) override;
|
||||
CefString GetLabelAt(int index) override;
|
||||
bool SetLabel(int command_id, const CefString& label) override;
|
||||
bool SetLabelAt(int index, const CefString& label) override;
|
||||
MenuItemType GetType(int command_id) override;
|
||||
MenuItemType GetTypeAt(int index) override;
|
||||
int GetGroupId(int command_id) override;
|
||||
int GetGroupIdAt(int index) override;
|
||||
bool SetGroupId(int command_id, int group_id) override;
|
||||
bool SetGroupIdAt(int index, int group_id) override;
|
||||
CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override;
|
||||
CefRefPtr<CefMenuModel> GetSubMenuAt(int index) override;
|
||||
bool IsVisible(int command_id) override;
|
||||
bool IsVisibleAt(int index) override;
|
||||
bool SetVisible(int command_id, bool visible) override;
|
||||
bool SetVisibleAt(int index, bool visible) override;
|
||||
bool IsEnabled(int command_id) override;
|
||||
bool IsEnabledAt(int index) override;
|
||||
bool SetEnabled(int command_id, bool enabled) override;
|
||||
bool SetEnabledAt(int index, bool enabled) override;
|
||||
bool IsChecked(int command_id) override;
|
||||
bool IsCheckedAt(int index) override;
|
||||
bool SetChecked(int command_id, bool checked) override;
|
||||
bool SetCheckedAt(int index, bool checked) override;
|
||||
bool HasAccelerator(int command_id) override;
|
||||
bool HasAcceleratorAt(int index) override;
|
||||
bool SetAccelerator(int command_id,
|
||||
int key_code,
|
||||
bool shift_pressed,
|
||||
bool ctrl_pressed,
|
||||
bool alt_pressed) override;
|
||||
bool SetAcceleratorAt(int index,
|
||||
int key_code,
|
||||
bool shift_pressed,
|
||||
bool ctrl_pressed,
|
||||
bool alt_pressed) override;
|
||||
bool RemoveAccelerator(int command_id) override;
|
||||
bool RemoveAcceleratorAt(int index) override;
|
||||
bool GetAccelerator(int command_id,
|
||||
int& key_code,
|
||||
bool& shift_pressed,
|
||||
bool& ctrl_pressed,
|
||||
bool& alt_pressed) override;
|
||||
bool GetAcceleratorAt(int index,
|
||||
int& key_code,
|
||||
bool& shift_pressed,
|
||||
bool& ctrl_pressed,
|
||||
bool& alt_pressed) override;
|
||||
bool SetColor(int command_id,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t color) override;
|
||||
bool SetColorAt(int index,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t color) override;
|
||||
bool GetColor(int command_id,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t& color) override;
|
||||
bool GetColorAt(int index,
|
||||
cef_menu_color_type_t color_type,
|
||||
cef_color_t& color) override;
|
||||
bool SetFontList(int command_id, const CefString& font_list) override;
|
||||
bool SetFontListAt(int index, const CefString& font_list) override;
|
||||
|
||||
ui::SimpleMenuModel* model() const { return model_; }
|
||||
|
||||
private:
|
||||
// Verify that the object is attached and being accessed from the correct
|
||||
// thread.
|
||||
bool VerifyContext();
|
||||
|
||||
// Returns true if |index| is valid.
|
||||
bool ValidIndex(int index);
|
||||
|
||||
CefRefPtr<CefSimpleMenuModelImpl> CreateNewSubMenu(
|
||||
ui::SimpleMenuModel* model);
|
||||
|
||||
base::PlatformThreadId supported_thread_id_;
|
||||
|
||||
ui::SimpleMenuModel* model_;
|
||||
ui::SimpleMenuModel::Delegate* const delegate_;
|
||||
StateDelegate* const state_delegate_;
|
||||
const bool is_owned_;
|
||||
const bool is_submenu_;
|
||||
|
||||
// Keep the submenus alive until they're removed, or we're destroyed.
|
||||
using SubMenuMap =
|
||||
std::map<ui::SimpleMenuModel*, CefRefPtr<CefSimpleMenuModelImpl>>;
|
||||
SubMenuMap submenumap_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefSimpleMenuModelImpl);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefSimpleMenuModelImpl);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_
|
Reference in New Issue
Block a user