@@ -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