Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274import AppKitimport ParcelConfigimport ParcelCoreimport SwiftUIimport UniformTypeIdentifiers
struct DefaultFloatAppsSection: View, SearchableSettingsPane { let model: SettingsModel
static let searchControls: [SettingsControl] = { DefaultFloatApps.all.map { app in SettingsControl( "settings.apps.default." + app.bundleID, section: .apps, title: app.settingsName, help: defaultFloatHelp, synonyms: [app.bundleID]) } }()
private static var defaultFloatHelp: String { SettingsText.string( "Utility apps keep their own size. Turn an app off to let normal windows tile; dialogs still float.") }
var body: some View { Section(SettingsText.string("Float by default")) { Text(Self.defaultFloatHelp) ForEach(DefaultFloatApps.all, id: \.bundleID) { app in Toggle( Self.control("settings.apps.default." + app.bundleID).title, isOn: Binding( get: { !model.configuration.classification.disabledDefaultFloatApps.contains(app.bundleID) }, set: { enabled in Task { do { try await model.setDefaultFloatApp(app.bundleID, enabled: enabled) } catch { model.report(error) } } }) ) .accessibilityIdentifier("settings.apps.default." + app.bundleID) .help(app.bundleID) .settingsSearchTarget(Self.control("settings.apps.default." + app.bundleID)) } } }}
struct WindowRulesSection: View, SearchableSettingsPane { let model: SettingsModel
static let searchControls: [SettingsControl] = { [ SettingsControl( "settings.rules.add-window", section: .rules, title: SettingsText.string("Add window rule"), help: matchingHelp), SettingsControl( "settings.rules.choose-application", section: .rules, title: SettingsText.string("Choose from Applications…"), help: SettingsText.string("Keep all windows from the chosen app floating.")), SettingsControl( "settings.rules.undo-last", section: .rules, title: SettingsText.string("Undo last window rule"), help: matchingHelp), SettingsControl( "settings.rules.remove-window", section: .rules, title: SettingsText.string("Remove window rule"), help: matchingHelp), SettingsControl( "settings.rules.space-move-consent", section: .rules, title: SettingsText.string("Allow automatic moves to this Space"), help: SettingsText.string( "Moving a window briefly switches desktops and uses your mouse and keyboard. Only new matching windows move. Leave this off to keep windows on their current desktops." )), ] }()
private static var matchingHelp: String { SettingsText.string("All conditions must match. Later rules take priority.") } @SwiftUI.State private var adding = false
var body: some View { Section(SettingsText.string("Window rules")) { Text(Self.matchingHelp) HStack { Button(Self.control("settings.rules.add-window").title) { adding = true } .accessibilityIdentifier("settings.rules.add-window") .settingsSearchTarget(Self.control("settings.rules.add-window")) Button(Self.control("settings.rules.undo-last").title) { Task { do { try await model.undoLastWindowRule() } catch { model.report(error) } } } .disabled(!model.canUndoLastWindowRule || model.isSaving) .accessibilityIdentifier("settings.rules.undo-last") .settingsSearchTarget(Self.control("settings.rules.undo-last")) } Button(Self.control("settings.rules.choose-application").title) { chooseApplication() } .accessibilityIdentifier("settings.rules.choose-application") .accessibilityHint(Self.control("settings.rules.choose-application").searchText.help) .settingsSearchTarget(Self.control("settings.rules.choose-application")) .disabled(model.isSaving) Text(Self.control("settings.rules.choose-application").searchText.help) .font(.caption).foregroundStyle(.secondary) ForEach(Array(model.configuration.rules.enumerated()), id: \.offset) { index, rule in VStack(alignment: .leading, spacing: 8) { ForEach(WindowRuleEditing.assignments(rule), id: \.0) { key, value in LabeledContent(WindowRulesText.field(key), value: ruleValue(value)) } if rule.space != nil { RuleSpaceMoveConsent( enabled: Binding( get: { rule.moveToSpace }, set: { enabled in Task { do { try await model.setWindowRuleMoveToSpace(enabled, at: index, matching: rule) } catch { model.report(error) } } }), searchInstance: String(index) ) .disabled(model.isSaving) .accessibilityIdentifier("settings.rules.move-to-space.\(index)") } Button(Self.control("settings.rules.remove-window").title, role: .destructive) { Task { do { try await model.removeWindowRule(at: index, matching: rule) } catch { model.report(error) } } } .accessibilityLabel(SettingsText.string("Remove window rule \((index + 1).formatted())")) .settingsSearchTarget(Self.control("settings.rules.remove-window"), instance: String(index)) } } } .sheet(isPresented: $adding) { WindowRuleEditor(model: model) } }
private func chooseApplication() { let panel = NSOpenPanel() panel.allowedContentTypes = [.applicationBundle] panel.directoryURL = URL(fileURLWithPath: "/Applications", isDirectory: true) panel.allowsMultipleSelection = false panel.begin { response in guard response == .OK else { return } let url = panel.url Task { @MainActor in do { let rule = try Self.floatingRule(for: url) try await model.addWindowRule(rule) } catch { model.report(error) } } } }
static func floatingRule(for selection: URL?) throws -> RuleConfiguration { guard let url = selection else { throw SettingsFailure(SettingsText.string("The selected app could not be read. Try again.")) } guard url.isFileURL, url.pathExtension.lowercased() == "app", let bundle = Bundle(url: url), let identifier = bundle.bundleIdentifier, !identifier.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw SettingsFailure( SettingsText.string( "This app has no bundle identifier. Create a window rule in Settings → Apps using its app name.")) } return RuleConfiguration(bundleID: identifier, action: .float) }
private func ruleValue(_ value: TOMLValue) -> String { switch value { case .string(let text): text case .integer(let number): number.formatted() case .bool(let enabled): enabled ? SettingsText.string("Yes") : SettingsText.string("No") default: preconditionFailure("Unexpected window rule value") } }}
private struct RuleSpaceMoveConsent: View { @Binding var enabled: Bool var searchInstance: String? = nil
var body: some View { VStack(alignment: .leading, spacing: 8) { Toggle(WindowRulesSection.control("settings.rules.space-move-consent").title, isOn: $enabled) .accessibilityLabel(WindowRulesSection.control("settings.rules.space-move-consent").title) .accessibilityIdentifier("settings.rules.space-move-consent") .settingsSearchTarget( WindowRulesSection.control("settings.rules.space-move-consent"), instance: searchInstance) Text(WindowRulesSection.control("settings.rules.space-move-consent").searchText.help) .font(.caption) } }}
struct WindowRuleEditor: View { let model: SettingsModel @Environment(\.dismiss) private var dismiss @SwiftUI.State private var bundleID = "" @SwiftUI.State private var appName = "" @SwiftUI.State private var title = "" @SwiftUI.State private var titleRegex = "" @SwiftUI.State private var role = "" @SwiftUI.State private var subrole = "" @SwiftUI.State private var display = "" @SwiftUI.State private var position = "" @SwiftUI.State private var space = "" @SwiftUI.State private var moveToSpace = false @SwiftUI.State private var action: RuleAction? = .float @SwiftUI.State private var layout: ConfigLayout? @SwiftUI.State private var saving = false @SwiftUI.State private var failure: String?
var body: some View { Form { TextField(SettingsText.string("Bundle identifier"), text: $bundleID) TextField(SettingsText.string("Application name"), text: $appName) TextField(SettingsText.string("Window title contains"), text: $title) TextField(SettingsText.string("Window title pattern"), text: $titleRegex) TextField(SettingsText.string("Accessibility role"), text: $role) TextField(SettingsText.string("Accessibility subrole"), text: $subrole) Picker(SettingsText.string("Action"), selection: $action) { Text(SettingsText.string("Unchanged")).tag(Optional<RuleAction>.none) ForEach(RuleAction.allCases, id: \.self) { Text($0.settingsTitle).tag(Optional($0)) } } Picker(SettingsText.string("Layout"), selection: $layout) { Text(SettingsText.string("Unchanged")).tag(Optional<ConfigLayout>.none) ForEach(ConfigLayout.allCases, id: \.self) { Text($0.settingsTitle).tag(Optional($0)) } } TextField(SettingsText.string("Display identifier"), text: $display) TextField(SettingsText.string("Position (zero-based)"), text: $position) TextField(SettingsText.string("Native Space number (1–16)"), text: $space) if !space.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { RuleSpaceMoveConsent(enabled: $moveToSpace) .accessibilityIdentifier("settings.rules.editor.move-to-space") } if let failure { Text(failure).textSelection(.enabled) } HStack { Button(SettingsText.string("Cancel")) { dismiss() } Button(SettingsText.string("Save")) { save() } } } .formStyle(.grouped) .disabled(saving) .frame(width: 540, height: 700) .onChange(of: space) { _, value in if value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { moveToSpace = false } } }
private func save() { saving = true Task { defer { saving = false } do { let rule = RuleConfiguration( bundleID: optional(bundleID), appName: optional(appName), titleContains: optional(title), titleRegex: optional(titleRegex), role: optional(role), subrole: optional(subrole), action: action, display: optional(display), position: try number(position), layout: layout, space: try number(space), moveToSpace: moveToSpace) try await model.addWindowRule(rule) dismiss() } catch { failure = SettingsFailure(error: error).message } } }
private func optional(_ value: String) -> String? { value.isEmpty ? nil : value } private func number(_ value: String) throws -> Int? { try WindowRuleEditing.optionalInteger(value) }}