Add quick-terminal-space-behavior option

This commit is contained in:
Soh Satoh
2024-12-31 21:55:03 +09:00
committed by Mitchell Hashimoto
parent 2206c509be
commit e2523c25cb
5 changed files with 106 additions and 2 deletions

View File

@@ -3,6 +3,12 @@ import Cocoa
import SwiftUI
import GhosttyKit
// This is a Apple's private function that we need to call to get the active space.
@_silgen_name("CGSGetActiveSpace")
func CGSGetActiveSpace(_ cid: Int) -> size_t
@_silgen_name("CGSMainConnectionID")
func CGSMainConnectionID() -> Int
/// Controller for the "quick" terminal.
class QuickTerminalController: BaseTerminalController {
override var windowNibName: NSNib.Name? { "QuickTerminal" }
@@ -18,6 +24,9 @@ class QuickTerminalController: BaseTerminalController {
/// application to the front.
private var previousApp: NSRunningApplication? = nil
// The active space when the quick terminal was last shown.
private var previousActiveSpace: size_t = 0
/// The configuration derived from the Ghostty config so we don't need to rely on references.
private var derivedConfig: DerivedConfig
@@ -81,6 +90,9 @@ class QuickTerminalController: BaseTerminalController {
delegate: self
))
// Change the collection behavior of the window depending on the configuration.
window.collectionBehavior = derivedConfig.quickTerminalSpaceBehavior.collectionBehavior
// Animate the window in
animateIn()
}
@@ -107,8 +119,27 @@ class QuickTerminalController: BaseTerminalController {
self.previousApp = nil
}
if (derivedConfig.quickTerminalAutoHide) {
animateOut()
if derivedConfig.quickTerminalAutoHide {
switch derivedConfig.quickTerminalSpaceBehavior {
case .remain:
if self.window?.isOnActiveSpace == true {
// If we lose focus on the active space, then we can animate out
animateOut()
}
case .move:
// Check if the reason for losing focus is due to an active space change
let currentActiveSpace = CGSGetActiveSpace(CGSMainConnectionID())
if previousActiveSpace == currentActiveSpace {
// If we lose focus on the active space, then we can animate out
animateOut()
} else {
// If we're from different space, then we bring the window back
DispatchQueue.main.async {
self.window?.makeKeyAndOrderFront(nil)
}
}
self.previousActiveSpace = currentActiveSpace
}
}
}
@@ -163,6 +194,9 @@ class QuickTerminalController: BaseTerminalController {
}
}
// Set previous active space
self.previousActiveSpace = CGSGetActiveSpace(CGSMainConnectionID())
// Animate the window in
animateWindowIn(window: window, from: position)
@@ -390,12 +424,16 @@ class QuickTerminalController: BaseTerminalController {
self.derivedConfig = DerivedConfig(config)
syncAppearance()
// Update window.collectionBehavior
self.window?.collectionBehavior = derivedConfig.quickTerminalSpaceBehavior.collectionBehavior
}
private struct DerivedConfig {
let quickTerminalScreen: QuickTerminalScreen
let quickTerminalAnimationDuration: Double
let quickTerminalAutoHide: Bool
let quickTerminalSpaceBehavior: QuickTerminalSpaceBehavior
let windowColorspace: String
let backgroundOpacity: Double
@@ -403,6 +441,7 @@ class QuickTerminalController: BaseTerminalController {
self.quickTerminalScreen = .main
self.quickTerminalAnimationDuration = 0.2
self.quickTerminalAutoHide = true
self.quickTerminalSpaceBehavior = .move
self.windowColorspace = ""
self.backgroundOpacity = 1.0
}
@@ -411,6 +450,7 @@ class QuickTerminalController: BaseTerminalController {
self.quickTerminalScreen = config.quickTerminalScreen
self.quickTerminalAnimationDuration = config.quickTerminalAnimationDuration
self.quickTerminalAutoHide = config.quickTerminalAutoHide
self.quickTerminalSpaceBehavior = config.quickTerminalSpaceBehavior
self.windowColorspace = config.windowColorspace
self.backgroundOpacity = config.backgroundOpacity
}