Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113import Foundationpublic import ParcelEnginepublic import SwiftUI
@MainActorpublic struct SetupsSettingsView: View, SearchableSettingsPane { static let searchControls: [SettingsControl] = { [ SettingsControl( "setups.save", section: .setups, title: SettingsText.string("Save Setup…"), help: SettingsText.string("Keep a window arrangement ready for the next time you need it.")), SettingsControl("setups.undoDelete", section: .setups, title: SettingsText.string("Undo Delete")), SettingsControl("setups.cancel-opening", section: .setups, title: SettingsText.string("Cancel opening")), ] }()
private let coordinator: SetupCoordinator private let settingsModel: SettingsModel? @State private var actions: SetupActionModel @State private var editor: SetupEditorModel? @State private var loadingFailure: String?
public init(coordinator: SetupCoordinator, settingsModel: SettingsModel? = nil) { self.coordinator = coordinator self.settingsModel = settingsModel _actions = State(initialValue: SetupActionModel(coordinator: coordinator)) }
public var body: some View { Form { Section { Text(Self.control("setups.save").searchText.help) .foregroundStyle(.secondary) HStack { Button(Self.control("setups.save").title, systemImage: "plus", action: newSetup) .accessibilityIdentifier("setups.save") .disabled(actions.isBusy) .settingsSearchTarget(Self.control("setups.save")) if coordinator.canUndoDelete { Button(Self.control("setups.undoDelete").title, action: undoDelete) .accessibilityIdentifier("setups.undoDelete") .disabled(actions.isBusy) .settingsSearchTarget(Self.control("setups.undoDelete")) } } if actions.isBusy { ProgressView(SettingsText.string("Working on setups…")) .accessibilityIdentifier("setups.busy") } if let loading = coordinator.loading { Text(SetupPresentation.loadingTitle(loading.name)) Button(Self.control("setups.cancel-opening").title, action: coordinator.cancelOpening) .settingsSearchTarget(Self.control("setups.cancel-opening")) } SetupFeedbackView( failure: actions.failure ?? coordinator.failure ?? loadingFailure, recoveryURL: coordinator.recoveryURL) } Section { if coordinator.setups.isEmpty { ContentUnavailableView { Label(SettingsText.string("No saved setups"), systemImage: "square.stack") } description: { Text(SettingsText.string("Arrange your windows, then choose Save Setup.")) } } else { ForEach(coordinator.setups) { setup in let index = coordinator.setups.firstIndex { $0.id == setup.id } SetupRowView( setup: setup, status: coordinator.status(for: setup.id), canMoveUp: index.map { $0 > 0 } ?? false, canMoveDown: index.map { $0 < coordinator.setups.count - 1 } ?? false, isBusy: actions.isBusy, open: { actions.run(.open(setup.id)) }, update: { actions.run(.update(setup.id)) }, edit: { editor = SetupEditorModel(setup: setup) }, delete: { actions.run(.delete(setup.id)) }, moveUp: { move(setup.id, offset: -1) }, moveDown: { move(setup.id, offset: 1) }) } } } header: { Text(SettingsText.string("Saved setups")) } } .formStyle(.grouped) .task { await load() } .sheet(item: $editor) { editor in SetupEditorView(model: editor, coordinator: coordinator, settingsModel: settingsModel) { self.editor = nil } } }
private func newSetup() { editor = SetupEditorModel() } private func undoDelete() { actions.run(.undoDelete) }
private func move(_ id: UUID, offset: Int) { guard let index = coordinator.setups.firstIndex(where: { $0.id == id }) else { return } let destination = index + offset guard coordinator.setups.indices.contains(destination) else { return } actions.run(.move(id, to: destination)) }
private func load() async { guard !coordinator.isBusy, !Task.isCancelled else { return } do { try await coordinator.load() loadingFailure = nil } catch { loadingFailure = coordinator.failure == nil ? error.localizedDescription : nil } }}