import SwiftUI /// Generic millisecond interval editor; callers own persistence and scheduling. struct IntervalControl: View { @Binding var milliseconds: Int var range: ClosedRange = 500...1_800_000 var step = 100 @State private var text = "" @FocusState private var focused: Bool var body: some View { HStack(spacing: 3) { TextField("sampling.seconds", text: $text) .labelsHidden() .textFieldStyle(.roundedBorder).multilineTextAlignment(.trailing) .frame(width: 64).focused($focused) .onChange(of: text) { _, value in if let seconds = Double(value.replacingOccurrences(of: ",", with: ".")), seconds.isFinite, seconds >= Double(range.lowerBound) / 1000, seconds <= Double(range.upperBound) / 1000 { milliseconds = quantized(seconds) } } .onSubmit { commit() } Text("sampling.unit").foregroundStyle(.secondary).fixedSize() Stepper("sampling.interval", value: Binding(get: { milliseconds }, set: { milliseconds = $0 synchronize() }), in: range, step: step).labelsHidden().fixedSize() } .font(.system(size: 11)).monospacedDigit().controlSize(.small) .fixedSize(horizontal: true, vertical: false) .onAppear { synchronize() } .onChange(of: milliseconds) { _, _ in if !focused { synchronize() } } .onChange(of: focused) { _, value in if !value { commit() } } .help(Text("sampling.bounds")) } private func quantized(_ seconds: Double) -> Int { let clamped = min(Double(range.upperBound), max(Double(range.lowerBound), seconds * 1000)) return min(range.upperBound, max(range.lowerBound, Int((clamped / Double(step)).rounded()) * step)) } private func synchronize() { text = String(format: "%.1f", Double(milliseconds) / 1000) } private func commit() { if let seconds = Double(text.replacingOccurrences(of: ",", with: ".")), seconds.isFinite { milliseconds = quantized(seconds) } synchronize() } }