Skip to content

Commit 64958b0

Browse files
committed
Cleanup
1 parent d777a44 commit 64958b0

1 file changed

Lines changed: 71 additions & 56 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -264,63 +264,13 @@ public final class SuggestionWidgetController {
264264
let firstScreen = NSScreen.screens.first
265265
let frame = CGRect(origin: position, size: size)
266266
if foundSize, foundPosition, let screen, let firstScreen {
267-
let proposedAnchorFrameOnTheRightSide = CGRect(
268-
x: frame.maxX - Style.widgetPadding - Style.widgetWidth,
269-
y: max(
270-
firstScreen.frame.height - frame.maxY + Style.widgetPadding,
271-
4 + screen.frame.minY
272-
),
273-
width: Style.widgetWidth,
274-
height: Style.widgetHeight
267+
let result = UpdateLocationStrategy.FixedToBottom().framesForWindows(
268+
editorFrame: frame,
269+
mainScreen: screen,
270+
activeScreen: firstScreen
275271
)
276-
277-
let proposedPanelX = proposedAnchorFrameOnTheRightSide.maxX + Style
278-
.widgetPadding * 2
279-
let putPanelToTheRight = screen.frame.maxX > proposedPanelX + Style.panelWidth
280-
281-
if putPanelToTheRight {
282-
let anchorFrame = proposedAnchorFrameOnTheRightSide
283-
let panelFrame = CGRect(
284-
x: proposedPanelX,
285-
y: anchorFrame.minY,
286-
width: Style.panelWidth,
287-
height: Style.panelHeight
288-
)
289-
widgetWindow.setFrame(anchorFrame, display: false, animate: animated)
290-
panelWindow.setFrame(panelFrame, display: false, animate: animated)
291-
} else {
292-
let proposedAnchorFrameOnTheLeftSide = CGRect(
293-
x: frame.minX + Style.widgetPadding + Style.widgetWidth,
294-
y: proposedAnchorFrameOnTheRightSide.origin.y,
295-
width: Style.widgetWidth,
296-
height: Style.widgetHeight
297-
)
298-
let proposedPanelX = proposedAnchorFrameOnTheLeftSide.minX - Style
299-
.widgetPadding * 2 - Style.panelWidth
300-
let putAnchorToTheLeft = proposedPanelX > screen.frame.minX
301-
302-
if putAnchorToTheLeft {
303-
let anchorFrame = proposedAnchorFrameOnTheLeftSide
304-
let panelFrame = CGRect(
305-
x: proposedPanelX,
306-
y: anchorFrame.minY,
307-
width: Style.panelWidth,
308-
height: Style.panelHeight
309-
)
310-
widgetWindow.setFrame(anchorFrame, display: false, animate: animated)
311-
panelWindow.setFrame(panelFrame, display: false, animate: animated)
312-
} else {
313-
let anchorFrame = proposedAnchorFrameOnTheRightSide
314-
let panelFrame = CGRect(
315-
x: anchorFrame.maxX - Style.panelWidth,
316-
y: anchorFrame.maxY + Style.widgetPadding,
317-
width: Style.panelWidth,
318-
height: Style.panelHeight
319-
)
320-
widgetWindow.setFrame(anchorFrame, display: false, animate: animated)
321-
panelWindow.setFrame(panelFrame, display: false, animate: animated)
322-
}
323-
}
272+
widgetWindow.setFrame(result.widgetFrame, display: false, animate: animated)
273+
panelWindow.setFrame(result.panelFrame, display: false, animate: animated)
324274

325275
panelWindow.orderFront(nil)
326276
widgetWindow.orderFront(nil)
@@ -408,3 +358,68 @@ private func convertToCodeLines(_ formatedCode: NSAttributedString) -> [NSAttrib
408358
}
409359
return output
410360
}
361+
362+
enum UpdateLocationStrategy {
363+
struct FixedToBottom {
364+
func framesForWindows(
365+
editorFrame: CGRect,
366+
mainScreen: NSScreen,
367+
activeScreen: NSScreen
368+
) -> (widgetFrame: CGRect, panelFrame: CGRect) {
369+
let proposedAnchorFrameOnTheRightSide = CGRect(
370+
x: editorFrame.maxX - Style.widgetPadding - Style.widgetWidth,
371+
y: max(
372+
activeScreen.frame.height - editorFrame.maxY + Style.widgetPadding,
373+
4 + mainScreen.frame.minY
374+
),
375+
width: Style.widgetWidth,
376+
height: Style.widgetHeight
377+
)
378+
379+
let proposedPanelX = proposedAnchorFrameOnTheRightSide.maxX + Style
380+
.widgetPadding * 2
381+
let putPanelToTheRight = mainScreen.frame.maxX > proposedPanelX + Style.panelWidth
382+
383+
if putPanelToTheRight {
384+
let anchorFrame = proposedAnchorFrameOnTheRightSide
385+
let panelFrame = CGRect(
386+
x: proposedPanelX,
387+
y: anchorFrame.minY,
388+
width: Style.panelWidth,
389+
height: Style.panelHeight
390+
)
391+
return (anchorFrame, panelFrame)
392+
} else {
393+
let proposedAnchorFrameOnTheLeftSide = CGRect(
394+
x: editorFrame.minX + Style.widgetPadding + Style.widgetWidth,
395+
y: proposedAnchorFrameOnTheRightSide.origin.y,
396+
width: Style.widgetWidth,
397+
height: Style.widgetHeight
398+
)
399+
let proposedPanelX = proposedAnchorFrameOnTheLeftSide.minX - Style
400+
.widgetPadding * 2 - Style.panelWidth
401+
let putAnchorToTheLeft = proposedPanelX > mainScreen.frame.minX
402+
403+
if putAnchorToTheLeft {
404+
let anchorFrame = proposedAnchorFrameOnTheLeftSide
405+
let panelFrame = CGRect(
406+
x: proposedPanelX,
407+
y: anchorFrame.minY,
408+
width: Style.panelWidth,
409+
height: Style.panelHeight
410+
)
411+
return (anchorFrame, panelFrame)
412+
} else {
413+
let anchorFrame = proposedAnchorFrameOnTheRightSide
414+
let panelFrame = CGRect(
415+
x: anchorFrame.maxX - Style.panelWidth,
416+
y: anchorFrame.maxY + Style.widgetPadding,
417+
width: Style.panelWidth,
418+
height: Style.panelHeight
419+
)
420+
return (anchorFrame, panelFrame)
421+
}
422+
}
423+
}
424+
}
425+
}

0 commit comments

Comments
 (0)