-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FocusCommand + refactorings
- Loading branch information
1 parent
bce5951
commit f4007b7
Showing
36 changed files
with
436 additions
and
210 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/.idea | ||
/build | ||
/.build | ||
|
||
# XCode User settings | ||
xcuserdata/ |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import Foundation | ||
|
||
class ViewModel: ObservableObject { | ||
static let shared = ViewModel() | ||
class TrayModel: ObservableObject { | ||
static let shared = TrayModel() | ||
|
||
private init() { | ||
} | ||
private init() {} | ||
|
||
@Published var focusedWorkspaceTrayText: String = currentEmptyWorkspace.name // config.first?.name ?? "W: 1" | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CloseAllWindowsButCurrentCommand: Command { | ||
func run() { | ||
precondition(Thread.current.isMainThread) | ||
// todo | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
protocol Command { | ||
@MainActor | ||
func run() async | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
struct ExecAndForgetCommand: Command { | ||
let bashCommand: String | ||
|
||
func run() async { | ||
precondition(Thread.current.isMainThread) | ||
try! Process.run(URL(filePath: "/bin/bash"), arguments: ["-c", bashCommand]) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/command/BashCommand.swift → src/command/ExecAndWaitCommand.swift
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
struct BashCommand: Command { | ||
struct ExecAndWaitCommand: Command { | ||
let bashCommand: String | ||
|
||
func run() async { | ||
|
This file contains 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 |
---|---|---|
@@ -1,14 +1,51 @@ | ||
struct FocusCommand: Command { | ||
let direction: Direction | ||
let direction: FDirection | ||
|
||
enum Direction: String { | ||
enum FDirection: String { | ||
case up, down, left, right | ||
|
||
case parent, child, floating, tiling, toggle_tiling_floating | ||
case parent, child //, floating, tiling, toggle_tiling_floating // not needed | ||
|
||
// todo support only if asked | ||
//case next, prev | ||
} | ||
|
||
func run() async { | ||
precondition(Thread.current.isMainThread) | ||
// todo | ||
guard let window = NSWorkspace.focusedApp?.macApp?.focusedWindow else { return } | ||
if let direction = direction.direction { | ||
let orientation = direction.orientation | ||
guard let topMostChild = window.parentsWithSelf.lazy.first(where: { | ||
$0.parent is Workspace || ($0.parent as? TilingContainer)?.orientation == orientation | ||
}) else { return } | ||
guard let parent = topMostChild.parent as? TilingContainer else { return } | ||
guard let index = parent.children.firstIndex(of: topMostChild) else { return } | ||
let mruIndexMap = window.workspace.mruWindows.mruIndexMap | ||
let window: MacWindow? = parent.children.getOrNil(atIndex: direction.isPositive ? index + 1 : index - 1)? | ||
.allLeafWindowsRecursive(snappedTo: direction.opposite) | ||
.minBy { mruIndexMap[$0] ?? Int.max } | ||
window?.focus() | ||
} else { | ||
// todo direction == .child || direction == .parent | ||
} | ||
} | ||
} | ||
|
||
extension FocusCommand.FDirection { | ||
var direction: Direction? { | ||
switch self { | ||
case .up: | ||
return .up | ||
case .down: | ||
return .down | ||
case .left: | ||
return .left | ||
case .right: | ||
return .right | ||
case .parent: | ||
return nil | ||
case .child: | ||
return nil | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
struct WorkspaceBackAndForth: Command { | ||
func run() async { | ||
precondition(Thread.current.isMainThread) | ||
// todo | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import TOMLKit | ||
|
||
// todo drop TomlBacktrace | ||
func parseCommand(_ raw: TOMLValueConvertible, _ backtrace: TomlBacktrace) -> Command { | ||
if let rawString = raw.string { | ||
return parseSingleCommand(rawString, backtrace) | ||
} else if let rawArray = raw.array { | ||
let commands: [Command] = (0..<rawArray.count).map { index in | ||
let indexBacktrace = backtrace + .index(index) | ||
let rawString: String = rawArray[index].string ?? | ||
expectedActualTypeError(expected: .string, actual: rawArray[index].type, indexBacktrace) | ||
return parseSingleCommand(rawString, indexBacktrace) | ||
} | ||
return CompositeCommand(subCommands: commands) | ||
} else { | ||
return expectedActualTypeError(expected: [.string, .array], actual: raw.type, backtrace) | ||
} | ||
} | ||
|
||
private func parseSingleCommand(_ raw: String, _ backtrace: TomlBacktrace) -> Command { | ||
let words = raw.split(separator: " ") | ||
let args = words[1...] | ||
let firstWord = String(words.first ?? "") | ||
if firstWord == "workspace" { | ||
return WorkspaceCommand(workspaceName: parseSingleArg(args, firstWord, backtrace)) | ||
} else if firstWord == "mode" { | ||
return ModeCommand(idToActivate: parseSingleArg(args, firstWord, backtrace)) | ||
} else if firstWord == "exec_and_wait" { | ||
return ExecAndWaitCommand(bashCommand: raw.removePrefix(firstWord)) | ||
} else if firstWord == "exec_and_forget" { | ||
return ExecAndForgetCommand(bashCommand: raw.removePrefix(firstWord)) | ||
} else if firstWord == "focus" { | ||
let direction = FocusCommand.FDirection(rawValue: parseSingleArg(args, firstWord, backtrace)) | ||
?? errorT("\(backtrace): Can't parse '\(firstWord)' direction") | ||
return FocusCommand(direction: direction) | ||
} else if firstWord == "move_through" { | ||
let direction = MoveThroughCommand.Direction(rawValue: parseSingleArg(args, firstWord, backtrace)) | ||
?? errorT("\(backtrace): Can't parse '\(firstWord)' direction") | ||
return MoveThroughCommand(direction: direction) | ||
} else if raw == "workspace_back_and_forth" { | ||
return WorkspaceBackAndForth() | ||
} else if raw == "reload_config" { | ||
return ReloadConfigCommand() | ||
} else if raw == "close_all_windows_but_current" { | ||
return CloseAllWindowsButCurrentCommand() | ||
} else if raw == "" { | ||
error("\(backtrace): Can't parse empty string command") | ||
} else { | ||
error("\(backtrace): Can't parse '\(raw)' command") | ||
} | ||
} | ||
|
||
private func parseSingleArg(_ args: ArraySlice<Swift.String.SubSequence>, _ command: String, _ backtrace: TomlBacktrace) -> String { | ||
args.singleOrNil().flatMap { String($0) } ?? errorT( | ||
"\(backtrace): \(command) must have only a single argument. But passed: '\(args.joined(separator: " "))'" | ||
) | ||
} | ||
|
||
private func expectedActualTypeError<T>(expected: TOMLType, actual: TOMLType, _ backtrace: TomlBacktrace) -> T { | ||
error("\(backtrace): Expected type is '\(expected)'. But actual type is '\(actual)'") | ||
} | ||
|
||
private func expectedActualTypeError<T>(expected: [TOMLType], actual: TOMLType, _ backtrace: TomlBacktrace) -> T { | ||
if let single = expected.singleOrNil() { | ||
return expectedActualTypeError(expected: single, actual: actual, backtrace) | ||
} else { | ||
error("\(backtrace): Expected types are \(expected.map { "'\($0.description)'" }.joined(separator: " or ")). But actual type is '\(actual)'") | ||
} | ||
} |
This file contains 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
Oops, something went wrong.