forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetPositionStrategy.swift
More file actions
179 lines (171 loc) · 7.11 KB
/
WidgetPositionStrategy.swift
File metadata and controls
179 lines (171 loc) · 7.11 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import AppKit
import Foundation
enum UpdateLocationStrategy {
struct AlignToTextCursor {
func framesForWindows(
editorFrame: CGRect,
mainScreen: NSScreen,
activeScreen: NSScreen,
editor: AXUIElement
) -> (
widgetFrame: CGRect,
panelFrame: CGRect,
tabFrame: CGRect,
alignPanelTopToAnchor: Bool
) {
guard let selectedRange: AXValue = try? editor
.copyValue(key: kAXSelectedTextRangeAttribute),
let rect: AXValue = try? editor.copyParameterizedValue(
key: kAXBoundsForRangeParameterizedAttribute,
parameters: selectedRange
)
else {
return FixedToBottom().framesForWindows(
editorFrame: editorFrame,
mainScreen: mainScreen,
activeScreen: activeScreen
)
}
var frame: CGRect = .zero
let found = AXValueGetValue(rect, .cgRect, &frame)
guard found else {
return FixedToBottom().framesForWindows(
editorFrame: editorFrame,
mainScreen: mainScreen,
activeScreen: activeScreen
)
}
return HorizontalMovable().framesForWindows(
y: activeScreen.frame.height - frame.maxY,
alignPanelTopToAnchor: nil,
editorFrame: editorFrame,
mainScreen: mainScreen,
activeScreen: activeScreen
)
}
}
struct FixedToBottom {
func framesForWindows(
editorFrame: CGRect,
mainScreen: NSScreen,
activeScreen: NSScreen
) -> (
widgetFrame: CGRect,
panelFrame: CGRect,
tabFrame: CGRect,
alignPanelTopToAnchor: Bool
) {
return HorizontalMovable().framesForWindows(
y: activeScreen.frame.height - editorFrame.maxY + Style.widgetPadding,
alignPanelTopToAnchor: false,
editorFrame: editorFrame,
mainScreen: mainScreen,
activeScreen: activeScreen
)
}
}
struct HorizontalMovable {
func framesForWindows(
y: CGFloat,
alignPanelTopToAnchor fixedAlignment: Bool?,
editorFrame: CGRect,
mainScreen: NSScreen,
activeScreen: NSScreen
) -> (
widgetFrame: CGRect,
panelFrame: CGRect,
tabFrame: CGRect,
alignPanelTopToAnchor: Bool
) {
let maxY = max(
y,
activeScreen.frame.height - editorFrame.maxY + Style.widgetPadding,
4 + mainScreen.frame.minY
)
let y = min(
maxY,
mainScreen.frame.maxY - 4,
activeScreen.frame.height - editorFrame.minY - Style.widgetHeight - Style
.widgetPadding
)
let proposedAnchorFrameOnTheRightSide = CGRect(
x: editorFrame.maxX - Style.widgetPadding - Style.widgetWidth,
y: y,
width: Style.widgetWidth,
height: Style.widgetHeight
)
let proposedPanelX = proposedAnchorFrameOnTheRightSide.maxX + Style
.widgetPadding * 2
let putPanelToTheRight = mainScreen.frame.maxX > proposedPanelX + Style.panelWidth
let alignPanelTopToAnchor = fixedAlignment ?? (y > activeScreen.frame.midY)
if putPanelToTheRight {
let anchorFrame = proposedAnchorFrameOnTheRightSide
let panelFrame = CGRect(
x: proposedPanelX,
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style.panelHeight : anchorFrame
.minY,
width: Style.panelWidth,
height: Style.panelHeight
)
let tabFrame = CGRect(
x: anchorFrame.origin.x,
y: alignPanelTopToAnchor
? anchorFrame.minY - Style.widgetHeight - Style.widgetPadding
: anchorFrame.maxY + Style.widgetPadding,
width: Style.widgetWidth,
height: Style.widgetHeight
)
return (anchorFrame, panelFrame, tabFrame, alignPanelTopToAnchor)
} else {
let proposedAnchorFrameOnTheLeftSide = CGRect(
x: editorFrame.minX + Style.widgetPadding,
y: proposedAnchorFrameOnTheRightSide.origin.y,
width: Style.widgetWidth,
height: Style.widgetHeight
)
let proposedPanelX = proposedAnchorFrameOnTheLeftSide.minX - Style
.widgetPadding * 2 - Style.panelWidth
let putAnchorToTheLeft = proposedPanelX > mainScreen.frame.minX
if putAnchorToTheLeft {
let anchorFrame = proposedAnchorFrameOnTheLeftSide
let panelFrame = CGRect(
x: proposedPanelX,
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style
.panelHeight : anchorFrame
.minY,
width: Style.panelWidth,
height: Style.panelHeight
)
let tabFrame = CGRect(
x: anchorFrame.origin.x,
y: alignPanelTopToAnchor
? anchorFrame.minY - Style.widgetHeight - Style.widgetPadding
: anchorFrame.maxY + Style.widgetPadding,
width: Style.widgetWidth,
height: Style.widgetHeight
)
return (anchorFrame, panelFrame, tabFrame, alignPanelTopToAnchor)
} else {
let anchorFrame = proposedAnchorFrameOnTheRightSide
let panelFrame = CGRect(
x: anchorFrame.maxX - Style.panelWidth,
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style.panelHeight - Style
.widgetHeight - Style.widgetPadding : anchorFrame.maxY + Style
.widgetPadding,
width: Style.panelWidth,
height: Style.panelHeight
)
let tabFrame = CGRect(
x: anchorFrame.origin.x,
y: alignPanelTopToAnchor
? anchorFrame.minY - Style.widgetHeight - Style.widgetPadding
: anchorFrame.maxY + Style.widgetPadding,
width: Style.widgetWidth,
height: Style.widgetHeight
)
return (anchorFrame, panelFrame, tabFrame, alignPanelTopToAnchor)
}
}
}
}
}