Skip to content

Commit

Permalink
feat: show minimized/hidden windows last in the list (closes #289)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Sep 8, 2020
1 parent ae9a922 commit bc760fb
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 171 deletions.
20 changes: 0 additions & 20 deletions src/api-wrappers/HelperExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ extension Collection {
}
}

extension Array where Element == Window {
func firstIndexThatMatches(_ element: AXUIElement, _ wid: CGWindowID?) -> Self.Index? {
// the window can be deallocated by the OS, in which case its `CGWindowID` will be `-1`
// we check for equality both on the AXUIElement, and the CGWindowID, in order to catch all scenarios
return firstIndex { $0.axUiElement == element || ($0.cgWindowId != -1 && $0.cgWindowId == wid) }
}

mutating func insertAndScaleRecycledPool(_ elements: [Element], at i: Int) {
insert(contentsOf: elements, at: i)
let neededRecycledViews = count - ThumbnailsView.recycledViews.count
if neededRecycledViews > 0 {
(1...neededRecycledViews).forEach { _ in ThumbnailsView.recycledViews.append(ThumbnailView()) }
}
}

mutating func insertAndScaleRecycledPool(_ element: Element, at i: Int) {
insertAndScaleRecycledPool([element], at: i)
}
}

extension NSView {
// constrain size to fittingSize
func fit() {
Expand Down
16 changes: 9 additions & 7 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Application: NSObject {
}

func removeWindowslessAppWindow() {
if let windowlessAppWindow = Windows.list.firstIndex { $0.isWindowlessApp == true && $0.application.pid == pid } {
if let windowlessAppWindow = (Windows.list.firstIndex { $0.isWindowlessApp == true && $0.application.pid == pid }) {
Windows.list.remove(at: windowlessAppWindow)
App.app.refreshOpenUi()
}
Expand All @@ -83,6 +83,7 @@ class Application: NSObject {
let subrole = try $0.subrole()
let role = try $0.role()
let isOnNormalLevel = $0.isOnNormalLevel(wid)
debugPrint(self.runningApplication.bundleIdentifier, wid, isOnNormalLevel, title, subrole, role)
if $0.isActualWindow(self.runningApplication, wid, isOnNormalLevel, title, subrole, role) {
return ($0, wid, title, try $0.isFullscreen(), try $0.isMinimized(), try $0.position())
}
Expand Down Expand Up @@ -110,12 +111,13 @@ class Application: NSObject {

private func addWindows(_ axWindows: [(AXUIElement, CGWindowID, String?, Bool, Bool, CGPoint?)]) -> [Window] {
let windows: [Window] = axWindows.compactMap { (axUiElement, wid, axTitle, isFullscreen, isMinimized, position) in
if Windows.list.firstIndexThatMatches(axUiElement, wid) == nil {
return Window(axUiElement, self, wid, axTitle, isFullscreen, isMinimized, position)
if (Windows.list.firstIndex { $0.isEqualRobust(axUiElement, wid) }) == nil {
let window = Window(axUiElement, self, wid, axTitle, isFullscreen, isMinimized, position)
Windows.appendAndUpdateFocus(window)
return window
}
return nil
}
Windows.list.insertAndScaleRecycledPool(windows, at: 0)
if App.app.appIsBeingUsed {
Windows.cycleFocusedWindowIndex(windows.count)
}
Expand All @@ -127,9 +129,9 @@ class Application: NSObject {
runningApplication.activationPolicy == .regular &&
!runningApplication.isTerminated &&
(Windows.list.firstIndex { $0.application.pid == pid }) == nil {
let window = [Window(self)]
Windows.list.insertAndScaleRecycledPool(window, at: Windows.list.count)
return window
let window = Window(self)
Windows.appendAndUpdateFocus(window)
return [window]
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions src/logic/Applications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class Applications {
var windowsOnTheLeftOfFocusedWindow = 0
for runningApp in runningApps {
// comparing pid here can fail here, as it can be already nil; we use isEqual here to avoid the issue
Applications.list.removeAll(where: { $0.runningApplication.isEqual(runningApp) })
Applications.list.removeAll { $0.runningApplication.isEqual(runningApp) }
Windows.list.enumerated().forEach { (index, window) in
if window.application.runningApplication.isEqual(runningApp) && index < Windows.focusedWindowIndex {
windowsOnTheLeftOfFocusedWindow += 1
}
}
Windows.list.removeAll(where: { $0.application.runningApplication.isEqual(runningApp) })
Windows.list.removeAll { $0.application.runningApplication.isEqual(runningApp) }
}
guard Windows.list.count > 0 else { App.app.hideUi(); return }
if windowsOnTheLeftOfFocusedWindow > 0 {
Expand Down
Loading

0 comments on commit bc760fb

Please sign in to comment.