|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+import SwiftUI
|
|
|
2
|
+import AppKit
|
|
|
3
|
+
|
|
|
4
|
+struct DesktopWidgetView: View {
|
|
|
5
|
+ let variant: WidgetVariant
|
|
|
6
|
+ var isPreview: Bool
|
|
|
7
|
+ private let authService = GoogleOAuthService.shared
|
|
|
8
|
+
|
|
|
9
|
+ private var isSignedIn: Bool {
|
|
|
10
|
+ if isPreview { return true }
|
|
|
11
|
+ return authService.loadTokens() != nil
|
|
|
12
|
+ }
|
|
|
13
|
+
|
|
|
14
|
+ private var topMeetings: [WidgetMeetingSnapshot] {
|
|
|
15
|
+ WidgetMeetingStore.load().prefix(3).map { $0 }
|
|
|
16
|
+ }
|
|
|
17
|
+
|
|
|
18
|
+ var body: some View {
|
|
|
19
|
+ ZStack {
|
|
|
20
|
+ RoundedRectangle(cornerRadius: 22, style: .continuous)
|
|
|
21
|
+ .fill(
|
|
|
22
|
+ LinearGradient(
|
|
|
23
|
+ colors: [
|
|
|
24
|
+ Color(red: 0.08, green: 0.48, blue: 0.86),
|
|
|
25
|
+ Color(red: 0.09, green: 0.66, blue: 0.46)
|
|
|
26
|
+ ],
|
|
|
27
|
+ startPoint: .topLeading,
|
|
|
28
|
+ endPoint: .bottomTrailing
|
|
|
29
|
+ )
|
|
|
30
|
+ )
|
|
|
31
|
+ .overlay(
|
|
|
32
|
+ RoundedRectangle(cornerRadius: 22, style: .continuous)
|
|
|
33
|
+ .stroke(Color.white.opacity(0.14), lineWidth: 1)
|
|
|
34
|
+ )
|
|
|
35
|
+
|
|
|
36
|
+ VStack(alignment: .leading, spacing: 12) {
|
|
|
37
|
+ widgetHeader
|
|
|
38
|
+ contentBody
|
|
|
39
|
+ if variant.size != .small {
|
|
|
40
|
+ Spacer(minLength: 0)
|
|
|
41
|
+ }
|
|
|
42
|
+ }
|
|
|
43
|
+ .padding(.horizontal, 14)
|
|
|
44
|
+ .padding(.bottom, variant.size == .medium ? 30 : 14)
|
|
|
45
|
+ .padding(.top, variant.size == .medium ? 34 : (variant.size == .small ? 10 : 14))
|
|
|
46
|
+ }
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ @ViewBuilder
|
|
|
50
|
+ private var contentBody: some View {
|
|
|
51
|
+ if !isSignedIn {
|
|
|
52
|
+ loggedOutContent
|
|
|
53
|
+ } else {
|
|
|
54
|
+ switch variant.size {
|
|
|
55
|
+ case .small:
|
|
|
56
|
+ VStack(alignment: .leading, spacing: 10) {
|
|
|
57
|
+ Text("Quick actions")
|
|
|
58
|
+ .font(.system(size: 11.5, weight: .semibold))
|
|
|
59
|
+ .foregroundStyle(.white.opacity(0.78))
|
|
|
60
|
+ compactActionButton(title: "Open Meet", icon: "video.fill", destination: .openMeetWeb)
|
|
|
61
|
+ HStack(spacing: 8) {
|
|
|
62
|
+ compactActionButton(title: "Schedule", icon: "clock.badge.checkmark", destination: .schedule)
|
|
|
63
|
+ compactActionButton(title: "Settings", icon: "gearshape.fill", destination: .settings)
|
|
|
64
|
+ }
|
|
|
65
|
+ }
|
|
|
66
|
+ case .medium:
|
|
|
67
|
+ meetingBlock(maxRows: 2)
|
|
|
68
|
+ LazyVGrid(columns: Array(repeating: GridItem(.flexible(minimum: 0), spacing: 8), count: 2), spacing: 8) {
|
|
|
69
|
+ ForEach(variant.quickActions.prefix(4)) { action in
|
|
|
70
|
+ actionTile(action)
|
|
|
71
|
+ }
|
|
|
72
|
+ }
|
|
|
73
|
+ case .large:
|
|
|
74
|
+ meetingBlock(maxRows: 3)
|
|
|
75
|
+ Divider().overlay(Color.white.opacity(0.18))
|
|
|
76
|
+ LazyVGrid(columns: Array(repeating: GridItem(.flexible(minimum: 0), spacing: 8), count: 2), spacing: 8) {
|
|
|
77
|
+ ForEach(variant.quickActions.prefix(6)) { action in
|
|
|
78
|
+ actionTile(action)
|
|
|
79
|
+ }
|
|
|
80
|
+ }
|
|
|
81
|
+ }
|
|
|
82
|
+ }
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ private var widgetHeader: some View {
|
|
|
86
|
+ HStack(spacing: 10) {
|
|
|
87
|
+ Image("HeaderLogo")
|
|
|
88
|
+ .resizable()
|
|
|
89
|
+ .scaledToFit()
|
|
|
90
|
+ .frame(width: 30, height: 30)
|
|
|
91
|
+ .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
|
|
|
92
|
+ VStack(alignment: .leading, spacing: 2) {
|
|
|
93
|
+ Text(variant.title)
|
|
|
94
|
+ .font(.system(size: 16, weight: .bold))
|
|
|
95
|
+ .foregroundStyle(.white.opacity(0.95))
|
|
|
96
|
+ Text(variant.subtitle)
|
|
|
97
|
+ .font(.system(size: 12, weight: .medium))
|
|
|
98
|
+ .foregroundStyle(.white.opacity(0.8))
|
|
|
99
|
+ }
|
|
|
100
|
+ Spacer(minLength: 0)
|
|
|
101
|
+ }
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ private func meetingBlock(maxRows: Int) -> some View {
|
|
|
105
|
+ VStack(alignment: .leading, spacing: 9) {
|
|
|
106
|
+ Text("Upcoming meetings")
|
|
|
107
|
+ .font(.system(size: 11.5, weight: .semibold))
|
|
|
108
|
+ .foregroundStyle(.white.opacity(0.80))
|
|
|
109
|
+ if topMeetings.isEmpty {
|
|
|
110
|
+ Text("No upcoming meetings")
|
|
|
111
|
+ .font(.system(size: 12, weight: .medium))
|
|
|
112
|
+ .foregroundStyle(.white.opacity(0.82))
|
|
|
113
|
+ } else {
|
|
|
114
|
+ VStack(alignment: .leading, spacing: 8) {
|
|
|
115
|
+ ForEach(topMeetings.prefix(maxRows)) { meeting in
|
|
|
116
|
+ meetingRow(meeting)
|
|
|
117
|
+ }
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ }
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ @ViewBuilder
|
|
|
124
|
+ private var loggedOutContent: some View {
|
|
|
125
|
+ VStack(alignment: .leading, spacing: 10) {
|
|
|
126
|
+ Text("Connect Google to use widget actions.")
|
|
|
127
|
+ .font(.system(size: 12, weight: .medium))
|
|
|
128
|
+ .foregroundStyle(.white.opacity(0.86))
|
|
|
129
|
+ Button(action: { open(action: WidgetQuickAction(id: "login", title: "Login", systemImage: "person.crop.circle.badge.checkmark", destination: .signIn)) }) {
|
|
|
130
|
+ HStack(spacing: 8) {
|
|
|
131
|
+ Image(systemName: "person.crop.circle.badge.checkmark")
|
|
|
132
|
+ .font(.system(size: 12, weight: .semibold))
|
|
|
133
|
+ Text("Sign in with Google")
|
|
|
134
|
+ .font(.system(size: 12.5, weight: .bold))
|
|
|
135
|
+ Spacer(minLength: 0)
|
|
|
136
|
+ }
|
|
|
137
|
+ .foregroundStyle(.white.opacity(0.95))
|
|
|
138
|
+ .padding(.horizontal, 12)
|
|
|
139
|
+ .padding(.vertical, 10)
|
|
|
140
|
+ .background(
|
|
|
141
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
142
|
+ .fill(Color.black.opacity(0.24))
|
|
|
143
|
+ )
|
|
|
144
|
+ .overlay(
|
|
|
145
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
146
|
+ .stroke(Color.white.opacity(0.16), lineWidth: 1)
|
|
|
147
|
+ )
|
|
|
148
|
+ }
|
|
|
149
|
+ .buttonStyle(.plain)
|
|
|
150
|
+ .disabled(isPreview)
|
|
|
151
|
+ .allowsHitTesting(!isPreview)
|
|
|
152
|
+ }
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ private func meetingRow(_ meeting: WidgetMeetingSnapshot) -> some View {
|
|
|
156
|
+ Button(action: {
|
|
|
157
|
+ if let link = meeting.joinLink?.trimmingCharacters(in: .whitespacesAndNewlines), !link.isEmpty {
|
|
|
158
|
+ WidgetAppNavigator.openMeetingLink(link)
|
|
|
159
|
+ } else {
|
|
|
160
|
+ WidgetAppNavigator.open(target: .schedule)
|
|
|
161
|
+ }
|
|
|
162
|
+ }) {
|
|
|
163
|
+ HStack(spacing: 8) {
|
|
|
164
|
+ Image(systemName: "video.fill")
|
|
|
165
|
+ .font(.system(size: 11, weight: .semibold))
|
|
|
166
|
+ .foregroundStyle(.white.opacity(0.88))
|
|
|
167
|
+ VStack(alignment: .leading, spacing: 1) {
|
|
|
168
|
+ Text(meeting.title)
|
|
|
169
|
+ .font(.system(size: 11.5, weight: .semibold))
|
|
|
170
|
+ .foregroundStyle(.white.opacity(0.93))
|
|
|
171
|
+ .lineLimit(1)
|
|
|
172
|
+ Text(meeting.timeText)
|
|
|
173
|
+ .font(.system(size: 10.5, weight: .medium))
|
|
|
174
|
+ .foregroundStyle(.white.opacity(0.74))
|
|
|
175
|
+ .lineLimit(1)
|
|
|
176
|
+ }
|
|
|
177
|
+ Spacer(minLength: 0)
|
|
|
178
|
+ }
|
|
|
179
|
+ .padding(.horizontal, 10)
|
|
|
180
|
+ .padding(.vertical, 7)
|
|
|
181
|
+ .background(
|
|
|
182
|
+ RoundedRectangle(cornerRadius: 10, style: .continuous)
|
|
|
183
|
+ .fill(Color.black.opacity(0.20))
|
|
|
184
|
+ )
|
|
|
185
|
+ .overlay(
|
|
|
186
|
+ RoundedRectangle(cornerRadius: 10, style: .continuous)
|
|
|
187
|
+ .stroke(Color.white.opacity(0.08), lineWidth: 1)
|
|
|
188
|
+ )
|
|
|
189
|
+ }
|
|
|
190
|
+ .buttonStyle(.plain)
|
|
|
191
|
+ .disabled(isPreview)
|
|
|
192
|
+ .allowsHitTesting(!isPreview)
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ private func compactActionButton(title: String, icon: String, destination: WidgetQuickAction.Destination) -> some View {
|
|
|
196
|
+ Button(action: { WidgetAppNavigator.open(target: destination) }) {
|
|
|
197
|
+ HStack(spacing: 5) {
|
|
|
198
|
+ Image(systemName: icon)
|
|
|
199
|
+ .font(.system(size: 10.5, weight: .semibold))
|
|
|
200
|
+ .frame(width: 14)
|
|
|
201
|
+ Text(title)
|
|
|
202
|
+ .font(.system(size: 11.5, weight: .bold))
|
|
|
203
|
+ .lineLimit(1)
|
|
|
204
|
+ .minimumScaleFactor(0.82)
|
|
|
205
|
+ .layoutPriority(1)
|
|
|
206
|
+ }
|
|
|
207
|
+ .foregroundStyle(.white.opacity(0.97))
|
|
|
208
|
+ .padding(.horizontal, 10)
|
|
|
209
|
+ .padding(.vertical, 8)
|
|
|
210
|
+ .frame(maxWidth: .infinity)
|
|
|
211
|
+ .background(
|
|
|
212
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
213
|
+ .fill(Color.black.opacity(0.30))
|
|
|
214
|
+ )
|
|
|
215
|
+ .overlay(
|
|
|
216
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
217
|
+ .stroke(Color.white.opacity(0.24), lineWidth: 1)
|
|
|
218
|
+ )
|
|
|
219
|
+ }
|
|
|
220
|
+ .buttonStyle(.plain)
|
|
|
221
|
+ .disabled(isPreview)
|
|
|
222
|
+ .allowsHitTesting(!isPreview)
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ private func actionTile(_ action: WidgetQuickAction) -> some View {
|
|
|
226
|
+ Button(action: { open(action: action) }) {
|
|
|
227
|
+ HStack(spacing: 8) {
|
|
|
228
|
+ Image(systemName: action.systemImage)
|
|
|
229
|
+ .font(.system(size: 11, weight: .semibold))
|
|
|
230
|
+ Text(action.title)
|
|
|
231
|
+ .font(.system(size: 12, weight: .bold))
|
|
|
232
|
+ .lineLimit(1)
|
|
|
233
|
+ Spacer(minLength: 0)
|
|
|
234
|
+ }
|
|
|
235
|
+ .foregroundStyle(.white.opacity(0.92))
|
|
|
236
|
+ .padding(.horizontal, 10)
|
|
|
237
|
+ .padding(.vertical, 9)
|
|
|
238
|
+ .background(
|
|
|
239
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
240
|
+ .fill(Color.black.opacity(0.24))
|
|
|
241
|
+ )
|
|
|
242
|
+ .overlay(
|
|
|
243
|
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
|
244
|
+ .stroke(Color.white.opacity(0.10), lineWidth: 1)
|
|
|
245
|
+ )
|
|
|
246
|
+ }
|
|
|
247
|
+ .buttonStyle(.plain)
|
|
|
248
|
+ .disabled(isPreview)
|
|
|
249
|
+ .allowsHitTesting(!isPreview)
|
|
|
250
|
+ }
|
|
|
251
|
+
|
|
|
252
|
+ private func open(action: WidgetQuickAction) {
|
|
|
253
|
+ guard !isPreview else { return }
|
|
|
254
|
+ WidgetAppNavigator.open(target: action.destination)
|
|
|
255
|
+ }
|
|
|
256
|
+}
|
|
|
257
|
+
|
|
|
258
|
+struct WidgetMeetingSnapshot: Codable, Identifiable {
|
|
|
259
|
+ let id: String
|
|
|
260
|
+ let title: String
|
|
|
261
|
+ let timeText: String
|
|
|
262
|
+ let joinLink: String?
|
|
|
263
|
+}
|
|
|
264
|
+
|
|
|
265
|
+enum WidgetMeetingStore {
|
|
|
266
|
+ static let key = "meetings.widget.topMeetings"
|
|
|
267
|
+
|
|
|
268
|
+ static func load() -> [WidgetMeetingSnapshot] {
|
|
|
269
|
+ guard let data = UserDefaults.standard.data(forKey: key) else { return [] }
|
|
|
270
|
+ return (try? JSONDecoder().decode([WidgetMeetingSnapshot].self, from: data)) ?? []
|
|
|
271
|
+ }
|
|
|
272
|
+}
|
|
|
273
|
+
|
|
|
274
|
+enum WidgetAppNavigator {
|
|
|
275
|
+ static func open(target: WidgetQuickAction.Destination) {
|
|
|
276
|
+ DispatchQueue.main.async {
|
|
|
277
|
+ if target == .refreshMeetings {
|
|
|
278
|
+ postTarget(target, delay: 0)
|
|
|
279
|
+ return
|
|
|
280
|
+ }
|
|
|
281
|
+ if target == .signIn {
|
|
|
282
|
+ postTarget(target, delay: 0)
|
|
|
283
|
+ return
|
|
|
284
|
+ }
|
|
|
285
|
+
|
|
|
286
|
+ bringAppToFront()
|
|
|
287
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.18) {
|
|
|
288
|
+ bringAppToFront()
|
|
|
289
|
+ }
|
|
|
290
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.42) {
|
|
|
291
|
+ bringAppToFront()
|
|
|
292
|
+ }
|
|
|
293
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.60) {
|
|
|
294
|
+ activateApplicationFallback()
|
|
|
295
|
+ bringAppToFront()
|
|
|
296
|
+ }
|
|
|
297
|
+ postTarget(target, delay: 0.30)
|
|
|
298
|
+ }
|
|
|
299
|
+ }
|
|
|
300
|
+
|
|
|
301
|
+ static func openMeetingLink(_ link: String) {
|
|
|
302
|
+ DispatchQueue.main.async {
|
|
|
303
|
+ bringAppToFront()
|
|
|
304
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.20) {
|
|
|
305
|
+ bringAppToFront()
|
|
|
306
|
+ }
|
|
|
307
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.36) {
|
|
|
308
|
+ NotificationCenter.default.post(
|
|
|
309
|
+ name: .widgetOpenMeetingLinkRequested,
|
|
|
310
|
+ object: nil,
|
|
|
311
|
+ userInfo: ["link": link]
|
|
|
312
|
+ )
|
|
|
313
|
+ }
|
|
|
314
|
+ }
|
|
|
315
|
+ }
|
|
|
316
|
+
|
|
|
317
|
+ private static func bringAppToFront() {
|
|
|
318
|
+ NSApp.unhide(nil)
|
|
|
319
|
+ NSRunningApplication.current.activate(options: [.activateAllWindows, .activateIgnoringOtherApps])
|
|
|
320
|
+ NSApp.activate(ignoringOtherApps: true)
|
|
|
321
|
+ NSApp.arrangeInFront(nil)
|
|
|
322
|
+ for window in NSApp.windows where window.contentViewController is ViewController {
|
|
|
323
|
+ if window.isMiniaturized {
|
|
|
324
|
+ window.deminiaturize(nil)
|
|
|
325
|
+ }
|
|
|
326
|
+ window.orderFrontRegardless()
|
|
|
327
|
+ window.makeKeyAndOrderFront(nil)
|
|
|
328
|
+ }
|
|
|
329
|
+ }
|
|
|
330
|
+
|
|
|
331
|
+ private static func activateApplicationFallback() {
|
|
|
332
|
+ let bundleID = Bundle.main.bundleIdentifier ?? "com.mqldev.meetingsapp"
|
|
|
333
|
+ let scriptSource = "tell application id \"\(bundleID)\" to activate"
|
|
|
334
|
+ if let script = NSAppleScript(source: scriptSource) {
|
|
|
335
|
+ var errorInfo: NSDictionary?
|
|
|
336
|
+ script.executeAndReturnError(&errorInfo)
|
|
|
337
|
+ }
|
|
|
338
|
+ }
|
|
|
339
|
+
|
|
|
340
|
+ private static func postTarget(_ target: WidgetQuickAction.Destination, delay: TimeInterval) {
|
|
|
341
|
+ DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
|
|
|
342
|
+ switch target {
|
|
|
343
|
+ case .joinMeetings:
|
|
|
344
|
+ NotificationCenter.default.post(name: .widgetOpenJoinMeetingsPage, object: nil)
|
|
|
345
|
+ case .schedule:
|
|
|
346
|
+ NotificationCenter.default.post(name: .widgetOpenSchedulePage, object: nil)
|
|
|
347
|
+ case .calendar:
|
|
|
348
|
+ NotificationCenter.default.post(name: .widgetOpenCalendarPage, object: nil)
|
|
|
349
|
+ case .settings:
|
|
|
350
|
+ NotificationCenter.default.post(name: .widgetOpenSettingsPage, object: nil)
|
|
|
351
|
+ case .signIn:
|
|
|
352
|
+ NotificationCenter.default.post(name: .widgetSignInRequested, object: nil)
|
|
|
353
|
+ case .openMeetWeb:
|
|
|
354
|
+ NotificationCenter.default.post(name: .widgetOpenMeetWebRequested, object: nil)
|
|
|
355
|
+ case .refreshMeetings:
|
|
|
356
|
+ NotificationCenter.default.post(name: .widgetRefreshRequested, object: nil)
|
|
|
357
|
+ }
|
|
|
358
|
+ }
|
|
|
359
|
+ }
|
|
|
360
|
+}
|
|
|
361
|
+
|
|
|
362
|
+extension Notification.Name {
|
|
|
363
|
+ static let widgetOpenJoinMeetingsPage = Notification.Name("widgetOpenJoinMeetingsPage")
|
|
|
364
|
+ static let widgetOpenSchedulePage = Notification.Name("widgetOpenSchedulePage")
|
|
|
365
|
+ static let widgetOpenCalendarPage = Notification.Name("widgetOpenCalendarPage")
|
|
|
366
|
+ static let widgetOpenSettingsPage = Notification.Name("widgetOpenSettingsPage")
|
|
|
367
|
+ static let widgetSignInRequested = Notification.Name("widgetSignInRequested")
|
|
|
368
|
+ static let widgetOpenMeetWebRequested = Notification.Name("widgetOpenMeetWebRequested")
|
|
|
369
|
+ static let widgetRefreshRequested = Notification.Name("widgetRefreshRequested")
|
|
|
370
|
+ static let widgetOpenMeetingLinkRequested = Notification.Name("widgetOpenMeetingLinkRequested")
|
|
|
371
|
+}
|