forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRewindTimelinePlayerView.swift
More file actions
514 lines (447 loc) · 13.9 KB
/
Copy pathRewindTimelinePlayerView.swift
File metadata and controls
514 lines (447 loc) · 13.9 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
@preconcurrency import AppKit
import OmiTheme
import SwiftUI
/// A full-screen timeline player view with playback controls
/// Similar to screenpipe's timeline view - shows the current frame with a timeline slider
struct RewindTimelinePlayerView: View {
@StateObject private var viewModel: TimelinePlayerViewModel
@Environment(\.dismiss) private var dismiss
init(screenshots: [Screenshot], initialIndex: Int = 0) {
_viewModel = StateObject(
wrappedValue: TimelinePlayerViewModel(
screenshots: screenshots,
initialIndex: initialIndex
))
}
var body: some View {
ZStack {
// Background
Color.clear.ignoresSafeArea()
// Main content
VStack(spacing: 0) {
// Top bar with close button and info
topBar
// Current frame display
Spacer()
frameDisplay
Spacer()
// Timeline and controls at bottom
VStack(spacing: OmiSpacing.md) {
// Timeline slider
timelineSlider
// Playback controls
playbackControls
// Time and app info
frameInfo
}
.padding(.horizontal, OmiSpacing.xxl)
.padding(.bottom, OmiSpacing.xxl)
.background(
LinearGradient(
colors: [.clear, .black.opacity(0.8)],
startPoint: .top,
endPoint: .bottom
)
.frame(height: 200)
.offset(y: -50)
)
}
}
.onKeyPress(.escape) {
dismiss()
return .handled
}
.onKeyPress(.space) {
viewModel.togglePlayback()
return .handled
}
.onKeyPress(.leftArrow) {
viewModel.previousFrame()
return .handled
}
.onKeyPress(.rightArrow) {
viewModel.nextFrame()
return .handled
}
// The one exemption from "hosted content paints no background": this is a player, and a screen
// recording letterboxed against a light mat hides where the frame ends. `glassMediaMat` flips the
// environment to dark so the same `Ink` ladder resolves light-on-black here.
.glassMediaMat()
}
// MARK: - Top Bar
private var topBar: some View {
HStack {
Button {
dismiss()
} label: {
Image(systemName: "xmark")
.scaledFont(size: OmiType.subheading, weight: .semibold)
.foregroundColor(Ink.primary)
.frame(width: 32, height: 32)
.background(Ink.rowFillHover)
.clipShape(Circle())
}
.buttonStyle(.plain)
Spacer()
// Screenshot count
Text("\(viewModel.currentIndex + 1) / \(viewModel.screenshots.count)")
.scaledFont(size: OmiType.body, weight: .medium, design: .monospaced)
.foregroundColor(Ink.secondary)
Spacer()
// Playback speed
Menu {
ForEach([0.5, 1.0, 2.0, 4.0, 8.0], id: \.self) { speed in
Button {
viewModel.playbackSpeed = speed
} label: {
HStack {
Text("\(speed, specifier: "%.1f")x")
if viewModel.playbackSpeed == speed {
Image(systemName: "checkmark")
}
}
}
}
} label: {
HStack(spacing: OmiSpacing.xxs) {
Image(systemName: "speedometer")
Text("\(viewModel.playbackSpeed, specifier: "%.1f")x")
}
.scaledFont(size: OmiType.caption, weight: .medium)
.foregroundColor(Ink.secondary)
.padding(.horizontal, OmiSpacing.sm)
.padding(.vertical, OmiSpacing.xs)
.background(Ink.rowFillHover)
.cornerRadius(OmiChrome.elementRadius)
}
.buttonStyle(.plain)
}
.padding(.horizontal, OmiSpacing.xl)
.padding(.top, OmiSpacing.lg)
}
// MARK: - Frame Display
private var frameDisplay: some View {
Group {
if viewModel.isLoading {
ProgressView()
.progressViewStyle(.circular)
.scaleEffect(1.5)
.tint(Ink.primary)
} else if let image = viewModel.currentImage {
Image(nsImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(OmiChrome.elementRadius)
.shadow(color: .black.opacity(0.5), radius: 20)
} else {
VStack(spacing: OmiSpacing.md) {
Image(systemName: "photo")
.scaledFont(size: 48)
.foregroundColor(Ink.secondary)
Text("Failed to load frame")
.scaledFont(size: OmiType.body)
.foregroundColor(Ink.secondary)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.horizontal, OmiSpacing.page)
}
// MARK: - Timeline Slider
private var timelineSlider: some View {
VStack(spacing: OmiSpacing.sm) {
// App activity visualization
GeometryReader { geometry in
HStack(spacing: 1) {
ForEach(Array(viewModel.appSegments.enumerated()), id: \.offset) { index, segment in
Rectangle()
.fill(segment.color)
.frame(width: max(2, geometry.size.width * segment.widthRatio))
.opacity(isInCurrentSegment(index) ? 1.0 : 0.6)
}
}
}
.frame(height: 8)
.cornerRadius(OmiChrome.stripRadius)
// Slider
Slider(
value: Binding(
get: { Double(viewModel.currentIndex) },
set: { viewModel.seekToIndex(Int($0)) }
),
in: 0...Double(max(0, viewModel.screenshots.count - 1)),
step: 1
)
.tint(Ink.accent)
}
}
private func isInCurrentSegment(_ segmentIndex: Int) -> Bool {
var startIndex = 0
for (index, segment) in viewModel.appSegments.enumerated() {
let endIndex = startIndex + segment.count - 1
if index == segmentIndex {
return viewModel.currentIndex >= startIndex && viewModel.currentIndex <= endIndex
}
startIndex = endIndex + 1
}
return false
}
// MARK: - Playback Controls
private var playbackControls: some View {
HStack(spacing: OmiSpacing.xxl) {
// Skip to start
Button {
viewModel.seekToIndex(0)
} label: {
Image(systemName: "backward.end.fill")
.scaledFont(size: OmiType.heading)
.foregroundColor(Ink.primary)
}
.buttonStyle(.plain)
.disabled(viewModel.currentIndex == 0)
.opacity(viewModel.currentIndex == 0 ? 0.5 : 1.0)
// Previous frame
Button {
viewModel.previousFrame()
} label: {
Image(systemName: "backward.frame.fill")
.scaledFont(size: 24)
.foregroundColor(Ink.primary)
}
.buttonStyle(.plain)
.disabled(viewModel.currentIndex == 0)
.opacity(viewModel.currentIndex == 0 ? 0.5 : 1.0)
// Play/Pause
Button {
viewModel.togglePlayback()
} label: {
Image(systemName: viewModel.isPlaying ? "pause.fill" : "play.fill")
.scaledFont(size: 32)
.foregroundColor(Ink.surface)
.frame(width: 64, height: 64)
.background(Ink.primary)
.clipShape(Circle())
}
.buttonStyle(.plain)
// Next frame
Button {
viewModel.nextFrame()
} label: {
Image(systemName: "forward.frame.fill")
.scaledFont(size: 24)
.foregroundColor(Ink.primary)
}
.buttonStyle(.plain)
.disabled(viewModel.currentIndex >= viewModel.screenshots.count - 1)
.opacity(viewModel.currentIndex >= viewModel.screenshots.count - 1 ? 0.5 : 1.0)
// Skip to end
Button {
viewModel.seekToIndex(viewModel.screenshots.count - 1)
} label: {
Image(systemName: "forward.end.fill")
.scaledFont(size: OmiType.heading)
.foregroundColor(Ink.primary)
}
.buttonStyle(.plain)
.disabled(viewModel.currentIndex >= viewModel.screenshots.count - 1)
.opacity(viewModel.currentIndex >= viewModel.screenshots.count - 1 ? 0.5 : 1.0)
}
}
// MARK: - Frame Info
private var frameInfo: some View {
HStack {
if let screenshot = viewModel.currentScreenshot {
// App icon and name
HStack(spacing: OmiSpacing.sm) {
AppIconView(appName: screenshot.appName, size: 20)
Text(screenshot.appName)
.scaledFont(size: OmiType.body, weight: .medium)
.foregroundColor(Ink.primary)
}
Spacer()
// Timestamp
Text(screenshot.formattedDate)
.scaledFont(size: OmiType.body, design: .monospaced)
.foregroundColor(Ink.secondary)
}
}
}
}
// MARK: - View Model
@MainActor
class TimelinePlayerViewModel: ObservableObject {
let screenshots: [Screenshot]
@Published var currentIndex: Int
@Published var currentImage: NSImage?
@Published var isLoading = false
@Published var isPlaying = false
@Published var playbackSpeed: Double = 1.0
private var playbackTimer: Timer?
// App segments for timeline visualization
struct AppSegment {
let appName: String
let color: Color
let count: Int
let widthRatio: CGFloat
}
var appSegments: [AppSegment] {
guard let first = screenshots.first else { return [] }
var segments: [AppSegment] = []
var currentApp = first.appName
var currentCount = 0
for screenshot in screenshots {
if screenshot.appName == currentApp {
currentCount += 1
} else {
segments.append(
AppSegment(
appName: currentApp,
color: colorForApp(currentApp),
count: currentCount,
widthRatio: CGFloat(currentCount) / CGFloat(screenshots.count)
))
currentApp = screenshot.appName
currentCount = 1
}
}
// Add final segment
segments.append(
AppSegment(
appName: currentApp,
color: colorForApp(currentApp),
count: currentCount,
widthRatio: CGFloat(currentCount) / CGFloat(screenshots.count)
))
return segments
}
var currentScreenshot: Screenshot? {
guard currentIndex >= 0 && currentIndex < screenshots.count else { return nil }
return screenshots[currentIndex]
}
init(screenshots: [Screenshot], initialIndex: Int) {
self.screenshots = screenshots
self.currentIndex = min(initialIndex, screenshots.count - 1)
Task {
await loadCurrentFrameOrFindValid()
}
}
/// Load the current frame, or if it fails, find the first valid frame
func loadCurrentFrameOrFindValid() async {
guard !screenshots.isEmpty else { return }
isLoading = true
// Try to load current frame
if let image = await tryLoadFrame(at: currentIndex) {
currentImage = image
isLoading = false
return
}
// Current frame failed - search for first valid frame
// Search forward first, then backward
for offset in 1..<screenshots.count {
// Try forward
let forwardIndex = currentIndex + offset
if forwardIndex < screenshots.count {
if let image = await tryLoadFrame(at: forwardIndex) {
currentIndex = forwardIndex
currentImage = image
isLoading = false
log("TimelinePlayer: Skipped to valid frame at index \(forwardIndex)")
return
}
}
// Try backward
let backwardIndex = currentIndex - offset
if backwardIndex >= 0 {
if let image = await tryLoadFrame(at: backwardIndex) {
currentIndex = backwardIndex
currentImage = image
isLoading = false
log("TimelinePlayer: Skipped to valid frame at index \(backwardIndex)")
return
}
}
}
// No valid frames found
currentImage = nil
isLoading = false
logError("TimelinePlayer: No valid frames found in timeline")
}
/// Try to load a frame at a specific index, returns nil if failed
private func tryLoadFrame(at index: Int) async -> NSImage? {
guard index >= 0 && index < screenshots.count else { return nil }
let screenshot = screenshots[index]
do {
return try await RewindStorage.shared.loadScreenshotImage(for: screenshot)
} catch {
// Don't log errors during search - only log when we find a valid frame or give up
return nil
}
}
func loadCurrentFrame() async {
guard let screenshot = currentScreenshot else { return }
isLoading = true
do {
let image = try await RewindStorage.shared.loadScreenshotImage(for: screenshot)
currentImage = image
} catch {
logError("TimelinePlayer: Failed to load frame: \(error)")
currentImage = nil
}
isLoading = false
}
func seekToIndex(_ index: Int) {
let newIndex = max(0, min(index, screenshots.count - 1))
guard newIndex != currentIndex else { return }
currentIndex = newIndex
Task {
await loadCurrentFrame()
}
}
func nextFrame() {
seekToIndex(currentIndex + 1)
}
func previousFrame() {
seekToIndex(currentIndex - 1)
}
func togglePlayback() {
if isPlaying {
stopPlayback()
} else {
startPlayback()
}
}
func startPlayback() {
guard !isPlaying else { return }
isPlaying = true
// Calculate interval based on speed (base is 1 second per frame at 1x)
let interval = 1.0 / playbackSpeed
playbackTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
Task { @MainActor in
guard let self = self else { return }
if self.currentIndex < self.screenshots.count - 1 {
self.nextFrame()
} else {
// Reached end, stop playback
self.stopPlayback()
}
}
}
}
func stopPlayback() {
isPlaying = false
playbackTimer?.invalidate()
playbackTimer = nil
}
private func colorForApp(_ appName: String) -> Color {
// Generate a consistent color for each app
let hash = appName.hashValue
let hue = Double(abs(hash) % 360) / 360.0
return Color(hue: hue, saturation: 0.6, brightness: 0.8)
}
}
#if canImport(PreviewsMacros)
#Preview {
RewindTimelinePlayerView(screenshots: [], initialIndex: 0)
.frame(width: 1200, height: 800)
}
#endif