Files
Kymo/kymo/UI/Components/IntervalControl.swift
wangairnan 6300db3d15 feat(panel): 调整资源卡片读数布局并保留趋势平移
逐核显示 CPU 占用,在 GPU 标题展示核数并补充分项读数。无电池设备显示整机功率。

存储与网络将曲线排在详情下方,CPU、GPU、内存和电池保留整卡趋势。数值直接刷新,曲线随采样平滑平移。顶栏展示版本号,底栏调整开机时间和刷新间隔。
2026-11-15 22:03:46 +08:00

49 lines
2.2 KiB
Swift

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
var fieldWidth: CGFloat = 64
@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: fieldWidth).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()
}
}