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 "uimodule.h"
23 :
24 : #include <QFontDatabase>
25 : #include <qqml.h>
26 :
27 : #include "modularity/ioc.h"
28 :
29 : #include "rcommand/icommandsregister.h"
30 : #include "rcommand/icommandsstate.h"
31 :
32 : #include "internal/navigationcommandsregister.h"
33 : #include "internal/navigationcommandsstate.h"
34 : #include "internal/uiengine.h"
35 : #include "internal/mainwindow.h"
36 : #include "internal/uiconfiguration.h"
37 : #include "internal/uicontextconfiguration.h"
38 : #include "internal/uistate.h"
39 : #include "internal/navigationcontroller.h"
40 : #include "internal/dragcontroller.h"
41 : #include "view/iconcodes.h"
42 : #include "view/widgetstyle.h"
43 :
44 : #ifdef Q_OS_MAC
45 : #include "internal/platform/macos/macosplatformtheme.h"
46 : #include "internal/windowscontroller.h"
47 : #elif defined(Q_OS_WIN)
48 : #include "internal/platform/windows/windowsplatformtheme.h"
49 : #include "internal/platform/windows/winwindowscontroller.h"
50 : #elif defined(Q_OS_LINUX)
51 : #include "internal/platform/linux/linuxplatformtheme.h"
52 : #include "internal/windowscontroller.h"
53 : #else
54 : #include "internal/platform/stub/stubplatformtheme.h"
55 : #include "internal/windowscontroller.h"
56 : #endif
57 :
58 : #include "global/api/iapiregister.h"
59 : #include "api/navigationapi.h"
60 : #include "api/keyboardapi.h"
61 : #include "api/themeapi.h"
62 :
63 : #include "muse_framework_config.h"
64 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
65 : #include "internal/uiactionsregister.h"
66 : #include "internal/navigationuiactions.h"
67 : #endif
68 :
69 : #include "log.h"
70 :
71 : using namespace muse::ui;
72 : using namespace muse::modularity;
73 :
74 : static const std::string module_name = "ui";
75 :
76 0 : std::string UiModule::moduleName() const
77 : {
78 0 : return module_name;
79 : }
80 :
81 0 : void UiModule::registerExports()
82 : {
83 0 : m_configuration = std::make_shared<UiConfiguration>();
84 :
85 0 : m_theme = std::make_shared<api::ThemeApi>(nullptr);
86 :
87 : #ifdef Q_OS_MAC
88 : m_platformTheme = std::make_shared<MacOSPlatformTheme>();
89 : #elif defined(Q_OS_WIN)
90 : m_platformTheme = std::make_shared<WindowsPlatformTheme>();
91 : #elif defined(Q_OS_LINUX)
92 0 : m_platformTheme = std::make_shared<LinuxPlatformTheme>();
93 : #else
94 : m_platformTheme = std::make_shared<StubPlatformTheme>();
95 : #endif
96 :
97 0 : globalIoc()->registerExport<IUiConfiguration>(moduleName(), m_configuration);
98 0 : globalIoc()->registerExport<IPlatformTheme>(moduleName(), m_platformTheme);
99 0 : }
100 :
101 0 : void UiModule::resolveImports()
102 : {
103 0 : auto cr = globalIoc()->resolve<muse::rcommand::ICommandsRegister>(module_name);
104 0 : if (cr) {
105 0 : cr->reg(std::make_shared<NavigationCommandsRegister>());
106 : }
107 0 : }
108 :
109 0 : void UiModule::registerApi()
110 : {
111 : using namespace muse::api;
112 :
113 0 : auto api = globalIoc()->resolve<IApiRegister>(moduleName());
114 0 : if (api) {
115 0 : api->regApiCreator(moduleName(), "MuseInternal.Navigation", new ApiCreator<muse::api::NavigationApi>());
116 0 : api->regApiCreator(moduleName(), "MuseInternal.Keyboard", new ApiCreator<muse::api::KeyboardApi>());
117 0 : api->regApiSingltone(moduleName(), "MuseApi.Theme", m_theme.get());
118 :
119 0 : qmlRegisterUncreatableMetaObject(IconCode::staticMetaObject, "MuseApi.Controls", 1, 0, "IconCode",
120 : "Not creatable as it is an enum type");
121 : }
122 0 : }
123 :
124 0 : void UiModule::onPreInit(const IApplication::RunMode&)
125 : {
126 0 : m_configuration->init();
127 0 : }
128 :
129 0 : void UiModule::onInit(const IApplication::RunMode& mode)
130 : {
131 0 : if (mode != IApplication::RunMode::GuiApp) {
132 0 : return;
133 : }
134 :
135 0 : if (QFontDatabase::addApplicationFont(":/ui/data/MusescoreIcon.ttf") == -1) {
136 0 : LOGE() << "Unable load icon font: `:/ui/data/MusescoreIcon.ttf`";
137 : }
138 : }
139 :
140 0 : void UiModule::onAllInited(const IApplication::RunMode& mode)
141 : {
142 0 : if (IApplication::RunMode::GuiApp != mode) {
143 0 : return;
144 : }
145 :
146 0 : m_theme->init();
147 :
148 0 : m_widgetStyle = new WidgetStyle(m_theme); // becomes owned by QApplication
149 0 : m_widgetStyle->init();
150 : }
151 :
152 0 : void UiModule::onDeinit()
153 : {
154 0 : m_configuration->deinit();
155 0 : }
156 :
157 : // Context
158 :
159 0 : IContextSetup* UiModule::newContext(const muse::modularity::ContextPtr& ctx) const
160 : {
161 0 : return new UiModuleContext(ctx);
162 : }
163 :
164 0 : void UiModuleContext::registerExports()
165 : {
166 0 : m_uistate = std::make_shared<UiState>(iocContext());
167 0 : m_uiengine = std::make_shared<UiEngine>(iocContext());
168 0 : m_keyNavigationController = std::make_shared<NavigationController>(iocContext());
169 :
170 : #ifdef Q_OS_MAC
171 : m_windowsController = std::make_shared<WindowsController>();
172 : #elif defined(Q_OS_WIN)
173 : m_windowsController = std::make_shared<WinWindowsController>(iocContext());
174 : #elif defined(Q_OS_LINUX)
175 0 : m_windowsController = std::make_shared<WindowsController>();
176 : #else
177 : m_windowsController = std::make_shared<WindowsController>();
178 : #endif
179 :
180 0 : ioc()->registerExport<IUiContextConfiguration>(module_name, new UiContextConfiguration(iocContext()));
181 0 : ioc()->registerExport<IUiState>(module_name, m_uistate);
182 0 : ioc()->registerExport<IUiEngine>(module_name, m_uiengine);
183 0 : ioc()->registerExport<INavigationController>(module_name, m_keyNavigationController);
184 0 : ioc()->registerExport<IDragController>(module_name, new DragController());
185 0 : ioc()->registerExport<IWindowsController>(module_name, m_windowsController);
186 0 : ioc()->registerExport<IMainWindow>(module_name, new MainWindow());
187 :
188 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
189 0 : m_uiactionsRegister = std::make_shared<UiActionsRegister>(iocContext());
190 0 : ioc()->registerExport<IUiActionsRegister>(module_name, m_uiactionsRegister);
191 : #endif
192 0 : }
193 :
194 0 : void UiModuleContext::resolveImports()
195 : {
196 0 : auto cs = ioc()->resolve<muse::rcommand::ICommandsState>(module_name);
197 0 : if (cs) {
198 0 : cs->reg(std::make_shared<NavigationCommandsState>(iocContext()));
199 : }
200 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
201 0 : auto ar = ioc()->resolve<IUiActionsRegister>(module_name);
202 0 : if (ar) {
203 0 : ar->reg(std::make_shared<NavigationUiActions>());
204 : }
205 : #endif
206 0 : }
207 :
208 0 : void UiModuleContext::onInit(const IApplication::RunMode& mode)
209 : {
210 0 : if (mode != IApplication::RunMode::GuiApp) {
211 0 : return;
212 : }
213 :
214 0 : m_keyNavigationController->init();
215 : }
216 :
217 0 : void UiModuleContext::onAllInited(const IApplication::RunMode&)
218 : {
219 : //! NOTE Some of the settings are taken from the workspace,
220 : //! we need to be sure that the workspaces are initialized.
221 : //! So, we loads these settings on onStartApp
222 0 : m_uistate->init();
223 :
224 : #ifdef MUSE_MODULE_ACTIONS_SUPPORT
225 : //! NOTE UIActions are collected from many modules, and these modules determine the state of their UIActions.
226 : //! All modules need to be initialized in order to get the correct state of UIActions.
227 : //! So, we do init on onStartApp
228 0 : m_uiactionsRegister->init();
229 : #endif
230 :
231 0 : auto api = globalIoc()->resolve<api::IApiRegister>(module_name);
232 0 : if (api) {
233 0 : auto obj = api->createApi("MuseApi.Theme", nullptr);
234 0 : api::ThemeApi* theme = dynamic_cast<api::ThemeApi*>(obj.first);
235 0 : assert(theme);
236 0 : if (theme) {
237 0 : m_uiengine->setTheme(theme);
238 : }
239 : }
240 :
241 0 : m_uiengine->init();
242 0 : }
|