camshooter 0.1.1

Select a webcam, preview the live stream, and grab PNG snapshots with a keypress.
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! The eframe application: a styled device picker that transitions into a live preview,
//! with capture feedback (screen flash, fading toast, last-shot thumbnail).

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, Sender, TryRecvError, channel};
use std::time::Duration;

use eframe::egui::load::SizedTexture;
use eframe::egui::{
    self, Align, Align2, Color32, CornerRadius, CursorIcon, Id, Layout, Margin, Order, RichText,
    Sense, Stroke, StrokeKind, TextureHandle, TextureOptions, vec2,
};
use image::RgbImage;

use crate::capture::{CameraOption, CaptureHandle, list_cameras, start_capture};
use crate::snapshot::{SnapshotSettings, save_snapshot};

// ---- Palette (neutral darks + one warm amber accent = "capture/record"). ----
const C_WINDOW: Color32 = Color32::from_rgb(14, 15, 17);
const C_PANEL: Color32 = Color32::from_rgb(22, 24, 27);
const C_CARD: Color32 = Color32::from_rgb(30, 33, 37);
const C_STROKE: Color32 = Color32::from_rgb(52, 57, 63);
const C_TEXT: Color32 = Color32::from_rgb(236, 238, 240);
const C_DIM: Color32 = Color32::from_rgb(162, 168, 176);
const C_ACCENT: Color32 = Color32::from_rgb(232, 163, 61);
const C_ERROR: Color32 = Color32::from_rgb(229, 83, 75);
const C_LETTERBOX: Color32 = Color32::from_rgb(6, 6, 7);

// ---- Animation / thumbnail tuning. ----
const FLASH_SECS: f64 = 0.25;
const TOAST_HOLD: f64 = 1.5; // fully opaque
const TOAST_FADE: f64 = 1.0; // then fade out
const TOAST_TOTAL: f64 = TOAST_HOLD + TOAST_FADE;
const THUMB_W: u32 = 160;
const THUMB_H: u32 = 90;

/// A finished snapshot, sent from the worker thread back to the UI. The worker also does
/// the (potentially slow) downscale so the UI thread only uploads the small thumbnail.
struct Snapshot {
    path: PathBuf,
    thumb_rgb: Vec<u8>,
    thumb_w: u32,
    thumb_h: u32,
}

type SnapResult = Result<Snapshot, String>;

/// The most recent snapshot's thumbnail + where it was written.
struct LastShot {
    texture: TextureHandle,
    dir: PathBuf,
    name: String,
}

/// Top-level application state.
enum State {
    Picker {
        cameras: Vec<CameraOption>,
        error: Option<String>,
    },
    Previewing {
        capture: CaptureHandle,
        camera_name: String,
    },
}

/// What the current frame's input/rendering decided should happen to the state machine.
enum Action {
    None,
    Start(CameraOption),
    BackToPicker(Option<String>),
    Quit,
}

pub struct CamshooterApp {
    settings: SnapshotSettings,
    state: State,
    texture: Option<TextureHandle>,
    /// Most recent frame, kept so a snapshot keypress always has pixels to save.
    last_frame: Option<RgbImage>,
    /// Live resolution, cached for the info bar (latest_frame() yields None on heartbeats).
    preview_dims: Option<(u32, u32)>,
    /// Start time (egui clock) of an active shutter flash.
    flash_start: Option<f64>,
    /// Transient message: (text, shown_at, is_error).
    toast: Option<(String, f64, bool)>,
    /// Thumbnail of the most recent snapshot.
    last_shot: Option<LastShot>,
    snap_tx: Sender<SnapResult>,
    snap_rx: Receiver<SnapResult>,
    /// True while a snapshot is encoding/writing, so a held key can't spawn unbounded
    /// worker threads. Cleared on the UI thread when the result is consumed.
    snap_in_flight: Arc<AtomicBool>,
}

