macos: better enable timing depending on process launch time

This commit is contained in:
Mitchell Hashimoto
2024-09-24 10:39:20 -07:00
parent 1ad904478d
commit ed7ac8aa21
2 changed files with 26 additions and 2 deletions

View File

@ -63,6 +63,10 @@ class AppDelegate: NSObject,
/// This is only true before application has become active. /// This is only true before application has become active.
private var applicationHasBecomeActive: Bool = false private var applicationHasBecomeActive: Bool = false
/// This is set in applicationDidFinishLaunching with the system uptime so we can determine the
/// seconds since the process was launched.
private var applicationLaunchTime: TimeInterval = 0
/// The ghostty global state. Only one per process. /// The ghostty global state. Only one per process.
let ghostty: Ghostty.App = Ghostty.App() let ghostty: Ghostty.App = Ghostty.App()
@ -73,6 +77,11 @@ class AppDelegate: NSObject,
let updaterController: SPUStandardUpdaterController let updaterController: SPUStandardUpdaterController
let updaterDelegate: UpdaterDelegate = UpdaterDelegate() let updaterDelegate: UpdaterDelegate = UpdaterDelegate()
/// The elapsed time since the process was started
var timeSinceLaunch: TimeInterval {
return ProcessInfo.processInfo.systemUptime - applicationLaunchTime
}
override init() { override init() {
terminalManager = TerminalManager(ghostty) terminalManager = TerminalManager(ghostty)
updaterController = SPUStandardUpdaterController( updaterController = SPUStandardUpdaterController(
@ -106,6 +115,9 @@ class AppDelegate: NSObject,
"ApplePressAndHoldEnabled": false, "ApplePressAndHoldEnabled": false,
]) ])
// Store our start time
applicationLaunchTime = ProcessInfo.processInfo.systemUptime
// Check if secure input was enabled when we last quit. // Check if secure input was enabled when we last quit.
if (UserDefaults.standard.bool(forKey: "SecureInput") != SecureInput.shared.enabled) { if (UserDefaults.standard.bool(forKey: "SecureInput") != SecureInput.shared.enabled) {
toggleSecureInput(self) toggleSecureInput(self)
@ -421,11 +433,22 @@ class AppDelegate: NSObject,
// If our reload adds global keybinds and we don't have ax permissions then // If our reload adds global keybinds and we don't have ax permissions then
// we need to request them. // we need to request them.
global: if (ghostty_app_has_global_keybinds(ghostty.app!)) { if (ghostty_app_has_global_keybinds(ghostty.app!)) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { if (timeSinceLaunch > 5) {
// If the process has been running for awhile we enable right away
// because no windows are likely to pop up.
GlobalEventTap.shared.enable()
} else {
// If the process just started, we wait a couple seconds to allow
// the initial windows and so on to load so our permissions dialog
// doesn't get buried.
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
GlobalEventTap.shared.enable() GlobalEventTap.shared.enable()
} }
} }
} else {
GlobalEventTap.shared.disable()
}
} }
/// Sync the appearance of our app with the theme specified in the config. /// Sync the appearance of our app with the theme specified in the config.

View File

@ -22,6 +22,7 @@ class GlobalEventTap {
// don't have permissions. // don't have permissions.
private var enableTimer: Timer? = nil private var enableTimer: Timer? = nil
// Private init so it can't be constructed outside of our singleton
private init() {} private init() {}
deinit { deinit {