Skip to content

Commit

Permalink
feat: show window partially on-screen (closes #727)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Dec 12, 2020
1 parent b8ad95c commit 00dd767
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/logic/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ class Application: NSObject {
let size = try $0.size()
let isOnNormalLevel = $0.isOnNormalLevel(wid)
if $0.isActualWindow(self.runningApplication, wid, isOnNormalLevel, title, subrole, role, size) {
return ($0, wid, title, try $0.isFullscreen(), try $0.isMinimized(), try $0.position())
return ($0, wid, title, try $0.isFullscreen(), try $0.isMinimized(), try $0.position(), size)
}
}
return nil
} as [(AXUIElement, CGWindowID, String?, Bool, Bool, CGPoint?)]
} as [(AXUIElement, CGWindowID, String?, Bool, Bool, CGPoint?, CGSize?)]
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
var windows = self.addWindows(axWindows)
Expand All @@ -115,10 +115,10 @@ 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
private func addWindows(_ axWindows: [(AXUIElement, CGWindowID, String?, Bool, Bool, CGPoint?, CGSize?)]) -> [Window] {
let windows: [Window] = axWindows.compactMap { (axUiElement, wid, axTitle, isFullscreen, isMinimized, position, size) in
if (Windows.list.firstIndex { $0.isEqualRobust(axUiElement, wid) }) == nil {
let window = Window(axUiElement, self, wid, axTitle, isFullscreen, isMinimized, position)
let window = Window(axUiElement, self, wid, axTitle, isFullscreen, isMinimized, position, size)
Windows.appendAndUpdateFocus(window)
return window
}
Expand Down
4 changes: 3 additions & 1 deletion src/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Window {
var isOnAllSpaces = false
var isWindowlessApp = false
var position: CGPoint?
var size: CGSize?
var spaceId = CGSSpaceID.max
var spaceIndex = SpaceIndex.max
var axUiElement: AXUIElement!
Expand All @@ -32,7 +33,7 @@ class Window {
kAXWindowMovedNotification,
]

init(_ axUiElement: AXUIElement, _ application: Application, _ wid: CGWindowID, _ axTitle: String?, _ isFullscreen: Bool, _ isMinimized: Bool, _ position: CGPoint?) {
init(_ axUiElement: AXUIElement, _ application: Application, _ wid: CGWindowID, _ axTitle: String?, _ isFullscreen: Bool, _ isMinimized: Bool, _ position: CGPoint?, _ size: CGSize?) {
// TODO: make a efficient batched AXUIElementCopyMultipleAttributeValues call once for each window, and store the values
self.axUiElement = axUiElement
self.application = application
Expand All @@ -42,6 +43,7 @@ class Window {
self.isFullscreen = isFullscreen
self.isMinimized = isMinimized
self.position = position
self.size = size
self.title = bestEffortTitle(axTitle)
if !Preferences.hideThumbnails {
refreshThumbnail()
Expand Down
5 changes: 3 additions & 2 deletions src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ class Windows {
}

static func isOnScreen(_ window: Window, _ screen: NSScreen) -> Bool {
if let position = window.position {
if let topLeftCorner = window.position, let size = window.size {
var screenFrameInQuartzCoordinates = screen.frame
screenFrameInQuartzCoordinates.origin.y = NSMaxY(NSScreen.screens[0].frame) - NSMaxY(screen.frame)
return screenFrameInQuartzCoordinates.contains(position)
let windowRect = CGRect(origin: topLeftCorner, size: size)
return windowRect.intersects(screenFrameInQuartzCoordinates)
}
return true
}
Expand Down
4 changes: 2 additions & 2 deletions src/logic/events/AccessibilityEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fileprivate func windowCreated(_ element: AXUIElement, _ pid: pid_t) throws {
let runningApp = NSRunningApplication(processIdentifier: pid),
element.isActualWindow(runningApp, wid, isOnNormalLevel, axTitle, subrole, role, size),
let app = (Applications.list.first { $0.pid == pid }) {
let window = Window(element, app, wid, axTitle, isFullscreen, isMinimized, position)
let window = Window(element, app, wid, axTitle, isFullscreen, isMinimized, position, size)
Windows.appendAndUpdateFocus(window)
Windows.cycleFocusedWindowIndex(1)
App.app.refreshOpenUi([window])
Expand Down Expand Up @@ -124,7 +124,7 @@ fileprivate func focusedWindowChanged(_ element: AXUIElement, _ pid: pid_t) thro
App.app.refreshOpenUi(windows)
} else if element.isActualWindow(runningApp, wid, isOnNormalLevel, axTitle, subrole, role, size),
let app = (Applications.list.first { $0.pid == pid }) {
let window = Window(element, app, wid, axTitle, isFullscreen, isMinimized, position)
let window = Window(element, app, wid, axTitle, isFullscreen, isMinimized, position, size)
Windows.appendAndUpdateFocus(window)
App.app.refreshOpenUi([window])
}
Expand Down

0 comments on commit 00dd767

Please sign in to comment.