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 : #include "shortcutscontroller.h"
23 :
24 : #include "log.h"
25 :
26 : #define SHORTCUTS_DEBUG 1
27 :
28 : #if SHORTCUTS_DEBUG
29 : #define SC_LOG() LOGI() << "[SC] "
30 : #else
31 : #define SC_LOG() LOGN()
32 : #endif
33 :
34 : using namespace muse::shortcuts;
35 : using namespace muse::rcommand;
36 :
37 0 : void ShortcutsController::init()
38 : {
39 0 : interactive()->currentUri().ch.onReceive(this, [this](const Uri&) {
40 : //! NOTE: enable process shortcuts only for non-widget objects
41 0 : setActive(!interactive()->topWindowIsWidget());
42 0 : });
43 0 : }
44 :
45 0 : void ShortcutsController::activate(const std::string& sequence)
46 : {
47 0 : if (!m_active) {
48 0 : return;
49 : }
50 :
51 0 : ShortcutList allowedShortcuts;
52 0 : const ShortcutList& commandShortcuts = commandShortcutsRegister()->shortcutsForSequence(sequence);
53 0 : SC_LOG() << "'" << sequence << "' : commandShortcuts: " << commandShortcuts.size() << ": " << commandShortcuts;
54 0 : for (const Shortcut& sc : commandShortcuts) {
55 0 : if (commandsState()->commandState(sc.command).enabled) {
56 0 : allowedShortcuts.push_back(sc);
57 : }
58 : }
59 0 : SC_LOG() << "'" << sequence << "' : allowedShortcuts: " << allowedShortcuts.size() << ": " << allowedShortcuts;
60 :
61 0 : if (allowedShortcuts.empty()) {
62 0 : return;
63 : }
64 :
65 0 : Shortcut activeShortcut;
66 0 : if (shortcutsResolver()) {
67 0 : activeShortcut = shortcutsResolver()->selectActive(allowedShortcuts);
68 : } else {
69 0 : activeShortcut = allowedShortcuts.front();
70 : }
71 :
72 0 : SC_LOG() << "activeShortcut: " << (activeShortcut.command.isValid() ? activeShortcut.command.toString() : std::string("-"));
73 0 : if (!activeShortcut.isValid()) {
74 0 : return;
75 : }
76 :
77 0 : commandDispatcher()->dispatch(activeShortcut.command);
78 0 : }
79 :
80 0 : bool ShortcutsController::active()
81 : {
82 0 : return m_active;
83 : }
84 :
85 0 : void ShortcutsController::setActive(bool active)
86 : {
87 0 : if (m_active == active) {
88 0 : return;
89 : }
90 0 : m_active = active;
91 0 : m_activeChanged.notify();
92 : }
93 :
94 0 : muse::async::Notification ShortcutsController::activeChanged() const
95 : {
96 0 : return m_activeChanged;
97 : }
|