Line data Source code
1 : #ifndef MUSE_UI_UIACTION_H
2 : #define MUSE_UI_UIACTION_H
3 :
4 : #include <optional>
5 :
6 : #include "actions/actiontypes.h"
7 : #include "global/types/mnemonicstring.h"
8 :
9 : #include "view/iconcodes.h"
10 :
11 : namespace muse::ui {
12 : struct UiContext
13 : {
14 : UiContext() = default;
15 : constexpr UiContext(const char* ctx)
16 : : const_data(ctx) {}
17 :
18 : inline bool operator ==(const UiContext& ctx) const
19 : {
20 : return std::strcmp(const_data, ctx.const_data) == 0;
21 : }
22 :
23 : inline bool operator !=(const UiContext& ctx) const
24 : {
25 : return !this->operator ==(ctx);
26 : }
27 :
28 : inline bool isNull() const { return const_data == nullptr; }
29 :
30 0 : std::string toString() const { return const_data ? std::string(const_data) : std::string(); }
31 :
32 : private:
33 : const char* const_data = nullptr;
34 : };
35 :
36 : //! NOTE Only general UI contexts are declared here, which do not depend on the specifics of the application.
37 : //! Application-specific UI contexts are declared in the `context/uicontext.h` file
38 : static constexpr UiContext UiCtxUnknown = "UiCtxUnknown";
39 : static constexpr UiContext UiCtxAny = "UiCtxAny";
40 :
41 : // pages
42 : static constexpr ui::UiContext UiCtxHomeOpened = "UiCtxHomeOpened";
43 : static constexpr ui::UiContext UiCtxProjectOpened = "UiCtxProjectOpened";
44 :
45 : // panels
46 : static constexpr ui::UiContext UiCtxProjectFocused = "UiCtxProjectFocused";
47 : static constexpr ui::UiContext UiCtxBrailleFocused = "UiCtxBrailleFocused";
48 :
49 : // dialogs
50 : static constexpr ui::UiContext UiCtxDialogOpened = "UiCtxDialogOpened";
51 :
52 : enum class Checkable {
53 : No = 0,
54 : Yes
55 : };
56 :
57 : struct UiAction
58 : {
59 : actions::ActionCode code;
60 : std::vector<actions::ActionCode> children; // example: action://copy -> [action://project/copy]
61 : UiContext uiCtx = UiCtxAny;
62 : std::string scCtx = "any";
63 : MnemonicString title;
64 : TranslatableString description;
65 : IconCode::Code iconCode = IconCode::Code::NONE;
66 : QString iconColor;
67 : Checkable checkable = Checkable::No;
68 :
69 0 : UiAction() = default;
70 119 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, Checkable ch = Checkable::No)
71 119 : : code(code), uiCtx(ctx), scCtx(scCtx), checkable(ch) {}
72 :
73 : UiAction(const actions::ActionCode& code, const std::vector<actions::ActionCode>& children,
74 : UiContext ctx, std::string scCtx, Checkable ch = Checkable::No)
75 : : code(code), children(children), uiCtx(ctx), scCtx(scCtx), checkable(ch) {}
76 :
77 2 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, const MnemonicString& title,
78 : Checkable ch = Checkable::No)
79 2 : : code(code), uiCtx(ctx), scCtx(scCtx), title(title), checkable(ch) {}
80 :
81 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, const MnemonicString& title,
82 : const TranslatableString& desc, Checkable ch = Checkable::No)
83 : : code(code), uiCtx(ctx), scCtx(scCtx), title(title), description(desc), checkable(ch) {}
84 :
85 1 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, const MnemonicString& title,
86 : const TranslatableString& desc, IconCode::Code icon, Checkable ch = Checkable::No)
87 1 : : code(code), uiCtx(ctx), scCtx(scCtx), title(title), description(desc), iconCode(icon), checkable(ch) {}
88 :
89 : UiAction(const actions::ActionCode& code, const std::vector<actions::ActionCode>& children,
90 : UiContext ctx, std::string scCtx, const MnemonicString& title, const TranslatableString& desc, IconCode::Code icon,
91 : Checkable ch = Checkable::No)
92 : : code(code), children(children), uiCtx(ctx), scCtx(scCtx), title(title), description(desc), iconCode(icon), checkable(ch) {}
93 :
94 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, const MnemonicString& title,
95 : const TranslatableString& desc, IconCode::Code icon, QString iconColor, Checkable ch = Checkable::No)
96 : : code(code), uiCtx(ctx), scCtx(scCtx), title(title), description(desc), iconCode(icon), iconColor(iconColor), checkable(ch) {}
97 :
98 1 : UiAction(const actions::ActionCode& code, UiContext ctx, std::string scCtx, const MnemonicString& title, IconCode::Code icon,
99 : Checkable ch = Checkable::No)
100 1 : : code(code), uiCtx(ctx), scCtx(scCtx), title(title), iconCode(icon), checkable(ch) {}
101 :
102 0 : bool isValid() const
103 : {
104 0 : return !code.empty();
105 : }
106 :
107 : bool operator==(const UiAction& other) const
108 : {
109 : return code == other.code
110 : && uiCtx == other.uiCtx
111 : && scCtx == other.scCtx
112 : && title == other.title
113 : && description == other.description
114 : && iconCode == other.iconCode
115 : && iconColor == other.iconColor
116 : && checkable == other.checkable;
117 : }
118 : };
119 :
120 : using UiActionList = std::vector<UiAction>;
121 :
122 : struct UiActionState
123 : {
124 : bool enabled = false;
125 : bool checked = false;
126 :
127 : inline bool operator ==(const UiActionState& st) const
128 : {
129 : return st.enabled == enabled && st.checked == checked;
130 : }
131 :
132 : inline bool operator !=(const UiActionState& st) const
133 : {
134 : return !this->operator ==(st);
135 : }
136 :
137 0 : static UiActionState make_disabled(bool checked = false)
138 : {
139 0 : return UiActionState { false, checked };
140 : }
141 :
142 : static UiActionState make_enabled(bool checked = false)
143 : {
144 : return UiActionState { true, checked };
145 : }
146 :
147 : QVariantMap toMap() const
148 : {
149 : return {
150 : { "enabled", enabled },
151 : { "checked", checked }
152 : };
153 : }
154 : };
155 : }
156 :
157 : #endif // MUSE_UI_UIACTION_H
|