-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54282a8
commit 21e7cff
Showing
11 changed files
with
139 additions
and
29 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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
func resizedObs(_ obs: AXObserver, ax: AXUIElement, notif: CFString, data: UnsafeMutableRawPointer?) { | ||
if let window = data?.window { | ||
resizeWithMouseIfNeeded(window) | ||
} | ||
refresh() | ||
} | ||
|
||
func resetResizeWithMouseIfPossible() { | ||
precondition(Thread.current.isMainThread) | ||
if currentlyResizedWithMouseWindowId != nil { | ||
currentlyResizedWithMouseWindowId = nil | ||
for workspace in Workspace.all { | ||
workspace.resetResizeWeightBeforeResizeRecursive() | ||
} | ||
refresh() | ||
} | ||
} | ||
|
||
var currentlyResizedWithMouseWindowId: UInt32? = nil | ||
|
||
private let adaptiveWeightBeforeResizeWithMouseKey = TreeNodeUserDataKey<CGFloat>(key: "adaptiveWeightBeforeResizeWithMouseKey") | ||
|
||
private func resizeWithMouseIfNeeded(_ window: MacWindow) { | ||
if window.workspace != Workspace.focused { | ||
return // Don't allow to resize windows of hidden workspaces | ||
} | ||
if focusedWindow != window { | ||
return | ||
} | ||
if NSEvent.pressedMouseButtons != 1 { // If mouse left button isn't pressed | ||
return | ||
} | ||
switch window.parent.kind { | ||
case .window: | ||
windowsCantHaveChildren() | ||
case .workspace: | ||
return // Nothing to do for floating windows | ||
case .tilingContainer: | ||
guard let rect = window.getRect() else { return } | ||
guard let lastLayoutedRect = window.lastLayoutedRect else { return } | ||
let (lParent, lOwnIndex) = window.closestParent(hasChildrenInDirection: .left) ?? (nil, nil) | ||
let (dParent, dOwnIndex) = window.closestParent(hasChildrenInDirection: .down) ?? (nil, nil) | ||
let (uParent, uOwnIndex) = window.closestParent(hasChildrenInDirection: .up) ?? (nil, nil) | ||
let (rParent, rOwnIndex) = window.closestParent(hasChildrenInDirection: .right) ?? (nil, nil) | ||
let table: [(CGFloat, TilingContainer?, Int?, Int?, Int?)] = [ | ||
(lastLayoutedRect.minX - rect.minX, lParent, lOwnIndex, 0, lOwnIndex), // Horizontal, to the left of the window | ||
(rect.maxY - lastLayoutedRect.maxY, dParent, dOwnIndex, dOwnIndex.map { $0 + 1 }, dParent?.children.count), // Vertical, to the down of the window | ||
(lastLayoutedRect.minY - rect.minY, uParent, uOwnIndex, 0, uOwnIndex), // Vertical, to the up of the window | ||
(rect.maxX - lastLayoutedRect.maxX, rParent, rOwnIndex, rOwnIndex.map { $0 + 1 }, rParent?.children.count), // Horizontal, to the right of the window | ||
] | ||
for (diff, parent, ownIndex, startIndex, pastTheEndIndex) in table { | ||
if let parent, let ownIndex, let startIndex, let pastTheEndIndex, let child = parent.children.getOrNil(atIndex: ownIndex), | ||
pastTheEndIndex - startIndex > 0 && abs(diff) > EPS { | ||
let delta = diff.div(pastTheEndIndex - startIndex)! | ||
let orientation = parent.orientation | ||
|
||
child.setWeight(orientation, child.getWeightBeforeResize(orientation) + diff) | ||
for sibling in parent.children[startIndex..<pastTheEndIndex] { | ||
sibling.setWeight(orientation, sibling.getWeightBeforeResize(orientation) - delta) | ||
} | ||
} | ||
} | ||
currentlyResizedWithMouseWindowId = window.windowId | ||
} | ||
} | ||
|
||
private extension TreeNode { | ||
func getWeightBeforeResize(_ orientation: Orientation) -> CGFloat { | ||
getUserData(key: adaptiveWeightBeforeResizeWithMouseKey) | ||
?? getWeight(orientation).also { putUserData(key: adaptiveWeightBeforeResizeWithMouseKey, data: $0) } | ||
} | ||
|
||
func resetResizeWeightBeforeResizeRecursive() { | ||
cleanUserData(key: adaptiveWeightBeforeResizeWithMouseKey) | ||
for child in children { | ||
child.resetResizeWeightBeforeResizeRecursive() | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
protocol AeroAny {} | ||
|
||
extension AeroAny { | ||
@discardableResult | ||
@inlinable | ||
func apply(_ block: (Self) -> Void) -> Self { | ||
block(self) | ||
return self | ||
} | ||
|
||
@discardableResult | ||
@inlinable | ||
func also(_ block: (Self) -> Void) -> Self { | ||
block(self) | ||
return self | ||
} | ||
|
||
@inlinable func takeIf(_ predicate: (Self) -> Bool) -> Self? { predicate(self) ? self : nil } | ||
} | ||
|
||
extension TreeNode: AeroAny {} | ||
extension Writer: AeroAny {} | ||
extension Int: AeroAny {} | ||
extension CGFloat: AeroAny {} |
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