diff --git a/crates/voelin-ui/src/streams.rs b/crates/voelin-ui/src/streams.rs index 013e608..84640a5 100644 --- a/crates/voelin-ui/src/streams.rs +++ b/crates/voelin-ui/src/streams.rs @@ -414,7 +414,8 @@ impl App { let test_pattern = cfg!(debug_assertions) || self.demo || std::env::var("VOELIN_TEST_PATTERN").is_ok_and(|v| v == "1"); - let sources = match self.video.sources(test_pattern) { + let restorable = self.settings.portal_restore_token.is_some(); + let sources = match self.video.sources(test_pattern, restorable) { Ok(sources) => { if self.share.is_none() { self.share_error.clear(); diff --git a/crates/voelin-ui/src/video.rs b/crates/voelin-ui/src/video.rs index add3a6f..0416b58 100644 --- a/crates/voelin-ui/src/video.rs +++ b/crates/voelin-ui/src/video.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use slint::{Image, Rgba8Pixel, SharedPixelBuffer}; use tokio::runtime::Handle; use tracing::warn; -use voelin_core::media::voelin_media::capture::SourceId; +use voelin_core::media::voelin_media::capture::{CaptureSource, SourceId}; use voelin_core::media::voelin_media::{Codec, Codecs, EncoderReport, VideoFrame, convert}; use voelin_core::media::{ self, AudioSourceSpec, EncoderPreference, Latest, LocalPreview, Streamer, StreamerConfig, @@ -63,7 +63,7 @@ pub(crate) struct Video { encoder: EncoderPreference, codec: Option, /// Sources of the share dialog, by index. - sources: Vec<(SourceId, String)>, + sources: Vec, } impl Video { @@ -199,8 +199,14 @@ impl Video { } /// Screens and windows to share, as (name, detail); the test pattern too - /// when asked for. - pub fn sources(&mut self, test_pattern: bool) -> Result, String> { + /// when asked for. With `restorable` (a portal restore token from an + /// earlier share) the portal comes twice: its dialog, which always asks, + /// and the last choice again without the dialog. + pub fn sources( + &mut self, + test_pattern: bool, + restorable: bool, + ) -> Result, String> { let mut error = None; let mut sources = match media::screen_sources() { Ok(list) => list, @@ -215,26 +221,9 @@ impl Video { if sources.is_empty() { return Err(error.unwrap_or_else(|| "Nothing to share was found.".into())); } - self.sources = sources.iter().map(|s| (s.id.clone(), s.name.clone())).collect(); - Ok(sources - .iter() - .map(|s| { - let name = match s.id { - SourceId::Portal => "Choose via system dialog".to_owned(), - _ => s.name.clone(), - }; - let detail = match (&s.id, s.primary) { - (SourceId::Monitor(_), true) => { - format!("{} · primary", size_text(s.width, s.height)) - } - (SourceId::Window(_), _) => { - format!("window {}", size_text(s.width, s.height)).trim().to_owned() - } - _ => size_text(s.width, s.height), - }; - (name, detail) - }) - .collect()) + let (rows, entries) = share_rows(&sources, restorable); + self.sources = entries; + Ok(rows) } /// Start capturing source `index` of [`Video::sources`]; `done` runs on @@ -246,7 +235,7 @@ impl Video { options: CaptureRequest, done: impl FnOnce(Result) + Send + 'static, ) { - let Some((source, name)) = self.sources.get(index).cloned() else { + let Some((source, name, restore)) = self.sources.get(index).cloned() else { done(Err("Pick something to share.".into())); return; }; @@ -264,7 +253,8 @@ impl Video { encoder: self.encoder.clone(), audio: options.audio, audio_sources: options.audio_sources, - restore_token: options.restore_token, + // The portal skips its dialog with a token: only when asked to. + restore_token: options.restore_token.filter(|_| restore), ..StreamerConfig::default() }; runtime.spawn(async move { @@ -368,6 +358,40 @@ fn deliver( } } +/// What a row of the share dialog starts: the source, its name, and whether +/// to restore the portal's last choice instead of asking. +type ShareEntry = (SourceId, String, bool); + +/// The share dialog's rows (name, detail) for `sources`, and what each row +/// starts (restoring only in the extra portal row with `restorable`). +fn share_rows( + sources: &[CaptureSource], + restorable: bool, +) -> (Vec<(String, String)>, Vec) { + let mut rows = Vec::with_capacity(sources.len() + 1); + let mut entries = Vec::with_capacity(sources.len() + 1); + for s in sources { + let name = match s.id { + SourceId::Portal => "Choose via system dialog".to_owned(), + _ => s.name.clone(), + }; + let detail = match (&s.id, s.primary) { + (SourceId::Monitor(_), true) => format!("{} · primary", size_text(s.width, s.height)), + (SourceId::Window(_), _) => { + format!("window {}", size_text(s.width, s.height)).trim().to_owned() + } + _ => size_text(s.width, s.height), + }; + rows.push((name, detail)); + entries.push((s.id.clone(), s.name.clone(), false)); + if s.id == SourceId::Portal && restorable { + rows.push(("Same as last time".to_owned(), "without the dialog".to_owned())); + entries.push((s.id.clone(), s.name.clone(), true)); + } + } + (rows, entries) +} + /// Share settings from the dialog. pub(crate) struct CaptureRequest { pub fps: u32, @@ -473,3 +497,30 @@ impl Decoder { stats.error.filter(|_| stats.decoded == 0) } } + +#[cfg(test)] +mod tests { + use super::*; + + /// "Choose via system dialog" never passes the restore token, so the + /// portal always asks; with a token the last choice gets its own row. + #[test] + fn the_portal_dialog_always_asks() { + let portal = CaptureSource { + id: SourceId::Portal, + name: "Screen".into(), + width: 0, + height: 0, + primary: false, + }; + let (rows, entries) = share_rows(std::slice::from_ref(&portal), false); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].0, "Choose via system dialog"); + assert!(!entries[0].2); + let (rows, entries) = share_rows(&[portal], true); + let names: Vec<_> = rows.iter().map(|(name, _)| name.as_str()).collect(); + assert_eq!(names, ["Choose via system dialog", "Same as last time"]); + let restore: Vec<_> = entries.iter().map(|e| e.2).collect(); + assert_eq!(restore, [false, true]); + } +} diff --git a/docs/studio.md b/docs/studio.md index c6a0e9f..bb0b01c 100644 --- a/docs/studio.md +++ b/docs/studio.md @@ -43,7 +43,7 @@ the UI) and a `background` (replacement, below). |---|---|---| | `screen` | `monitor`, `backend` (`portal`, `wlroots`, `x11`, `windows`; none: this session's), `cursor` | a capture backend, frames borrowed and copied into pooled frames | | `window` | `handle` (X11 id, `HWND`), `backend`, `cursor` | the same | -| `portal` | `restore_token`, `cursor` | the ScreenCast portal's dialog (Wayland); the token is written back so it is asked once | +| `portal` | `restore_token`, `cursor` | the ScreenCast portal's dialog (Wayland); with a `restore_token` the portal may restore its last choice without asking. The UI adds portal sources without one, so the dialog asks each time the source starts | | `camera` | `device` (a `camera::Camera::id`; empty: the first), `size`, `fps`, `mirror` | see [Cameras](#cameras) | | `image` | `path` | PNG or JPEG, alpha kept, read once | | `text` | `text`, `font` (none: the bundled Inter), `size_px`, `colour`, `backdrop`, `align`, `padding` | rasterised once with `ab_glyph` |