impl CamshooterApp {
    pub fn new(cc: &eframe::CreationContext<'_>, settings: SnapshotSettings) -> Self {
        apply_theme(&cc.egui_ctx);
        let (snap_tx, snap_rx) = channel();
        let state = match list_cameras() {
            Ok(cameras) => State::Picker {
                cameras,
                error: None,
            },
            Err(e) => State::Picker {
                cameras: Vec::new(),
                error: Some(format!("Could not list cameras: {e}")),
            },
        };
        Self {
            settings,
            state,
            texture: None,
            last_frame: None,
            preview_dims: None,
            flash_start: None,
            toast: None,
            last_shot: None,
            snap_tx,
            snap_rx,
            snap_in_flight: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Apply a decided transition, resetting all per-session UI state so nothing carries
    /// across cameras.
    fn apply(&mut self, action: Action, ctx: &egui::Context) {
        match action {
            Action::None => {}
            Action::Start(opt) => {
                self.stop_capture();
                self.reset_session();
                let capture = start_capture(opt.index.clone(), ctx.clone());
                self.state = State::Previewing {
                    capture,
                    camera_name: opt.name,
                };
            }
            Action::BackToPicker(err) => {
                self.stop_capture();
                self.reset_session();
                let cameras = list_cameras().unwrap_or_default();
                self.state = State::Picker {
                    cameras,
                    error: err,
                };
            }
            Action::Quit => {
                self.stop_capture(); // release the device/LED before the window closes
                ctx.send_viewport_cmd(egui::ViewportCommand::Close);
            }
        }
    }

    /// Clear everything tied to a single preview session.
    fn reset_session(&mut self) {
        // Discard any in-flight snapshot result from the previous session so it can't
        // land its toast/thumbnail in the next one. (`snap_tx` stays alive on `self`, so
        // this only ever drains, never disconnects.)
        while self.snap_rx.try_recv().is_ok() {}
        self.texture = None;
        self.last_frame = None;
        self.preview_dims = None;
        self.flash_start = None;
        self.toast = None;
        self.last_shot = None;
        self.snap_in_flight.store(false, Ordering::Release);
    }

    /// Stop the capture thread if one is running (joins it, freeing the device).
    fn stop_capture(&mut self) {
        if let State::Previewing { capture, .. } = &mut self.state {
            capture.stop();
        }
    }

    /// Drain finished snapshot saves: build the thumbnail texture, set the toast, and
    /// release the in-flight guard. Runs on the UI thread (needs `ctx` for the texture).
    fn drain_snapshots(&mut self, ctx: &egui::Context, now: f64) {
        loop {
            match self.snap_rx.try_recv() {
                Ok(Ok(snap)) => {
                    self.snap_in_flight.store(false, Ordering::Release);
                    let name = snap
                        .path
                        .file_name()
                        .map(|n| n.to_string_lossy().into_owned())
                        .unwrap_or_default();
                    let dir = snap
                        .path
                        .parent()
                        .map(Path::to_path_buf)
                        .unwrap_or_else(|| PathBuf::from("."));
                    let color = egui::ColorImage::from_rgb(
                        [snap.thumb_w as usize, snap.thumb_h as usize],
                        &snap.thumb_rgb,
                    );
                    let texture = ctx.load_texture("last_shot", color, TextureOptions::LINEAR);
                    self.toast = Some((format!("Saved {name}"), now, false));
                    self.last_shot = Some(LastShot { texture, dir, name });
                }
                Ok(Err(e)) => {
                    self.snap_in_flight.store(false, Ordering::Release);
                    self.toast = Some((format!("Snapshot failed: {e}"), now, true));
                }
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => {
                    // No live worker; the stored sender keeps this from normally firing.
                    self.snap_in_flight.store(false, Ordering::Release);
                    break;
                }
            }
        }
    }
}

impl eframe::App for CamshooterApp {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        let ctx = ui.ctx().clone();
        let now = ctx.input(|i| i.time);

        self.drain_snapshots(&ctx, now);

        // Expire animations (before painting, so the resolving frame draws cleanly).
        if let Some(start) = self.flash_start
            && now - start >= FLASH_SECS
        {
            self.flash_start = None;
        }
        if let Some((_, shown, _)) = &self.toast
            && now - *shown >= TOAST_TOTAL
        {
            self.toast = None;
        }

        let action = match &mut self.state {
            State::Picker { cameras, error } => render_picker(ui, cameras, error.as_deref()),
            State::Previewing {
                capture,
                camera_name,
            } => {
                if let Some(err) = capture.take_error() {
                    Action::BackToPicker(Some(err))
                } else {
                    render_preview(
                        ui,
                        capture,
                        camera_name.as_str(),
                        &self.settings,
                        now,
                        &mut self.texture,
                        &mut self.last_frame,
                        &mut self.preview_dims,
                        &mut self.flash_start,
                        &self.toast,
                        &self.last_shot,
                        &self.snap_tx,
                        &self.snap_in_flight,
                    )
                }
            }
        };

        self.apply(action, &ctx);

        // Keep animating smoothly while a flash or toast is active.
        if self.flash_start.is_some() || self.toast.is_some() {
            ctx.request_repaint();
        }
    }

    fn on_exit(&mut self) {
        // Belt-and-suspenders: ensure the camera is released on any exit path.
        self.stop_capture();
    }
}

/// One-time dark theme applied at startup.
fn apply_theme(ctx: &egui::Context) {
    use egui::{FontFamily::Monospace, FontFamily::Proportional, FontId, TextStyle};

    ctx.set_visuals(egui::Visuals::dark());
    ctx.all_styles_mut(|style| {
        let v = &mut style.visuals;
        v.panel_fill = C_PANEL;
        v.window_fill = C_WINDOW;
        v.extreme_bg_color = C_LETTERBOX;
        v.override_text_color = Some(C_TEXT);
        v.selection.bg_fill = C_ACCENT.linear_multiply(0.35);
        v.selection.stroke = Stroke::new(1.0, C_ACCENT);
        v.hyperlink_color = C_ACCENT;
        v.window_corner_radius = CornerRadius::same(10);
        for w in [
            &mut v.widgets.noninteractive,
            &mut v.widgets.inactive,
            &mut v.widgets.hovered,
            &mut v.widgets.active,
            &mut v.widgets.open,
        ] {
            w.corner_radius = CornerRadius::same(6);
        }
        style.spacing.item_spacing = vec2(10.0, 8.0);
        style.spacing.button_padding = vec2(12.0, 7.0);
        style.text_styles = [
            (TextStyle::Heading, FontId::new(22.0, Proportional)),
            (TextStyle::Body, FontId::new(15.0, Proportional)),
            (TextStyle::Button, FontId::new(15.0, Proportional)),
            (TextStyle::Monospace, FontId::new(13.0, Monospace)),
            (TextStyle::Small, FontId::new(12.5, Proportional)),
        ]
        .into();
    });
}

/// Render the device picker. Returns `Start(cam)` when the user picks one.
fn render_picker(ui: &mut egui::Ui, cameras: &[CameraOption], error: Option<&str>) -> Action {
    let ctx = ui.ctx().clone();
    let mut action = Action::None;

    // Number-key shortcuts 1..=9 select the Nth camera.
    const NUM_KEYS: [egui::Key; 9] = [
        egui::Key::Num1,
        egui::Key::Num2,
        egui::Key::Num3,
        egui::Key::Num4,
        egui::Key::Num5,
        egui::Key::Num6,
        egui::Key::Num7,
        egui::Key::Num8,
        egui::Key::Num9,
    ];
    for (i, key) in NUM_KEYS.iter().enumerate() {
        if i < cameras.len() && ctx.input(|input| input.key_pressed(*key)) {
            action = Action::Start(cameras[i].clone());
        }
    }

    // Quit from the picker on Esc or Q. The picker is the root screen, so there's nothing
    // to go "back" to — Esc means quit here. Q matches the preview screen's quit key.
    if ctx.input(|i| i.key_pressed(egui::Key::Escape) || i.key_pressed(egui::Key::Q)) {
        action = Action::Quit;
    }

    egui::CentralPanel::default().show_inside(ui, |ui| {
        ui.add_space(28.0);
        ui.vertical_centered(|ui| {
            ui.set_max_width(440.0);
            ui.heading(RichText::new("camshooter").color(C_ACCENT));
            ui.label(RichText::new("Choose a camera").color(C_DIM));
            ui.add_space(14.0);

            if let Some(err) = error {
                ui.colored_label(C_ERROR, err);
                ui.add_space(8.0);
            }

            if cameras.is_empty() {
                ui.label(RichText::new("No webcams detected.").color(C_DIM));
                ui.add_space(8.0);
                if ui.button("⟳  Refresh").clicked() {
                    action = Action::BackToPicker(None); // re-enumerates
                }
            } else {
                for (i, cam) in cameras.iter().enumerate() {
                    if camera_card(ui, i + 1, &cam.name).clicked() {
                        action = Action::Start(cam.clone());
                    }
                }
                ui.add_space(10.0);
                ui.label(
                    RichText::new("Tip: press 1–9 to pick by number")
                        .small()
                        .color(C_DIM),
                );
            }

            // Always show how to leave — important when no cameras are detected.
            ui.add_space(4.0);
            ui.label(RichText::new("Esc / Q = quit").small().color(C_DIM));
        });
    });

    action
}

/// A clickable, full-width camera card: icon + name + `[N]` badge, accent border on hover.
fn camera_card(ui: &mut egui::Ui, n: usize, name: &str) -> egui::Response {
    let resp = egui::Frame::default()
        .fill(C_CARD)
        .corner_radius(CornerRadius::same(10))
        .inner_margin(Margin::symmetric(14, 12))
        .stroke(Stroke::new(1.0, C_STROKE))
        .show(ui, |ui| {
            ui.set_width(ui.available_width());
            ui.horizontal(|ui| {
                ui.colored_label(C_ACCENT, "");
                ui.add_space(2.0);
                ui.label(RichText::new(name).strong());
                ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                    ui.label(RichText::new(format!("[{n}]")).color(C_DIM));
                });
            });
        })
        .response
        .interact(Sense::click());

    if resp.hovered() {
        ui.painter().rect_stroke(
            resp.rect,
            CornerRadius::same(10),
            Stroke::new(1.5, C_ACCENT),
            StrokeKind::Inside,
        );
    }
    resp.on_hover_cursor(CursorIcon::PointingHand)
}

