-
Notifications
You must be signed in to change notification settings - Fork 1.1k
macos: when toggling visibility, hide individual windows not app #8636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mjdouglas
wants to merge
1
commit into
ghostty-org:main
Choose a base branch
from
mjdouglas:mjd/fix-toggle-visibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−25
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,7 +107,7 @@ class AppDelegate: NSObject, | |
} | ||
|
||
/// Tracks the windows that we hid for toggleVisibility. | ||
private var hiddenState: ToggleVisibilityState? = nil | ||
private var windowStash: HiddenWindowStash = HiddenWindowStash() | ||
|
||
/// The observer for the app appearance. | ||
private var appearanceObserver: NSKeyValueObservation? = nil | ||
|
@@ -294,8 +294,8 @@ class AppDelegate: NSObject, | |
} | ||
|
||
func applicationDidBecomeActive(_ notification: Notification) { | ||
// If we're back manually then clear the hidden state because macOS handles it. | ||
self.hiddenState = nil | ||
// Restore hidden windows, because MacOS won't do it for us | ||
windowStash.restoreAll() | ||
|
||
// Clear the dock badge when the app becomes active | ||
self.setDockBadge(nil) | ||
|
@@ -1039,27 +1039,22 @@ class AppDelegate: NSObject, | |
|
||
/// Toggles visibility of all Ghosty Terminal windows. When hidden, activates Ghostty as the frontmost application | ||
@IBAction func toggleVisibility(_ sender: Any) { | ||
// If we have focus, then we hide all windows. | ||
if NSApp.isActive { | ||
if windowStash.hasHiddenWindows() { | ||
// If we're not active, we want to become active | ||
NSApp.activate(ignoringOtherApps: true) | ||
|
||
windowStash.restoreAll() | ||
} else { | ||
// Toggle visibility doesn't do anything if the focused window is native | ||
// fullscreen. This is only relevant if Ghostty is active. | ||
guard let keyWindow = NSApp.keyWindow, | ||
!keyWindow.styleMask.contains(.fullScreen) else { return } | ||
|
||
// Keep track of our hidden state to restore properly | ||
self.hiddenState = .init() | ||
windowStash.hideAll() | ||
|
||
// Deactivate the app, as we no longer have any visible windows | ||
NSApp.hide(nil) | ||
return | ||
} | ||
|
||
// If we're not active, we want to become active | ||
NSApp.activate(ignoringOtherApps: true) | ||
|
||
// Bring all windows to the front. Note: we don't use NSApp.unhide because | ||
// that will unhide ALL hidden windows. We want to only bring forward the | ||
// ones that we hid. | ||
hiddenState?.restore() | ||
hiddenState = nil | ||
} | ||
|
||
@IBAction func bringAllToFront(_ sender: Any) { | ||
|
@@ -1096,14 +1091,18 @@ class AppDelegate: NSObject, | |
} | ||
} | ||
|
||
private struct ToggleVisibilityState { | ||
let hiddenWindows: [Weak<NSWindow>] | ||
let keyWindow: Weak<NSWindow>? | ||
private struct HiddenWindowStash { | ||
private var hiddenWindows: [Weak<NSWindow>] = [] | ||
private var keyWindow: Weak<NSWindow>? | ||
|
||
init() { | ||
init() {} | ||
|
||
func hasHiddenWindows() -> Bool { !hiddenWindows.isEmpty } | ||
|
||
mutating func hideAll() { | ||
// We need to know the key window so that we can bring focus back to the | ||
// right window if it was hidden. | ||
self.keyWindow = if let keyWindow = NSApp.keyWindow { | ||
self.keyWindow = if let keyWindow = NSApp.keyWindow, !keyWindow.styleMask.contains(.nonactivatingPanel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reasoning behind ignoring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The quick terminal is a non-activating panel. |
||
.init(keyWindow) | ||
} else { | ||
nil | ||
|
@@ -1113,15 +1112,23 @@ class AppDelegate: NSObject, | |
// want to bring back these windows if we remove the toggle. | ||
// | ||
// We also ignore fullscreen windows because they don't hide anyways. | ||
self.hiddenWindows = NSApp.windows.filter { | ||
let windows = NSApp.windows.filter { | ||
$0.isVisible && | ||
!$0.styleMask.contains(.nonactivatingPanel) && | ||
!$0.styleMask.contains(.fullScreen) | ||
}.map { Weak($0) } | ||
windows.forEach { $0.value?.orderOut(nil) } | ||
hiddenWindows.append(contentsOf: windows) | ||
} | ||
|
||
func restore() { | ||
mutating func restoreAll() { | ||
hiddenWindows.forEach { $0.value?.orderFrontRegardless() } | ||
keyWindow?.value?.makeKey() | ||
if let keyWindow = self.keyWindow?.value ?? hiddenWindows.compactMap(\.value).last { | ||
keyWindow.makeKeyAndOrderFront(nil) | ||
} | ||
|
||
hiddenWindows.removeAll() | ||
keyWindow = nil | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still seems to use NSApp.hide?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see how that's confusing. Windows are hidden with
window.orderOut()
inhideAll()
, but we still needNSApp.hide(nil)
to deactivate the app and bring whatever app was running behind Ghostty in the stack to the foreground.