Skip to content

Commit a2ac749

Browse files
committed
Fix calculation of toast on screen
1 parent a17fbd9 commit a2ac749

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

macos/sol-macOS/managers/ToastManager.swift

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
class ToastManager {
22
private var toastWindow: Toast = Toast(contentRect: .zero)
3-
3+
44
static public let shared = ToastManager()
5-
6-
func showToast(_ text: String, variant: String, timeout: NSNumber?, image: NSImage?) {
7-
guard let mainScreen = PanelManager.shared.getPreferredScreen()
5+
6+
func showToast(
7+
_ text: String, variant: String, timeout: NSNumber?, image: NSImage?
8+
) {
9+
guard let screen = PanelManager.shared.getPreferredScreen()
810
else {
911
return
1012
}
11-
13+
1214
let variantEnum: ToastVariant =
13-
switch variant {
14-
case "error": .error
15-
case "success": .success
16-
default: .none
17-
}
18-
15+
switch variant {
16+
case "error": .error
17+
case "success": .success
18+
default: .none
19+
}
20+
1921
let toastView = ToastView(
2022
text: text,
2123
variant: variantEnum,
@@ -26,55 +28,57 @@ class ToastManager {
2628
}
2729
}
2830
)
29-
toastView.layoutSubtreeIfNeeded() // Ensure layout is performed
30-
31+
toastView.layoutSubtreeIfNeeded()
32+
3133
let contentSize = toastView.intrinsicContentSize
32-
toastView.frame = NSRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
33-
34+
let size = NSRect(
35+
x: 0, y: 0, width: contentSize.width, height: contentSize.height)
36+
toastView.frame = size
37+
3438
let effectView = NSVisualEffectView(
35-
frame: NSRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
39+
frame: size
3640
)
3741
effectView.autoresizingMask = [.width, .height]
3842
effectView.material = .hudWindow // Or other material
3943
effectView.blendingMode = .behindWindow
4044
effectView.state = .active
41-
45+
4246
toastWindow.contentView = effectView
4347
toastWindow.contentView!.addSubview(toastView)
44-
48+
4549
// Add the effect view to the content view of the window, NOT the ToastView
4650
toastWindow
4751
.setFrame(
48-
NSRect(
49-
x: 0,
50-
y: 0,
51-
width: contentSize.width,
52-
height: contentSize.height
53-
),
52+
size,
5453
display: true
5554
)
56-
57-
let x = mainScreen.frame.size.width / 2 - toastWindow.frame.width / 2
58-
var y = mainScreen.frame.origin.y + mainScreen.frame.size.height * 0.1
59-
55+
toastWindow.setContentSize(contentSize)
56+
57+
// 0 is calculated from the origin of the main screen, which means screen.frame.origin.x can be negative
58+
let x =
59+
screen.frame.origin.x
60+
+ (screen.frame.size.width / 2 - contentSize.width / 2)
61+
var y = screen.frame.origin.y + screen.frame.size.height * 0.1
62+
6063
if image != nil {
61-
y = mainScreen.frame.origin.y + toastWindow.frame.height
64+
y = screen.frame.origin.y + toastWindow.frame.height
6265
}
63-
66+
6467
toastWindow.setFrameOrigin(
6568
NSPoint(
6669
x: x,
6770
y: y
6871
))
69-
72+
7073
toastWindow.makeKeyAndOrderFront(nil)
71-
72-
let deadline = timeout != nil ? DispatchTime.now() + timeout!.doubleValue : .now() + 2
74+
75+
let deadline =
76+
timeout != nil ? DispatchTime.now() + timeout!.doubleValue : .now() + 2
7377
DispatchQueue.main.asyncAfter(deadline: deadline) {
7478
self.dismissToast()
7579
}
7680
}
77-
81+
7882
func dismissToast() {
7983
toastWindow.orderOut(nil)
8084
}

0 commit comments

Comments
 (0)