diff --git a/Sources/PureMouseAppSupport/Presets.swift b/Sources/PureMouseAppSupport/Presets.swift index f5f36fe..26fa676 100644 --- a/Sources/PureMouseAppSupport/Presets.swift +++ b/Sources/PureMouseAppSupport/Presets.swift @@ -98,7 +98,11 @@ public enum DPIPresets { /// The step of `steps` nearest to `dpi` (the sensor's own list; the daemon snaps the same way). public static func snap(_ dpi: Int, to steps: [Int]) -> Int { - steps.min { abs($0 - dpi) < abs($1 - dpi) } ?? dpi + func distance(_ value: Int) -> UInt { + value >= dpi ? UInt(bitPattern: value) &- UInt(bitPattern: dpi) + : UInt(bitPattern: dpi) &- UInt(bitPattern: value) + } + return steps.min { distance($0) < distance($1) } ?? dpi } } diff --git a/Sources/PureMouseSettingsUI/PointerPane.swift b/Sources/PureMouseSettingsUI/PointerPane.swift index 3d8b3d5..c53751a 100644 --- a/Sources/PureMouseSettingsUI/PointerPane.swift +++ b/Sources/PureMouseSettingsUI/PointerPane.swift @@ -1,13 +1,23 @@ import PureMouseAppSupport +import PureMouseKit import SwiftUI +struct PointerEditingState { + var typed: Int? + var reading = DPIReading() + + mutating func scopeChanged() { + typed = nil + reading.clear() + } +} + struct PointerPane: View { @Environment(AppModel.self) private var model @Environment(\.openURL) private var openURL @Environment(\.settings) private var settings - @State private var typed: Int? + @State private var editing = PointerEditingState() @State private var exactValue = false - @State private var reading = DPIReading() static let restoreMessage = "Pointer speed and presets return to PureMouse's defaults." @@ -26,8 +36,8 @@ struct PointerPane: View { private var configured: Int? { model.config.resolvedInt(["dpi"], in: model.scope) } private var live: Int? { model.liveDevice(in: model.scope)?.dpi } - private var current: Int { reading.current(live: live, configured: configured) } - private var estimate: Bool { reading.isEstimate(live: live, configured: configured) } + private var current: Int { editing.reading.current(live: live, configured: configured) } + private var estimate: Bool { editing.reading.isEstimate(live: live, configured: configured) } var body: some View { let steps = steps @@ -46,13 +56,13 @@ struct PointerPane: View { set: { DPIReading.step(at: $0, in: steps).map { setDPI($0, debounce: true) } }), range: 0...Double(max(steps.count - 1, 1)), low: PointerWords.slow, high: PointerWords.fast, - format: { _ in reading.readout(live: live, configured: configured) }, + format: { _ in editing.reading.readout(live: live, configured: configured) }, showsReadout: false, availability: layout.availability, mouseDefault: estimate, standing: speed) // A sensor with a single setting has nothing to slide between. .disabled(steps.count < 2) LabeledContent { if !exactValue { - Text(reading.readout(live: live, configured: configured)) + Text(editing.reading.readout(live: live, configured: configured)) .monospacedDigit().foregroundStyle(.secondary) } } label: { @@ -61,7 +71,7 @@ struct PointerPane: View { if exactValue { LabeledContent { HStack(spacing: 4) { - TextField("Pointer speed", value: Binding(get: { typed ?? current }, set: { typed = $0 }), + TextField("Pointer speed", value: Binding(get: { editing.typed ?? current }, set: { editing.typed = $0 }), format: .number.grouping(.automatic)) .textFieldStyle(.plain) .multilineTextAlignment(.trailing) @@ -98,16 +108,16 @@ struct PointerPane: View { } .buttonStyle(.link) PaneFooter(restoreDefaults: { - typed = nil - reading.clear() + editing.scopeChanged() model.update(in: target) { $0.restorePointerDefaults(in: scope) } }, restoreMessage: model.selectedMouse.map { Self.restoreMessage(for: $0.name) } ?? Self.restoreMessage) } } .disabled(model.readOnly) } - .onChange(of: live) { reading.reported(live, steps: steps) } - .onChange(of: model.writeError) { if model.writeError != nil { reading.clear() } } + .onChange(of: scope) { editing.scopeChanged() } + .onChange(of: live) { editing.reading.reported(live, steps: steps) } + .onChange(of: model.writeError) { if model.writeError != nil { editing.reading.clear() } } .onChange(of: exactValue) { settings?.refit() } .sheet(isPresented: Binding(get: { settings?.sheet == .presets }, set: { if !$0 { settings?.sheet = nil } })) { PresetsSheet(current: current, steps: reportedSteps) { setDPI($0) } @@ -115,14 +125,15 @@ struct PointerPane: View { } private func setDPI(_ dpi: Int, debounce: Bool = false) { - typed = nil + editing.typed = nil guard !model.readOnly else { return } - reading.request(dpi) + editing.reading.request(dpi) model.setDPI(dpi, debounce: debounce, in: model.scopeTarget) } private func commitTyped(_ steps: [Int]) { - guard let typed else { return } - setDPI(DPIPresets.snap(typed, to: steps)) + guard let typed = editing.typed else { return } + let clamped = min(max(typed, Config.dpiRange.lowerBound), Config.dpiRange.upperBound) + setDPI(DPIPresets.snap(clamped, to: steps)) } } diff --git a/Tests/PureMouseAppSupportTests/PointerPresetsTests.swift b/Tests/PureMouseAppSupportTests/PointerPresetsTests.swift index f8d9617..6089ea0 100644 --- a/Tests/PureMouseAppSupportTests/PointerPresetsTests.swift +++ b/Tests/PureMouseAppSupportTests/PointerPresetsTests.swift @@ -21,6 +21,14 @@ import Testing #expect(DPIPresets.newPreset(current: 1650, presets: Array(full.dropLast()), steps: []) == 1650) } + @Test func snappingHandlesIntegerExtremes() { + let steps = [25600, 50, 800] + #expect(DPIPresets.snap(Int.min, to: steps) == 50) + #expect(DPIPresets.snap(Int.max, to: steps) == 25600) + #expect(DPIPresets.snap(Int.min, to: [Int.max, Int.min]) == Int.min) + #expect(DPIPresets.snap(Int.max, to: [Int.min, Int.max]) == Int.max) + } + @Test func theCheckedPresetIsTheOneTheSensorRunsAt() { let steps = [800, 1000, 1600, 1650, 3200] #expect(DPIPresets.selected(in: [1000, 1650], current: 1650, steps: steps) == 1650) diff --git a/Tests/PureMouseSettingsTests/MousePanesTests.swift b/Tests/PureMouseSettingsTests/MousePanesTests.swift index 241a368..34426aa 100644 --- a/Tests/PureMouseSettingsTests/MousePanesTests.swift +++ b/Tests/PureMouseSettingsTests/MousePanesTests.swift @@ -390,3 +390,13 @@ private let swappedVolume = #"{"thumbWheel":{"action":"volume","reversed":true}, // The fitting one first; the one that fills the room (and truncates) only when that does not fit. #expect(components.contains("ViewThatFits(in: .horizontal) {\n menu.fixedSize()\n menu\n }")) } + +@Test func pointerEditingClearsPendingValuesOnScopeChange() { + var editing = PointerEditingState() + editing.reading.request(1600) + editing.typed = 3200 + #expect(editing.reading.current(live: 800, configured: nil) == 1600) + editing.scopeChanged() + #expect(editing.typed == nil) + #expect(editing.reading.current(live: 800, configured: nil) == 800) +}