diff --git a/AGENTS.md b/AGENTS.md index 0e44316..36d4d09 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,3 +7,4 @@ - Target the Linux KMS backend: let the backend choose screen dimensions and fullscreen behavior. Do not force window sizes or fullscreen in application code. - Brand the header as `winf photobooth`, using the standard winf.at favicon, without a tagline or live-camera badge. - Size instruction panels to their content and use consistent padding at the screen edges. +- Scale branding, icons, typography, and spacing with screen dimensions; cap the centered instruction panel width on large monitors. diff --git a/src/capture.rs b/src/capture.rs new file mode 100644 index 0000000..01d911f --- /dev/null +++ b/src/capture.rs @@ -0,0 +1,119 @@ +use std::time::{Duration, Instant}; + +#[derive(Default)] +enum Phase { + #[default] + Tracking, + Countdown(Instant), + AwaitingRelease, +} + +#[derive(Default)] +pub struct Sequence { + phase: Phase, +} + +#[derive(Default)] +pub struct Presentation { + pub countdown: i32, + pub flash_opacity: f32, + pub awaiting_release: bool, +} + +impl Sequence { + /// Returns true exactly once when a countdown finishes, even if frames skip. + pub fn tick(&mut self, complete: bool, hand_present: bool, now: Instant) -> bool { + match self.phase { + Phase::Tracking if complete => self.phase = Phase::Countdown(now), + Phase::Countdown(start) if now.duration_since(start) >= Duration::from_secs(3) => { + self.phase = Phase::AwaitingRelease; + return true; + } + Phase::AwaitingRelease if !hand_present => self.phase = Phase::Tracking, + _ => {} + } + false + } + + pub fn tracking(&self) -> bool { + matches!(self.phase, Phase::Tracking) + } + + pub fn presentation(&self, now: Instant) -> Presentation { + match self.phase { + Phase::Countdown(start) => { + let elapsed = now.duration_since(start).as_secs_f32(); + Presentation { + countdown: (3 - elapsed as i32).clamp(1, 3), + // A brief translucent white flash when the palm circle fills. + flash_opacity: 0.75 * (1.0 - elapsed / 0.4).clamp(0.0, 1.0), + awaiting_release: false, + } + } + Phase::AwaitingRelease => Presentation { + awaiting_release: true, + ..Default::default() + }, + Phase::Tracking => Presentation::default(), + } + } +} + +pub fn take_picture() { + // TODO: Take a picture here after the countdown. No capture or file writes yet. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn flash_countdown_and_single_capture_then_release() { + let start = Instant::now(); + let mut sequence = Sequence::default(); + assert!(!sequence.tick(false, true, start)); + assert!(sequence.tracking()); + assert!(!sequence.tick(true, true, start)); + assert!(!sequence.tracking()); + assert_eq!(sequence.presentation(start).countdown, 3); + assert_eq!(sequence.presentation(start).flash_opacity, 0.75); + assert_eq!( + sequence + .presentation(start + Duration::from_millis(200)) + .flash_opacity, + 0.375 + ); + for (ms, number) in [(999, 3), (1000, 2), (1999, 2), (2000, 1), (2999, 1)] { + let now = start + Duration::from_millis(ms); + // Lowering the hand must not cancel an already-started countdown. + assert!(!sequence.tick(false, false, now)); + assert_eq!(sequence.presentation(now).countdown, number); + assert_eq!(sequence.presentation(now).flash_opacity, 0.0); + } + assert!(sequence.tick(true, true, start + Duration::from_secs(3))); + assert!( + sequence + .presentation(start + Duration::from_secs(3)) + .awaiting_release + ); + assert_eq!( + sequence + .presentation(start + Duration::from_secs(3)) + .countdown, + 0 + ); + assert!(!sequence.tick(true, true, start + Duration::from_secs(4))); + assert!(!sequence.tracking()); + assert!(!sequence.tick(false, false, start + Duration::from_secs(5))); + assert!(sequence.tracking()); + } + + #[test] + fn delayed_frame_still_finishes_only_once() { + let now = Instant::now(); + let mut sequence = Sequence::default(); + sequence.tick(true, true, now); + assert!(sequence.tick(false, false, now + Duration::from_secs(10))); + assert!(!sequence.tick(false, false, now + Duration::from_secs(11))); + } +} diff --git a/src/main.rs b/src/main.rs index c12883c..93f7d1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,9 +16,16 @@ use slint::{Rgb8Pixel, SharedPixelBuffer}; slint::include_modules!(); +mod capture; mod hand_pose; mod palm; +struct PreviewFrame { + buffer: SharedPixelBuffer, + hand_detected: bool, + presentation: capture::Presentation, +} + fn main() -> Result<(), Box> { let mut cam = videoio::VideoCapture::new(0, videoio::CAP_V4L2).expect("Failed to get video capture"); @@ -48,7 +55,11 @@ fn main() -> Result<(), Box> { ui.window() .on_close_requested(|| slint::CloseRequestResponse::KeepWindowShown); - let latest_frame = Arc::new(Mutex::new(Some(frame_to_buffer(&frame)?))); + let latest_frame = Arc::new(Mutex::new(Some(PreviewFrame { + buffer: frame_to_buffer(&frame)?, + hand_detected: false, + presentation: capture::Presentation::default(), + }))); let running = Arc::new(AtomicBool::new(true)); let worker_frame = Arc::clone(&latest_frame); @@ -56,7 +67,6 @@ fn main() -> Result<(), Box> { let tracker = Arc::new(Mutex::new(palm::Tracker::default())); let detection_tracker = Arc::clone(&tracker); - let ui_tracker = Arc::clone(&tracker); // A rendezvous channel drops samples while inference is busy, avoiding a backlog. let (detection_tx, detection_rx) = mpsc::sync_channel::(0); let detection_worker = thread::spawn(move || { @@ -76,6 +86,7 @@ fn main() -> Result<(), Box> { }); let worker = thread::spawn(move || -> opencv::Result<()> { + let mut sequence = capture::Sequence::default(); let mut last_detection = Instant::now() - palm::DETECTION_INTERVAL; while worker_running.load(Ordering::Relaxed) { if !cam.read(&mut frame)? || frame.empty() { @@ -88,11 +99,24 @@ fn main() -> Result<(), Box> { let _ = detection_tx.try_send(frame.try_clone()?); last_detection = now; } - tracker.lock().unwrap().draw(&mut frame, now)?; - let buffer = frame_to_buffer(&frame)?; + let mut tracker = tracker.lock().unwrap(); + let hand_detected = tracker.has_palm(now); + if sequence.tick(tracker.complete(now), hand_detected, now) { + capture::take_picture(); + } + if sequence.tracking() { + tracker.draw(&mut frame, now)?; + } + drop(tracker); + let preview = PreviewFrame { + buffer: frame_to_buffer(&frame)?, + hand_detected, + presentation: sequence.presentation(now), + }; - // Replace any undisplayed frame with the newest one. - *worker_frame.lock().unwrap() = Some(buffer); + // Publish image and presentation together so a countdown frame never + // accidentally contains the palm circle from an older capture frame. + *worker_frame.lock().unwrap() = Some(preview); } Ok(()) @@ -105,11 +129,14 @@ fn main() -> Result<(), Box> { slint::TimerMode::Repeated, Duration::from_millis(33), move || { - let buffer = latest_frame.lock().unwrap().take(); + let preview = latest_frame.lock().unwrap().take(); - if let (Some(app), Some(buffer)) = (ui_handle.upgrade(), buffer) { - app.set_video_frame(slint::Image::from_rgb8(buffer)); - app.set_hand_detected(ui_tracker.lock().unwrap().has_palm(Instant::now())); + if let (Some(app), Some(preview)) = (ui_handle.upgrade(), preview) { + app.set_video_frame(slint::Image::from_rgb8(preview.buffer)); + app.set_hand_detected(preview.hand_detected); + app.set_countdown(preview.presentation.countdown); + app.set_flash_opacity(preview.presentation.flash_opacity); + app.set_awaiting_release(preview.presentation.awaiting_release); } }, ); diff --git a/src/palm.rs b/src/palm.rs index 845078f..521ffa1 100644 --- a/src/palm.rs +++ b/src/palm.rs @@ -185,6 +185,11 @@ impl Tracker { Some((self.palm?, (elapsed - 1.0).clamp(0.0, 1.0))) } + pub fn complete(&self, now: Instant) -> bool { + self.visible(now) + .is_some_and(|(_, progress)| progress >= 1.0) + } + pub fn has_palm(&self, now: Instant) -> bool { self.last_seen .is_some_and(|seen| now.duration_since(seen) < STALE_AFTER) @@ -271,6 +276,7 @@ mod tests { for ms in [0, 250, 500, 750, 1000, 1250, 1500, 1750, 2000] { let now = start + Duration::from_millis(ms); tracker.update(palm(100.0), now); + assert_eq!(tracker.complete(now), ms >= 2000); assert_eq!( tracker.visible(now).unwrap().1, ((ms as f64 / 1000.0) - 1.0).clamp(0.0, 1.0) diff --git a/ui/app-window.slint b/ui/app-window.slint index 1283c69..12e76ea 100644 --- a/ui/app-window.slint +++ b/ui/app-window.slint @@ -1,4 +1,5 @@ component Instruction inherits Rectangle { + in property ui-scale: 1; in property number; in property german; in property english; @@ -8,44 +9,44 @@ component Instruction inherits Rectangle { preferred-width: 0px; vertical-stretch: 0; background: active ? root.accent.with-alpha(0.13) : #f5f7f8; - border-radius: 16px; + border-radius: 16px * root.ui-scale; - height: max(36px, german-text.height + 6px + english-text.height) + 36px; + height: max(36px * root.ui-scale, german-text.height + 6px * root.ui-scale + english-text.height) + 36px * root.ui-scale; Rectangle { - x: 18px; - y: 18px; - width: 36px; - height: 36px; - border-radius: 18px; + x: 18px * root.ui-scale; + y: 18px * root.ui-scale; + width: 36px * root.ui-scale; + height: 36px * root.ui-scale; + border-radius: 18px * root.ui-scale; background: root.active ? root.accent : #e6ebed; Text { text: root.number; color: #20302b; - font-size: 17px; + font-size: 17px * root.ui-scale; font-weight: 700; horizontal-alignment: center; vertical-alignment: center; } } german-text := Text { - x: 68px; - y: 18px; - width: parent.width - 86px; + x: 68px * root.ui-scale; + y: 18px * root.ui-scale; + width: parent.width - 86px * root.ui-scale; height: self.preferred-height; text: root.german; color: #20302b; - font-size: 18px; + font-size: 18px * root.ui-scale; font-weight: 600; wrap: word-wrap; } english-text := Text { x: german-text.x; - y: german-text.y + german-text.height + 6px; + y: german-text.y + german-text.height + 6px * root.ui-scale; width: german-text.width; height: self.preferred-height; text: root.english; color: #52635d; - font-size: 14px; + font-size: 14px * root.ui-scale; wrap: word-wrap; } } @@ -56,10 +57,15 @@ export component AppWindow inherits Window { default-font-family: "DejaVu Sans"; in property video-frame; in property hand-detected: false; + in property countdown: 0; + in property flash-opacity: 0; + in property awaiting-release: false; + // Scale by the shorter screen edge so 1080p and 4K displays remain readable. + private property ui-scale: clamp(min(root.width / 800px, root.height / 800px), 0.85, 2.5); private property primary: #84bf65; private property secondary: #4caee3; - private property inset: root.width < 700px ? 20px : 32px; - private property compact: root.width < 800px; + private property inset: 24px * root.ui-scale; + private property compact: root.width < 900px * root.ui-scale; Image { width: parent.width; @@ -68,45 +74,54 @@ export component AppWindow inherits Window { image-fit: cover; } + Rectangle { + width: parent.width; + height: parent.height; + background: white; + opacity: root.flash-opacity; + } + header := Rectangle { x: root.inset + root.safe-area-insets.left; y: root.inset + root.safe-area-insets.top; width: self.preferred-width; height: self.preferred-height; - border-radius: 18px; + border-radius: 18px * root.ui-scale; background: #fffffff5; HorizontalLayout { - padding: 14px; - padding-right: 22px; - spacing: 12px; + padding: 14px * root.ui-scale; + padding-right: 22px * root.ui-scale; + spacing: 12px * root.ui-scale; Image { source: @image-url("assets/winf-icon.png"); - width: 44px; - height: 44px; + width: 44px * root.ui-scale; + height: 44px * root.ui-scale; image-fit: contain; } Text { text: "winf photobooth"; color: #20302b; - font-size: root.width < 500px ? 20px : 24px; + font-size: 24px * root.ui-scale; font-weight: 600; vertical-alignment: center; } } } - // Wrap the text before sizing the panel so its bottom inset stays at 16px. + // Wrap the text before sizing the panel, with consistent scaled padding. panel := Rectangle { - x: root.inset + root.safe-area-insets.left; + visible: root.countdown == 0 && !root.awaiting-release; + x: root.safe-area-insets.left + (root.width - root.safe-area-insets.left - root.safe-area-insets.right - self.width) / 2; y: root.height - self.height - root.inset - root.safe-area-insets.bottom; - width: root.width - root.inset * 2 - root.safe-area-insets.left - root.safe-area-insets.right; - height: root.compact ? first.height + second.height + 44px : max(first.height, second.height) + 32px; - border-radius: 22px; + width: min(1040px * root.ui-scale, root.width - root.inset * 2 - root.safe-area-insets.left - root.safe-area-insets.right); + height: root.compact ? first.height + second.height + 44px * root.ui-scale : max(first.height, second.height) + 32px * root.ui-scale; + border-radius: 22px * root.ui-scale; background: #fffffff5; first := Instruction { - x: 16px; - y: 16px; - width: root.compact ? parent.width - 32px : (parent.width - 44px) / 2; + ui-scale: root.ui-scale; + x: 16px * root.ui-scale; + y: 16px * root.ui-scale; + width: root.compact ? parent.width - 32px * root.ui-scale : (parent.width - 44px * root.ui-scale) / 2; number: "1"; german: "Hebe deine offene Hand und zeige die Handfläche zur Kamera."; english: "Raise your open hand with your palm facing the camera."; @@ -114,8 +129,9 @@ export component AppWindow inherits Window { active: !root.hand-detected; } second := Instruction { - x: root.compact ? 16px : first.x + first.width + 12px; - y: root.compact ? first.y + first.height + 12px : 16px; + ui-scale: root.ui-scale; + x: root.compact ? 16px * root.ui-scale : first.x + first.width + 12px * root.ui-scale; + y: root.compact ? first.y + first.height + 12px * root.ui-scale : 16px * root.ui-scale; width: first.width; number: "2"; german: "Halte deine Hand ruhig, bis der Kreis vollständig gefüllt ist."; @@ -124,4 +140,71 @@ export component AppWindow inherits Window { active: root.hand-detected; } } + if root.countdown > 0: Rectangle { + x: (root.width - self.width) / 2; + y: (root.height - self.height) / 2; + width: min(420px * root.ui-scale, root.width - root.inset * 2); + height: 340px * root.ui-scale; + border-radius: 32px * root.ui-scale; + background: #fffffff0; + VerticalLayout { + padding: 24px * root.ui-scale; + spacing: 8px * root.ui-scale; + Text { + text: root.countdown; + color: #20302b; + font-size: 180px * root.ui-scale; + font-weight: 700; + horizontal-alignment: center; + vertical-alignment: center; + } + Text { + text: "Bitte in die Kamera schauen."; + color: #20302b; + font-size: 18px * root.ui-scale; + font-weight: 600; + horizontal-alignment: center; + wrap: word-wrap; + } + Text { + text: "Please look at the camera."; + color: #52635d; + font-size: 14px * root.ui-scale; + horizontal-alignment: center; + wrap: word-wrap; + } + } + } + + if root.awaiting-release: Rectangle { + x: (root.width - self.width) / 2; + y: root.height - self.height - root.inset - root.safe-area-insets.bottom; + width: panel.width; + height: release-german.height + release-english.height + 6px * root.ui-scale + 36px * root.ui-scale; + background: #fffffff5; + border-radius: 22px * root.ui-scale; + release-german := Text { + x: 18px * root.ui-scale; + y: 18px * root.ui-scale; + width: parent.width - 36px * root.ui-scale; + height: self.preferred-height; + text: "Senke deine Hand, um einen neuen Countdown zu starten."; + font-size: 18px * root.ui-scale; + font-weight: 600; + color: #20302b; + horizontal-alignment: center; + wrap: word-wrap; + } + release-english := Text { + x: release-german.x; + y: release-german.y + release-german.height + 6px * root.ui-scale; + width: release-german.width; + height: self.preferred-height; + text: "Lower your hand to start a new countdown."; + font-size: 14px * root.ui-scale; + color: #52635d; + horizontal-alignment: center; + wrap: word-wrap; + } + } }