2012-04-19 22:31:46 +02:00
|
|
|
// Copyright (c) 2012 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_MENU_MODEL_IMPL_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "include/cef_menu_model.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "include/cef_menu_model_delegate.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
#include "base/threading/platform_thread.h"
|
2021-03-04 23:36:57 +01:00
|
|
|
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom-forward.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
#include "ui/base/models/menu_model.h"
|
2017-02-25 06:03:31 +01:00
|
|
|
#include "ui/gfx/font_list.h"
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
class CefMenuModelImpl : public CefMenuModel {
|
|
|
|
public:
|
|
|
|
class Delegate {
|
|
|
|
public:
|
|
|
|
// Perform the action associated with the specified |command_id| and
|
|
|
|
// optional |event_flags|.
|
|
|
|
virtual void ExecuteCommand(CefRefPtr<CefMenuModelImpl> source,
|
|
|
|
int command_id,
|
2017-05-17 11:29:28 +02:00
|
|
|
cef_event_flags_t event_flags) = 0;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
// Called when the user moves the mouse outside the menu and over the owning
|
|
|
|
// window.
|
|
|
|
virtual void MouseOutsideMenu(CefRefPtr<CefMenuModelImpl> source,
|
|
|
|
const gfx::Point& screen_point) {}
|
|
|
|
|
|
|
|
// Called on unhandled open/close submenu keyboard commands. |is_rtl| will
|
|
|
|
// be true if the menu is displaying a right-to-left language.
|
|
|
|
virtual void UnhandledOpenSubmenu(CefRefPtr<CefMenuModelImpl> source,
|
|
|
|
bool is_rtl) {}
|
|
|
|
virtual void UnhandledCloseSubmenu(CefRefPtr<CefMenuModelImpl> source,
|
|
|
|
bool is_rtl) {}
|
|
|
|
|
|
|
|
// Called when the menu is about to show.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void MenuWillShow(CefRefPtr<CefMenuModelImpl> source) = 0;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
// Called when the menu has closed.
|
2017-05-17 11:29:28 +02:00
|
|
|
virtual void MenuClosed(CefRefPtr<CefMenuModelImpl> source) = 0;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2014-05-23 19:47:07 +02:00
|
|
|
// Allows the delegate to modify a menu item label before it's displayed.
|
2017-02-22 19:05:27 +01:00
|
|
|
virtual bool FormatLabel(CefRefPtr<CefMenuModelImpl> source,
|
2021-04-21 00:52:34 +02:00
|
|
|
std::u16string& label) = 0;
|
2014-05-23 19:47:07 +02:00
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
protected:
|
|
|
|
virtual ~Delegate() {}
|
|
|
|
};
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
// Either |delegate| or |menu_model_delegate| must be non-nullptr.
|
|
|
|
// If |delegate| is non-nullptr it must outlive this class.
|
|
|
|
CefMenuModelImpl(Delegate* delegate,
|
2017-02-22 19:05:27 +01:00
|
|
|
CefRefPtr<CefMenuModelDelegate> menu_model_delegate,
|
|
|
|
bool is_submenu);
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
CefMenuModelImpl(const CefMenuModelImpl&) = delete;
|
|
|
|
CefMenuModelImpl& operator=(const CefMenuModelImpl&) = delete;
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
~CefMenuModelImpl() override;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
// CefMenuModel methods.
|
2017-02-22 19:05:27 +01:00
|
|
|
bool IsSubMenu() override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool Clear() override;
|
2022-07-25 19:49:32 +02:00
|
|
|
size_t GetCount() override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool AddSeparator() override;
|
|
|
|
bool AddItem(int command_id, const CefString& label) override;
|
|
|
|
bool AddCheckItem(int command_id, const CefString& label) override;
|
2017-05-17 11:29:28 +02:00
|
|
|
bool AddRadioItem(int command_id,
|
|
|
|
const CefString& label,
|
|
|
|
int group_id) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefMenuModel> AddSubMenu(int command_id,
|
2017-05-17 11:29:28 +02:00
|
|
|
const CefString& label) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool InsertSeparatorAt(size_t index) override;
|
|
|
|
bool InsertItemAt(size_t index,
|
|
|
|
int command_id,
|
|
|
|
const CefString& label) override;
|
|
|
|
bool InsertCheckItemAt(size_t index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int command_id,
|
|
|
|
const CefString& label) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool InsertRadioItemAt(size_t index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int command_id,
|
|
|
|
const CefString& label,
|
|
|
|
int group_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
CefRefPtr<CefMenuModel> InsertSubMenuAt(size_t index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int command_id,
|
|
|
|
const CefString& label) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool Remove(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool RemoveAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
int GetIndexOf(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
int GetCommandIdAt(size_t index) override;
|
|
|
|
bool SetCommandIdAt(size_t index, int command_id) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
CefString GetLabel(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
CefString GetLabelAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool SetLabel(int command_id, const CefString& label) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetLabelAt(size_t index, const CefString& label) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
MenuItemType GetType(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
MenuItemType GetTypeAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
int GetGroupId(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
int GetGroupIdAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool SetGroupId(int command_id, int group_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetGroupIdAt(size_t index, int group_id) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
CefRefPtr<CefMenuModel> GetSubMenuAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool IsVisible(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsVisibleAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool SetVisible(int command_id, bool visible) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetVisibleAt(size_t index, bool visible) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool IsEnabled(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsEnabledAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool SetEnabled(int command_id, bool enabled) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetEnabledAt(size_t index, bool enabled) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool IsChecked(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsCheckedAt(size_t index) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool SetChecked(int command_id, bool checked) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetCheckedAt(size_t index, bool checked) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool HasAccelerator(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool HasAcceleratorAt(size_t index) override;
|
2017-05-17 11:29:28 +02:00
|
|
|
bool SetAccelerator(int command_id,
|
|
|
|
int key_code,
|
|
|
|
bool shift_pressed,
|
|
|
|
bool ctrl_pressed,
|
|
|
|
bool alt_pressed) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool SetAcceleratorAt(size_t index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int key_code,
|
|
|
|
bool shift_pressed,
|
|
|
|
bool ctrl_pressed,
|
|
|
|
bool alt_pressed) override;
|
2014-11-12 20:25:15 +01:00
|
|
|
bool RemoveAccelerator(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool RemoveAcceleratorAt(size_t index) override;
|
2017-05-17 11:29:28 +02:00
|
|
|
bool GetAccelerator(int command_id,
|
|
|
|
int& key_code,
|
|
|
|
bool& shift_pressed,
|
|
|
|
bool& ctrl_pressed,
|
|
|
|
bool& alt_pressed) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool GetAcceleratorAt(size_t index,
|
2017-05-17 11:29:28 +02:00
|
|
|
int& key_code,
|
|
|
|
bool& shift_pressed,
|
|
|
|
bool& ctrl_pressed,
|
|
|
|
bool& alt_pressed) override;
|
2017-02-25 06:03:31 +01:00
|
|
|
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;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
// Callbacks from the ui::MenuModel implementation.
|
2022-07-25 19:49:32 +02:00
|
|
|
void ActivatedAt(size_t index, cef_event_flags_t event_flags);
|
2017-02-22 19:05:27 +01:00
|
|
|
void MouseOutsideMenu(const gfx::Point& screen_point);
|
|
|
|
void UnhandledOpenSubmenu(bool is_rtl);
|
|
|
|
void UnhandledCloseSubmenu(bool is_rtl);
|
2022-07-25 19:49:32 +02:00
|
|
|
bool GetTextColor(size_t index,
|
2017-02-25 06:03:31 +01:00
|
|
|
bool is_accelerator,
|
|
|
|
bool is_hovered,
|
|
|
|
SkColor* override_color) const;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool GetBackgroundColor(size_t index,
|
2017-02-25 06:03:31 +01:00
|
|
|
bool is_hovered,
|
|
|
|
SkColor* override_color) const;
|
2012-04-19 22:31:46 +02:00
|
|
|
void MenuWillShow();
|
2016-10-17 20:14:44 +02:00
|
|
|
void MenuWillClose();
|
2022-07-25 19:49:32 +02:00
|
|
|
std::u16string GetFormattedLabelAt(size_t index);
|
|
|
|
const gfx::FontList* GetLabelFontListAt(size_t index) const;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
// Verify that only a single reference exists to all CefMenuModelImpl objects.
|
|
|
|
bool VerifyRefCount();
|
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
// Helper for adding custom menu items originating from the renderer process.
|
2021-03-04 23:36:57 +01:00
|
|
|
void AddMenuItem(const blink::mojom::CustomContextMenuItem& menu_item);
|
2015-09-09 16:05:39 +02:00
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
ui::MenuModel* model() const { return model_.get(); }
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Used when created via CefMenuManager.
|
2017-02-22 19:05:27 +01:00
|
|
|
Delegate* delegate() const { return delegate_; }
|
2016-01-19 21:09:01 +01:00
|
|
|
void set_delegate(Delegate* delegate) { delegate_ = delegate; }
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
// Used for menus run via CefWindowImpl::ShowMenu to provide more accurate
|
|
|
|
// menu close notification.
|
|
|
|
void set_auto_notify_menu_closed(bool val) { auto_notify_menu_closed_ = val; }
|
|
|
|
void NotifyMenuClosed();
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
private:
|
|
|
|
struct Item;
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
using ItemVector = std::vector<Item>;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
|
|
|
// Functions for inserting items into |items_|.
|
|
|
|
void AppendItem(const Item& item);
|
2022-07-25 19:49:32 +02:00
|
|
|
void InsertItemAt(const Item& item, size_t index);
|
2012-04-19 22:31:46 +02:00
|
|
|
void ValidateItem(const Item& item);
|
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
// Notify the delegate asynchronously.
|
|
|
|
void OnMouseOutsideMenu(const gfx::Point& screen_point);
|
|
|
|
void OnUnhandledOpenSubmenu(bool is_rtl);
|
|
|
|
void OnUnhandledCloseSubmenu(bool is_rtl);
|
2012-04-19 22:31:46 +02:00
|
|
|
void OnMenuClosed();
|
|
|
|
|
|
|
|
// Verify that the object is being accessed from the correct thread.
|
|
|
|
bool VerifyContext();
|
|
|
|
|
|
|
|
base::PlatformThreadId supported_thread_id_;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Used when created via CefMenuManager.
|
2012-04-19 22:31:46 +02:00
|
|
|
Delegate* delegate_;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Used when created via CefMenuModel::CreateMenuModel().
|
|
|
|
CefRefPtr<CefMenuModelDelegate> menu_model_delegate_;
|
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
const bool is_submenu_;
|
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
ItemVector items_;
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<ui::MenuModel> model_;
|
2012-04-19 22:31:46 +02:00
|
|
|
|
2017-02-25 06:03:31 +01:00
|
|
|
// Style information.
|
|
|
|
cef_color_t default_colors_[CEF_MENU_COLOR_COUNT] = {0};
|
|
|
|
gfx::FontList default_font_list_;
|
|
|
|
bool has_default_font_list_ = false;
|
|
|
|
|
2017-02-22 19:05:27 +01:00
|
|
|
bool auto_notify_menu_closed_ = true;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2012-04-19 22:31:46 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(CefMenuModelImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
|