@@ -18,10 +18,8 @@ public struct WelcomeWindowView<RecentsView: View, SubtitleView: View>: View {
1818
1919 @FocusState private var focusedField : FocusTarget ?
2020
21- @State private var eventMonitor : Any ?
2221 @State private var recentProjects : [ URL ] = RecentsStore . recentProjectURLs ( )
2322 @State private var selection : Set < URL > = [ ]
24- @State private var actionCount : Int = 0
2523
2624 private let buildActions : ( _ dismissWindow: @escaping ( ) -> Void ) -> WelcomeActions
2725 private let onDrop : ( ( _ url: URL , _ dismiss: @escaping ( ) -> Void ) -> Void ) ?
@@ -95,32 +93,10 @@ public struct WelcomeWindowView<RecentsView: View, SubtitleView: View>: View {
9593 selection = [ recentProjects [ 0 ] ]
9694 }
9795
98- // Determine how many actions are defined
99- switch actions {
100- case . none:
101- actionCount = 0
102- case . one:
103- actionCount = 1
104- case . two:
105- actionCount = 2
106- case . three:
107- actionCount = 3
108- }
109-
11096 // Initial focus
11197 focusedField = . recentProjects
112-
113- // Monitor key input
114- self . eventMonitor = NSEvent . addLocalMonitorForEvents ( matching: [ . keyDown] ) { event in
115- return handleKeyDown ( event)
116- }
117- }
118- . onDisappear {
119- if let monitor = eventMonitor {
120- NSEvent . removeMonitor ( monitor)
121- eventMonitor = nil
122- }
12398 }
99+
124100 . onDrop ( of: [ . fileURL] , isTargeted: . constant( true ) ) { providers in
125101 NSApp . activate ( ignoringOtherApps: true )
126102 providers. forEach {
@@ -135,90 +111,4 @@ public struct WelcomeWindowView<RecentsView: View, SubtitleView: View>: View {
135111 return true
136112 }
137113 }
138-
139- // MARK: - Keyboard Handling
140-
141- private func handleKeyDown( _ event: NSEvent ) -> NSEvent ? {
142-
143- // cmd+W
144- if event. modifierFlags. contains ( . command) , event. keyCode == 13 {
145- dismissWindow ( )
146- return nil
147- }
148-
149- switch event. keyCode {
150- case 126 : // Arrow Up
151- if focusedField == . recentProjects {
152- return handleArrowUp ( ) ? nil : event
153- }
154- case 125 : // Arrow Down
155- if focusedField == . recentProjects {
156- return handleArrowDown ( ) ? nil : event
157- }
158- case 36 , 76 : // Return / Enter
159- if focusedField == . recentProjects {
160- return handleReturnKey ( ) ? nil : event
161- } else if focusedField == . dismissButton {
162- dismissWindow ( )
163- return nil
164- }
165- case 48 : // Tab
166- switchFocus ( )
167- return nil
168- default :
169- break
170- }
171- return event
172- }
173-
174- private func handleArrowUp( ) -> Bool {
175- guard let current = selection. first. flatMap ( { recentProjects. firstIndex ( of: $0) } ) else {
176- selection = Set ( recentProjects. suffix ( 1 ) )
177- return true
178- }
179- if current > 0 {
180- selection = [ recentProjects [ current - 1 ] ]
181- }
182- return true
183- }
184-
185- private func handleArrowDown( ) -> Bool {
186- guard let current = selection. first. flatMap ( { recentProjects. firstIndex ( of: $0) } ) else {
187- selection = Set ( recentProjects. prefix ( 1 ) )
188- return true
189- }
190- if current < recentProjects. count - 1 {
191- selection = [ recentProjects [ current + 1 ] ]
192- }
193- return true
194- }
195-
196- private func handleReturnKey( ) -> Bool {
197- guard let selected = selection. first else { return false }
198- NSDocumentController . shared. openDocument ( at: selected) {
199- dismissWindow ( )
200- }
201- return true
202- }
203-
204- private func switchFocus( ) {
205- let focusOrder : [ FocusTarget ] = {
206- var order : [ FocusTarget ] = [ . dismissButton]
207- if actionCount >= 1 { order. append ( . action1) }
208- if actionCount >= 2 { order. append ( . action2) }
209- if actionCount >= 3 { order. append ( . action3) }
210- order. append ( . recentProjects)
211- return order
212- } ( )
213-
214- guard let current = focusedField,
215- let currentIndex = focusOrder. firstIndex ( of: current) else {
216- focusedField = . dismissButton // start at dismiss
217- return
218- }
219-
220- let nextIndex = ( currentIndex + 1 ) % focusOrder. count
221- focusedField = focusOrder [ nextIndex]
222- }
223-
224114}
0 commit comments