Skip to content

Commit

Permalink
Focus is now reset back to the volume slider on dismiss.
Browse files Browse the repository at this point in the history
  • Loading branch information
roeybiran committed Mar 2, 2024
1 parent dc3c39f commit 762bed7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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">
</BreakpointContent>
Expand Down
9 changes: 5 additions & 4 deletions Knobby/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
Expand All @@ -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()
Expand Down Expand Up @@ -70,14 +70,15 @@ struct ContentView: View {
.onAppear {
viewModel.onAppear()
}
.onChange(of: focusedField) {
.onChange(of: focusedSlider) {
viewModel.onFocusedSliderChanged($1)
}
.onChange(of: viewModel.isVisible) {
withAnimation(.bouncy) {
offset = viewModel.isVisible ? .zero : -kHeight
} completion: {
if !viewModel.isVisible {
focusedSlider = .volume
NSApplication.shared.keyWindow?.close()
}
}
Expand Down
16 changes: 12 additions & 4 deletions Knobby/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ final class ViewModel {
}
}

private func dismiss() {
isVisible = false
}

func onVolumeSliderChange(to value: Float) {
audioToolboxClient.setVolume(value)
volumeValue = audioToolboxClient.getVolume()
Expand All @@ -47,19 +51,22 @@ final class ViewModel {
func onAppear() {
volumeValue = audioToolboxClient.getVolume()
brightnessValue = brightnessClient.getBrightness()
focusedSlider = .volume
}

func onFocusedSliderChanged(_ slider: ContentView.FocusedSlider?) {
focusedSlider = slider
}

func onToggleApp() {
isVisible.toggle()
if isVisible {
dismiss()
} else {
isVisible = true
}
}

func onResignKey() {
isVisible = false
dismiss()
}

func onKeyPress(_ keyCode: UInt16) -> KeyPress.Result {
Expand All @@ -74,7 +81,8 @@ final class ViewModel {
case kVK_ANSI_K:
newValue = 1
case kVK_Escape:
isVisible = false
dismiss()
return .handled
default:
return .ignored
}
Expand Down

0 comments on commit 762bed7

Please sign in to comment.