import AppKit import SwiftUI // SettingsWindow shows the settings of EPOD Window Bar. In v1 the window shows // the excluded applications list (spec section 7.13). The user adds an // application from the running applications. The user removes an application // from the list. EPOD Window Bar does not resize the windows of an excluded // application. @MainActor final class SettingsWindow: NSObject, NSWindowDelegate { private let settings: Settings private var window: NSWindow? init(settings: Settings) { self.settings = settings super.init() } func show() { if window == nil { build() } window?.center() window?.makeKeyAndOrderFront(nil) NSApp.activate(ignoringOtherApps: true) } private func build() { let root = ExcludedAppsView(settings: settings) let hosting = NSHostingView(rootView: root) let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 440, height: 380), styleMask: [.titled, .closable], backing: .buffered, defer: false) window.title = "EPOD Window Bar — Settings" window.isReleasedWhenClosed = false window.contentView = hosting window.delegate = self self.window = window } // windowShouldClose hides the window. The window stays in memory. func windowShouldClose(_ sender: NSWindow) -> Bool { window?.orderOut(nil) return false } } // AppEntry is one running application in the Add menu. private struct AppEntry: Identifiable { let name: String let bundleID: String var id: String { bundleID } } // ExcludedAppsView is the SwiftUI content of the settings window. private struct ExcludedAppsView: View { @Bindable var settings: Settings @State private var selection: String? var body: some View { VStack(alignment: .leading, spacing: 10) { Text("Excluded applications") .font(.headline) Text("EPOD Window Bar does not resize the windows of an excluded application.") .font(.caption) .foregroundStyle(.secondary) List(selection: $selection) { ForEach(settings.excludedBundleIDs, id: \.self) { bundleID in Text(displayName(for: bundleID)) .tag(bundleID) } } .frame(minHeight: 220) HStack { Menu("Add") { let apps = runningApps() if apps.isEmpty { Text("No application is available.") } else { ForEach(apps) { app in Button(app.name) { settings.addExcluded(app.bundleID) } } } } .frame(width: 90) Button("Remove") { if let selection = selection { settings.removeExcluded(selection) self.selection = nil } } .disabled(selection == nil) Spacer() } } .padding() } // runningApps returns the regular running applications that are not on the // excluded list. The function sorts the applications by name. private func runningApps() -> [AppEntry] { var seen = Set() var result: [AppEntry] = [] for app in NSWorkspace.shared.runningApplications { guard app.activationPolicy == .regular else { continue } guard let bundleID = app.bundleIdentifier else { continue } guard settings.excludedBundleIDs.contains(bundleID) == false else { continue } guard seen.contains(bundleID) == false else { continue } seen.insert(bundleID) result.append(AppEntry(name: app.localizedName ?? bundleID, bundleID: bundleID)) } return result.sorted { $0.name.lowercased() < $1.name.lowercased() } } // displayName returns the application name for a bundle ID. The function // falls back to the bundle ID if no running application has this ID. private func displayName(for bundleID: String) -> String { for app in NSWorkspace.shared.runningApplications where app.bundleIdentifier == bundleID { return app.localizedName ?? bundleID } return bundleID } }