Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ShlomoCode committed Jul 19, 2024
1 parent bf3f7da commit 508d244
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 189 deletions.
4 changes: 0 additions & 4 deletions DockDoor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
0549E5DF2C48BB9500E3AE8D /* AXUIElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0549E5DE2C48BB9500E3AE8D /* AXUIElement.swift */; };
0549E5E12C48BBD100E3AE8D /* CGWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0549E5E02C48BBD100E3AE8D /* CGWindow.swift */; };
0549E5E32C48BD8000E3AE8D /* CGWindowID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0549E5E22C48BD8000E3AE8D /* CGWindowID.swift */; };
0549E5E52C48BF4500E3AE8D /* BackgroundWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0549E5E42C48BF4500E3AE8D /* BackgroundWork.swift */; };
054E3D112C49B93A002046A8 /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 054E3D102C49B93A002046A8 /* Window.swift */; };
054E3D132C49CAB5002046A8 /* CacheUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 054E3D122C49CAB5002046A8 /* CacheUtil.swift */; };
0596C2C32C3E060D00DCABEF /* PrivateApis.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0596C2C22C3E060D00DCABEF /* PrivateApis.swift */; };
Expand Down Expand Up @@ -63,7 +62,6 @@
0549E5DE2C48BB9500E3AE8D /* AXUIElement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AXUIElement.swift; sourceTree = "<group>"; };
0549E5E02C48BBD100E3AE8D /* CGWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGWindow.swift; sourceTree = "<group>"; };
0549E5E22C48BD8000E3AE8D /* CGWindowID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGWindowID.swift; sourceTree = "<group>"; };
0549E5E42C48BF4500E3AE8D /* BackgroundWork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundWork.swift; sourceTree = "<group>"; };
054E3D102C49B93A002046A8 /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
054E3D122C49CAB5002046A8 /* CacheUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheUtil.swift; sourceTree = "<group>"; };
0596C2C22C3E060D00DCABEF /* PrivateApis.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivateApis.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -220,7 +218,6 @@
BB157B7F2C0E8E6700997315 /* MessageUtil.swift */,
BBF6C6232C1B4B4400BF1D40 /* KeybindHelper.swift */,
BB1CBD5C2C1BCA4F003969BC /* Misc Utils.swift */,
0549E5E42C48BF4500E3AE8D /* BackgroundWork.swift */,
BBE7CC932C225D9700AB8F4D /* App Icon.swift */,
BB7CA33C2C31F1B00012E303 /* WindowClosureObservers.swift */,
054E3D122C49CAB5002046A8 /* CacheUtil.swift */,
Expand Down Expand Up @@ -354,7 +351,6 @@
files = (
0531F8D22C3CC04600327808 /* AppearanceSettingsView.swift in Sources */,
BB6A4F1C2C1CFCFF00CB4C9E /* PermissionsSettingsView.swift in Sources */,
0549E5E52C48BF4500E3AE8D /* BackgroundWork.swift in Sources */,
BB3967FD2C291D5A004F1AB6 /* StackedShadow.swift in Sources */,
BBF4139A2C40A64A00AA6733 /* FluidGradient.swift in Sources */,
BB7CA33D2C31F1B00012E303 /* WindowClosureObservers.swift in Sources */,
Expand Down
12 changes: 6 additions & 6 deletions DockDoor/logic/DockObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ final class DockObserver {
}

let eventsOfInterest: CGEventMask = (1 << CGEventType.mouseMoved.rawValue) |
(1 << CGEventType.rightMouseDown.rawValue) |
(1 << CGEventType.leftMouseDown.rawValue) |
(1 << CGEventType.otherMouseDown.rawValue)
(1 << CGEventType.rightMouseDown.rawValue) |
(1 << CGEventType.leftMouseDown.rawValue) |
(1 << CGEventType.otherMouseDown.rawValue)

eventTap = CGEvent.tapCreate(
tap: .cghidEventTap,
Expand Down Expand Up @@ -119,17 +119,17 @@ final class DockObserver {
// Task { [weak self] in
// guard let self = self else { return }
// do {
let activeWindows = WindowsUtil.getRunningAppWindows(for: currentAppUnderMouse)
let appWindows = WindowsUtil.getRunningAppWindows(for: currentAppUnderMouse)
// await MainActor.run {
if activeWindows.isEmpty {
if appWindows.isEmpty {
SharedPreviewWindowCoordinator.shared.hidePreviewWindow()
} else {
let mouseScreen = DockObserver.screenContainingPoint(currentMouseLocation) ?? NSScreen.main!
let convertedMouseLocation = DockObserver.nsPointFromCGPoint(currentMouseLocation, forScreen: mouseScreen)
// Show HoverWindow (using shared instance)
SharedPreviewWindowCoordinator.shared.showWindow(
appName: currentAppUnderMouse.localizedName!,
windows: activeWindows,
windows: appWindows,
mouseLocation: convertedMouseLocation,
mouseScreen: mouseScreen,
onWindowTap: { [weak self] in
Expand Down
77 changes: 0 additions & 77 deletions DockDoor/logic/Utilities/BackgroundWork.swift

This file was deleted.

12 changes: 0 additions & 12 deletions DockDoor/logic/Utilities/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,3 @@ extension pid_t {
return kinfo.kp_proc.p_stat == SZOMB
}
}

extension DispatchQoS {
func toQualityOfService() -> QualityOfService {
switch self {
case .userInteractive: return .userInteractive
case .userInitiated: return .userInitiated
case .utility: return .utility
case .background: return .background
default: return .default
}
}
}
34 changes: 4 additions & 30 deletions DockDoor/logic/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,34 @@ class Window: Equatable {
let wid: CGWindowID
let level: CGWindowLevel
let title: String!
let subrole: String?
let role: String?
let size: CGSize?
let window: SCWindow?
let appName: String
let bundleID: String?
let app: NSRunningApplication
var image: CGImage?
var axElement: AXUIElement!
var closeButton: AXUIElement?
let isMinimized: Bool
let isHidden: Bool
let isFullscreen: Bool

// Equatable protocol
static func == (lhs: Window, rhs: Window) -> Bool {
return lhs.wid == rhs.wid
}

init(wid: CGWindowID, level: CGWindowLevel, title: String!, subrole: String?, role: String?, size: CGSize?, window: SCWindow?, appName: String, bundleID: String?, app: NSRunningApplication, image: CGImage? = nil, axElement: AXUIElement, closeButton: AXUIElement? = nil, isMinimized: Bool, isHidden: Bool) {
init(wid: CGWindowID, app: NSRunningApplication, level: CGWindowLevel, title: String!, size: CGSize?, appName: String, bundleID: String?, image: CGImage? = nil, axElement: AXUIElement, closeButton: AXUIElement? = nil, isMinimized: Bool, isFullscreen: Bool) {
self.wid = wid
self.app = app
self.level = level
self.title = title
self.subrole = subrole
self.role = role
self.size = size
self.window = window
self.appName = appName
self.bundleID = bundleID
self.image = image
self.axElement = axElement
self.closeButton = closeButton
self.isMinimized = isMinimized
self.isHidden = isHidden
self.isFullscreen = isFullscreen
}

/// Toggles the full-screen state of a window.
Expand All @@ -56,12 +50,6 @@ class Window: Equatable {
/// Toggles the minimize state of the window.
func toggleMinimize() {
if self.isMinimized {
// Check if the parent app is hidden
if self.app.isHidden {
// Unhide the entire app
app.unhide()
}

// Un-minimize the window
self.axElement.setAttribute(kAXMinimizedAttribute, false)

Expand All @@ -72,21 +60,7 @@ class Window: Equatable {
self.axElement.setAttribute(kAXMinimizedAttribute, true)
}
}

/// Toggles the hidden state of the window.
func toggleHidden() {
// Toggle the hidden state
let newHiddenState = !self.isHidden
self.axElement.setAttribute(kAXHiddenAttribute, newHiddenState)

// If we're unhiding the app, focus on the specific window
if !newHiddenState {
// Activate the application and specific window with best guess
self.app.activate()
self.focus()
}
}


/// Brings the window to the front and focuses it.
func focus() {
self.axElement.performAction(kAXRaiseAction)
Expand Down
85 changes: 46 additions & 39 deletions DockDoor/logic/WindowsUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class WindowsUtil {
let filteredBundleIdentifiers: [String] = ["com.apple.notificationcenterui"] // filters widgets

/// Captures the image of a given window.
static func getWindowImage(windowID: CGWindowID, bestResolution: Bool) -> CGImage? {
static func getWindowImage(windowID: CGWindowID, bestResolution: Bool) -> CGImage {
CacheUtil.clearExpiredCache()

if let cachedImage = CacheUtil.getCachedImage(for: windowID) {
Expand Down Expand Up @@ -46,54 +46,61 @@ final class WindowsUtil {
}
}

/// Retrieves the close button for a given window reference.
static func getCloseButton(for windowRef: AXUIElement) -> AXUIElement? {
var closeButton: AnyObject?
let result = AXUIElementCopyAttributeValue(windowRef, kAXCloseButtonAttribute as CFString, &closeButton)

// Ensure the result is success and closeButton is not nil
guard result == .success, let closeButtonElement = closeButton else {
return nil
}

return (closeButtonElement as! AXUIElement)
}

static func fetchWindowInfo(axWindow: AXUIElement, app: NSRunningApplication) -> Window? {
return nil // TODO: ?
if let wid = try? axWindow.cgWindowId(),
let title = try? axWindow.title(),
let subrole = try? axWindow.subrole(),
let role = try? axWindow.role(),
let size = try? axWindow.size(),
let level = try? wid.level(),
let isFullscreen = try? axWindow.isFullscreen(),
let isMinimized = try? axWindow.isMinimized(),
let closeButton = try? axWindow.closeButton() {
if AXUIElement.isActualWindow(app, wid, level, title, subrole, role, size) {
let image = getWindowImage(windowID: wid, bestResolution: true)
return Window(
wid: wid,
app: app,
level: level,
title: title,
size: size,
appName: app.localizedName ?? "",
bundleID: app.bundleIdentifier,
image: image,
axElement: axWindow,
closeButton: closeButton,
isMinimized: isMinimized,
isFullscreen: isFullscreen
)
} else {
print("It's not an actual window", axWindow)
}
} else {
print("some error in fetchWindowInfo")
}
return nil
}

static func getRunningAppWindows(for app: NSRunningApplication) -> [Window] {
let appElement = AXUIElementCreateApplication(app.processIdentifier)

// / guard let owningApplication = window.owningApplication,
// window.isOnScreen,
// window.windowLayer == 0,
// window.frame.size.width >= 0,
// window.frame.size.height >= 0,
// !filteredBundleIdentifiers.contains(owningApplication.bundleIdentifier) else {
// return nil
// }

var windows: CFTypeRef?
guard AXUIElementCopyAttributeValue(appElement, kAXWindowsAttribute as CFString, &windows) == .success, let windows = windows as? [AXUIElement] else {
print("Failed to get windows of app \(app)")
return []
}

return windows.compactMap { window in
if let windowInfo = fetchWindowInfo(axWindow: window, app: app) {
if !AXUIElement.isActualWindow(app, windowInfo.wid,windowInfo.level, windowInfo.title, windowInfo.subrole, windowInfo.role,
windowInfo.size) {
print("it's not actual window", windowInfo)
return nil
do {
guard let windows = try appElement.attribute(kAXWindowsAttribute, [AXUIElement].self) else {
return []
}
return windows.compactMap { window in
if let windowIntance = fetchWindowInfo(axWindow: window, app: app) {
return windowIntance
} else {
return nil // probably not real window
}
return windowInfo
} else {
return nil
}
} catch {
print("Field to get app windows")
return []
}
}

}

actor LimitedTaskGroup<T> {
Expand Down
Loading

0 comments on commit 508d244

Please sign in to comment.