hex/src/screenshots/recording.rs
Dax 631b60b414
screenshots: choose the save folder from labs (#1843)
* labs: choose the screenshots folder from its card

Labs › Screenshots' Saves to row was read-only text naming the folder's
last part. It now reads the folder in full with the home folder as `~`, on
a Secondary button that opens the one native folder chooser (the seam Labs
› Import audio uses) at the folder in use, with Reveal beside it opening
that folder (making it first, as a save would). A pick is written to
screenshots.folder at once, so it applies live to the next shot or
recording; a cancel changes nothing, and a click while a chooser is open
opens no second one.

A chooser that couldn't open, or the last save that failed
("Couldn't save to ~/…: <why>"), reads as a danger note under the row.
The screenshots module keeps that failure bounded at 300 characters and
clears it when a save works or another folder is chosen.

The folder label is one shared `controls::folder_label` now, used by
Annotate's and Meetings' folder rows too.

Closes #1836

* labs: keep the screenshots save error with the folder that failed

Review fixes for the screenshots folder card:

- `screenshots::save_error` keeps the folder the save meant with the
  reason, so a folder changed since through `hex settings` or
  `settings.json` can't make the note name the wrong one.
- The editor's Save records a failed shot PNG or trimmed recording movie
  into the screenshots folder, and clears the note when it works, so the
  docs' "cleared when a later save works" covers every save into it.
- `take` clears the note as soon as the write succeeds, before the
  clipboard copy, so a clipboard failure can't resurrect a stale note.
- Reveal takes `&mut Ui` and puts a failed `create_dir_all` on the card,
  as Meetings' and Annotate's folder hosts do.
- `apply_chosen_folder` only clears the note and reports a change once
  `ui.update` saved the pick; a refused settings save reads as the card's
  danger note instead.
- The test records a failure into an older folder and checks the note
  names it (not the current setting), then that a pick clears it, with the
  process-wide error reset before and after.

Also: the file dialog's `Target::Folder` doc names its callers, the new
Japanese string uses フォルダー like the rest of the table, and the docs
say a visible window's 250 ms tick carries a background save failure, so
no extra wake is kept.

* labs: redraw the screenshots card when a save fails

A window's poll that returns false runs again on the next tick but leaves
the frame alone, so a save that failed on its own thread never showed its
note. `screenshots::save_error_generation` counts each recorded or cleared
save error, `FolderChoice` keeps the count it last drew, and the card's
poll reports the difference, so the visible window's 250 ms tick draws it.

`apply_chosen_folder` returns true for a pick whether or not the settings
save worked: the card changes either way (the new label or the danger
note). The test now settles the window after recording the error and
after the pick, so only the poll can make the note appear and disappear.
2026-11-14 23:23:59 -05:00

119 lines
4.4 KiB
Rust

//! A Screenshots recording once written: its movie is saved to the
//! screenshots folder as "Hex Recording … .mp4", as a shot's PNG is, and
//! its card shows in the corner from its first frame, pointing at that
//! file. The recording itself is an Annotate capture
//! (`CaptureCommand::Clip`) that skips the clipboard and the router; its
//! bundle keeps Annotate's own copy, which Annotate's retention may trash.
use std::path::{Path, PathBuf};
use crate::history::captures::{CaptureKind, CaptureWord};
use crate::rune_ui::screenshot_thumbnail::Recording;
use crate::rune_ui::screenshot_thumbnail::editor::clip;
/// The capture host's hook for each written recording, on its finishing
/// thread: the movie is saved and the card read on a thread of its own.
pub(super) fn written(bundle: PathBuf) {
let spawned = std::thread::Builder::new()
.name("hex-recording-card".into())
.spawn(move || {
if let Err(error) = show(&bundle) {
tracing::warn!(%error, bundle = %bundle.display(), "could not show the recording");
crate::feedback::play(crate::feedback::Tone::Error);
}
});
if let Err(error) = spawned {
tracing::warn!(%error, "could not show the recording");
}
}
fn show(bundle: &Path) -> anyhow::Result<()> {
use scrawl_capture::helper::{self, FrameTimes};
let (recorded, words) = clip::recorded(bundle)?;
let folder = crate::app_settings::screenshot_settings().folder();
// A failed save still shows the card, on Annotate's copy.
let movie = match save(&recorded, &folder) {
Ok(movie) => {
tracing::info!(path = %movie.display(), "recording saved");
super::clear_save_error();
movie
}
Err(error) => {
tracing::warn!(%error, folder = %folder.display(), "could not save the recording");
super::record_save_error(&folder, &error);
crate::feedback::play(crate::feedback::Tone::Error);
recorded
}
};
let probe = helper::probe(&movie)?;
let duration_ms = probe.duration_ms;
let frame = helper::frames(&movie, FrameTimes::At(&[0]), super::THUMB_PX)?
.into_iter()
.next()
.ok_or_else(|| anyhow::anyhow!("the recording has no frames"))?;
let timed = words
.iter()
.map(|word| CaptureWord {
t0: word.t0,
t1: word.t1,
text: word.text.clone(),
})
.collect();
super::remember(
super::Remembered {
kind: CaptureKind::Recording,
file: &movie,
size: [probe.width, probe.height],
duration_ms,
timed,
},
(frame.width, frame.height, &frame.rgba),
);
if let Some(overlays) = super::OVERLAYS.get() {
let screen = super::screen_under_pointer();
overlays.thumbnail.show_recording(
movie,
Recording { duration_ms, words },
frame.width,
frame.height,
frame.rgba,
screen,
);
}
Ok(())
}
/// `movie` copied to a fresh "Hex Recording … .mp4" in `folder`, making the
/// folder if it's missing. On one volume the copy is a clone, so it's
/// instant and takes no space until either changes.
fn save(movie: &Path, folder: &Path) -> anyhow::Result<PathBuf> {
std::fs::create_dir_all(folder)?;
let path = super::save::recording_path(folder);
std::fs::copy(movie, &path)?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_recording_saves_beside_the_shots_and_never_over_one() {
let folder =
std::env::temp_dir().join(format!("hex-recording-save-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&folder);
let source = std::env::temp_dir().join(format!("hex-recording-{}.mp4", std::process::id()));
std::fs::write(&source, b"movie").unwrap();
let first = save(&source, &folder).unwrap();
let second = save(&source, &folder).unwrap();
let name = first.file_name().unwrap().to_string_lossy().into_owned();
assert!(
name.starts_with("Hex Recording ") && name.ends_with(".mp4"),
"{name}"
);
assert_ne!(first, second);
assert_eq!(std::fs::read(&second).unwrap(), b"movie");
std::fs::remove_dir_all(&folder).unwrap();
std::fs::remove_file(&source).unwrap();
}
}