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) 2026 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 :
23 : #include "shortcutsresolver.h"
24 :
25 : #include <algorithm>
26 :
27 : #include "global/stringutils.h"
28 :
29 : #include "log.h"
30 :
31 : using namespace muse;
32 : using namespace muse::shortcuts;
33 :
34 13 : void ShortcutsResolver::setScope(const Scope& scope)
35 : {
36 13 : m_scopes[strings::toUpper(scope.code)] = scope;
37 13 : }
38 :
39 125 : const IShortcutsResolver::Scope* ShortcutsResolver::findScope(const std::string& code) const
40 : {
41 125 : auto it = m_scopes.find(strings::toUpper(code));
42 125 : return it != m_scopes.end() ? &it->second : nullptr;
43 : }
44 :
45 72 : bool ShortcutsResolver::isScopeActive(const std::string& code) const
46 : {
47 72 : const Scope* scope = findScope(code);
48 72 : return !scope || !scope->isActive || scope->isActive();
49 : }
50 :
51 50 : int ShortcutsResolver::scopePriority(const std::string& code) const
52 : {
53 50 : const Scope* scope = findScope(code);
54 50 : return scope ? scope->priority : 0;
55 : }
56 :
57 16 : Shortcut ShortcutsResolver::selectActive(const ShortcutList& list) const
58 : {
59 16 : IF_ASSERT_FAILED(!list.empty()) {
60 0 : return Shortcut();
61 : }
62 :
63 : //! NOTE An active scope first, then higher priority, then a real command before the none command.
64 : //! The shortcuts with the same key keep their order.
65 16 : ShortcutList sorted = list;
66 16 : std::stable_sort(sorted.begin(), sorted.end(), [this](const Shortcut& a, const Shortcut& b) {
67 28 : const bool aIsActive = isScopeActive(a.scope);
68 28 : const bool bIsActive = isScopeActive(b.scope);
69 28 : if (aIsActive != bIsActive) {
70 3 : return aIsActive;
71 : }
72 :
73 25 : const int aPriority = scopePriority(a.scope);
74 25 : const int bPriority = scopePriority(b.scope);
75 25 : if (aPriority != bPriority) {
76 4 : return aPriority > bPriority;
77 : }
78 :
79 21 : const bool aIsNone = a.command == rcommand::NONE_COMMAND;
80 21 : const bool bIsNone = b.command == rcommand::NONE_COMMAND;
81 21 : return !aIsNone && bIsNone;
82 : });
83 :
84 16 : const Shortcut& probableShortcut = sorted.front();
85 16 : if (!isScopeActive(probableShortcut.scope)) {
86 : //! NOTE If the probable shortcut is not active, return an invalid shortcut.
87 1 : return Shortcut();
88 : }
89 :
90 15 : return probableShortcut;
91 16 : }
92 :
93 3 : TranslatableString ShortcutsResolver::scopeTitle(const std::string& scopeCode) const
94 : {
95 3 : const std::string upperScopeCode = strings::toUpper(scopeCode);
96 :
97 3 : const Scope* scope = findScope(scopeCode);
98 3 : if (scope && !scope->title.isEmpty()) {
99 2 : return scope->title;
100 : }
101 :
102 1 : LOGW() << "Unknown scope: " << scopeCode;
103 1 : return TranslatableString::untranslatable(String::fromStdString(upperScopeCode));
104 3 : }
|