-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMobileUI_ios.mm
325 lines (265 loc) · 12.9 KB
/
MobileUI_ios.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*!
* Copyright (c) 2016 J-P Nurmi
* Copyright (c) 2023 Emeric Grange
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "MobileUI_private.h"
#include <QGuiApplication>
#include <QScreen>
#include <QWindow>
#include <QTimer>
#include <objc/objc.h>
#include <objc/message.h>
#include <UIKit/UIKit.h>
/* ************************************************************************** */
@interface QIOSViewController : UIViewController
@property (nonatomic, assign) BOOL prefersStatusBarHidden;
@property (nonatomic, assign) UIStatusBarAnimation preferredStatusBarUpdateAnimation;
@property (nonatomic, assign) UIStatusBarStyle preferredStatusBarStyle;
@end
static bool isQColorLight(const QColor color)
{
double darkness = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0;
return (darkness < 0.2);
}
UIStatusBarStyle statusBarStyle(const MobileUI::Theme theme)
{
if (theme == MobileUI::Dark) return UIStatusBarStyleLightContent;
else if (@available(iOS 13.0, *)) return UIStatusBarStyleDarkContent;
else return UIStatusBarStyleDefault;
}
static void setPreferredStatusBarStyle(UIWindow *window, UIStatusBarStyle style)
{
QIOSViewController *viewController = static_cast<QIOSViewController *>([window rootViewController]);
if (!viewController || viewController.preferredStatusBarStyle == style) return;
viewController.preferredStatusBarStyle = style;
[viewController setNeedsStatusBarAppearanceUpdate];
}
void updatePreferredStatusBarStyle()
{
UIStatusBarStyle style = statusBarStyle(MobileUIPrivate::statusbarTheme);
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (keyWindow) setPreferredStatusBarStyle(keyWindow, style);
}
/* ************************************************************************** */
int MobileUIPrivate::getDeviceTheme()
{
if (@available(iOS 13.0, *))
{
if ([[[[[UIApplication sharedApplication] keyWindow] rootViewController] traitCollection] userInterfaceStyle] == UIUserInterfaceStyleDark)
{
return MobileUI::Theme::Dark;
}
else
{
return MobileUI::Theme::Light;
}
}
return 0;
}
void MobileUIPrivate::refreshUI_async()
{
QTimer::singleShot( 0, []() { updatePreferredStatusBarStyle(); }); // now
QTimer::singleShot( 20, []() { updatePreferredStatusBarStyle(); }); // after a frame
QTimer::singleShot(200, []() { updatePreferredStatusBarStyle(); }); // after rotation animation?
}
/* ************************************************************************** */
void MobileUIPrivate::setColor_statusbar(const QColor &color)
{
// derive the theme from the color
MobileUIPrivate::statusbarTheme = static_cast<MobileUI::Theme>(!isQColorLight(color));
setTheme_statusbar(MobileUIPrivate::statusbarTheme);
}
void MobileUIPrivate::setTheme_statusbar(const MobileUI::Theme theme)
{
Q_UNUSED(theme)
updatePreferredStatusBarStyle();
if (!MobileUIPrivate::areRefreshSlotsConnected)
{
QObject::connect(qApp, &QGuiApplication::applicationStateChanged,
qApp, [](Qt::ApplicationState state) { if (state == Qt::ApplicationActive) updatePreferredStatusBarStyle(); });
QScreen *screen = qApp->primaryScreen();
if (screen)
{
QObject::connect(screen, &QScreen::orientationChanged,
qApp, [](Qt::ScreenOrientation) { refreshUI_async(); });
}
QWindowList windows = qApp->allWindows();
if (windows.size() && windows.at(0))
{
QWindow *window = windows.at(0);
QObject::connect(window, &QWindow::visibilityChanged,
qApp, [](QWindow::Visibility) { refreshUI_async(); });
}
MobileUIPrivate::areRefreshSlotsConnected = true;
}
}
/* ************************************************************************** */
void MobileUIPrivate::setColor_navbar(const QColor &color)
{
Q_UNUSED(color)
}
void MobileUIPrivate::setTheme_navbar(const MobileUI::Theme theme)
{
Q_UNUSED(theme)
}
/* ************************************************************************** */
int MobileUIPrivate::getStatusbarHeight()
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
int MobileUIPrivate::getNavbarHeight()
{
return 0;
}
int MobileUIPrivate::getSafeAreaTop()
{
if (@available(iOS 11.0, *))
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (keyWindow) return keyWindow.safeAreaInsets.top;
}
return 0;
}
int MobileUIPrivate::getSafeAreaLeft()
{
if (@available(iOS 11.0, *))
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (keyWindow) return keyWindow.safeAreaInsets.left;
}
return 0;
}
int MobileUIPrivate::getSafeAreaRight()
{
if (@available(iOS 11.0, *))
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (keyWindow) return keyWindow.safeAreaInsets.right;
}
return 0;
}
int MobileUIPrivate::getSafeAreaBottom()
{
if (@available(iOS 11.0, *))
{
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (keyWindow) return keyWindow.safeAreaInsets.bottom;
}
return 0;
}
/* ************************************************************************** */
void MobileUIPrivate::setScreenAlwaysOn(const bool on)
{
if (on)
{
[[UIApplication sharedApplication] setIdleTimerDisabled: YES];
}
else
{
[[UIApplication sharedApplication] setIdleTimerDisabled: NO];
}
}
void MobileUIPrivate::setScreenOrientation(const MobileUI::ScreenOrientation orientation)
{
if (@available(iOS 16.0, *))
{
// For reference, the values from iOS:
// UIInterfaceOrientationMaskAll, // The view controller supports all interface orientations.
// UIInterfaceOrientationMaskAllButUpsideDown, // The view controller supports all but the upside-down portrait interface orientation.
// UIInterfaceOrientationMaskPortrait, // The view controller supports a portrait interface orientation.
// UIInterfaceOrientationMaskPortraitUpsideDown,// The view controller supports an upside-down portrait interface orientation.
// UIInterfaceOrientationMaskLandscape, // The view controller supports both landscape-left and landscape-right interface orientation.
// UIInterfaceOrientationMaskLandscapeLeft, // The view controller supports a landscape-left interface orientation.
// UIInterfaceOrientationMaskLandscapeRight, // The view controller supports a landscape-right interface orientation.
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
if (!keyWindow) return;
UIWindowScene *windowScene = keyWindow.windowScene;
if (!windowScene) return;
UIWindowSceneGeometryPreferences *value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskAll];
if (orientation == MobileUI::Portrait) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
else if (orientation == MobileUI::Portrait_upsidedown) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortraitUpsideDown];
else if (orientation == MobileUI::Landscape_left) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscapeLeft];
else if (orientation == MobileUI::Landscape_right) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscapeRight];
else if (orientation == MobileUI::Landscape_sensor) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
// these aren't supported, so we default to regular mode
else if (orientation == MobileUI::Portrait_sensor) value = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
[windowScene requestGeometryUpdateWithPreferences:value errorHandler:^(NSError * _Nonnull error) {
qDebug() << "Cannot requestGeometryUpdate: unsupported?";
}];
}
else
{
// For reference, the enum values from iOS:
// UIInterfaceOrientationUnknown = 0, // The orientation of the device is unknown.
// UIInterfaceOrientationPortrait, // The device is in portrait mode, with the device upright and the Home button on the bottom.
// UIInterfaceOrientationPortraitUpsideDown, // The device is in portrait mode but is upside down, with the device upright and the Home button at the top.
// UIInterfaceOrientationLandscapeLeft, // The device is in landscape mode, with the device upright and the Home button on the left.
// UIInterfaceOrientationLandscapeRight, // The device is in landscape mode, with the device upright and the Home button on the right.
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
if (orientation == MobileUI::Portrait) value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
else if (orientation == MobileUI::Portrait_upsidedown) value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
else if (orientation == MobileUI::Landscape_left) value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
else if (orientation == MobileUI::Landscape_right) value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
// these aren't supported, so we default to regular mode
else if (orientation == MobileUI::Portrait_sensor) value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
else if (orientation == MobileUI::Landscape_sensor) value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
}
/* ************************************************************************** */
int MobileUIPrivate::getScreenBrightness()
{
Class uiScreenClass = (Class)objc_getClass("UIScreen");
SEL mainScreenSelector = sel_registerName("mainScreen");
id mainScreen = ((id(*)(Class, SEL))objc_msgSend)(uiScreenClass, mainScreenSelector);
SEL brightnessSelector = sel_registerName("brightness");
CGFloat brightness = ((CGFloat(*)(id, SEL))objc_msgSend)(mainScreen, brightnessSelector);
return brightness * 100;
}
void MobileUIPrivate::setScreenBrightness(const int value)
{
Class uiScreenClass = (Class)objc_getClass("UIScreen");
SEL mainScreenSelector = sel_registerName("mainScreen");
id mainScreen = ((id(*)(Class, SEL))objc_msgSend)(uiScreenClass, mainScreenSelector);
float brightness = value / 100.f; // brightness is 0.0 to 1.0
if (brightness < 0.0f) brightness = 0.0f;
if (brightness > 1.0f) brightness = 1.0f;
SEL setBrightnessSelector = sel_registerName("setBrightness:");
((void(*)(id, SEL, CGFloat))objc_msgSend)(mainScreen, setBrightnessSelector, brightness);
}
/* ************************************************************************** */
void MobileUIPrivate::vibrate()
{
// available impacts: light, medium, heavy, soft, rigid
// available notifications: error, success, warning
// "impact" feedback
UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleMedium)];
//[generator prepare];
[generator impactOccurred];
generator = nil;
}
/* ************************************************************************** */
void MobileUIPrivate::backToHomeScreen()
{
return;
}
/* ************************************************************************** */