Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore/Audacity CLA applies
4 : *
5 : * Copyright (C) 2026 MuseScore/Audacity and others
6 : *
7 : * This program is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License version 3 as
9 : * published by the Free Software Foundation.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "commandsstate.h"
21 :
22 : using namespace muse;
23 : using namespace muse::rcommand;
24 :
25 3 : void CommandsState::reg(const IModuleCommandsStatePtr& module)
26 : {
27 3 : IF_ASSERT_FAILED(module) {
28 0 : return;
29 : }
30 :
31 3 : const std::string& moduleName = module->moduleName();
32 3 : IF_ASSERT_FAILED(!moduleName.empty()) {
33 0 : return;
34 : }
35 :
36 3 : IF_ASSERT_FAILED(m_modules.find(moduleName) == m_modules.end()) {
37 0 : LOGW() << "module already registered: " << moduleName;
38 0 : return;
39 : }
40 :
41 3 : m_modules[moduleName] = module;
42 :
43 3 : module->commandStateChanged().onReceive(this, [this](const Command& command, const CommandState& state) {
44 12 : m_cache[command] = state;
45 12 : m_commandStateChanged.send(command, state);
46 12 : });
47 :
48 3 : module->init();
49 3 : }
50 :
51 0 : void CommandsState::unreg(const IModuleCommandsStatePtr& module)
52 : {
53 0 : IF_ASSERT_FAILED(module) {
54 0 : return;
55 : }
56 :
57 0 : const std::string& moduleName = module->moduleName();
58 0 : IF_ASSERT_FAILED(!moduleName.empty()) {
59 0 : return;
60 : }
61 :
62 0 : module->commandStateChanged().disconnect(this);
63 0 : module->deinit();
64 :
65 0 : m_modules.erase(moduleName);
66 0 : }
67 :
68 0 : async::Channel<Command, CommandState> CommandsState::commandStateChanged() const
69 : {
70 0 : return m_commandStateChanged;
71 : }
72 :
73 6 : CommandState CommandsState::commandState(const Command& command) const
74 : {
75 6 : if (command == NONE_COMMAND) {
76 0 : return CommandState(true);
77 : }
78 :
79 : {
80 6 : auto it = m_cache.find(command);
81 6 : if (it != m_cache.end()) {
82 6 : return it->second;
83 : }
84 : }
85 :
86 0 : const std::string& moduleName = commandsRegister()->commandModuleName(command);
87 0 : IF_ASSERT_FAILED(!moduleName.empty()) {
88 0 : return CommandState();
89 : }
90 :
91 0 : auto mit = m_modules.find(moduleName);
92 0 : IF_ASSERT_FAILED(mit != m_modules.end()) {
93 0 : return CommandState();
94 : }
95 :
96 0 : const IModuleCommandsStatePtr& module = mit->second;
97 0 : IF_ASSERT_FAILED(module) {
98 0 : return CommandState();
99 : }
100 :
101 0 : CommandState state = module->commandState(command);
102 :
103 0 : m_cache[command] = state;
104 :
105 0 : return state;
106 : }
|