-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoGUI.swift
200 lines (179 loc) · 6.33 KB
/
DemoGUI.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#if os(macOS)
// A simple GUI that interactively renders a static map onto a MapKit view.
import Combine
import Geodesy
import StaticMap
import SwiftUI
import MapKit
import Utils
// Fix a naming conflict with Utils's Binding
private typealias Binding = SwiftUI.Binding
private struct MapOptions: Hashable {
var region: CoordinateRegion?
var size: Vec2<Double> = .init(both: 256)
var opacity: Double = 1.0
var markOutdated = false
var resizeOffset: Vec2<Double> = .zero()
var sizeWithResize: Vec2<Double> {
size + resizeOffset * 2
}
var staticMap: StaticMap {
StaticMap(
size: sizeWithResize.map { Int($0.rounded()) },
center: region?.center,
span: region?.span
)
}
}
private struct ResizeHandle: View {
@Binding var offset: Vec2<Double>
var onSubmit: () -> Void
var body: some View {
Image(systemName: "arrowtriangle.left.and.line.vertical.and.arrowtriangle.right.fill")
.rotationEffect(.degrees(45))
.offset(x: offset.x, y: offset.y)
.gesture(
DragGesture()
.onChanged { value in
offset = Vec2(x: value.translation.width, y: value.translation.height)
}
.onEnded { _ in
onSubmit()
offset = .zero()
}
)
}
}
private struct OverlayPanel: View {
let errorMessage: String?
@Binding var mapOptions: MapOptions
var body: some View {
VStack {
if let errorMessage {
Text("Error while rendering map: \(errorMessage)")
} else {
HStack {
Text("Opacity")
Slider(value: $mapOptions.opacity)
.frame(width: 100)
}
}
}
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(
errorMessage != nil
? AnyShapeStyle(.red.opacity(0.5))
: AnyShapeStyle(.regularMaterial)
)
)
.fixedSize()
}
}
@available(macOS 15.0, *)
private struct ContentView: View {
@State private var mapImage: NSImage?
@State private var mapOptions = MapOptions()
@State private var errorMessage: String?
private let mapUpdateSubject = PassthroughSubject<Void, Never>()
var body: some View {
GeometryReader { geometry in
let clipShape = RoundedRectangle(cornerRadius: 10)
.size(width: mapOptions.sizeWithResize.x, height: mapOptions.sizeWithResize.y, anchor: .center)
ZStack(alignment: .bottom) {
Map(initialPosition: .region(MKCoordinateRegion(
center: .init(latitude: 51.5, longitude: 0.0),
span: .init(latitudeDelta: 2, longitudeDelta: 2)
)))
.mapControlVisibility(.hidden)
.onMapCameraChange(frequency: .continuous) { context in
let totalRegion: MKCoordinateRegion = context.region
mapOptions.markOutdated = true
mapOptions.region = CoordinateRegion(
center: Coordinates(totalRegion.center),
span: CoordinateSpan(
latitudeDelta: .init(degrees: totalRegion.span.latitudeDelta * mapOptions.size.y / geometry.size.height),
longitudeDelta: .init(degrees: totalRegion.span.longitudeDelta * mapOptions.size.x / geometry.size.width)
)
)
scheduleStaticMapUpdate()
}
.overlay {
Group {
Rectangle()
.subtracting(clipShape)
.fill(.black.opacity(0.5))
if let mapImage {
Image(nsImage: mapImage)
.clipShape(clipShape)
.opacity((mapOptions.markOutdated ? 0.3 : 1) * mapOptions.opacity)
}
}
.allowsHitTesting(false)
let baseOffset = mapOptions.size / 2 + Vec2(both: 10)
ResizeHandle(offset: $mapOptions.resizeOffset) {
mapOptions.size += mapOptions.resizeOffset * 2
}
.offset(x: baseOffset.x, y: baseOffset.y)
}
OverlayPanel(errorMessage: errorMessage, mapOptions: $mapOptions)
.padding()
}
}
.task {
await updateStaticMap()
}
.onChange(of: mapOptions.sizeWithResize) {
scheduleStaticMapUpdate()
}
.onReceive(mapUpdateSubject.debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)) {
Task {
await updateStaticMap()
}
}
}
private func scheduleStaticMapUpdate() {
mapUpdateSubject.send()
}
private func updateStaticMap() async {
do {
NSLog("Regenerating static map")
let cairoImage = try await mapOptions.staticMap.render { logMessage in
NSLog("%@", logMessage)
}
let pngData = try cairoImage.pngEncoded()
mapImage = NSImage(data: pngData)
mapOptions.markOutdated = false
errorMessage = nil
} catch {
errorMessage = "\(error)"
}
}
}
@available(macOS 15, *)
struct DemoGUIApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
// Some boilerplate to make the app run properly without a bundle
// https://forums.swift.org/t/is-it-possible-to-developer-a-swiftui-app-using-only-swiftpm/71755/2
DispatchQueue.main.async {
NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
if let window = NSApp.windows.first {
window.makeKeyAndOrderFront(nil)
window.collectionBehavior = .canJoinAllSpaces
}
}
if #available(macOS 15, *) {
DemoGUIApp.main()
} else {
fatalError("The demo GUI requires macOS 15!")
}
#else
fatalError("The demo GUI currently only runs on macOS!")
#endif