feat(menubar): 支持固定居中面板与独立刷新频率及图表浮层
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Generic millisecond interval editor; callers own persistence and scheduling.
|
||||
struct IntervalControl: View {
|
||||
@Binding var milliseconds: Int
|
||||
var range: ClosedRange<Int> = 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)
|
||||
.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: $milliseconds, 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user