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