Skip to content

Commit be51d40

Browse files
committed
Better transition
1 parent c40e46b commit be51d40

File tree

5 files changed

+66
-25
lines changed

5 files changed

+66
-25
lines changed

Core/Sources/Service/GUI/PromptToCodeProvider+Service.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extension PromptToCodeProvider {
6060
let handler = PseudoCommandHandler()
6161
await handler.acceptSuggestion()
6262
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
63+
try await Task.sleep(nanoseconds: 200_000_000)
6364
app.activate()
6465
}
6566
}

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ final class WidgetDataSource {
5555
let presenter = PresentInWindowSuggestionPresenter()
5656
presenter.closeChatRoom(fileURL: url)
5757
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
58-
app.activate()
58+
Task { @MainActor in
59+
try await Task.sleep(nanoseconds: 200_000_000)
60+
app.activate()
61+
}
5962
}
6063
},
6164
onSwitchContext: { [weak self] in
@@ -122,7 +125,10 @@ final class WidgetDataSource {
122125
let presenter = PresentInWindowSuggestionPresenter()
123126
presenter.closePromptToCode(fileURL: url)
124127
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
125-
app.activate()
128+
Task { @MainActor in
129+
try await Task.sleep(nanoseconds: 200_000_000)
130+
app.activate()
131+
}
126132
}
127133
}
128134
)
@@ -179,6 +185,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
179185
if let app = ActiveApplicationMonitor.previousActiveApplication,
180186
app.isXcode
181187
{
188+
try await Task.sleep(nanoseconds: 200_000_000)
182189
app.activate()
183190
}
184191
}
@@ -190,6 +197,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
190197
if let app = ActiveApplicationMonitor.previousActiveApplication,
191198
app.isXcode
192199
{
200+
try await Task.sleep(nanoseconds: 200_000_000)
193201
app.activate()
194202
}
195203
}

Core/Sources/SuggestionWidget/FeatureReducers/ChatPanelFeature.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ struct ChatPanelFeature: ReducerProtocol {
3939
switch action {
4040
case .hideButtonClicked:
4141
state.isPanelDisplayed = false
42-
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
43-
app.activate()
42+
43+
return .run { _ in
44+
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
45+
try await Task.sleep(nanoseconds: 200_000_000)
46+
app.activate()
47+
}
4448
}
45-
return .none
4649

4750
case .toggleChatPanelDetachedButtonClicked:
4851
state.chatPanelInASeparateWindow.toggle()
@@ -55,7 +58,7 @@ struct ChatPanelFeature: ReducerProtocol {
5558
case .attachChatPanel:
5659
state.chatPanelInASeparateWindow = false
5760
return .none
58-
61+
5962
case .closeChatPanel:
6063
state.chat = nil
6164
return .none
@@ -69,7 +72,7 @@ struct ChatPanelFeature: ReducerProtocol {
6972
guard let provider = await fetchChatProvider(
7073
fileURL: xcodeInspector.activeDocumentURL
7174
) else { return }
72-
75+
7376
if oldChatProviderId != provider.id {
7477
await send(.updateChatProvider(provider))
7578
}
@@ -106,3 +109,4 @@ struct ChatPanelFeature: ReducerProtocol {
106109
.chatForFile(at: fileURL)
107110
}
108111
}
112+

Core/Sources/SuggestionWidget/FeatureReducers/PanelFeature.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ struct PanelFeature: ReducerProtocol {
1919
case presentPromptToCode
2020
case presentPanelContent(SharedPanelFeature.Content, shouldDisplay: Bool)
2121
case discardPanelContent
22-
case updatePanelContent
2322
case removeDisplayedContent
23+
case switchToAnotherEditorAndUpdateContent
2424

2525
case sharedPanel(SharedPanelFeature.Action)
2626
case suggestionPanel(SuggestionPanelFeature.Action)
@@ -48,13 +48,13 @@ struct PanelFeature: ReducerProtocol {
4848
) else { return }
4949
let content = SharedPanelFeature.Content.suggestion(provider)
5050
await send(.presentPanelContent(content, shouldDisplay: true))
51-
}
51+
}.animation(.easeInOut(duration: 0.2))
5252

5353
case let .presentError(errorDescription):
5454
return .run { send in
5555
let content = SharedPanelFeature.Content.error(errorDescription)
5656
await send(.presentPanelContent(content, shouldDisplay: true))
57-
}
57+
}.animation(.easeInOut(duration: 0.2))
5858

5959
case .presentPromptToCode:
6060
return .run { send in
@@ -68,7 +68,7 @@ struct PanelFeature: ReducerProtocol {
6868
try await Task.sleep(nanoseconds: 150_000_000)
6969
await NSApplication.shared.activate(ignoringOtherApps: true)
7070
await windows.sharedPanelWindow.makeKey()
71-
}
71+
}.animation(.easeInOut(duration: 0.2))
7272

