mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-17 17:26:09 +03:00
macos: set the proper app focus state
This commit is contained in:
@ -84,12 +84,25 @@ extension Ghostty {
|
|||||||
self.app = app
|
self.app = app
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
// Subscribe to notifications for keyboard layout change so that we can update Ghostty.
|
// Set our initial focus state
|
||||||
NotificationCenter.default.addObserver(
|
ghostty_app_set_focus(app, NSApp.isActive)
|
||||||
|
|
||||||
|
let center = NotificationCenter.default
|
||||||
|
center.addObserver(
|
||||||
self,
|
self,
|
||||||
selector: #selector(keyboardSelectionDidChange(notification:)),
|
selector: #selector(keyboardSelectionDidChange(notification:)),
|
||||||
name: NSTextInputContext.keyboardSelectionDidChangeNotification,
|
name: NSTextInputContext.keyboardSelectionDidChangeNotification,
|
||||||
object: nil)
|
object: nil)
|
||||||
|
center.addObserver(
|
||||||
|
self,
|
||||||
|
selector: #selector(applicationDidBecomeActive(notification:)),
|
||||||
|
name: NSApplication.didBecomeActiveNotification,
|
||||||
|
object: nil)
|
||||||
|
center.addObserver(
|
||||||
|
self,
|
||||||
|
selector: #selector(applicationDidResignActive(notification:)),
|
||||||
|
name: NSApplication.didResignActiveNotification,
|
||||||
|
object: nil)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
self.readiness = .ready
|
self.readiness = .ready
|
||||||
@ -100,11 +113,7 @@ extension Ghostty {
|
|||||||
self.app = nil
|
self.app = nil
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
// Remove our observer
|
NotificationCenter.default.removeObserver(self)
|
||||||
NotificationCenter.default.removeObserver(
|
|
||||||
self,
|
|
||||||
name: NSTextInputContext.keyboardSelectionDidChangeNotification,
|
|
||||||
object: nil)
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,6 +275,19 @@ extension Ghostty {
|
|||||||
ghostty_app_keyboard_changed(app)
|
ghostty_app_keyboard_changed(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Called when the app becomes active.
|
||||||
|
@objc private func applicationDidBecomeActive(notification: NSNotification) {
|
||||||
|
guard let app = self.app else { return }
|
||||||
|
ghostty_app_set_focus(app, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called when the app becomes inactive.
|
||||||
|
@objc private func applicationDidResignActive(notification: NSNotification) {
|
||||||
|
guard let app = self.app else { return }
|
||||||
|
ghostty_app_set_focus(app, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: Ghostty Callbacks (macOS)
|
// MARK: Ghostty Callbacks (macOS)
|
||||||
|
|
||||||
static func closeSurface(_ userdata: UnsafeMutableRawPointer?, processAlive: Bool) {
|
static func closeSurface(_ userdata: UnsafeMutableRawPointer?, processAlive: Bool) {
|
||||||
|
@ -288,6 +288,7 @@ pub fn setQuit(self: *App) !void {
|
|||||||
/// This is separate from surface focus events. See the `focused`
|
/// This is separate from surface focus events. See the `focused`
|
||||||
/// field for more information.
|
/// field for more information.
|
||||||
pub fn focusEvent(self: *App, focused: bool) void {
|
pub fn focusEvent(self: *App, focused: bool) void {
|
||||||
|
log.debug("focus event focused={}", .{focused});
|
||||||
self.focused = focused;
|
self.focused = focused;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user