/// Render the live preview plus capture feedback. Returns `Quit`/`BackToPicker` on keys.
#[allow(clippy::too_many_arguments)]
fn render_preview(
    ui: &mut egui::Ui,
    capture: &CaptureHandle,
    camera_name: &str,
    settings: &SnapshotSettings,
    now: f64,
    texture: &mut Option<TextureHandle>,
    last_frame: &mut Option<RgbImage>,
    preview_dims: &mut Option<(u32, u32)>,
    flash_start: &mut Option<f64>,
    toast: &Option<(String, f64, bool)>,
    last_shot: &Option<LastShot>,
    snap_tx: &Sender<SnapResult>,
    snap_in_flight: &Arc<AtomicBool>,
) -> Action {
    let ctx = ui.ctx().clone();

    // Pull the freshest frame (Some only when a new one arrived) and update the texture.
    if let Some(frame) = capture.latest_frame() {
        *preview_dims = Some((frame.width(), frame.height()));
        let size = [frame.width() as usize, frame.height() as usize];
        let color = egui::ColorImage::from_rgb(size, frame.as_raw());
        match texture {
            Some(tex) => tex.set(color, TextureOptions::LINEAR),
            None => *texture = Some(ctx.load_texture("preview", color, TextureOptions::LINEAR)),
        }
        *last_frame = Some(frame);
    }

    // Input.
    let snap = ctx.input(|i| i.key_pressed(egui::Key::Space) || i.key_pressed(egui::Key::S));
    let quit = ctx.input(|i| i.key_pressed(egui::Key::Q));
    let back = ctx.input(|i| i.key_pressed(egui::Key::Escape));

    // Snapshot: flash immediately, then encode + downscale off-thread (one in flight).
    if snap
        && let Some(frame) = last_frame.clone()
        && !snap_in_flight.swap(true, Ordering::AcqRel)
    {
        *flash_start = Some(now);
        let settings = settings.clone();
        let tx = snap_tx.clone();
        let ctx2 = ctx.clone();
        std::thread::spawn(move || {
            let result: SnapResult = save_snapshot(&frame, &settings)
                .map_err(|e| e.to_string())
                .map(|path| {
                    let thumb = image::imageops::thumbnail(&frame, THUMB_W, THUMB_H);
                    Snapshot {
                        path,
                        thumb_w: thumb.width(),
                        thumb_h: thumb.height(),
                        thumb_rgb: thumb.into_raw(),
                    }
                });
            let _ = tx.send(result);
            ctx2.request_repaint();
        });
    }

    // Top info bar: camera name + live resolution.
    egui::Panel::top("info").show_inside(ui, |ui| {
        ui.add_space(2.0);
        ui.horizontal(|ui| {
            ui.colored_label(C_ACCENT, "");
            ui.label(RichText::new(camera_name).strong());
            if let Some((w, h)) = *preview_dims {
                ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                    ui.label(RichText::new(format!("{w}×{h}")).color(C_DIM));
                });
            }
        });
        ui.add_space(2.0);
    });

    // Bottom hint bar.
    egui::Panel::bottom("hint").show_inside(ui, |ui| {
        ui.add_space(2.0);
        ui.label(
            RichText::new("Space  snapshot      ·      Esc  back      ·      Q  quit")
                .small()
                .color(C_DIM),
        );
        ui.add_space(2.0);
    });

    // Central video (letterboxed) + flash overlay on top.
    egui::CentralPanel::default()
        .frame(egui::Frame::default().fill(C_LETTERBOX))
        .show_inside(ui, |ui| {
            let avail = ui.available_size();
            ui.centered_and_justified(|ui| {
                if let Some(tex) = texture {
                    ui.add(
                        egui::Image::new(SizedTexture::new(tex.id(), tex.size_vec2()))
                            .maintain_aspect_ratio(true)
                            .fit_to_exact_size(avail),
                    );
                } else {
                    ui.label(RichText::new("Starting camera…").color(C_DIM));
                }
            });

            if let Some(start) = *flash_start {
                let progress = ((now - start) / FLASH_SECS).clamp(0.0, 1.0);
                let alpha = ((1.0 - progress) * 200.0) as u8;
                if alpha > 0 {
                    ui.painter()
                        .rect_filled(ui.max_rect(), 0, Color32::from_white_alpha(alpha));
                }
            }
        });

    draw_toast(&ctx, toast, now);
    draw_last_shot(&ctx, last_shot);

    // Heartbeat so we still notice a dead capture thread even if frames stop arriving.
    ctx.request_repaint_after(Duration::from_millis(100));

    if quit {
        Action::Quit
    } else if back {
        Action::BackToPicker(None)
    } else {
        Action::None
    }
}

