1
1
class ToastManager {
2
2
private var toastWindow : Toast = Toast ( contentRect: . zero)
3
-
3
+
4
4
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 ( )
8
10
else {
9
11
return
10
12
}
11
-
13
+
12
14
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
+
19
21
let toastView = ToastView (
20
22
text: text,
21
23
variant: variantEnum,
@@ -26,55 +28,57 @@ class ToastManager {
26
28
}
27
29
}
28
30
)
29
- toastView. layoutSubtreeIfNeeded ( ) // Ensure layout is performed
30
-
31
+ toastView. layoutSubtreeIfNeeded ( )
32
+
31
33
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
+
34
38
let effectView = NSVisualEffectView (
35
- frame: NSRect ( x : 0 , y : 0 , width : contentSize . width , height : contentSize . height )
39
+ frame: size
36
40
)
37
41
effectView. autoresizingMask = [ . width, . height]
38
42
effectView. material = . hudWindow // Or other material
39
43
effectView. blendingMode = . behindWindow
40
44
effectView. state = . active
41
-
45
+
42
46
toastWindow. contentView = effectView
43
47
toastWindow. contentView!. addSubview ( toastView)
44
-
48
+
45
49
// Add the effect view to the content view of the window, NOT the ToastView
46
50
toastWindow
47
51
. setFrame (
48
- NSRect (
49
- x: 0 ,
50
- y: 0 ,
51
- width: contentSize. width,
52
- height: contentSize. height
53
- ) ,
52
+ size,
54
53
display: true
55
54
)
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
+
60
63
if image != nil {
61
- y = mainScreen . frame. origin. y + toastWindow. frame. height
64
+ y = screen . frame. origin. y + toastWindow. frame. height
62
65
}
63
-
66
+
64
67
toastWindow. setFrameOrigin (
65
68
NSPoint (
66
69
x: x,
67
70
y: y
68
71
) )
69
-
72
+
70
73
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
73
77
DispatchQueue . main. asyncAfter ( deadline: deadline) {
74
78
self . dismissToast ( )
75
79
}
76
80
}
77
-
81
+
78
82
func dismissToast( ) {
79
83
toastWindow. orderOut ( nil )
80
84
}
0 commit comments