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 m_motionPreferenceObserver = QMacNotificationObserver(nil,
50 UIAccessibilityReduceMotionStatusDidChangeNotification, [] {
51 QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
52 });
53}
54
56{
57}
58
59QPalette QIOSTheme::s_systemPalette;
60
62{
63 Q_DECL_IMPORT QPalette qt_fusionPalette(void);
64 s_systemPalette = qt_fusionPalette();
65
66 const auto disabledText = qt_mac_toQBrush(UIColor.tertiaryLabelColor.CGColor);
67
68 s_systemPalette.setBrush(QPalette::Window, qt_mac_toQBrush(UIColor.systemGroupedBackgroundColor.CGColor));
69 s_systemPalette.setBrush(QPalette::Active, QPalette::WindowText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
70 s_systemPalette.setBrush(QPalette::Disabled, QPalette::WindowText, disabledText);
71
72 s_systemPalette.setBrush(QPalette::Base, qt_mac_toQBrush(UIColor.secondarySystemGroupedBackgroundColor.CGColor));
73 s_systemPalette.setBrush(QPalette::Active, QPalette::Text, qt_mac_toQBrush(UIColor.labelColor.CGColor));
74 s_systemPalette.setBrush(QPalette::Disabled, QPalette::Text, disabledText);
75
76 s_systemPalette.setBrush(QPalette::Button, qt_mac_toQBrush(UIColor.secondarySystemBackgroundColor.CGColor));
77 s_systemPalette.setBrush(QPalette::Active, QPalette::ButtonText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
78 s_systemPalette.setBrush(QPalette::Disabled, QPalette::ButtonText, disabledText);
79
80 s_systemPalette.setBrush(QPalette::Active, QPalette::BrightText, qt_mac_toQBrush(UIColor.lightTextColor.CGColor));
81 s_systemPalette.setBrush(QPalette::Active, QPalette::PlaceholderText, qt_mac_toQBrush(UIColor.placeholderTextColor.CGColor));
82
83 s_systemPalette.setBrush(QPalette::Active, QPalette::Link, qt_mac_toQBrush(UIColor.linkColor.CGColor));
84 s_systemPalette.setBrush(QPalette::Active, QPalette::LinkVisited, qt_mac_toQBrush(UIColor.linkColor.CGColor));
85
86 s_systemPalette.setBrush(QPalette::Highlight, QColor(11, 70, 150, 60));
87 s_systemPalette.setBrush(QPalette::HighlightedText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
88
89 s_systemPalette.setBrush(QPalette::ToolTipText, qt_mac_toQBrush(UIColor.labelColor.CGColor));
90 s_systemPalette.setBrush(QPalette::Disabled, QPalette::ToolTipText, disabledText);
91
92 if (@available(ios 15.0, *))
93 s_systemPalette.setBrush(QPalette::Accent, qt_mac_toQBrush(UIColor.tintColor.CGColor));
94}
95
96const QPalette *QIOSTheme::palette(QPlatformTheme::Palette type) const
97{
98 if (type == QPlatformTheme::SystemPalette)
99 return &s_systemPalette;
100 return 0;
101}
102
103#if !defined(Q_OS_TVOS) && !defined(Q_OS_VISIONOS)
108
110{
111 return new QIOSMenu;
112}
113#endif
114
115bool QIOSTheme::usePlatformNativeDialog(QPlatformTheme::DialogType type) const
116{
117 switch (type) {
118 case FileDialog:
119 case MessageDialog:
120 case ColorDialog:
121 case FontDialog:
122 return !qt_apple_isApplicationExtension();
123 default:
124 return false;
125 }
126}
127
128QPlatformDialogHelper *QIOSTheme::createPlatformDialogHelper(QPlatformTheme::DialogType type) const
129{
130 switch (type) {
131#ifndef Q_OS_TVOS
132 case FileDialog:
133 return new QIOSFileDialog();
134 break;
135 case MessageDialog:
136 return new QIOSMessageDialog();
137 break;
138 case ColorDialog:
139 return new QIOSColorDialog();
140 break;
141 case FontDialog:
142 return new QIOSFontDialog();
143 break;
144#endif
145 default:
146 return 0;
147 }
148}
149
150QVariant QIOSTheme::themeHint(ThemeHint hint) const
151{
152 switch (hint) {
153 case QPlatformTheme::StyleNames:
154 return QStringList(QStringLiteral("Fusion"));
155 case KeyboardScheme:
156 return QVariant(int(MacKeyboardScheme));
157 case PreferFileIconFromTheme:
158 return true;
159 default:
160 return QPlatformTheme::themeHint(hint);
161 }
162}
163
165{
166#if defined(Q_OS_VISIONOS)
167 // On visionOS the concept of light or dark mode does not
168 // apply, as the UI is constantly changing based on what
169 // the lighting conditions are outside the headset, but
170 // the OS reports itself as always being in dark mode.
171 return Qt::ColorScheme::Dark;
172#else
173 if (s_colorSchemeOverride != Qt::ColorScheme::Unknown)
174 return s_colorSchemeOverride;
175
176 // Set the appearance based on the QUIWindow
177 // Fallback to the UIScreen if no window is created yet
178 UIUserInterfaceStyle appearance = UIScreen.mainScreen.traitCollection.userInterfaceStyle;
179 NSArray<UIWindow *> *windows = qt_apple_sharedApplication().windows;
180 for (UIWindow *window in windows) {
181 if ([window isKindOfClass:[QUIWindow class]]) {
182 appearance = static_cast<QUIWindow*>(window).traitCollection.userInterfaceStyle;
183 break;
184 }
185 }
186
187 return appearance == UIUserInterfaceStyleDark
188 ? Qt::ColorScheme::Dark
189 : Qt::ColorScheme::Light;
190#endif
191}
192
193void QIOSTheme::requestColorScheme(Qt::ColorScheme scheme)
194{
195#if defined(Q_OS_VISIONOS)
196 Q_UNUSED(scheme);
197#else
198 s_colorSchemeOverride = scheme;
199
200 const NSArray<UIWindow *> *windows = qt_apple_sharedApplication().windows;
201 for (UIWindow *window in windows) {
202 // don't apply a theme to windows we don't own
203 if (qt_objc_cast<QUIWindow*>(window))
204 applyTheme(window);
205 }
206#endif
207}
208
210{
211 return UIAccessibilityDarkerSystemColorsEnabled() ? Qt::ContrastPreference::HighContrast : Qt::ContrastPreference::NoPreference;
212}
213
215{
216 return UIAccessibilityIsReduceMotionEnabled() ? Qt::MotionPreference::ReducedMotion : Qt::MotionPreference::NoPreference;
217}
218
219void QIOSTheme::applyTheme(UIWindow *window)
220{
221 const UIUserInterfaceStyle style = []{
222 switch (s_colorSchemeOverride) {
223 case Qt::ColorScheme::Dark:
224 return UIUserInterfaceStyleDark;
225 case Qt::ColorScheme::Light:
226 return UIUserInterfaceStyleLight;
227 case Qt::ColorScheme::Unknown:
228 return UIUserInterfaceStyleUnspecified;
229 }
230 }();
231
232 window.overrideUserInterfaceStyle = style;
233}
234
235const QFont *QIOSTheme::font(Font type) const
236{
237 const auto *platformIntegration = QGuiApplicationPrivate::platformIntegration();
238 const auto *coreTextFontDatabase = static_cast<QCoreTextFontDatabase *>(platformIntegration->fontDatabase());
239 return coreTextFontDatabase->themeFont(type);
240}
241
242QIconEngine *QIOSTheme::createIconEngine(const QString &iconName) const
243{
244 return new QAppleIconEngine(iconName);
245}
246
247QIcon QIOSTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions iconOptions) const
248{
249 return QIcon(new QAppleFileIconEngine(fileInfo, iconOptions));
250}
251
252
253QT_END_NAMESPACE
QPlatformMenu * createPlatformMenu() const override
Definition qiostheme.mm:109
QIcon fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions options={}) const override
Return an icon for fileInfo, observing iconOptions.
Definition qiostheme.mm:247
const QFont * font(Font type=SystemFont) const override
Definition qiostheme.mm:235
QVariant themeHint(ThemeHint hint) const override
Definition qiostheme.mm:150
Qt::ContrastPreference contrastPreference() const override
Definition qiostheme.mm:209
void requestColorScheme(Qt::ColorScheme scheme) override
Definition qiostheme.mm:193
static void initializeSystemPalette()
Definition qiostheme.mm:61
Qt::ColorScheme colorScheme() const override
Definition qiostheme.mm:164
QPlatformMenuItem * createPlatformMenuItem() const override
Definition qiostheme.mm:104
Qt::MotionPreference motionPreference() const override
Definition qiostheme.mm:214
#define QStringLiteral(str)
Definition qstring.h:1825