Skip to content

Commit

Permalink
Enhanced SystemCommand with Minimize All Functionality
Browse files Browse the repository at this point in the history
Added a new functionality to the SystemCommand enumeration that allows users to minimize all open windows. This feature is designed to quickly declutter the desktop and provide users with a more organized workspace. As a consequence, the SystemMinimizeAllWindows runner has been created to handle the execution of this command. To support this new command, corresponding icons and display values have been included to facilitate user interaction and enhance overall usability. Additionally, integrated this functionality into the SystemCommandRunner for seamless operation within the application.
  • Loading branch information
zenangst committed Nov 29, 2023
1 parent c1c1529 commit dd458a7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
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

0 comments on commit dd458a7

Please sign in to comment.