-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSettingsView.swift
72 lines (60 loc) · 1.78 KB
/
SettingsView.swift
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// SettingsView.swift
// PingBar
//
// Created by Peter Kristensen on 20/11/2021.
//
import SwiftUI
struct WindowAccessor: NSViewRepresentable {
@Binding var window: NSWindow?
func makeNSView(context _: Context) -> NSView {
let view = NSView()
DispatchQueue.main.async {
self.window = view.window
}
return view
}
func updateNSView(_: NSView, context _: Context) {}
}
struct SettingsView: View {
@AppStorage("pingHost") var host = ""
@AppStorage("pingDelay") var delay = 1.0
@AppStorage("timePrecision") var precision = 2.0
@AppStorage("doPing") var enabled = false
@State var window: NSWindow?
var body: some View {
Form {
TextField("Host", text: $host)
HStack {
Slider(value: $delay, in: 0.5 ... 5) {
Text("Delay")
}
Text("\(String(format: "%.2f", delay))")
}
HStack {
Slider(value: $precision, in: 0 ... 5, step: 1) {
Text("Precision")
}
Text("\(String(format: "%.0f", precision))")
}
Toggle("Enabled", isOn: $enabled)
}
.background(WindowAccessor(window: $window))
.padding(.all, 20)
.frame(width: 300)
.onAppear() {
NSApp.setActivationPolicy(.regular)
}
.onReceive(NotificationCenter.default.publisher(for: NSWindow.willCloseNotification)) { n in
let w = n.object as? NSWindow
if window == w {
NSApp.setActivationPolicy(.accessory)
}
}
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView()
}
}