Skip to content

Commit

Permalink
fix: patch issues where the key window is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianFahrbach committed Dec 27, 2024
1 parent 1efc8a2 commit c2a2bae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
19 changes: 10 additions & 9 deletions macos/Chronos-macOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let origin = CGPoint(x: Int(midScreenX), y: posScreenY)
let size = CGSize(width: 460, height: 548)
let frame = NSRect(origin: origin, size: size)
windowController.window!.setFrame(frame, display: true)
windowController.window!.center()
windowController.window!.makeKeyAndOrderFront(self)

windowController.window?.setFrame(frame, display: true)
windowController.window?.center()
windowController.window?.makeKeyAndOrderFront(self)
}

//
// Reopen window on dock icon click
//

func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
windowController.window!.makeKeyAndOrderFront(self)
windowController.window?.makeKeyAndOrderFront(self)
return true
}

//
// The actions for the menubar items
//

@IBAction func openGitHubURL(_ sender: AnyObject) {
let url = URL(string: "https://github.com/nice-af/chronos-app")
NSWorkspace.shared.open(url!)
Expand All @@ -86,13 +87,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
//
// Toggle the app icon visibility
//

@objc func setappVisibility(notification: NSNotification) -> Void {
guard let appIconSetting = notification.object as? String else {
print("Notification object is not a string")
return
}

if (appIconSetting == "both" || appIconSetting == "menuBarOnly") {
statusBarManager.showStatusBar()
} else {
Expand All @@ -113,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
//
// Listen to notification event
//

let un = UNUserNotificationCenter.current()
struct TrackingReminderData: Codable {
let title: String
Expand Down
2 changes: 1 addition & 1 deletion macos/Chronos-macOS/StatusBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class StatusBarManager: NSObject {
if self.windowController.window!.isKeyWindow {
self.windowController.window!.close()
} else {
self.windowController.window!.makeKeyAndOrderFront(self)
self.windowController.window?.makeKeyAndOrderFront(self)
NSApplication.shared.activate(ignoringOtherApps: true)
}
}
Expand Down
27 changes: 27 additions & 0 deletions patches/react-native-macos+0.76.6.patch
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
diff --git a/node_modules/react-native-macos/React/Base/RCTUtils.m b/node_modules/react-native-macos/React/Base/RCTUtils.m
index d91af71..3512a3b 100644
--- a/node_modules/react-native-macos/React/Base/RCTUtils.m
+++ b/node_modules/react-native-macos/React/Base/RCTUtils.m
@@ -635,6 +635,22 @@ RCTUIWindow *__nullable RCTKeyWindow(void) // [macOS]

return nil;
#else // [macOS
+ // React Native can't handle multiple windows, since the mobile platforms usually have only one window.
+ // This leads to the issue where our window can't be found because it doesn't have focus / keyboard focus.
+ // We fix this by looking for our custom window in the list of windows.
+ // @see https://github.com/microsoft/react-native-macos/issues/2296
+ if ([NSApp keyWindow] != nil) {
+ // We have a key window, so we can return it.
+ return [NSApp keyWindow];
+ } else {
+ for (NSWindow *window in [NSApp windows]) {
+ if ([[window className] isEqualToString:@"Chronos.CustomWindow"]) {
+ // We found our custom window, even though it is not the key window. We can return it.
+ return window;
+ }
+ }
+ }
+ // No key window found and no custom window found. This shouldn't happen.
return [NSApp keyWindow];
#endif // macOS]
}
diff --git a/node_modules/react-native-macos/React/UIUtils/RCTUIUtils.m b/node_modules/react-native-macos/React/UIUtils/RCTUIUtils.m
index 90ce88b..b3de174 100644
--- a/node_modules/react-native-macos/React/UIUtils/RCTUIUtils.m
Expand Down

0 comments on commit c2a2bae

Please sign in to comment.