2021-04-09 01:15:51 +02:00
|
|
|
// 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
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
#include <optional>
|
2021-04-09 01:15:51 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/threading/platform_thread.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_menu_model.h"
|
2021-04-09 01:15:51 +02:00
|
|
|
#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,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<ui::Accelerator> accel) = 0;
|
2021-04-09 01:15:51 +02:00
|
|
|
|
|
|
|
protected:
|
2024-01-20 23:48:57 +01:00
|
|
|
virtual ~StateDelegate() = default;
|
2021-04-09 01:15:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// |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);
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
CefSimpleMenuModelImpl(const CefSimpleMenuModelImpl&) = delete;
|
|
|
|
CefSimpleMenuModelImpl& operator=(const CefSimpleMenuModelImpl&) = delete;
|
|
|
|
|
2021-04-09 01:15:51 +02:00
|
|
|
~CefSimpleMenuModelImpl() override;
|
|
|
|
|
|
|
|
// Must be called before the object is deleted.
|
|
|
|
void Detach();
|
|
|
|
|
|
|
|
// CefMenuModel methods.
|
|
|
|
bool IsSubMenu() override;
|
|
|
|
bool Clear() override;
|
2022-07-25 19:49:32 +02:00
|
|
|
size_t GetCount() override;
|
2021-04-09 01:15:51 +02:00
|
|
|
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;
|
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,
|
2021-04-09 01:15:51 +02:00
|
|
|
int command_id,
|
|
|
|
const CefString& label) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool InsertRadioItemAt(size_t index,
|
2021-04-09 01:15:51 +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,
|
2021-04-09 01:15:51 +02:00
|
|
|
int command_id,
|
|
|
|
const CefString& label) override;
|
|
|
|
bool Remove(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool RemoveAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
CefString GetLabel(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
CefString GetLabelAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
MenuItemType GetType(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
MenuItemType GetTypeAt(size_t index) override;
|
2021-04-09 01:15:51 +02:00
|
|
|
int GetGroupId(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
int GetGroupIdAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
CefRefPtr<CefMenuModel> GetSubMenuAt(size_t index) override;
|
2021-04-09 01:15:51 +02:00
|
|
|
bool IsVisible(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsVisibleAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
bool IsEnabled(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsEnabledAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
bool IsChecked(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool IsCheckedAt(size_t index) override;
|
2021-04-09 01:15:51 +02: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;
|
2021-04-09 01:15:51 +02:00
|
|
|
bool HasAccelerator(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool HasAcceleratorAt(size_t index) override;
|
2021-04-09 01:15:51 +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,
|
2021-04-09 01:15:51 +02:00
|
|
|
int key_code,
|
|
|
|
bool shift_pressed,
|
|
|
|
bool ctrl_pressed,
|
|
|
|
bool alt_pressed) override;
|
|
|
|
bool RemoveAccelerator(int command_id) override;
|
2022-07-25 19:49:32 +02:00
|
|
|
bool RemoveAcceleratorAt(size_t index) override;
|
2021-04-09 01:15:51 +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,
|
2021-04-09 01:15:51 +02:00
|
|
|
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.
|
2022-07-25 19:49:32 +02:00
|
|
|
bool ValidIndex(size_t index);
|
2021-04-09 01:15:51 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_
|