Skip to content

Commit

Permalink
Call refresh before and after every Command
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Sep 17, 2023
1 parent c9d53fc commit 7c393f7
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/AeroSpaceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct AeroSpaceApp: App {
Text("Workspaces:")
ForEach(Workspace.all) { workspace in
Button {
WorkspaceCommand.switchToWorkspace(workspace)
Task { await WorkspaceCommand(workspaceName: workspace.name).run() }
} label: {
Toggle(isOn: workspace.name == viewModel.focusedWorkspaceTrayText
? Binding(get: { true }, set: { _, _ in })
Expand Down
2 changes: 1 addition & 1 deletion src/command/CloseAllWindowsButCurrentCommand.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CloseAllWindowsButCurrentCommand: Command {
func run() {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
// todo
}
Expand Down
11 changes: 10 additions & 1 deletion src/command/Command.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
protocol Command {
@MainActor
func run() async
func runWithoutRefresh() async
}

extension Command {
@MainActor
func run() async {
refresh()
await runWithoutRefresh()
refresh(startSession: false)
}
}
4 changes: 2 additions & 2 deletions src/command/CompositeCommand.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
struct CompositeCommand: Command {
let subCommands: [Command]

func run() async {
func runWithoutRefresh() async {
precondition(Thread.current.isMainThread)
for command in subCommands {
await command.run()
await command.runWithoutRefresh()
}
}
}
2 changes: 1 addition & 1 deletion src/command/ExecAndForgetCommand.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct ExecAndForgetCommand: Command {
let bashCommand: String

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
try! Process.run(URL(filePath: "/bin/bash"), arguments: ["-c", bashCommand])
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/ExecAndWaitCommand.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct ExecAndWaitCommand: Command {
let bashCommand: String

func run() async {
func runWithoutRefresh() async {
precondition(Thread.current.isMainThread)
await withCheckedContinuation { (continuation: CheckedContinuation<(), Never>) in
let process = Process()
Expand Down
2 changes: 1 addition & 1 deletion src/command/FocusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct FocusCommand: Command {
//case next, prev
}

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
guard let currentWindow = focusedWindow ?? Workspace.focused.mruWindows.mostRecent else { return }
if let direction = direction.cardinalOrNil {
Expand Down
3 changes: 1 addition & 2 deletions src/command/LayoutCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ struct LayoutCommand: Command {
self.toggleBetween = toggleBetween
}

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
guard let window = focusedWindow ?? Workspace.focused.mruWindows.mostRecent else { return }
let targetLayout = toggleBetween.firstIndex(of: window.verboseLayout)
.flatMap { toggleBetween.getOrNil(atIndex: $0 + 1) } ?? toggleBetween.first
if let parent = window.parent as? TilingContainer {
parent.layout = targetLayout?.simpleLayout ?? errorT("TODO")
parent.orientation = targetLayout?.orientation ?? errorT("TODO")
refresh()
} else {
precondition(window.parent is Workspace)
// todo
Expand Down
2 changes: 1 addition & 1 deletion src/command/ModeCommand.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
struct ModeCommand: Command {
let idToActivate: String

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
for (modeId, mode) in config.modes {
if modeId == idToActivate {
Expand Down
2 changes: 1 addition & 1 deletion src/command/MoveThroughCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct MoveThroughCommand: Command {
case left, down, up, right
}

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)

}
Expand Down
2 changes: 1 addition & 1 deletion src/command/ReloadConfigCommand.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct ReloadConfigCommand: Command {
func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
reloadConfig()
refresh()
Expand Down
2 changes: 1 addition & 1 deletion src/command/WorkspaceBackAndForth.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct WorkspaceBackAndForth: Command {
func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
// todo
}
Expand Down
15 changes: 7 additions & 8 deletions src/command/WorkspaceCommand.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
struct WorkspaceCommand : Command {
let workspaceName: String

func run() async {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
WorkspaceCommand.switchToWorkspace(Workspace.get(byName: workspaceName))
}

static func switchToWorkspace(_ workspace: Workspace) {
let workspace = Workspace.get(byName: workspaceName)
debug("Switch to workspace: \(workspace.name)")
refresh()
if let window = workspace.mruWindows.mostRecent ?? workspace.anyLeafWindowRecursive { // switch to not empty workspace
window.focus()
// The switching itself will be done by refreshWorkspaces and layoutWorkspaces later in refresh
Expand All @@ -19,12 +15,15 @@ struct WorkspaceCommand : Command {
if let focusedMonitor = NSScreen.focusedMonitorOrNilIfDesktop ?? NSScreen.main?.monitor {
focusedMonitor.setActiveWorkspace(workspace)
}
defocusAllWindows()
WorkspaceCommand.defocusAllWindows()
}
refresh(startSession: false)
debug("End switch to workspace: \(workspace.name)")
}

static func switchToWorkspace(_ workspace: Workspace) async {
await WorkspaceCommand(workspaceName: workspace.name).run()
}

private static func defocusAllWindows() {
// Since AeroSpace doesn't show any windows, focusing AeroSpace defocuses all windows
let current = NSRunningApplication.current
Expand Down

0 comments on commit 7c393f7

Please sign in to comment.