Skip to content

Commit 87f607e

Browse files
committed
Make updateWindowLocation more clear
1 parent de11573 commit 87f607e

1 file changed

Lines changed: 63 additions & 78 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 63 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -443,96 +443,80 @@ extension SuggestionWidgetController {
443443
/// - note: It's possible to get the scroll view's position by getting position on the focus
444444
/// element.
445445
private func updateWindowLocation(animated: Bool = false) {
446-
func hide() {
447-
if panelWindow.alphaValue != 0 {
448-
panelWindow.alphaValue = 0
449-
}
450-
if widgetWindow.alphaValue != 0 {
451-
widgetWindow.alphaValue = 0
452-
}
453-
if tabWindow.alphaValue != 0 {
454-
tabWindow.alphaValue = 0
455-
}
456-
if !UserDefaults.shared.value(for: \.chatPanelInASeparateWindow) {
457-
if chatWindow.alphaValue != 0 {
458-
chatWindow.alphaValue = 0
459-
}
460-
}
461-
}
462-
463446
guard UserDefaults.shared.value(for: \.suggestionPresentationMode) == .floatingWidget
464447
else {
465-
hide()
448+
panelWindow.alphaValue = 0
449+
widgetWindow.alphaValue = 0
450+
tabWindow.alphaValue = 0
451+
chatWindow.alphaValue = 0
466452
return
467453
}
468454

469455
let detachChat = UserDefaults.shared.value(for: \.chatPanelInASeparateWindow)
470456

471-
if detachChat {
472-
chatWindow.alphaValue = chatWindowViewModel.chat != nil ? 1 : 0
473-
} else {
474-
chatWindow.alphaValue = 0
475-
}
476-
477-
if let xcode = ActiveApplicationMonitor.activeXcode {
478-
let application = AXUIElementCreateApplication(xcode.processIdentifier)
479-
if let focusElement = application.focusedElement,
480-
focusElement.description == "Source Editor",
481-
let parent = focusElement.parent,
482-
let frame = parent.rect,
483-
let screen = NSScreen.screens.first(where: { $0.frame.origin == .zero }),
484-
let firstScreen = NSScreen.main
485-
{
486-
let mode = UserDefaults.shared.value(for: \.suggestionWidgetPositionMode)
487-
switch mode {
488-
case .fixedToBottom:
489-
let result = UpdateLocationStrategy.FixedToBottom().framesForWindows(
490-
editorFrame: frame,
491-
mainScreen: screen,
492-
activeScreen: firstScreen
493-
)
494-
widgetWindow.setFrame(result.widgetFrame, display: false, animate: animated)
495-
panelWindow.setFrame(result.panelFrame, display: false, animate: animated)
496-
tabWindow.setFrame(result.tabFrame, display: false, animate: animated)
497-
suggestionPanelViewModel.alignTopToAnchor = result.alignPanelTopToAnchor
498-
case .alignToTextCursor:
499-
let result = UpdateLocationStrategy.AlignToTextCursor().framesForWindows(
500-
editorFrame: frame,
501-
mainScreen: screen,
502-
activeScreen: firstScreen,
503-
editor: focusElement
504-
)
505-
widgetWindow.setFrame(result.widgetFrame, display: false, animate: animated)
506-
panelWindow.setFrame(result.panelFrame, display: false, animate: animated)
507-
tabWindow.setFrame(result.tabFrame, display: false, animate: animated)
508-
suggestionPanelViewModel.alignTopToAnchor = result.alignPanelTopToAnchor
509-
}
510-
511-
if detachChat {
512-
if chatWindow.alphaValue == 0 {
513-
chatWindow.setFrame(panelWindow.frame, display: false, animate: false)
514-
}
515-
} else {
516-
chatWindow.setFrame(panelWindow.frame, display: false, animate: false)
517-
if chatWindow.alphaValue != 1 {
518-
chatWindow.alphaValue = 1
457+
if let widgetFrames = {
458+
if let xcode = ActiveApplicationMonitor.latestXcode {
459+
let application = AXUIElementCreateApplication(xcode.processIdentifier)
460+
if let focusElement = application.focusedElement,
461+
focusElement.description == "Source Editor",
462+
let parent = focusElement.parent,
463+
let frame = parent.rect,
464+
let screen = NSScreen.screens.first(where: { $0.frame.origin == .zero }),
465+
let firstScreen = NSScreen.main
466+
{
467+
let mode = UserDefaults.shared.value(for: \.suggestionWidgetPositionMode)
468+
switch mode {
469+
case .fixedToBottom:
470+
return UpdateLocationStrategy.FixedToBottom().framesForWindows(
471+
editorFrame: frame,
472+
mainScreen: screen,
473+
activeScreen: firstScreen
474+
)
475+
case .alignToTextCursor:
476+
return UpdateLocationStrategy.AlignToTextCursor().framesForWindows(
477+
editorFrame: frame,
478+
mainScreen: screen,
479+
activeScreen: firstScreen,
480+
editor: focusElement
481+
)
519482
}
520483
}
521-
522-
if panelWindow.alphaValue != 1 {
523-
panelWindow.alphaValue = 1
524-
}
525-
if widgetWindow.alphaValue != 1 {
526-
widgetWindow.alphaValue = 1
527-
}
528-
if tabWindow.alphaValue != 1 {
529-
tabWindow.alphaValue = 1
484+
}
485+
return nil
486+
}() {
487+
widgetWindow.setFrame(widgetFrames.widgetFrame, display: false, animate: animated)
488+
panelWindow.setFrame(widgetFrames.panelFrame, display: false, animate: animated)
489+
tabWindow.setFrame(widgetFrames.tabFrame, display: false, animate: animated)
490+
suggestionPanelViewModel.alignTopToAnchor = widgetFrames.alignPanelTopToAnchor
491+
if detachChat {
492+
if chatWindow.alphaValue == 0 {
493+
chatWindow.setFrame(panelWindow.frame, display: false, animate: false)
530494
}
531-
return
495+
} else {
496+
chatWindow.setFrame(panelWindow.frame, display: false, animate: false)
532497
}
533498
}
534499

535-
hide()
500+
if let app = ActiveApplicationMonitor.activeApplication, app.isXcode {
501+
let application = AXUIElementCreateApplication(app.processIdentifier)
502+
let noFocus = application.focusedWindow == nil
503+
panelWindow.alphaValue = noFocus ? 0 : 1
504+
widgetWindow.alphaValue = noFocus ? 0 : 1
505+
tabWindow.alphaValue = noFocus ? 0 : 1
506+
507+
if detachChat {
508+
chatWindow.alphaValue = chatWindowViewModel.chat != nil ? 1 : 0
509+
} else {
510+
chatWindow.alphaValue = noFocus ? 0 : 1
511+
}
512+
} else {
513+
panelWindow.alphaValue = 0
514+
widgetWindow.alphaValue = 0
515+
tabWindow.alphaValue = 0
516+
if !detachChat {
517+
chatWindow.alphaValue = 0
518+
}
519+
}
536520
}
537521

538522
private func updateContentForActiveEditor(fileURL: URL? = nil) async {
@@ -579,7 +563,8 @@ extension SuggestionWidgetController: NSWindowDelegate {
579563

580564
public func windowDidBecomeKey(_ notification: Notification) {
581565
guard (notification.object as? NSWindow) === chatWindow else { return }
582-
let screenFrame = NSScreen.screens.first(where: { $0.frame.origin == .zero })?.frame ?? .zero
566+
let screenFrame = NSScreen.screens.first(where: { $0.frame.origin == .zero })?
567+
.frame ?? .zero
583568
var mouseLocation = NSEvent.mouseLocation
584569
let windowFrame = chatWindow.frame
585570
if mouseLocation.y > windowFrame.maxY - 40 {

0 commit comments

Comments
 (0)