Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_UI_UIACTIONSREGISTER_H
23 : #define MUSE_UI_UIACTIONSREGISTER_H
24 :
25 : #include <vector>
26 : #include <unordered_map>
27 :
28 : #include "../iuiactionsregister.h"
29 : #include "modularity/ioc.h"
30 : #include "iuicontextresolver.h"
31 : #include "async/asyncable.h"
32 :
33 : #include "rcommand/icommandsregister.h"
34 : #include "rcommand/icommandsstate.h"
35 :
36 : namespace muse::ui {
37 : class UiActionsRegister : public IUiActionsRegister, public Contextable, public async::Asyncable
38 : {
39 : GlobalInject<rcommand::ICommandsRegister> commandsRegister;
40 : ContextInject<rcommand::ICommandsState> commandsState = { this };
41 : ContextInject<IUiContextResolver> uicontextResolver = { this };
42 :
43 : public:
44 0 : UiActionsRegister(const modularity::ContextPtr& iocCtx)
45 0 : : Contextable(iocCtx) {}
46 :
47 : void init();
48 :
49 : void reg(const IUiActionsModulePtr& module) override;
50 : void unreg(const IUiActionsModulePtr& module) override;
51 :
52 : std::vector<UiAction> actionList() const override;
53 :
54 : const UiAction& action(const actions::ActionCode& code) const override;
55 : const actions::ActionCode& parentActionCode(const actions::ActionCode& code) const override;
56 :
57 : async::Channel<UiActionList> actionsChanged() const override;
58 :
59 : UiActionState actionState(const actions::ActionCode& code) const override;
60 : async::Channel<actions::ActionCodeList> actionStateChanged() const override;
61 :
62 : private:
63 :
64 : struct Info
65 : {
66 : IUiActionsModulePtr module;
67 : ui::UiAction action;
68 : ui::UiActionState state;
69 :
70 0 : bool isValid() const
71 : {
72 0 : return module != nullptr && action.isValid();
73 : }
74 : };
75 :
76 : Info& info(const actions::ActionCode& code);
77 : const Info& info(const actions::ActionCode& code) const;
78 :
79 : void updateActions(const UiActionList& actions);
80 :
81 : void updateEnabled(const actions::ActionCodeList& codes);
82 : void updateEnabledAll();
83 : void requestUpdateEnabledAll();
84 : void doUpdateEnabled(Info& inf, const IUiContextResolverPtr& ctxResolver, const UiContext& currentCtx,
85 : actions::ActionCodeList& changedList);
86 :
87 : void updateCheckedAll();
88 : void updateChecked(const actions::ActionCodeList& codes);
89 :
90 : std::unordered_map<actions::ActionCode, Info> m_actions;
91 : mutable std::unordered_map<actions::ActionCode, UiAction> m_commandActions;
92 : async::Channel<UiActionList> m_actionsChanged;
93 : async::Channel<actions::ActionCodeList> m_actionStateChanged;
94 :
95 : bool m_isUpdateEnabledAllRequested = false;
96 : };
97 : }
98 :
99 : #endif // MUSE_UI_UIACTIONSREGISTER_H
|