Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/PureMouseAppSupport/Presets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
41 changes: 26 additions & 15 deletions Sources/PureMouseSettingsUI/PointerPane.swift
Original file line number Diff line number Diff line change
@@ -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."

Expand All @@ -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
Expand All @@ -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: {
Expand All @@ -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)
Expand Down Expand Up @@ -98,31 +108,32 @@ 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) }
}
}

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))
}
}
8 changes: 8 additions & 0 deletions Tests/PureMouseAppSupportTests/PointerPresetsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions Tests/PureMouseSettingsTests/MousePanesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading