electron/shell/browser/ui/electron_menu_model.h

130 lines
3.8 KiB
C
Raw Normal View History

2015-08-10 04:39:05 +00:00
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
#define SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
2015-08-10 04:39:05 +00:00
2015-09-01 11:48:11 +00:00
#include <map>
#include <string>
#include <vector>
2015-09-01 11:48:11 +00:00
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
2015-08-10 04:39:05 +00:00
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/optional.h"
2015-08-10 04:39:05 +00:00
#include "ui/base/models/simple_menu_model.h"
#include "url/gurl.h"
2015-08-10 04:39:05 +00:00
namespace electron {
2015-08-10 04:39:05 +00:00
class ElectronMenuModel : public ui::SimpleMenuModel {
2015-08-10 04:39:05 +00:00
public:
#if defined(OS_MAC)
struct SharingItem {
SharingItem();
SharingItem(SharingItem&&);
SharingItem(const SharingItem&) = delete;
~SharingItem();
base::Optional<std::vector<std::string>> texts;
base::Optional<std::vector<GURL>> urls;
base::Optional<std::vector<base::FilePath>> file_paths;
};
#endif
2015-08-10 04:39:05 +00:00
class Delegate : public ui::SimpleMenuModel::Delegate {
public:
~Delegate() override {}
virtual bool GetAcceleratorForCommandIdWithParams(
int command_id,
bool use_default_accelerator,
ui::Accelerator* accelerator) const = 0;
virtual bool ShouldRegisterAcceleratorForCommandId(
int command_id) const = 0;
virtual bool ShouldCommandIdWorkWhenHidden(int command_id) const = 0;
#if defined(OS_MAC)
virtual bool GetSharingItemForCommandId(int command_id,
SharingItem* item) const = 0;
#endif
private:
// ui::SimpleMenuModel::Delegate:
bool GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const override;
2015-08-10 04:39:05 +00:00
};
class Observer : public base::CheckedObserver {
2015-08-10 04:39:05 +00:00
public:
~Observer() override {}
2015-08-10 04:39:05 +00:00
2018-01-27 14:35:58 +00:00
// Notifies the menu will open.
virtual void OnMenuWillShow() {}
2015-08-10 04:39:05 +00:00
// Notifies the menu has been closed.
2018-01-27 14:35:58 +00:00
virtual void OnMenuWillClose() {}
2015-08-10 04:39:05 +00:00
};
explicit ElectronMenuModel(Delegate* delegate);
~ElectronMenuModel() override;
2015-08-10 04:39:05 +00:00
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
void SetToolTip(int index, const std::u16string& toolTip);
std::u16string GetToolTipAt(int index);
void SetRole(int index, const std::u16string& role);
std::u16string GetRoleAt(int index);
void SetSecondaryLabel(int index, const std::u16string& sublabel);
std::u16string GetSecondaryLabelAt(int index) const override;
bool GetAcceleratorAtWithParams(int index,
bool use_default_accelerator,
ui::Accelerator* accelerator) const;
bool ShouldRegisterAcceleratorAt(int index) const;
bool WorksWhenHiddenAt(int index) const;
#if defined(OS_MAC)
// Return the SharingItem of menu item.
bool GetSharingItemAt(int index, SharingItem* item) const;
// Set/Get the SharingItem of this menu.
void SetSharingItem(SharingItem item);
const base::Optional<SharingItem>& GetSharingItem() const;
#endif
2015-09-01 11:48:11 +00:00
2015-08-10 04:39:05 +00:00
// ui::SimpleMenuModel:
void MenuWillClose() override;
2018-01-27 14:35:58 +00:00
void MenuWillShow() override;
2015-08-10 04:39:05 +00:00
base::WeakPtr<ElectronMenuModel> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
using SimpleMenuModel::GetSubmenuModelAt;
ElectronMenuModel* GetSubmenuModelAt(int index);
2015-08-10 04:39:05 +00:00
private:
Delegate* delegate_; // weak ref.
#if defined(OS_MAC)
base::Optional<SharingItem> sharing_item_;
#endif
std::map<int, std::u16string> toolTips_; // command id -> tooltip
std::map<int, std::u16string> roles_; // command id -> role
std::map<int, std::u16string> sublabels_; // command id -> sublabel
2015-09-02 07:16:49 +00:00
base::ObserverList<Observer> observers_;
2015-08-10 04:39:05 +00:00
base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(ElectronMenuModel);
2015-08-10 04:39:05 +00:00
};
} // namespace electron
2015-08-10 04:39:05 +00:00
#endif // SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_