From 762bed772a7e835fa586cfdf4674950f45935a95 Mon Sep 17 00:00:00 2001 From: roeybiran Date: Sat, 2 Mar 2024 20:56:40 +0200 Subject: [PATCH] Focus is now reset back to the volume slider on dismiss. --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 4 ++-- Knobby/ContentView.swift | 9 +++++---- Knobby/ViewModel.swift | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Knobby.xcodeproj/xcuserdata/roey.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/Knobby.xcodeproj/xcuserdata/roey.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 1ec3938..d42ebea 100644 --- a/Knobby.xcodeproj/xcuserdata/roey.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/Knobby.xcodeproj/xcuserdata/roey.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -30,8 +30,8 @@ filePath = "Knobby/ViewModel.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "44" - endingLineNumber = "44" + startingLineNumber = "48" + endingLineNumber = "48" landmarkName = "onBrightnessSliderChange(to:)" landmarkType = "7"> diff --git a/Knobby/ContentView.swift b/Knobby/ContentView.swift index 530c214..b984e6c 100644 --- a/Knobby/ContentView.swift +++ b/Knobby/ContentView.swift @@ -8,7 +8,7 @@ private let kWidth = 480.0 struct ContentView: View { @Bindable var viewModel: ViewModel - @FocusState private var focusedField: FocusedSlider? + @FocusState private var focusedSlider: FocusedSlider? @State private var isPopoverShown = false @State private var offset = -kHeight let statusItem: NSStatusItem @@ -30,7 +30,7 @@ struct ContentView: View { Image(systemName: "speaker.wave.3.fill") .accessibilityLabel("Volume") } - .focused($focusedField, equals: .volume) + .focused($focusedSlider, equals: .volume) Slider( value: .init( get: { viewModel.brightnessValue }, @@ -39,7 +39,7 @@ struct ContentView: View { Image(systemName: "sun.max.fill") .accessibilityLabel("Brightness") } - .focused($focusedField, equals: .brightness) + .focused($focusedSlider, equals: .brightness) Button { isPopoverShown.toggle() @@ -70,7 +70,7 @@ struct ContentView: View { .onAppear { viewModel.onAppear() } - .onChange(of: focusedField) { + .onChange(of: focusedSlider) { viewModel.onFocusedSliderChanged($1) } .onChange(of: viewModel.isVisible) { @@ -78,6 +78,7 @@ struct ContentView: View { offset = viewModel.isVisible ? .zero : -kHeight } completion: { if !viewModel.isVisible { + focusedSlider = .volume NSApplication.shared.keyWindow?.close() } } diff --git a/Knobby/ViewModel.swift b/Knobby/ViewModel.swift index fc0d3e2..1365481 100644 --- a/Knobby/ViewModel.swift +++ b/Knobby/ViewModel.swift @@ -34,6 +34,10 @@ final class ViewModel { } } + private func dismiss() { + isVisible = false + } + func onVolumeSliderChange(to value: Float) { audioToolboxClient.setVolume(value) volumeValue = audioToolboxClient.getVolume() @@ -47,7 +51,6 @@ final class ViewModel { func onAppear() { volumeValue = audioToolboxClient.getVolume() brightnessValue = brightnessClient.getBrightness() - focusedSlider = .volume } func onFocusedSliderChanged(_ slider: ContentView.FocusedSlider?) { @@ -55,11 +58,15 @@ final class ViewModel { } func onToggleApp() { - isVisible.toggle() + if isVisible { + dismiss() + } else { + isVisible = true + } } func onResignKey() { - isVisible = false + dismiss() } func onKeyPress(_ keyCode: UInt16) -> KeyPress.Result { @@ -74,7 +81,8 @@ final class ViewModel { case kVK_ANSI_K: newValue = 1 case kVK_Escape: - isVisible = false + dismiss() + return .handled default: return .ignored }