diff --git a/src/command/BashCommand.swift b/src/command/BashCommand.swift index e54acaa4..f1511c19 100644 --- a/src/command/BashCommand.swift +++ b/src/command/BashCommand.swift @@ -1,8 +1,16 @@ struct BashCommand: Command { let bashCommand: String - func run() { - let process = try! Process.run(URL(filePath: "/bin/bash"), arguments: ["-c", bashCommand]) - process.waitUntilExit() + func run() async { + precondition(Thread.current.isMainThread) + await withCheckedContinuation { (continuation: CheckedContinuation<(), Never>) in + let process = Process() + process.executableURL = URL(filePath: "/bin/bash") + process.arguments = ["-c", bashCommand] + process.terminationHandler = { _ in + continuation.resume() + } + try! process.run() + } } } diff --git a/src/command/Command.swift b/src/command/Command.swift index 218dc9e6..fc97f350 100644 --- a/src/command/Command.swift +++ b/src/command/Command.swift @@ -1,3 +1,3 @@ protocol Command { - func run() + func run() async } diff --git a/src/command/CompositeCommand.swift b/src/command/CompositeCommand.swift index 280eda62..89b08d52 100644 --- a/src/command/CompositeCommand.swift +++ b/src/command/CompositeCommand.swift @@ -1,9 +1,10 @@ struct CompositeCommand: Command { let subCommands: [Command] - func run() { + func run() async { + precondition(Thread.current.isMainThread) for command in subCommands { - command.run() + await command.run() } } } diff --git a/src/command/FocusCommand.swift b/src/command/FocusCommand.swift index a1e6e26d..25d999df 100644 --- a/src/command/FocusCommand.swift +++ b/src/command/FocusCommand.swift @@ -7,7 +7,8 @@ struct FocusCommand: Command { case parent, child, floating, tiling, toggle_tiling_floating } - func run() { + func run() async { + precondition(Thread.current.isMainThread) // todo } } diff --git a/src/command/LayoutCommand.swift b/src/command/LayoutCommand.swift index 390813d2..82e3ae63 100644 --- a/src/command/LayoutCommand.swift +++ b/src/command/LayoutCommand.swift @@ -11,7 +11,8 @@ struct LayoutCommand: Command { case floating } - func run() { + func run() async { + precondition(Thread.current.isMainThread) // todo } } diff --git a/src/command/ModeCommand.swift b/src/command/ModeCommand.swift index 6ba0ca47..11abee45 100644 --- a/src/command/ModeCommand.swift +++ b/src/command/ModeCommand.swift @@ -1,7 +1,8 @@ struct ModeCommand: Command { let idToActivate: String - func run() { + func run() async { + precondition(Thread.current.isMainThread) for (modeId, mode) in config.modes { if modeId == idToActivate { mode.activate() diff --git a/src/command/MoveThroughCommand.swift b/src/command/MoveThroughCommand.swift index 1285b4e0..98d8f281 100644 --- a/src/command/MoveThroughCommand.swift +++ b/src/command/MoveThroughCommand.swift @@ -5,7 +5,8 @@ struct MoveThroughCommand: Command { case left, down, up, right } - func run() { + func run() async { + precondition(Thread.current.isMainThread) } } diff --git a/src/command/ReloadConfigCommand.swift b/src/command/ReloadConfigCommand.swift index dcaa23b1..fa42b1ad 100644 --- a/src/command/ReloadConfigCommand.swift +++ b/src/command/ReloadConfigCommand.swift @@ -1,5 +1,6 @@ struct ReloadConfigCommand: Command { - func run() { + func run() async { + precondition(Thread.current.isMainThread) reloadConfig() refresh() } diff --git a/src/command/WorkspaceCommand.swift b/src/command/WorkspaceCommand.swift index 4a931be2..235d6804 100644 --- a/src/command/WorkspaceCommand.swift +++ b/src/command/WorkspaceCommand.swift @@ -1,7 +1,8 @@ struct WorkspaceCommand : Command { let workspaceName: String - func run() { + func run() async { + precondition(Thread.current.isMainThread) switchToWorkspace(Workspace.get(byName: workspaceName)) } } diff --git a/src/config/Config.swift b/src/config/Config.swift index 33e7d37e..e9bc4242 100644 --- a/src/config/Config.swift +++ b/src/config/Config.swift @@ -39,7 +39,9 @@ class HotkeyBinding { } func activate() { - hotKey = HotKey(key: key, modifiers: modifiers, keyUpHandler: command.run) + hotKey = HotKey(key: key, modifiers: modifiers, keyUpHandler: { [self] in + Task { await command.run() } + }) } func deactivate() { diff --git a/src/refresh.swift b/src/refresh.swift index b021ed5e..14509529 100644 --- a/src/refresh.swift +++ b/src/refresh.swift @@ -3,6 +3,7 @@ import Foundation /// It's one of the most important function of the whole application. /// The function is called as a feedback response on every user input func refresh(startSession: Bool = true, endSession: Bool = true) { + precondition(Thread.current.isMainThread) debug("refresh (startSession=\(startSession), endSession=\(endSession)) \(Date.now.formatted(date: .abbreviated, time: .standard))") if startSession { NSWorkspace.activeApp = nil