Skip to content

Commit

Permalink
A bunch of refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Oct 15, 2023
1 parent 61b97a8 commit 9bf885c
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
13 changes: 7 additions & 6 deletions src/command/LayoutCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ struct LayoutCommand: Command {
.flatMap { toggleBetween.getOrNil(atIndex: $0 + 1) }
.orElse { toggleBetween.first }
.map { $0 == .main ? config.mainLayout : $0 }
if let parent = window.parent as? TilingContainer {
switch window.parent.kind {
case .tilingContainer(let parent):
parent.layout = targetLayout?.simpleLayout ?? errorT("TODO")
parent.orientation = targetLayout?.orientation ?? errorT("TODO")
} else {
precondition(window.parent is Workspace)
// todo
case .workspace:
break // todo
}
}
}
Expand Down Expand Up @@ -53,14 +53,15 @@ private extension ConfigLayout {

private extension Window {
var configLayout: ConfigLayout {
if let parent = parent as? TilingContainer {
switch parent.kind {
case .tilingContainer(let parent):
switch parent.layout {
case .List:
return parent.orientation == .H ? .h_list : .v_list
case .Accordion:
return parent.orientation == .H ? .h_accordion : .v_accordion
}
} else {
case .workspace:
return .floating
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/command/MoveThroughCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ struct MoveThroughCommand: Command {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
guard let currentWindow = focusedWindowOrEffectivelyFocused else { return }
if let parent = currentWindow.parent as? TilingContainer {
switch currentWindow.parent.kind {
case .tilingContainer(let parent):
let indexOfCurrent = currentWindow.ownIndex
let indexOfSiblingTarget = indexOfCurrent + direction.focusOffset
if parent.orientation == direction.orientation && parent.children.indices.contains(indexOfSiblingTarget) {
Expand All @@ -20,16 +21,20 @@ struct MoveThroughCommand: Command {
} else {
moveOut(window: currentWindow, direction: direction)
}
} else if let _ = currentWindow.parent as? Workspace { // floating window
// todo
case .workspace: // floating window
break // todo
}
}
}

private func moveOut(window: Window, direction: CardinalDirection) {
let innerMostChild = window.parents.first(where: {
// todo rewrite "is Workspace" part once "sticky" is introduced
$0.parent is Workspace || ($0.parent as? TilingContainer)?.orientation == direction.orientation
switch $0.parent?.kind {
case .workspace, nil:
return true // Stop searching
case .tilingContainer(let parent):
return parent.orientation == direction.orientation
}
}) as! TilingContainer
let bindTo: TilingContainer
let bindToIndex: Int
Expand Down
3 changes: 1 addition & 2 deletions src/command/parseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ private func parseSingleCommand(_ raw: String) -> ParsedCommand<Command> {
.flatMap { CardinalDirection(rawValue: $0).orFailure("Can't parse '\(firstWord)' direction") }
.map { FocusCommand(direction: $0) }
} else if firstWord == "move-through" {
let bar: ParsedCommand<Command> = parseSingleArg(args, firstWord)
return parseSingleArg(args, firstWord)
.flatMap { CardinalDirection(rawValue: $0).orFailure("Can't parse '\(firstWord)' direction") }
.map { MoveThroughCommand(direction: $0) }
return bar
} else if firstWord == "layout" {
return args.mapOrFailure { parseLayout(String($0)) }
.flatMap {
Expand Down
4 changes: 2 additions & 2 deletions src/tree/MacWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ final class MacWindow: Window, CustomStringConvertible {
let index: Int
if shouldFloat(axWindow) {
parent = workspace
index = BIND_LAST_INDEX
index = INDEX_BIND_LAST
} else {
let mruWindow = workspace.mostRecentWindow
if let mruWindow, let tilingParent = mruWindow.parent as? TilingContainer {
parent = tilingParent
index = mruWindow.ownIndex + 1
} else {
parent = workspace.rootTilingContainer
index = BIND_LAST_INDEX
index = INDEX_BIND_LAST
}
}
let window = MacWindow(id, app, axWindow, parent: parent, adaptiveWeight: WEIGHT_AUTO, index: index)
Expand Down
6 changes: 3 additions & 3 deletions src/tree/TreeNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TreeNode: Equatable {
}

@discardableResult
func bindTo(parent newParent: NonLeafTreeNode, adaptiveWeight: CGFloat, index: Int = BIND_LAST_INDEX) -> PreviousBindingData? { // todo make index parameter mandatory
func bindTo(parent newParent: NonLeafTreeNode, adaptiveWeight: CGFloat, index: Int = INDEX_BIND_LAST) -> PreviousBindingData? { // todo make index parameter mandatory
if _parent === newParent {
error("Binding to the same parent doesn't make sense")
}
Expand Down Expand Up @@ -86,7 +86,7 @@ class TreeNode: Equatable {
} else {
self.adaptiveWeight = adaptiveWeight
}
newParent._children.insert(self, at: index != BIND_LAST_INDEX ? index : newParent._children.count)
newParent._children.insert(self, at: index != INDEX_BIND_LAST ? index : newParent._children.count)
_parent = newParent
markAsMostRecentChild()
return result
Expand Down Expand Up @@ -146,7 +146,7 @@ private let WEIGHT_FLOATING = CGFloat(-2)
/// Reset weight is bind to workspace (aka "floating windows")
let WEIGHT_AUTO = CGFloat(-1)

let BIND_LAST_INDEX = -1
let INDEX_BIND_LAST = -1

struct PreviousBindingData {
let adaptiveWeight: CGFloat
Expand Down
5 changes: 3 additions & 2 deletions src/tree/TreeNodeEx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ extension TreeNode {
return true
}
})!
if let parent = innermostChild.parent as? TilingContainer {
switch innermostChild.parent?.kind {
case .tilingContainer(let parent):
precondition(parent.orientation == direction.orientation)
return (parent, innermostChild.ownIndexOrNil!)
} else {
case .workspace, nil:
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tree/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension Workspace {
let containers = children.filterIsInstance(of: TilingContainer.self)
switch containers.count {
case 0:
return TilingContainer.newHList(parent: self, adaptiveWeight: 1, index: BIND_LAST_INDEX) // todo createDefaultWorkspaceContainer(self)
return TilingContainer.newHList(parent: self, adaptiveWeight: 1, index: INDEX_BIND_LAST) // todo createDefaultWorkspaceContainer(self)
case 1:
return containers.singleOrNil()!
default:
Expand Down
2 changes: 1 addition & 1 deletion test/tree/TestWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class TestWindow: Window, CustomStringConvertible {
@discardableResult
init(id: UInt32, parent: NonLeafTreeNode, adaptiveWeight: CGFloat = 1, rect: Rect? = nil) {
_rect = rect
super.init(id: id, parent: parent, adaptiveWeight: adaptiveWeight, index: BIND_LAST_INDEX)
super.init(id: id, parent: parent, adaptiveWeight: adaptiveWeight, index: INDEX_BIND_LAST)
TestApp.shared.windows.append(self)
}

Expand Down
4 changes: 2 additions & 2 deletions test/tree/TilingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

extension TilingContainer {
static func newHList(parent: NonLeafTreeNode, adaptiveWeight: CGFloat) -> TilingContainer {
newHList(parent: parent, adaptiveWeight: adaptiveWeight, index: BIND_LAST_INDEX)
newHList(parent: parent, adaptiveWeight: adaptiveWeight, index: INDEX_BIND_LAST)
}

static func newVList(parent: NonLeafTreeNode, adaptiveWeight: CGFloat) -> TilingContainer {
newVList(parent: parent, adaptiveWeight: adaptiveWeight, index: BIND_LAST_INDEX)
newVList(parent: parent, adaptiveWeight: adaptiveWeight, index: INDEX_BIND_LAST)
}
}

0 comments on commit 9bf885c

Please sign in to comment.