Skip to content

Commit 22bf255

Browse files
committed
Update to control prompt to code focus state in reducer
1 parent a0d46ca commit 22bf255

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCode.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public struct PromptToCode: ReducerProtocol {
4343
}
4444
}
4545
}
46+
47+
public enum FocusField: Equatable {
48+
case textField
49+
}
4650

4751
public var id: URL { documentURL }
4852
public var history: HistoryNode
@@ -63,6 +67,7 @@ public struct PromptToCode: ReducerProtocol {
6367
@BindingState public var prompt: String
6468
@BindingState public var isContinuous: Bool
6569
@BindingState public var isAttachedToSelectionRange: Bool
70+
@BindingState public var focusedField: FocusField? = .textField
6671

6772
public var filename: String { documentURL.lastPathComponent }
6873
public var canRevert: Bool { history != .empty }
@@ -114,6 +119,7 @@ public struct PromptToCode: ReducerProtocol {
114119

115120
public enum Action: Equatable, BindableAction {
116121
case binding(BindingAction<State>)
122+
case focusOnTextField
117123
case selectionRangeToggleTapped
118124
case modifyCodeButtonTapped
119125
case revertButtonTapped
@@ -142,6 +148,10 @@ public struct PromptToCode: ReducerProtocol {
142148
switch action {
143149
case .binding:
144150
return .none
151+
152+
case .focusOnTextField:
153+
state.focusedField = .textField
154+
return .none
145155

146156
case .selectionRangeToggleTapped:
147157
state.isAttachedToSelectionRange.toggle()

Core/Sources/SuggestionWidget/FeatureReducers/PromptToCodeGroup.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ public struct PromptToCodeGroup: ReducerProtocol {
8686
Reduce { state, action in
8787
switch action {
8888
case let .activateOrCreatePromptToCode(s):
89-
guard state.activePromptToCode == nil else {
90-
return .none
89+
if let promptToCode = state.activePromptToCode {
90+
return .run { send in
91+
await send(.promptToCode(promptToCode.id, .focusOnTextField))
92+
}
9193
}
9294
return .run { send in
9395
await send(.createPromptToCode(s))

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ extension PromptToCodePanel {
290290

291291
struct Toolbar: View {
292292
let store: StoreOf<PromptToCode>
293-
@FocusState var isInputAreaFocused: Bool
293+
@FocusState var focusField: PromptToCode.State.FocusField?
294294

295295
struct RevertButtonState: Equatable {
296296
var isResponding: Bool
@@ -299,6 +299,7 @@ extension PromptToCodePanel {
299299

300300
struct InputFieldState: Equatable {
301301
@BindingViewState var prompt: String
302+
@BindingViewState var focusField: PromptToCode.State.FocusField?
302303
var isResponding: Bool
303304
}
304305

@@ -326,15 +327,12 @@ extension PromptToCodePanel {
326327
.keyboardShortcut(KeyEquivalent.return, modifiers: [.shift])
327328
}
328329
.background {
329-
Button(action: { isInputAreaFocused = true }) {
330+
Button(action: { focusField = .textField }) {
330331
EmptyView()
331332
}
332333
.keyboardShortcut("l", modifiers: [.command])
333334
}
334335
}
335-
.onAppear {
336-
isInputAreaFocused = true
337-
}
338336
.padding(8)
339337
.background(.ultraThickMaterial)
340338
}
@@ -366,7 +364,13 @@ extension PromptToCodePanel {
366364
var inputField: some View {
367365
WithViewStore(
368366
store,
369-
observe: { InputFieldState(prompt: $0.$prompt, isResponding: $0.isResponding) }
367+
observe: {
368+
InputFieldState(
369+
prompt: $0.$prompt,
370+
focusField: $0.$focusedField,
371+
isResponding: $0.isResponding
372+
)
373+
}
370374
) { viewStore in
371375
ZStack(alignment: .center) {
372376
// a hack to support dynamic height of TextEditor
@@ -389,8 +393,9 @@ extension PromptToCodePanel {
389393
.opacity(viewStore.state.isResponding ? 0.5 : 1)
390394
.disabled(viewStore.state.isResponding)
391395
}
396+
.focused($focusField, equals: .textField)
397+
.bind(viewStore.$focusField, to: $focusField)
392398
}
393-
.focused($isInputAreaFocused)
394399
.padding(8)
395400
.fixedSize(horizontal: false, vertical: true)
396401
}

0 commit comments

Comments
 (0)