/// Bottom-center toast that holds, then fades.
fn draw_toast(ctx: &egui::Context, toast: &Option<(String, f64, bool)>, now: f64) {
    let Some((msg, shown, is_err)) = toast else {
        return;
    };
    let elapsed = now - *shown;
    let alpha = if elapsed < TOAST_HOLD {
        1.0
    } else {
        (1.0 - (elapsed - TOAST_HOLD) / TOAST_FADE).clamp(0.0, 1.0)
    } as f32;
    if alpha <= 0.0 {
        return;
    }
    let border = if *is_err { C_ERROR } else { C_ACCENT };
    egui::Area::new(Id::new("toast"))
        .order(Order::Foreground)
        .default_size(vec2(320.0, 44.0)) // avoid a one-frame invisible sizing pass
        .anchor(Align2::CENTER_BOTTOM, vec2(0.0, -76.0))
        .show(ctx, |ui| {
            egui::Frame::default()
                .fill(Color32::from_black_alpha((220.0 * alpha) as u8))
                .corner_radius(CornerRadius::same(8))
                .inner_margin(Margin::symmetric(14, 8))
                .stroke(Stroke::new(1.0, border.gamma_multiply(alpha)))
                .show(ui, |ui| {
                    ui.label(RichText::new(msg).color(C_TEXT.gamma_multiply(alpha)));
                });
        });
}

