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

Enhanced SystemCommand with Minimize All Functionality #452

Merged
merged 1 commit into from
Nov 29, 2023
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
5 changes: 4 additions & 1 deletion App/Sources/Core/Models/Commands/SystemCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct SystemCommand: MetaDataProviding {
switch self {
case .activateLastApplication: "Activate Last Application"
case .applicationWindows: "Application Windows"
case .minimizeAllOpenWindows: "Minimize All Open Windows"
case .missionControl: "Mission Control"
case .showDesktop: "Show Desktop"
case .moveFocusToNextWindowGlobal: "Move Focus to Next Window (All Windows)"
Expand All @@ -27,20 +28,22 @@ struct SystemCommand: MetaDataProviding {
case .moveFocusToPreviousWindowFront: "/System/Library/CoreServices/WidgetKit Simulator.app/Contents/Resources/AppIcon.icns"
case .moveFocusToNextWindow, .moveFocusToNextWindowGlobal: "/System/Library/CoreServices/WidgetKit Simulator.app/Contents/Resources/AppIcon.icns"
case .moveFocusToPreviousWindow, .moveFocusToPreviousWindowGlobal: "/System/Library/CoreServices/WidgetKit Simulator.app/Contents/Resources/AppIcon.icns"
case .minimizeAllOpenWindows: "/System/Applications/Mission Control.app/Contents/Resources/AppIcon.icns"
case .missionControl: "/System/Applications/Mission Control.app/Contents/Resources/AppIcon.icns"
case .showDesktop: "/System/Library/CoreServices/Dock.app/Contents/Resources/Dock.icns"
}
}

case activateLastApplication
case applicationWindows
case minimizeAllOpenWindows
case missionControl
case moveFocusToNextWindowFront
case moveFocusToPreviousWindowFront
case moveFocusToNextWindow
case moveFocusToPreviousWindow
case moveFocusToNextWindowGlobal
case moveFocusToPreviousWindowGlobal
case missionControl
case showDesktop
}
var kind: Kind
Expand Down
3 changes: 2 additions & 1 deletion App/Sources/Core/Runners/KeyboardCowboyEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ final class KeyboardCowboyEngine {
self?.reload(with: application)
}

WindowStore.shared.subscribe(to: UserSpace.shared.$frontMostApplication)

guard KeyboardCowboy.env() == .production else { return }

applicationTriggerController.subscribe(to: UserSpace.shared.$frontMostApplication)
applicationTriggerController.subscribe(to: UserSpace.shared.$runningApplications)
applicationTriggerController.subscribe(to: contentStore.groupStore.$groups)
WindowStore.shared.subscribe(to: UserSpace.shared.$frontMostApplication)
}

private func reload(with application: UserSpace.Application) {
Expand Down
34 changes: 34 additions & 0 deletions App/Sources/Core/Runners/System/SystemMinimizeAllWindows.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Apps
import Foundation
import Carbon
import Cocoa
import MachPort

enum SystemMinimizeAllWindows {
static func run(_ snapshot: UserSpace.Snapshot,
machPort: MachPortEventController) throws {
Task {
let menuBar = MenuBarCommandRunner()
var uniqueRunningApplications: [Application] = []
for window in snapshot.windows.visibleWindowsInSpace {
guard let runningApplication = NSWorkspace.shared.runningApplications
.first(where: { $0.localizedName == window.ownerName }),
let bundleIdentifier = runningApplication.bundleIdentifier else { continue }

if !uniqueRunningApplications.contains(where: { $0.bundleIdentifier == bundleIdentifier }),
let app = ApplicationStore.shared.application(for: bundleIdentifier) {
uniqueRunningApplications.append(app)
let configuration = NSWorkspace.OpenConfiguration()
configuration.activates = true

let url = URL(fileURLWithPath: app.path)
try Task.checkCancellation()
try await NSWorkspace.shared.openApplication(at: url, configuration: configuration)
try await menuBar.execute(.init(tokens: [
.menuItem(name: "Window"), .menuItem(name: "Minimize All")
]))
}
}
}
}
}
3 changes: 3 additions & 0 deletions App/Sources/Core/Runners/SystemCommandRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ final class SystemCommandRunner: @unchecked Sendable {
kind: command.kind,
snapshot: snapshot
)
case .minimizeAllOpenWindows:
guard let machPort else { return }
try SystemMinimizeAllWindows.run(snapshot, machPort: machPort)
case .showDesktop:
Dock.run(.showDesktop)
case .applicationWindows:
Expand Down
Loading