forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandleToast.swift
More file actions
49 lines (44 loc) · 1.68 KB
/
HandleToast.swift
File metadata and controls
49 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Dependencies
import SwiftUI
import Toast
struct ToastHandler: View {
@ObservedObject var toastController: ToastController
let namespace: String?
init(toastController: ToastController, namespace: String?) {
_toastController = .init(wrappedValue: toastController)
self.namespace = namespace
}
var body: some View {
VStack(spacing: 4) {
ForEach(toastController.messages) { message in
if let n = message.namespace, n != namespace {
EmptyView()
} else {
message.content
.foregroundColor(.white)
.padding(8)
.background({
switch message.type {
case .info: return Color.accentColor
case .error: return Color(nsColor: .systemRed)
case .warning: return Color(nsColor: .systemOrange)
}
}() as Color, in: RoundedRectangle(cornerRadius: 8))
.shadow(color: Color.black.opacity(0.2), radius: 4)
}
}
}
.padding()
.allowsHitTesting(false)
}
}
extension View {
func handleToast(namespace: String? = nil) -> some View {
@Dependency(\.toastController) var toastController
return overlay(alignment: .bottom) {
ToastHandler(toastController: toastController, namespace: namespace)
}.environment(\.toast) { [toastController] content, type in
toastController.toast(content: content, type: type, namespace: namespace)
}
}
}