mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 02:29:35 +08:00
30 lines
906 B
Swift
30 lines
906 B
Swift
import SwiftUI
|
|
|
|
extension View {
|
|
func keyboardNavigation(_ onMove: @escaping (_ direction: MoveCommandDirection) -> Void) -> some View {
|
|
modifier(KeyboardNavigationModifier(onMove: onMove))
|
|
}
|
|
}
|
|
|
|
private struct KeyboardNavigationModifier: ViewModifier {
|
|
var onMove: (MoveCommandDirection) -> Void
|
|
|
|
@FocusState private var isFocused
|
|
|
|
func body(content: Content) -> some View {
|
|
content
|
|
.overlay {
|
|
/// Horrible hack to hide the focus ring while still allowing for keyboard navigation.
|
|
Rectangle()
|
|
.frame(width: 0, height: 0)
|
|
.opacity(0)
|
|
.focusable(true)
|
|
.focused($isFocused)
|
|
.onMoveCommand { direction in
|
|
onMove(direction)
|
|
}
|
|
}
|
|
.task { isFocused = true }
|
|
}
|
|
}
|