Skip to content

Commit e0add4e

Browse files
committed
feat: Implement circular arrow key navigation
The previous implementation of arrow key navigation was incomplete, only handling a few specific transitions. This made navigating the grid of actions with the keyboard feel broken and unintuitive. This commit replaces the sparse `if/else` logic with a comprehensive `switch` statement for each arrow key direction (`up`, `down`, `left`, `right`). This ensures that navigation is circular and predictable from any focused action button, wrapping around the 2x2 grid as expected.
1 parent 95dd03a commit e0add4e

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

Shutdown/ContentView.swift

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,34 +130,62 @@ struct ContentView: View {
130130
}
131131

132132
private func handleRightArrowKey() {
133-
if focusedAction == .sleepComputer {
133+
switch focusedAction {
134+
case .sleepComputer:
134135
focusedAction = .restartComputer
135-
} else if focusedAction == .shutdownComputer {
136+
case .restartComputer:
137+
focusedAction = .sleepComputer
138+
case .shutdownComputer:
136139
focusedAction = .logoutUser
140+
case .logoutUser:
141+
focusedAction = .shutdownComputer
142+
case .none:
143+
focusedAction = .sleepComputer
137144
}
138145
}
139146

140147
private func handleLeftArrowKey() {
141-
if focusedAction == .restartComputer {
148+
switch focusedAction {
149+
case .sleepComputer:
150+
focusedAction = .restartComputer
151+
case .restartComputer:
142152
focusedAction = .sleepComputer
143-
} else if focusedAction == .logoutUser {
153+
case .shutdownComputer:
154+
focusedAction = .logoutUser
155+
case .logoutUser:
144156
focusedAction = .shutdownComputer
157+
case .none:
158+
focusedAction = .sleepComputer
145159
}
146160
}
147161

148162
private func handleUpArrowKey() {
149-
if focusedAction == .shutdownComputer {
163+
switch focusedAction {
164+
case .sleepComputer:
165+
focusedAction = .shutdownComputer
166+
case .restartComputer:
167+
focusedAction = .logoutUser
168+
case .shutdownComputer:
150169
focusedAction = .sleepComputer
151-
} else if focusedAction == .logoutUser {
170+
case .logoutUser:
152171
focusedAction = .restartComputer
172+
case .none:
173+
focusedAction = .sleepComputer
153174
}
154175
}
155176

156177
private func handleDownArrowKey() {
157-
if focusedAction == .sleepComputer {
178+
switch focusedAction {
179+
case .sleepComputer:
158180
focusedAction = .shutdownComputer
159-
} else if focusedAction == .restartComputer {
181+
case .restartComputer:
160182
focusedAction = .logoutUser
183+
case .shutdownComputer:
184+
focusedAction = .sleepComputer
185+
case .logoutUser:
186+
focusedAction = .restartComputer
187+
case .none:
188+
focusedAction = .sleepComputer
161189
}
162190
}
163191
}

0 commit comments

Comments
 (0)