Skip to content

Commit c55fbaa

Browse files
committed
Support displaying next to the completion panel
1 parent ada1289 commit c55fbaa

1 file changed

Lines changed: 62 additions & 18 deletions

File tree

Core/Sources/SuggestionWidget/WidgetPositionStrategy.swift

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,47 +228,91 @@ enum UpdateLocationStrategy {
228228
let proposedX = selectionFrame.maxX - 40
229229
let maxY = max(
230230
proposedY,
231-
mainScreen.frame.height - editorFrame.maxY,
232231
4 + activeScreen.frame.minY
233232
)
234233
let y = min(
235234
maxY,
236-
activeScreen.frame.maxY - 4,
237-
mainScreen.frame.height - editorFrame.minY
235+
activeScreen.frame.maxY - 4
238236
)
239-
print(y, activeScreen.frame.minY, activeScreen.frame.maxY)
240-
let alignPanelTopToAnchor = y - Style.panelHeight >= activeScreen.frame.minY
241237

242-
if let completionPanel, let completionPanelRect = completionPanel.rect {
243-
return .init(
244-
frame: completionPanelRect,
245-
alignPanelTop: alignPanelTopToAnchor
246-
)
247-
} else {
238+
let alignPanelTopToAnchor = y - Style.inlineSuggestionMaxWidth >= activeScreen.frame.minY
239+
240+
let caseIgnoreCompletionPanel = {
241+
(alignPanelTopToAnchor: Bool) -> WidgetLocation.PanelLocation? in
242+
let x: Double = {
243+
if proposedX + Style.inlineSuggestionMinWidth <= activeScreen.frame.maxX {
244+
return proposedX
245+
}
246+
return activeScreen.frame.maxX - Style.inlineSuggestionMinWidth
247+
}()
248248
if alignPanelTopToAnchor {
249249
// case: present below selection
250250
return .init(
251251
frame: .init(
252-
x: proposedX,
253-
y: y - Style.panelHeight,
252+
x: x,
253+
y: y - Style.inlineSuggestionMaxWidth,
254254
width: Style.inlineSuggestionMinWidth,
255-
height: Style.panelHeight
255+
height: Style.inlineSuggestionMaxWidth
256256
),
257257
alignPanelTop: alignPanelTopToAnchor
258258
)
259259
} else {
260-
// case: present above selection
261260
return .init(
262261
frame: .init(
263-
x: proposedX,
264-
y: y + selectionFrame.height,
262+
x: x,
263+
y: y + selectionFrame.height - Style.widgetPadding,
265264
width: Style.inlineSuggestionMinWidth,
266-
height: Style.panelHeight
265+
height: Style.inlineSuggestionMaxWidth
267266
),
268267
alignPanelTop: alignPanelTopToAnchor
269268
)
270269
}
271270
}
271+
272+
let caseConsiderCompletionPanel = {
273+
(completionPanelRect: CGRect) -> WidgetLocation.PanelLocation? in
274+
let completionPanelBelowCursor = completionPanelRect.minY >= selectionFrame.midY
275+
276+
switch (completionPanelBelowCursor, alignPanelTopToAnchor) {
277+
case (true, false), (false, true):
278+
return caseIgnoreCompletionPanel(alignPanelTopToAnchor)
279+
case (true, true), (false, false):
280+
let y = completionPanelBelowCursor
281+
? y - Style.inlineSuggestionMaxWidth
282+
: y + selectionFrame.height - Style.widgetPadding
283+
if let x = {
284+
let proposedX = completionPanelRect.maxX + Style.widgetPadding
285+
if proposedX + Style.inlineSuggestionMinWidth <= activeScreen.frame.maxX {
286+
return proposedX
287+
}
288+
let leftSideX = completionPanelRect.minX
289+
- Style.widgetPadding
290+
- Style.inlineSuggestionMinWidth
291+
if leftSideX >= activeScreen.frame.minX {
292+
return leftSideX
293+
}
294+
return nil
295+
}() {
296+
print(mainScreen.frame, completionPanelRect, y)
297+
return .init(
298+
frame: .init(
299+
x: x,
300+
y: y,
301+
width: Style.inlineSuggestionMinWidth,
302+
height: Style.inlineSuggestionMaxWidth
303+
),
304+
alignPanelTop: alignPanelTopToAnchor
305+
)
306+
}
307+
return caseIgnoreCompletionPanel(!alignPanelTopToAnchor)
308+
}
309+
}
310+
311+
if let completionPanel, let completionPanelRect = completionPanel.rect {
312+
return caseConsiderCompletionPanel(completionPanelRect)
313+
} else {
314+
return caseIgnoreCompletionPanel(alignPanelTopToAnchor)
315+
}
272316
}
273317
}
274318

0 commit comments

Comments
 (0)