Skip to content

Commit 7603691

Browse files
CoyAceeliasnaur
authored andcommitted
app: [iOS] fix focus event for iOS 13.0+ with backward compatibility
UIScene notifications are the correct way to track window focus on iOS 13.0+, because the Key Window API is scene-level on iOS 13.0+, but we need to maintain support for iOS 12 and earlier. Implementation strategy: - iOS 13.0+: Use UIScene notifications (DidActivate/WillDeactivate) - iOS 12 and earlier: Keep existing UIWindow notifications as fallback - Add runtime version checks to select the appropriate API Key changes in os_ios.m: - Add @available(iOS 13.0, *) checks - Register both notification types with conditional logic - Ensure proper cleanup of observers when moving between windows See: https://developer.apple.com/documentation/uikit/uiscene?language=objc Fixes: https://lists.sr.ht/~eliasnaur/gio/%3CCAMAFT9Uyh_JWrkQQt+AmekJWFBqhZPsP_3ZxC1fUNB+=VGGorw@mail.gmail.com%3E Signed-off-by: CoyAce <akeycoy@gmail.com> Signed-off-by: Elias Naur <mail@eliasnaur.com>
1 parent 92fa23b commit 7603691

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

app/os.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Config struct {
4747
Decorated bool
4848
// TopMost windows render above all other non-top-most windows.
4949
TopMost bool
50-
// Focused reports whether has the keyboard focus.
50+
// Focused reports whether the window is focused.
5151
Focused bool
5252
// decoHeight is the height of the fallback decoration for platforms such
5353
// as Wayland that may need fallback client-side decorations.

app/os_ios.m

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,59 @@ + (Class)layerClass {
134134
return gio_layerClass();
135135
}
136136
- (void)willMoveToWindow:(UIWindow *)newWindow {
137+
self.contentScaleFactor = newWindow.screen.nativeScale;
138+
if (@available(iOS 13.0, *)) {
139+
[self registerSceneNotifications:newWindow];
140+
}else{
141+
[self registerWindowNotifications:newWindow];
142+
}
143+
}
144+
145+
- (void)registerSceneNotifications:(UIWindow *)newWindow {
146+
if (self.window != nil) {
147+
[[NSNotificationCenter defaultCenter] removeObserver:self
148+
name:UISceneDidActivateNotification
149+
object:self.window.windowScene];
150+
[[NSNotificationCenter defaultCenter] removeObserver:self
151+
name:UISceneWillDeactivateNotification
152+
object:self.window.windowScene];
153+
}
154+
[[NSNotificationCenter defaultCenter] addObserver:self
155+
selector:@selector(onSceneDidActivate:)
156+
name:UISceneDidActivateNotification
157+
object:newWindow.windowScene];
158+
159+
[[NSNotificationCenter defaultCenter] addObserver:self
160+
selector:@selector(onSceneWillDeactivate:)
161+
name:UISceneWillDeactivateNotification
162+
object:newWindow.windowScene];
163+
}
164+
165+
- (void)onSceneDidActivate:(NSNotification *)note API_AVAILABLE(ios(13.0)){
166+
onFocus(self.handle, YES);
167+
}
168+
169+
- (void)onSceneWillDeactivate:(NSNotification *)note API_AVAILABLE(ios(13.0)){
170+
onFocus(self.handle, NO);
171+
}
172+
173+
- (void)registerWindowNotifications:(UIWindow *)newWindow {
137174
if (self.window != nil) {
138175
[[NSNotificationCenter defaultCenter] removeObserver:self
139-
name:UIWindowDidBecomeKeyNotification
140-
object:self.window];
176+
name:UIWindowDidBecomeKeyNotification
177+
object:self.window];
141178
[[NSNotificationCenter defaultCenter] removeObserver:self
142-
name:UIWindowDidResignKeyNotification
143-
object:self.window];
179+
name:UIWindowDidResignKeyNotification
180+
object:self.window];
144181
}
145-
self.contentScaleFactor = newWindow.screen.nativeScale;
146182
[[NSNotificationCenter defaultCenter] addObserver:self
147-
selector:@selector(onWindowDidBecomeKey:)
148-
name:UIWindowDidBecomeKeyNotification
149-
object:newWindow];
183+
selector:@selector(onWindowDidBecomeKey:)
184+
name:UIWindowDidBecomeKeyNotification
185+
object:newWindow];
150186
[[NSNotificationCenter defaultCenter] addObserver:self
151-
selector:@selector(onWindowDidResignKey:)
152-
name:UIWindowDidResignKeyNotification
153-
object:newWindow];
187+
selector:@selector(onWindowDidResignKey:)
188+
name:UIWindowDidResignKeyNotification
189+
object:newWindow];
154190
}
155191

156192
- (void)onWindowDidBecomeKey:(NSNotification *)note {

0 commit comments

Comments
 (0)