Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update window focus logic for consistency #550

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ import Windows

enum SystemWindowRelativeFocusDown {
static func findNextWindow(_ currentWindow: WindowModel, windows: [WindowModel]) -> WindowModel? {
let sortedWindows = windows.sorted { $0.rect.origin.y < $1.rect.origin.y }
let sortedWindows = windows.systemWindows.sorted {
$0.index < $1.index &&
$0.window.rect.origin.y < $1.window.rect.origin.y
}

let intersectingWindows = sortedWindows.filter { window in
let intersectingWindows = sortedWindows.filter { systemWindow in
let currentMinX = currentWindow.rect.origin.x
let currentMaxX = currentWindow.rect.maxX
let windowMinX = window.rect.origin.x
let windowMaxX = window.rect.maxX
return window.rect.origin.y > currentWindow.rect.origin.y
let windowMinX = systemWindow.window.rect.origin.x
let windowMaxX = systemWindow.window.rect.maxX
return systemWindow.window.rect.origin.y > currentWindow.rect.origin.y
&& windowMinX <= currentMaxX && windowMaxX >= currentMinX
}

return intersectingWindows.first
for systemWindow in intersectingWindows {
if systemWindow.window.rect.origin.y >= currentWindow.rect.origin.y {
return systemWindow.window
}
}

for systemWindow in sortedWindows {
if systemWindow.window.rect.origin.y >= currentWindow.rect.origin.y {
return systemWindow.window
}
}

return currentWindow
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,43 @@ import Windows

enum SystemWindowRelativeFocusLeft {
static func findNextWindow(_ currentWindow: WindowModel, windows: [WindowModel]) -> WindowModel? {
let sortedWindows = windows.sorted { $0.rect.origin.x > $1.rect.origin.x }
let sortedWindows = windows.systemWindows.sorted {
$0.index < $1.index &&
$0.window.rect.origin.x > $1.window.rect.origin.x
}

let intersectingWindows = sortedWindows.filter { window in
let intersectingWindows = sortedWindows.filter { model in
let currentMinY = currentWindow.rect.origin.y
let currentMaxY = currentWindow.rect.origin.y + currentWindow.rect.size.height
let windowMinY = window.rect.origin.y
let windowMaxY = window.rect.origin.y + window.rect.size.height
let windowMinY = model.window.rect.origin.y
let windowMaxY = model.window.rect.origin.y + model.window.rect.size.height
return windowMinY < currentMaxY && windowMaxY > currentMinY
}

for window in intersectingWindows {
if window.rect.origin.x <= currentWindow.rect.origin.x {
return window
for systemWindow in intersectingWindows {
if systemWindow.window.rect.origin.x <= currentWindow.rect.origin.x {
return systemWindow.window
}
}

for window in sortedWindows {
if window.rect.origin.x <= currentWindow.rect.origin.x {
return window
for systemWindow in sortedWindows {
if systemWindow.window.rect.origin.x <= currentWindow.rect.origin.x {
return systemWindow.window
}
}

return nil
return currentWindow
}
}

struct SystemWindowModel: Hashable, Sendable {
let window: WindowModel
let index: Int
}

extension Array<WindowModel> {
var systemWindows: [SystemWindowModel] { enumerated().reduce(into: [], { result, entry in
result.append(SystemWindowModel(window: entry.element, index: entry.offset))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ import Windows

enum SystemWindowRelativeFocusRight {
static func findNextWindow(_ currentWindow: WindowModel, windows: [WindowModel]) -> WindowModel? {
let sortedWindows = windows.sorted { $0.rect.origin.x < $1.rect.origin.x }
let sortedWindows = windows.systemWindows.sorted {
$0.index < $1.index &&
$0.window.rect.origin.x > $1.window.rect.origin.x
}

let intersectingWindows = sortedWindows.filter { window in
let intersectingWindows = sortedWindows.filter { systemWindow in
let currentMinY = currentWindow.rect.origin.y
let currentMaxY = currentWindow.rect.origin.y + currentWindow.rect.size.height
let windowMinY = window.rect.origin.y
let windowMaxY = window.rect.origin.y + window.rect.size.height
let windowMinY = systemWindow.window.rect.origin.y
let windowMaxY = systemWindow.window.rect.origin.y + systemWindow.window.rect.size.height
return windowMinY < currentMaxY && windowMaxY > currentMinY
}

for window in intersectingWindows {
if window.rect.origin.x >= currentWindow.rect.origin.x {
return window
for systemWindow in intersectingWindows {
if systemWindow.window.rect.origin.x >= currentWindow.rect.origin.x {
return systemWindow.window
}
}

for window in sortedWindows {
if window.rect.origin.x >= currentWindow.rect.origin.x {
return window
for systemWindow in sortedWindows {
if systemWindow.window.rect.origin.x >= currentWindow.rect.origin.x {
return systemWindow.window
}
}

return nil
return currentWindow
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ import Windows

enum SystemWindowRelativeFocusUp {
static func findNextWindow(_ currentWindow: WindowModel, windows: [WindowModel]) -> WindowModel? {
let sortedWindows = windows.sorted { $0.rect.origin.y > $1.rect.origin.y }

let intersectingWindows = sortedWindows.filter { window in
let sortedWindows = windows.systemWindows.sorted {
$0.index < $1.index &&
$0.window.rect.origin.x > $1.window.rect.origin.x &&
$0.window.rect.origin.y > $1.window.rect.origin.y
}
let intersectingWindows = sortedWindows.filter { systemWindow in
let currentMinX = currentWindow.rect.origin.x
let currentMaxX = currentWindow.rect.maxX
let windowMinX = window.rect.origin.x
let windowMaxX = window.rect.maxX
return window.rect.origin.y < currentWindow.rect.origin.y
let windowMinX = systemWindow.window.rect.origin.x
let windowMaxX = systemWindow.window.rect.maxX
return systemWindow.window.rect.origin.y < currentWindow.rect.origin.y
&& windowMinX <= currentMaxX && windowMaxX >= currentMinX
}

return intersectingWindows.first
for systemWindow in intersectingWindows {
if systemWindow.window.rect.origin.y <= currentWindow.rect.origin.y {
return systemWindow.window
}
}

for systemWindow in sortedWindows {
if systemWindow.window.rect.origin.y <= currentWindow.rect.origin.y {
return systemWindow.window
}
}

return currentWindow
}
}
Loading