7373
case let .presentPanelContent(content, shouldDisplay):
7474
state.sharedPanelState.content = content
@@ -94,10 +94,23 @@ struct PanelFeature: ReducerProtocol {
9494

9595
case .discardPanelContent:
9696
return .run { send in
97-
await send(.updatePanelContent)
98-
}
97+
let fileURL = xcodeInspector.activeDocumentURL
98+
if let provider = await fetchPromptToCodeProvider(fileURL: fileURL) {
99+
await send(.presentPanelContent(
100+
.promptToCode(provider),
101+
shouldDisplay: false
102+
))
103+
} else if let provider = await fetchSuggestionProvider(fileURL: fileURL) {
104+
await send(.presentPanelContent(
105+
.suggestion(provider),
106+
shouldDisplay: false
107+
))
108+
} else {
109+
await send(.removeDisplayedContent)
110+
}
111+
}.animation(.easeInOut(duration: 0.2))
99112

100-
case .updatePanelContent:
113+
case .switchToAnotherEditorAndUpdateContent:
101114
return .run { send in
102115
let fileURL = xcodeInspector.activeDocumentURL
103116
if let provider = await fetchPromptToCodeProvider(fileURL: fileURL) {

Core/Sources/SuggestionWidget/FeatureReducers/WidgetFeature.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,23 @@ struct WidgetFeature: ReducerProtocol {
131131
}
132132

133133
case .circularWidget(.widgetClicked):
134-
if state._circularWidgetState.isDisplayingContent {
134+
let isDisplayingContent = state._circularWidgetState.isDisplayingContent
135+
if isDisplayingContent {
135136
state.panelState.sharedPanelState.isPanelDisplayed = false
136137
state.panelState.suggestionPanelState.isPanelDisplayed = false
137138
state.chatPanelState.isPanelDisplayed = false
138-
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
139-
app.activate()
140-
}
141139
} else {
142140
state.panelState.sharedPanelState.isPanelDisplayed = true
143141
state.panelState.suggestionPanelState.isPanelDisplayed = true
144142
state.chatPanelState.isPanelDisplayed = true
145143
}
146-
return .none
144+
return .run { _ in
145+
guard isDisplayingContent else { return }
146+
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
147+
try await Task.sleep(nanoseconds: 200_000_000)
148+
app.activate()
149+
}
150+
}
147151

148152
default: return .none
149153
}
@@ -312,17 +316,22 @@ struct WidgetFeature: ReducerProtocol {
312316
for await notification in notifications {
313317
try Task.checkCancellation()
314318

315-
await send(.updateWindowLocation(animated: false))
316-
await send(.updateWindowOpacity)
317-
318319
if [
319320
kAXFocusedUIElementChangedNotification,
320321
kAXApplicationActivatedNotification,
322+
kAXMainWindowChangedNotification,
323+
kAXFocusedWindowChangedNotification,
321324
].contains(notification.name) {
325+
await hidePanelWindows()
326+
await send(.panel(.removeDisplayedContent))
327+
await send(.updateWindowLocation(animated: false))
328+
await send(.updateWindowOpacity)
322329
await send(.observeEditorChange)
323-
324-
await send(.panel(.updatePanelContent))
330+
await send(.panel(.switchToAnotherEditorAndUpdateContent))
325331
await send(.chatPanel(.updateContent))
332+
} else {
333+
await send(.updateWindowLocation(animated: false))
334+
await send(.updateWindowOpacity)
326335
}
327336
}
328337
}.cancellable(id: CancelID.observeWindowChange, cancelInFlight: true)
@@ -373,7 +382,7 @@ struct WidgetFeature: ReducerProtocol {
373382
case .updateActiveApplication:
374383
if let app = activeApplicationMonitor.activeApplication, app.isXcode {
375384
return .run { send in
376-
await send(.panel(.updatePanelContent))
385+
await send(.panel(.switchToAnotherEditorAndUpdateContent))
377386
await send(.chatPanel(.updateContent))
378387
await send(.updateWindowLocation(animated: false))
379388
await send(.updateWindowOpacity)
@@ -549,6 +558,12 @@ struct WidgetFeature: ReducerProtocol {
549558
}
550559
}
551560

561+
@MainActor
562+
func hidePanelWindows() {
563+
windows.sharedPanelWindow.alphaValue = 0
564+
windows.suggestionPanelWindow.alphaValue = 0
565+
}
566+
552567
func generateWidgetLocation() -> WidgetLocation? {
553568
if let application = xcodeInspector.latestActiveXcode?.appElement {
554569
if let focusElement = xcodeInspector.focusedEditor?.element,

0 commit comments

Comments
 (0)