/// Bottom-right thumbnail of the last snapshot; click opens the output folder.
fn draw_last_shot(ctx: &egui::Context, last_shot: &Option<LastShot>) {
    let Some(shot) = last_shot else {
        return;
    };
    egui::Area::new(Id::new("last_shot"))
        .order(Order::Foreground)
        .default_size(vec2(172.0, 116.0)) // avoid a one-frame invisible sizing pass
        .anchor(Align2::RIGHT_BOTTOM, vec2(-16.0, -68.0))
        .show(ctx, |ui| {
            egui::Frame::default()
                .fill(C_PANEL)
                .corner_radius(CornerRadius::same(8))
                .inner_margin(Margin::same(6))
                .stroke(Stroke::new(1.0, C_STROKE))
                .show(ui, |ui| {
                    ui.vertical(|ui| {
                        let resp = ui.add(
                            egui::Image::new(&shot.texture)
                                .fit_to_exact_size(vec2(160.0, 90.0))
                                .sense(Sense::click()),
                        );
                        ui.label(RichText::new(&shot.name).small().color(C_DIM));
                        if resp.on_hover_cursor(CursorIcon::PointingHand).clicked() {
                            open_folder(&shot.dir);
                        }
                    });
                });
        });
}

/// Open a folder in the desktop file manager (best-effort).
fn open_folder(dir: &Path) {
    let _ = std::process::Command::new("xdg-open").arg(dir).spawn();
}