diff --git a/lib/platform/held_keys.dart b/lib/platform/held_keys.dart index 8f158e0d8..3296f840c 100644 --- a/lib/platform/held_keys.dart +++ b/lib/platform/held_keys.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:ui' show ViewFocusEvent, ViewFocusState; import 'package:flutter/gestures.dart'; import 'package:flutter/services.dart'; @@ -48,6 +49,10 @@ abstract final class HeldKeys { if (event is PointerDownEvent && _anyHeld) unawaited(sync('a press')); }); AppLifecycleListener(onResume: () => unawaited(sync('back in front'))); + // A window key again with the app in front all along: the keyboard + // back from another app's panel (macos/Runner/FocusWatch.swift), its + // modifier's key up gone to the panel. + WidgetsBinding.instance.addObserver(_KeyAgain()); } /// Whether ⌘ or Ctrl is held, either on any platform, for a click now: @@ -128,3 +133,12 @@ abstract final class HeldKeys { keyboard.isMetaPressed; } } + +class _KeyAgain with WidgetsBindingObserver { + @override + void didChangeViewFocus(ViewFocusEvent event) { + if (event.state == ViewFocusState.focused) { + unawaited(HeldKeys.sync('the window key again')); + } + } +} diff --git a/macos/Runner/FocusWatch.swift b/macos/Runner/FocusWatch.swift index 696952ca4..52c4cbae6 100644 --- a/macos/Runner/FocusWatch.swift +++ b/macos/Runner/FocusWatch.swift @@ -181,7 +181,7 @@ final class FocusWatch { return over.count > 8 ? "\(shown), \(over.count - 8) more" : shown } - private static func bounds(_ info: [String: Any]) -> CGRect? { + fileprivate static func bounds(_ info: [String: Any]) -> CGRect? { guard let dictionary = info[kCGWindowBounds as String] as? NSDictionary, let rect = CGRect(dictionaryRepresentation: dictionary), rect.width > 0, rect.height > 0 @@ -189,3 +189,149 @@ final class FocusWatch { return rect } } + +/// Gives the keyboard back to the app's window that lent it to another +/// app's panel, once the panel is gone. +/// +/// A panel that takes the keyboard without bringing its app in front (the +/// Launchpad's, Spotlight's, Bob's to translate a selection or a screenshot) +/// leaves this app the active one, its traffic lights lit; the window only +/// resigns key. Gone, it gives the keyboard back to no window: nothing told +/// this app it was back (it never left), and keys typed went nowhere till a +/// click on the window. Its modifier going up, lost with it, kept held for +/// the app (see lib/platform/held_keys.dart). +/// +/// So, the app active with no window key after one of its windows resigned +/// it: the windows of other apps over it are looked at till none shows +/// that was not already there as it was last key (the Dock, the menu bar's, +/// Spotlight's hidden one), then the window is made key again. Written to +/// the event log (`[focus]`). +final class KeyReturn { + static let shared = KeyReturn() + + /// How often the windows over it are looked at while it is lent. + static let interval: TimeInterval = 0.25 + + /// How long it is looked after: then a click brings it back, as before. + static let patience: TimeInterval = 600 + + private var started = false + + /// The windows of other apps over each window as it was last key, by its + /// number: what is always there. + private var furniture: [Int: Set] = [:] + + /// How many looks in a row find the panel gone before the window is key. + static let goneTicks = 2 + + private weak var lent: NSWindow? + private var lentAt: Date? + private var timer: Timer? + /// Whether a window of another app has been seen over it while lent: one + /// never seen is given no keyboard back. + private var seen = false + private var gone = 0 + + /// Watches the app's windows (BaoWindow), once. From the main thread. + func start() { + if started { return } + started = true + let center = NotificationCenter.default + center.addObserver( + forName: NSWindow.didBecomeKeyNotification, object: nil, queue: .main + ) { [weak self] notification in + guard let window = notification.object as? BaoWindow else { return } + self?.becameKey(window) + } + center.addObserver( + forName: NSWindow.didResignKeyNotification, object: nil, queue: .main + ) { [weak self] notification in + guard let window = notification.object as? BaoWindow else { return } + // AppKit has the next key window, or the app inactive, a moment after. + DispatchQueue.main.async { self?.resigned(window) } + } + } + + private func becameKey(_ window: BaoWindow) { + stop() + if let over = Self.foreignWindowsOver(window) { + furniture[window.windowNumber] = over + } + } + + private func resigned(_ window: BaoWindow) { + guard NSApp.isActive, NSApp.keyWindow == nil, window.isVisible, !window.isMiniaturized, + furniture[window.windowNumber] != nil + else { return } + stop() + lent = window + lentAt = Date() + let timer = Timer(timeInterval: Self.interval, repeats: true) { [weak self] _ in + self?.look() + } + RunLoop.main.add(timer, forMode: .common) + self.timer = timer + } + + private func look() { + guard let window = lent, let since = lentAt, + NSApp.isActive, NSApp.keyWindow == nil, window.isVisible, !window.isMiniaturized, + Date().timeIntervalSince(since) < Self.patience, + let always = furniture[window.windowNumber], + let over = Self.foreignWindowsOver(window) + else { + stop() + return + } + // The panel, or the next one it opened, still up. + if !over.subtracting(always).isEmpty { + seen = true + gone = 0 + return + } + // Not taken from it before the panel has shown (it would close it), nor + // between one panel and the next it opens. + gone += 1 + if !seen || gone < Self.goneTicks { return } + stop() + window.makeKey() + HangWatchdog.shared.appendEvent( + "focus", + String( + format: "key again after %.1f s, lent to another app's panel", + Date().timeIntervalSince(since))) + } + + private func stop() { + timer?.invalidate() + timer = nil + lent = nil + lentAt = nil + seen = false + gone = 0 + } + + /// The on-screen windows of other apps over [window] that show (not the + /// menu bar's strip): nil when the system cannot say. + private static func foreignWindowsOver(_ window: NSWindow) -> Set? { + guard window.windowNumber > 0, + let above = CGWindowListCopyWindowInfo( + [.optionOnScreenAboveWindow, .excludeDesktopElements], + CGWindowID(window.windowNumber)) as? [[String: Any]] + else { return nil } + let menuBar = Int(CGWindowLevelForKey(.mainMenuWindow)) + let status = Int(CGWindowLevelForKey(.statusWindow)) + var ids = Set() + for info in above { + let pid = info[kCGWindowOwnerPID as String] as? Int ?? 0 + if pid == Int(getpid()) { continue } + let alpha = info[kCGWindowAlpha as String] as? Double ?? 1 + if alpha <= 0 { continue } + guard let rect = FocusWatch.bounds(info) else { continue } + let layer = info[kCGWindowLayer as String] as? Int ?? 0 + if (layer == menuBar || layer == status) && rect.height <= 40 { continue } + if let id = info[kCGWindowNumber as String] as? Int { ids.insert(CGWindowID(id)) } + } + return ids + } +} diff --git a/macos/Runner/MainFlutterWindow.swift b/macos/Runner/MainFlutterWindow.swift index b18084a51..fe3f39370 100644 --- a/macos/Runner/MainFlutterWindow.swift +++ b/macos/Runner/MainFlutterWindow.swift @@ -710,6 +710,8 @@ class MainFlutterWindow: BaoWindow { HangWatchdog.shared.start(messenger: messenger) // Who takes the front when it goes oddly (see FocusWatch.swift). FocusWatch.shared.start(window: self) + // The keyboard back from another app's panel (see FocusWatch.swift). + KeyReturn.shared.start() // Notifications, the Dock's badge and the menu bar icon (see // lib/notifications/).