Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiostheme.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qiostheme.h"
6
7#include <QtCore/QStringList>
8#include <QtCore/QVariant>
9#include <QtCore/private/qcore_mac_p.h>
10
11#include <QtGui/QFont>
12#include <QtGui/private/qcoregraphics_p.h>
13
14#include <QtGui/private/qcoretextfontdatabase_p.h>
15#include <QtGui/private/qapplefileiconengine_p.h>
16#include <QtGui/private/qappleiconengine_p.h>
17#include <QtGui/private/qguiapplication_p.h>
18#include <qpa/qplatformintegration.h>
19
20#include <UIKit/UIFont.h>
21#include <UIKit/UIInterface.h>
22
23#if !defined(Q_OS_TVOS) && !defined(Q_OS_VISIONOS)
24#include "qiosmenu.h"
25#endif
26
27#if !defined(Q_OS_TVOS)
28#include "qiosfiledialog.h"
30#include "qiosfontdialog.h"
32#include "qiosscreen.h"
33#include "quiwindow.h"
34#endif
35
36QT_BEGIN_NAMESPACE
37
38const char *QIOSTheme::name = "ios";
39
41{
43
44 m_contentSizeCategoryObserver = QMacNotificationObserver(nil,
45 UIContentSizeCategoryDidChangeNotification, [] {
46 qCDebug(lcQpaFonts) << "Contents size category changed to" << UIApplication.sharedApplication.preferredContentSizeCategory;
47 QPlatformFontDatabase::repopulateFontDatabase();
48 });
49}
50
52{
53}
54
55QPalette QIOSTheme::s_systemPalette;
56
58{
59 Q_DECL_IMPORT QPalette qt_fusionPalette(void);
60 s_systemPalette = qt_fusionPalette();
61
62 const auto disabledText = qt_mac_toQBrush(UIColor.tertiaryLabelColor.CGColor);
63
64 s_systemPalette.setBrush(QPalette::Window, qt_mac_toQBrush(UIColor.systemGroupedBackgroundColor.CGColor));
65 s_systemPalette.setBrush(QPalette::Active, QPalette::WindowText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
66 s_systemPalette.setBrush(QPalette::Disabled, QPalette::WindowText, disabledText);
67
68 s_systemPalette.setBrush(QPalette::Base, qt_mac_toQBrush(UIColor.secondarySystemGroupedBackgroundColor.CGColor));
69 s_systemPalette.setBrush(QPalette::Active, QPalette::Text, qt_mac_toQBrush(UIColor.labelColor.CGColor));
70 s_systemPalette.setBrush(QPalette::Disabled, QPalette::Text, disabledText);
71
72 s_systemPalette.setBrush(QPalette::Button, qt_mac_toQBrush(UIColor.secondarySystemBackgroundColor.CGColor));
73 s_systemPalette.setBrush(QPalette::Active, QPalette::ButtonText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
74 s_systemPalette.setBrush(QPalette::Disabled, QPalette::ButtonText, disabledText);
75
76 s_systemPalette.setBrush(QPalette::Active, QPalette::BrightText, qt_mac_toQBrush(UIColor.lightTextColor.CGColor));
77 s_systemPalette.setBrush(QPalette::Active, QPalette::PlaceholderText, qt_mac_toQBrush(UIColor.placeholderTextColor.CGColor));
78
79 s_systemPalette.setBrush(QPalette::Active, QPalette::Link, qt_mac_toQBrush(UIColor.linkColor.CGColor));
80 s_systemPalette.setBrush(QPalette::Active, QPalette::LinkVisited, qt_mac_toQBrush(UIColor.linkColor.CGColor));
81
82 s_systemPalette.setBrush(QPalette::Highlight, QColor(11, 70, 150, 60));
83 s_systemPalette.setBrush(QPalette::HighlightedText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
84
85 s_systemPalette.setBrush(QPalette::ToolTipText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
86 s_systemPalette.setBrush(QPalette::Disabled, QPalette::ToolTipText, disabledText);
87
88 if (@available(ios 15.0, *))
89 s_systemPalette.setBrush(QPalette::Accent, qt_mac_toQBrush(UIColor.tintColor.CGColor));
90}
91
92const QPalette *QIOSTheme::palette(QPlatformTheme::Palette type) const
93{
94 if (type == QPlatformTheme::SystemPalette)
95 return &s_systemPalette;
96 return 0;
97}
98
99#if !defined(Q_OS_TVOS) && !defined(Q_OS_VISIONOS)
104
106{
107 return new QIOSMenu;
108}
109#endif
110
111bool QIOSTheme::usePlatformNativeDialog(QPlatformTheme::DialogType type) const
112{
113 switch (type) {
114 case FileDialog:
115 case MessageDialog:
116 case ColorDialog:
117 case FontDialog:
118 return !qt_apple_isApplicationExtension();
119 default:
120 return false;
121 }
122}
123
124QPlatformDialogHelper *QIOSTheme::createPlatformDialogHelper(QPlatformTheme::DialogType type) const
125{
126 switch (type) {
127#ifndef Q_OS_TVOS
128 case FileDialog:
129 return new QIOSFileDialog();
130 break;
131 case MessageDialog:
132 return new QIOSMessageDialog();
133 break;
134 case ColorDialog:
135 return new QIOSColorDialog();
136 break;
137 case FontDialog:
138 return new QIOSFontDialog();
139 break;
140#endif
141 default:
142 return 0;
143 }
144}
145
146QVariant QIOSTheme::themeHint(ThemeHint hint) const
147{
148 switch (hint) {
149 case QPlatformTheme::StyleNames:
150 return QStringList(QStringLiteral("Fusion"));
151 case KeyboardScheme:
152 return QVariant(int(MacKeyboardScheme));
153 case PreferFileIconFromTheme:
154 return true;
155 default:
156 return QPlatformTheme::themeHint(hint);
157 }
158}
159
161{
162#if defined(Q_OS_VISIONOS)
163 // On visionOS the concept of light or dark mode does not
164 // apply, as the UI is constantly changing based on what
165 // the lighting conditions are outside the headset, but
166 // the OS reports itself as always being in dark mode.
167 return Qt::ColorScheme::Dark;
168#else
169 if (s_colorSchemeOverride != Qt::ColorScheme::Unknown)
170 return s_colorSchemeOverride;
171
172 // Set the appearance based on the QUIWindow
173 // Fallback to the UIScreen if no window is created yet
174 UIUserInterfaceStyle appearance = UIScreen.mainScreen.traitCollection.userInterfaceStyle;
175 NSArray<UIWindow *> *windows = qt_apple_sharedApplication().windows;
176 for (UIWindow *window in windows) {
177 if ([window isKindOfClass:[QUIWindow class]]) {
178 appearance = static_cast<QUIWindow*>(window).traitCollection.userInterfaceStyle;
179 break;
180 }
181 }
182
183 return appearance == UIUserInterfaceStyleDark
184 ? Qt::ColorScheme::Dark
185 : Qt::ColorScheme::Light;
186#endif
187}
188
189void QIOSTheme::requestColorScheme(Qt::ColorScheme scheme)
190{
191#if defined(Q_OS_VISIONOS)
192 Q_UNUSED(scheme);
193#else
194 s_colorSchemeOverride = scheme;
195
196 const NSArray<UIWindow *> *windows = qt_apple_sharedApplication().windows;
197 for (UIWindow *window in windows) {
198 // don't apply a theme to windows we don't own
199 if (qt_objc_cast<QUIWindow*>(window))
200 applyTheme(window);
201 }
202#endif
203}
204
205void QIOSTheme::applyTheme(UIWindow *window)
206{
207 const UIUserInterfaceStyle style = []{
208 switch (s_colorSchemeOverride) {
209 case Qt::ColorScheme::Dark:
210 return UIUserInterfaceStyleDark;
211 case Qt::ColorScheme::Light:
212 return UIUserInterfaceStyleLight;
213 case Qt::ColorScheme::Unknown:
214 return UIUserInterfaceStyleUnspecified;
215 }
216 }();
217
218 window.overrideUserInterfaceStyle = style;
219}
220
221const QFont *QIOSTheme::font(Font type) const
222{
223 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
224 const auto *coreTextFontDatabase = static_cast<QCoreTextFontDatabase *>(platformIntegration->fontDatabase());
225 return coreTextFontDatabase->themeFont(type);
226}
227
228QIconEngine *QIOSTheme::createIconEngine(const QString &iconName) const
229{
230 return new QAppleIconEngine(iconName);
231}
232
233QIcon QIOSTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions iconOptions) const
234{
235 return QIcon(new QAppleFileIconEngine(fileInfo, iconOptions));
236}
237
238
239QT_END_NAMESPACE
QPlatformMenu * createPlatformMenu() const override
Definition qiostheme.mm:105
QIcon fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions options={}) const override
Return an icon for fileInfo, observing iconOptions.
Definition qiostheme.mm:233
const QFont * font(Font type=SystemFont) const override
Definition qiostheme.mm:221
QVariant themeHint(ThemeHint hint) const override
Definition qiostheme.mm:146
void requestColorScheme(Qt::ColorScheme scheme) override
Definition qiostheme.mm:189
static void initializeSystemPalette()
Definition qiostheme.mm:57
Qt::ColorScheme colorScheme() const override
Definition qiostheme.mm:160
QPlatformMenuItem * createPlatformMenuItem() const override
Definition qiostheme.mm:100
#define QStringLiteral(str)
Definition qstring.h:1826