1// Aseprite
2// Copyright (C) 2020-2022 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_UI_APP_MENUITEM_H_INCLUDED
9#define APP_UI_APP_MENUITEM_H_INCLUDED
10#pragma once
11
12#include "app/commands/params.h"
13#include "app/ui/key.h"
14#include "os/menus.h"
15#include "os/shortcut.h"
16#include "ui/menu.h"
17
18#include <memory>
19
20namespace app {
21
22 // A widget that represent a menu item of the application.
23 //
24 // It's like a MenuItme, but it has a extra properties: the name of
25 // the command to be executed when it's clicked (also that command is
26 // used to check the availability of the command).
27 class AppMenuItem : public ui::MenuItem {
28 public:
29 struct Native {
30 os::MenuItemRef menuItem = nullptr;
31 os::Shortcut shortcut;
32 app::KeyContext keyContext = app::KeyContext::Any;
33 };
34
35 AppMenuItem(const std::string& text,
36 const std::string& commandId = std::string(),
37 const Params& params = Params());
38 AppMenuItem(const std::string& text, std::nullptr_t) = delete;
39
40 KeyPtr key() { return m_key; }
41 void setKey(const KeyPtr& key);
42
43 void setIsRecentFileItem(bool state) { m_isRecentFileItem = state; }
44 bool isRecentFileItem() const { return m_isRecentFileItem; }
45
46 std::string getCommandId() const { return m_commandId; }
47 Command* getCommand() const;
48 const Params& getParams() const { return m_params; }
49
50 Native* native() const { return m_native.get(); }
51 void setNative(const Native& native);
52 void disposeNative();
53 void syncNativeMenuItemKeyShortcut();
54
55 static void setContextParams(const Params& params);
56
57 protected:
58 bool onProcessMessage(ui::Message* msg) override;
59 void onSizeHint(ui::SizeHintEvent& ev) override;
60 void onClick() override;
61 void onValidate() override;
62
63 private:
64 KeyPtr m_key;
65 std::string m_commandId;
66 Params m_params;
67 bool m_isRecentFileItem;
68 std::unique_ptr<Native> m_native;
69
70 static Params s_contextParams;
71 };
72
73} // namespace app
74
75#endif
76