Skip to content

Commit

Permalink
5/n Reorganize FrozenTreeNode and closedWindowsCache
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Nov 27, 2024
1 parent 15ec7d0 commit 82ec7be
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
46 changes: 46 additions & 0 deletions Sources/AppBundle/tree/frozen/FrozenTreeNode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import AppKit
import Common

enum FrozenTreeNode {
case container(FrozenContainer)
case window(FrozenWindow)
}

struct FrozenContainer {
let children: [FrozenTreeNode]
let layout: Layout
let orientation: Orientation
let weight: CGFloat

init(_ container: TilingContainer) {
children = container.children.map {
switch $0.nodeCases {
case .window(let w): .window(FrozenWindow(w))
case .tilingContainer(let c): .container(FrozenContainer(c))
case .workspace,
.macosMinimizedWindowsContainer,
.macosHiddenAppsWindowsContainer,
.macosFullscreenWindowsContainer,
.macosPopupWindowsContainer:
illegalChildParentRelation(child: $0, parent: container)
}
}
layout = container.layout
orientation = container.orientation
weight = getWeightOrNil(container) ?? 1
}
}

struct FrozenWindow {
let id: UInt32
let weight: CGFloat

init(_ window: Window) {
id = window.windowId
weight = getWeightOrNil(window) ?? 1
}
}

private func getWeightOrNil(_ node: TreeNode) -> CGFloat? {
((node.parent as? TilingContainer)?.orientation).map { node.getWeight($0) }
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import AppKit
import Common

/// First line of defence against lock screen
///
/// When you lock the screen, all accessibility API becomes unobservable (all attributes become empty, window id
/// becomes nil, etc.) which tricks AeroSpace into thinking that all windows were closed.
/// That's why every time a window dies AeroSpace caches the "entire world" (unless window is already presented in the cache)
/// so that once the screen is unlocked, AeroSpace could restore windows to where they were
private var closedWindowsCache = FrozenWorld(workspaces: [], monitors: [])

struct FrozenMonitor {
let topLeftCorner: CGPoint
Expand All @@ -25,62 +32,10 @@ struct FrozenWorkspace {
floatingWindows = workspace.floatingWindows.map(FrozenWindow.init)
macosUnconventionalWindows =
workspace.macOsNativeHiddenAppsWindowsContainer.children.map { FrozenWindow($0 as! Window) } +
workspace.macOsNativeFullscreenWindowsContainer.children.map { FrozenWindow($0 as! Window) }
}
}

enum FrozenTreeNode {
case container(FrozenContainer)
case window(FrozenWindow)
}

struct FrozenContainer {
let children: [FrozenTreeNode]
let layout: Layout
let orientation: Orientation
let weight: CGFloat

init(_ container: TilingContainer) {
children = container.children.map {
switch $0.nodeCases {
case .window(let w): .window(FrozenWindow(w))
case .tilingContainer(let c): .container(FrozenContainer(c))
case .workspace,
.macosMinimizedWindowsContainer,
.macosHiddenAppsWindowsContainer,
.macosFullscreenWindowsContainer,
.macosPopupWindowsContainer:
illegalChildParentRelation(child: $0, parent: container)
}
}
layout = container.layout
orientation = container.orientation
weight = getWeightOrNil(container) ?? 1
workspace.macOsNativeFullscreenWindowsContainer.children.map { FrozenWindow($0 as! Window) }
}
}

struct FrozenWindow {
let id: UInt32
let weight: CGFloat

init(_ window: Window) {
id = window.windowId
weight = getWeightOrNil(window) ?? 1
}
}

func getWeightOrNil(_ node: TreeNode) -> CGFloat? {
((node.parent as? TilingContainer)?.orientation).map { node.getWeight($0) }
}

/// First line of defence against lock screen
///
/// When you lock the screen, all accessibility API becomes unobservable (all attributes become empty, window id
/// becomes nil, etc.) which tricks AeroSpace into thinking that all windows were closed.
/// That's why every time a window dies AeroSpace caches the "entire world" (unless window is already presented in the cache)
/// so that once the screen is unlocked, AeroSpace could restore windows to where they were
private var closedWindowsCache = FrozenWorld(workspaces: [], monitors: [])

func cacheClosedWindowIfNeeded(window: Window) {
if closedWindowsCache.windowIds.contains(window.windowId) {
return // already cached
Expand Down Expand Up @@ -134,8 +89,7 @@ private func restoreTreeRecursive(frozenContainer: FrozenContainer, parent: NonL
index: index
)

loop:
for (index, child) in frozenContainer.children.enumerated() {
loop: for (index, child) in frozenContainer.children.enumerated() {
switch child {
case .window(let w):
// Stop the loop if can't find the window, because otherwise all the subsequent windows will have incorrect index
Expand Down

0 comments on commit 82ec7be

Please sign in to comment.