-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWidgetWindowsController+FixErrorPanel.swift
More file actions
91 lines (79 loc) · 3.27 KB
/
WidgetWindowsController+FixErrorPanel.swift
File metadata and controls
91 lines (79 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import AppKit
import XcodeInspector
extension WidgetWindowsController {
@MainActor
func hideFixErrorWindow() {
windows.fixErrorPanelWindow.alphaValue = 0
windows.fixErrorPanelWindow.setIsVisible(false)
}
@MainActor
func displayFixErrorWindow() {
windows.fixErrorPanelWindow.setIsVisible(true)
windows.fixErrorPanelWindow.alphaValue = 1
windows.fixErrorPanelWindow.orderFrontRegardless()
}
func setupFixErrorPanelObservers() {
store.publisher
.map(\.fixErrorPanelState.errorAnnotations)
.removeDuplicates()
.sink { [weak self] _ in
Task { @MainActor [weak self] in
await self?.updateFixErrorPanelWindowLocation()
}
}.store(in: &cancellable)
store.publisher
.map(\.fixErrorPanelState.isPanelDisplayed)
.removeDuplicates()
.sink { [weak self ] _ in
Task { @MainActor [weak self] in
await self?.updateFixErrorPanelWindowLocation()
}
}.store(in: &cancellable)
}
@MainActor
func updateFixErrorPanelWindowLocation() async {
guard let activeApp = await XcodeInspector.shared.safe.activeApplication,
(activeApp.isXcode || activeApp.isCopilotForXcodeExtensionService)
else {
hideFixErrorWindow()
return
}
let state = store.withState { $0.fixErrorPanelState }
guard state.isPanelDisplayed,
let focusedEditor = state.focusedEditor,
let scrollViewRect = focusedEditor.element.parent?.rect
else {
hideFixErrorWindow()
return
}
let annotations = state.errorAnnotationsAtCursorPosition
guard !annotations.isEmpty,
let annotationRect = annotations.first(where: { $0.rect != nil})?.rect,
scrollViewRect.contains(annotationRect),
let screen = NSScreen.screens.first(where: { $0.frame.origin == .zero })
else {
hideFixErrorWindow()
return
}
var fixErrorPanelWindowFrame = windows.fixErrorPanelWindow.frame
fixErrorPanelWindowFrame.origin.x = annotationRect.minX - fixErrorPanelWindowFrame.width - Style.fixPanelToAnnotationSpacing
// Locate the window to the middle in Y
fixErrorPanelWindowFrame.origin.y = screen.frame.maxY - annotationRect.minY - annotationRect.height / 2 - fixErrorPanelWindowFrame.height / 2 + screen.frame.minY
windows.fixErrorPanelWindow.setFrame(fixErrorPanelWindowFrame, display: true, animate: true)
displayFixErrorWindow()
}
@MainActor
func handleFixErrorEditorNotification(notification: SourceEditor.AXNotification) async {
switch notification.kind {
case .scrollPositionChanged:
store.send(.fixErrorPanel(.onScrollPositionChanged))
case .valueChanged:
store.send(.fixErrorPanel(.onEditorContentChanged))
case .selectedTextChanged:
store.send(.fixErrorPanel(.onCursorPositionChanged))
default:
break
}
await updateFixErrorPanelWindowLocation()
}
}