import ParcelConfig import SwiftUI struct MotionSpeedPreferences: View { let model: SettingsModel @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.locale) private var locale private var speed: Double { model.motionSpeedSave.pendingSpeed ?? model.configuration.motion.speed } private var selection: MotionSpeed? { MotionSpeed(rawValue: speed) } private var unavailable: Bool { !model.configuration.motion.enabled || reduceMotion } private var customTitle: String { let number = speed.formatted(.number.locale(locale).grouping(.never).precision(.significantDigits(1...17))) return SettingsText.string("Custom (\(number)×)") } var body: some View { VStack(alignment: .leading, spacing: 12) { MotionPreview(motion: previewMotion) Picker( SettingsView.control("settings.motion.speed").title, selection: Binding( get: { selection }, set: { step in if let step { choose(step) } }) ) { ForEach(MotionSpeed.allCases) { step in Text(step.title).tag(Optional(step)) } } .pickerStyle(.segmented) .accessibilityLabel(SettingsView.control("settings.motion.speed").title) .accessibilityValue(selection?.title ?? customTitle) .accessibilityIdentifier("settings.motion.speed") .settingsSearchTarget(SettingsView.control("settings.motion.speed")) .disabled(unavailable) if let selection { Text(Self.caption(title: selection.title, speed: speed, locale: locale)) .font(.caption) .foregroundStyle(.secondary) .accessibilityHidden(true) } else { HStack { Text(customTitle) Button(SettingsText.string("Reset to Default")) { choose(.default) } .disabled(unavailable) } } if unavailable { Text(SettingsText.string("Motion speed is unavailable while motion is off or Reduce Motion is on.")) .font(.caption) .foregroundStyle(.secondary) } } .onDisappear { model.motionSpeedSave.cancel() } } static func caption(title: String, speed: Double, locale: Locale, bundle: Bundle = .module) -> String { let number = speed.formatted(.number.locale(locale).grouping(.never)) return String(localized: "\(title) · \(number)×", table: "Appearance", bundle: bundle, locale: locale) } private var previewMotion: MotionConfiguration { var motion = model.configuration.motion motion.speed = speed return motion } private func choose(_ step: MotionSpeed) { guard !unavailable else { return } model.motionSpeedSave.choose( step, save: { speed in try await model.setValue(.float(speed), for: .motion(.speed)) }, report: { error in model.report(error) }) } }