import Foundation public import ParcelConfig import ParcelCore public enum WindowRuleEditing { public static func alwaysFloat(bundleID: String, title: String, role: String?) throws -> RuleConfiguration { guard !bundleID.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: bundleID, titleRegex: title.isEmpty ? nil : "^" + NSRegularExpression.escapedPattern(for: title) + "$", role: role.flatMap { $0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : $0 }, action: .float ) } public static func tilesApp(_ bundleID: String, configuration: ParcelConfiguration) -> Bool { if let action = configuration.rules.last(where: { isAppWide($0, bundleID: bundleID) })?.action { return action == .tile } if configuration.floatRules.contains(where: { $0.bundleID == bundleID && $0.titleRegex == nil && $0.role == nil }) { return false } return !DefaultFloatApps.all.contains(where: { $0.bundleID == bundleID }) || configuration.classification.disabledDefaultFloatApps.contains(bundleID) } static func isAppWide(_ rule: RuleConfiguration, bundleID: String) -> Bool { rule.bundleID == bundleID && rule.appName == nil && rule.titleContains == nil && rule.titleRegex == nil && rule.role == nil && rule.subrole == nil && rule.action != nil } static func isBareAppRule(_ rule: RuleConfiguration, bundleID: String) -> Bool { isAppWide(rule, bundleID: bundleID) && rule.display == nil && rule.position == nil && rule.layout == nil && rule.space == nil && !rule.moveToSpace } static func optionalInteger(_ text: String, locale: Locale = .current) throws -> Int? { guard !text.isEmpty else { return nil } guard let value = Int(exactly: try SettingsNumberParser.integer(text, locale: locale)) else { throw SettingsFailure(SettingsText.string("Enter a whole number.")) } return value } static func assignments(_ rule: RuleConfiguration) -> [(String, TOMLValue)] { var fields: [(String, TOMLValue)] = [] for (key, value) in [ ("bundle-id", rule.bundleID), ("app-name", rule.appName), ("title-contains", rule.titleContains), ("title-regex", rule.titleRegex), ("role", rule.role), ("subrole", rule.subrole), ("action", rule.action?.rawValue), ("display", rule.display), ("layout", rule.layout?.rawValue), ] { if let value { fields.append((key, .string(value))) } } if let position = rule.position { fields.append(("position", .integer(Int64(position)))) } if let space = rule.space { fields.append(("space", .integer(Int64(space)))) } if rule.moveToSpace { fields.append(("move-to-space", .bool(true))) } return fields } } public struct WindowRuleUndo: Equatable, Sendable { private let id = UUID() public let index: Int let rule: RuleConfiguration init(index: Int, rule: RuleConfiguration) { self.index = index self.rule = rule } } extension SettingsModel { public var canUndoLastWindowRule: Bool { guard let pendingWindowRuleUndo else { return false } return configuration.rules.indices.contains(pendingWindowRuleUndo.index) && configuration.rules[pendingWindowRuleUndo.index] == pendingWindowRuleUndo.rule } @discardableResult public func addWindowRule(_ rule: RuleConfiguration) async throws -> Int? { try await addUndoableWindowRule(rule)?.index } public func addUndoableWindowRule(_ rule: RuleConfiguration) async throws -> WindowRuleUndo? { var insertion: WindowRuleUndo? try await edit(.addWindowRule(rule)) { before, after in guard after.rules == before.rules + [rule] else { return } let undo = WindowRuleUndo(index: before.rules.count, rule: rule) insertion = undo self.pendingWindowRuleUndo = undo } return insertion } public func undoLastWindowRule() async throws { guard let pendingWindowRuleUndo else { throw SettingsFailure(SettingsText.string("There is no saved window rule to undo. Review Settings → Apps.")) } try await undoWindowRule(pendingWindowRuleUndo) } public func undoWindowRule(_ undo: WindowRuleUndo) async throws { try await edit( .removeWindowRule(index: undo.index, original: undo.rule), beforeCommit: { guard self.canUndoLastWindowRule, self.pendingWindowRuleUndo == undo else { throw SettingsFailure.conflict } }) } public func removeWindowRule(at index: Int, matching original: RuleConfiguration) async throws { try await edit(.removeWindowRule(index: index, original: original)) } public func setWindowRuleMoveToSpace(_ enabled: Bool, at index: Int, matching original: RuleConfiguration) async throws { try await edit(.windowRuleConsent(index: index, original: original, enabled: enabled)) } public func setDefaultFloatApp(_ bundleID: String, enabled: Bool) async throws { var disabled = configuration.classification.disabledDefaultFloatApps if enabled { disabled.remove(bundleID) } else { disabled.insert(bundleID) } let range = SourceRange(start: SourceLocation(), end: SourceLocation()) try await setValue( .array(disabled.sorted().map { TOMLNode(value: .string($0), range: range) }), for: .disabledDefaultFloatApps ) } public func setAppTiling(_ bundleID: String, enabled: Bool) async throws { try await edit(.appTiling(bundleID: bundleID, enabled: enabled)) } }