Skip to content

Commit b0c309a

Browse files
committed
Tweak window location
1 parent e5ed6fa commit b0c309a

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

Core/Sources/SuggestionWidget/WidgetPositionStrategy.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ enum UpdateLocationStrategy {
6767
activeScreen: NSScreen,
6868
hideCircularWidget: Bool = UserDefaults.shared.value(for: \.hideCircularWidget),
6969
preferredInsideEditorMinWidth: Double = UserDefaults.shared
70-
.value(for: \.preferWidgetToStayInsideEditorWhenWidthGreaterThan)
70+
.value(for: \.preferWidgetToStayInsideEditorWhenWidthGreaterThan),
71+
editorFrameExpendedSize: CGSize = .zero
7172
) -> WidgetLocation {
7273
return HorizontalMovable().framesForWindows(
7374
y: mainScreen.frame.height - editorFrame.maxY + Style.widgetPadding,
@@ -76,7 +77,8 @@ enum UpdateLocationStrategy {
7677
mainScreen: mainScreen,
7778
activeScreen: activeScreen,
7879
preferredInsideEditorMinWidth: preferredInsideEditorMinWidth,
79-
hideCircularWidget: hideCircularWidget
80+
hideCircularWidget: hideCircularWidget,
81+
editorFrameExpendedSize: editorFrameExpendedSize
8082
)
8183
}
8284
}
@@ -89,7 +91,8 @@ enum UpdateLocationStrategy {
8991
mainScreen: NSScreen,
9092
activeScreen: NSScreen,
9193
preferredInsideEditorMinWidth: Double,
92-
hideCircularWidget: Bool = UserDefaults.shared.value(for: \.hideCircularWidget)
94+
hideCircularWidget: Bool = UserDefaults.shared.value(for: \.hideCircularWidget),
95+
editorFrameExpendedSize: CGSize = .zero
9396
) -> WidgetLocation {
9497
let maxY = max(
9598
y,
@@ -121,8 +124,9 @@ enum UpdateLocationStrategy {
121124
proposedAnchorFrameOnTheRightSide = widgetFrameOnTheRightSide
122125
}
123126

124-
let proposedPanelX = proposedAnchorFrameOnTheRightSide.maxX + Style
125-
.widgetPadding * 2
127+
let proposedPanelX = proposedAnchorFrameOnTheRightSide.maxX
128+
+ Style.widgetPadding * 2
129+
- editorFrameExpendedSize.width
126130
let putPanelToTheRight = {
127131
if editorFrame.size.width >= preferredInsideEditorMinWidth { return false }
128132
return activeScreen.frame.maxX > proposedPanelX + Style.panelWidth
@@ -133,8 +137,9 @@ enum UpdateLocationStrategy {
133137
let anchorFrame = proposedAnchorFrameOnTheRightSide
134138
let panelFrame = CGRect(
135139
x: proposedPanelX,
136-
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style.panelHeight : anchorFrame
137-
.minY,
140+
y: alignPanelTopToAnchor
141+
? anchorFrame.maxY - Style.panelHeight
142+
: anchorFrame.minY - editorFrameExpendedSize.height,
138143
width: Style.panelWidth,
139144
height: Style.panelHeight
140145
)
@@ -175,8 +180,10 @@ enum UpdateLocationStrategy {
175180
proposedAnchorFrameOnTheLeftSide = widgetFrameOnTheLeftSide
176181
}
177182

178-
let proposedPanelX = proposedAnchorFrameOnTheLeftSide.minX - Style
179-
.widgetPadding * 2 - Style.panelWidth
183+
let proposedPanelX = proposedAnchorFrameOnTheLeftSide.minX
184+
- Style.widgetPadding * 2
185+
- Style.panelWidth
186+
+ editorFrameExpendedSize.width
180187
let putAnchorToTheLeft = {
181188
if editorFrame.size.width >= preferredInsideEditorMinWidth {
182189
if editorFrame.maxX <= activeScreen.frame.maxX {
@@ -190,9 +197,9 @@ enum UpdateLocationStrategy {
190197
let anchorFrame = proposedAnchorFrameOnTheLeftSide
191198
let panelFrame = CGRect(
192199
x: proposedPanelX,
193-
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style
194-
.panelHeight : anchorFrame
195-
.minY,
200+
y: alignPanelTopToAnchor
201+
? anchorFrame.maxY - Style.panelHeight
202+
: anchorFrame.minY - editorFrameExpendedSize.height,
196203
width: Style.panelWidth,
197204
height: Style.panelHeight
198205
)
@@ -217,9 +224,11 @@ enum UpdateLocationStrategy {
217224
let anchorFrame = proposedAnchorFrameOnTheRightSide
218225
let panelFrame = CGRect(
219226
x: anchorFrame.maxX - Style.panelWidth,
220-
y: alignPanelTopToAnchor ? anchorFrame.maxY - Style.panelHeight - Style
221-
.widgetHeight - Style.widgetPadding : anchorFrame.maxY + Style
222-
.widgetPadding,
227+
y: alignPanelTopToAnchor
228+
? anchorFrame.maxY - Style.panelHeight - Style.widgetHeight
229+
- Style.widgetPadding
230+
: anchorFrame.maxY + Style.widgetPadding
231+
- editorFrameExpendedSize.height,
223232
width: Style.panelWidth,
224233
height: Style.panelHeight
225234
)
@@ -393,9 +402,11 @@ enum UpdateLocationStrategy {
393402
var firstLineRange: CFRange = .init()
394403
let foundFirstLine = AXValueGetValue(selectedRange, .cfRange, &firstLineRange)
395404
firstLineRange.length = 0
396-
397-
#warning("FIXME: When selection is too low and out of the screen, the selection range becomes something else.")
398-
405+
406+
#warning(
407+
"FIXME: When selection is too low and out of the screen, the selection range becomes something else."
408+
)
409+
399410
if foundFirstLine,
400411
let firstLineSelectionRange = AXValueCreate(.cfRange, &firstLineRange),
401412
let firstLineRect: AXValue = try? editor.copyParameterizedValue(
@@ -413,3 +424,4 @@ enum UpdateLocationStrategy {
413424
return selectionFrame
414425
}
415426
}
427+

Core/Sources/SuggestionWidget/WidgetWindowsController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,24 @@ extension WidgetWindowsController {
315315
frame = rect
316316
}
317317

318+
var expendedSize = CGSize.zero
318319
if ["Xcode.WorkspaceWindow"].contains(window.identifier) {
319320
// extra padding to bottom so buttons won't be covered
320321
frame.size.height -= 40
321322
} else {
322323
// move a bit away from the window so buttons won't be covered
323324
frame.origin.x -= Style.widgetPadding + Style.widgetWidth / 2
324325
frame.size.width += Style.widgetPadding * 2 + Style.widgetWidth
326+
expendedSize.width = (Style.widgetPadding * 2 + Style.widgetWidth) / 2
327+
expendedSize.height += Style.widgetPadding
325328
}
326329

327330
return UpdateLocationStrategy.FixedToBottom().framesForWindows(
328331
editorFrame: frame,
329332
mainScreen: screen,
330333
activeScreen: firstScreen,
331-
preferredInsideEditorMinWidth: 9_999_999_999 // never
334+
preferredInsideEditorMinWidth: 9_999_999_999, // never
335+
editorFrameExpendedSize: expendedSize
332336
)
333337
}
334338
}
@@ -472,7 +476,7 @@ extension WidgetWindowsController {
472476
!(now.timeIntervalSince(lastUpdateWindowLocationTime) > 3)
473477

474478
updateWindowLocationTask?.cancel()
475-
let interval: TimeInterval = 0.1
479+
let interval: TimeInterval = 0.05
476480

477481
if shouldThrottle {
478482
let delay = max(

0 commit comments

Comments
 (0)