diff --git a/crates/app/src/export_ui.rs b/crates/app/src/export_ui.rs index 8b6df46..0c04e7d 100644 --- a/crates/app/src/export_ui.rs +++ b/crates/app/src/export_ui.rs @@ -903,99 +903,6 @@ pub(crate) const MBPS_MAX: u32 = 50; /// than being dropped keystroke by keystroke. pub(crate) const MBPS_DIGITS: usize = 3; -/// A number being typed into a card row: the digits so far and, once a commit -/// has been refused, why. Text-field semantics on a card that has no text field -/// -- typing, backspace, arrows that step, enter that commits and escape that -/// gives up -- held as state and driven by the root's key handler, since -/// nothing in these cards takes gpui focus. -#[derive(Clone, Debug, Default, PartialEq, Eq)] -pub(crate) struct NumberEdit { - pub(crate) text: String, - pub(crate) refusal: Option, -} - -impl NumberEdit { - /// Starts on the number the row already carries, so backspace edits it - /// rather than the field opening empty over a value that is still in force. - /// Zero is no number at all -- it is what the card opens at, before anyone - /// has typed one. - pub(crate) fn new(value: u32) -> Self { - NumberEdit { - text: match value { - 0 => String::new(), - v => v.to_string(), - }, - refusal: None, - } - } - - /// A digit against what is there. The one past [`MBPS_DIGITS`] is refused - /// *out loud*: a keystroke dropped in silence is how the old digit capture - /// left the card showing a number the user had already typed past. - pub(crate) fn digit(&mut self, digit: u32) { - if self.text.chars().count() >= MBPS_DIGITS { - self.refusal = Some(format!("{MBPS_DIGITS} digits is already past the ceiling")); - return; - } - match char::from_digit(digit, 10) { - Some(c) => { - self.text.push(c); - self.refusal = None; - } - None => self.refusal = Some("digits only".into()), - } - } - - /// Erases the last digit, and the refusal with it: the number on screen has - /// changed, so the reason the old one was refused no longer describes it. - pub(crate) fn backspace(&mut self) { - self.text.pop(); - self.refusal = None; - } - - /// The arrows, which is how a number gets picked rather than typed. Steps - /// from what is in the field -- an empty one starts at the floor, so the - /// first press up is `MBPS_MIN` and not a jump to some remembered value -- - /// and stays inside the range, because a step is a walk through the legal - /// numbers rather than a way out of them. - pub(crate) fn step(&mut self, by: i32) { - let at = self - .text - .parse::() - .unwrap_or(MBPS_MIN as i32 - by.signum()); - self.text = (at + by) - .clamp(MBPS_MIN as i32, MBPS_MAX as i32) - .to_string(); - self.refusal = None; - } - - /// The number, or `None` with the reason recorded where the row will read - /// it. Never clamped: 45 committed as 20 is a number the user did not type. - pub(crate) fn commit(&mut self) -> Option { - match commit_mbps(&self.text) { - Ok(mbps) => Some(mbps), - Err(why) => { - self.refusal = Some(why); - None - } - } - } - - /// What the row shows while it is being typed into: the digits, the caret - /// that says they are landing *here*, and either the refusal or the two - /// keys that end the edit. - pub(crate) fn detail(&self) -> String { - format!( - "{}▏ Mbps — {}", - self.text, - match &self.refusal { - Some(why) => why.as_str(), - None => "enter commits · esc cancels", - } - ) - } -} - /// A typed bitrate as the card takes it, or the reason it is not one. The words /// are the row's: they are what the field shows in place of its hint. pub(crate) fn commit_mbps(text: &str) -> Result { diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 557ae42..6d2da0f 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -562,6 +562,12 @@ struct Player { /// keyboard), so the field is a modal state on the player exactly as a /// waiting rebind row is, and the root's handler is what types into it. mbps_edit: Option, + /// The Clip tab's transition duration row, the same way (DEBT #111): + /// the anchor clip's frame count while it is being typed, `None` when + /// nobody is. Opened on the row's own click, closed on the same field's + /// enter/esc the export card's field already answers to + /// ([`Player::param_card_key`]). + transition_edit: Option, /// What the *sound* is coded at, in kbps, for every format that encodes it /// -- the AAC inside a video export as much as an MP3. Kept across closes /// like the picture's quality, and starts at the figure this program wrote @@ -1024,6 +1030,7 @@ fn main() { quality: Quality::Medium, custom_mbps: 0, mbps_edit: None, + transition_edit: None, // ...and the rate the sound has always been written at. audio_kbps: DEFAULT_AUDIO_KBPS, // Picture and sound, which is what an export was before diff --git a/crates/app/src/player/cards.rs b/crates/app/src/player/cards.rs index e0130a7..dfdae2a 100644 --- a/crates/app/src/player/cards.rs +++ b/crates/app/src/player/cards.rs @@ -1739,6 +1739,24 @@ impl Player { } return true; } + // The Clip tab's transition duration field (DEBT #111): the same + // keyboard door as the export card's bitrate field, checked first + // since it can be open while no card is (the Clip tab is always on + // screen, not modal). + if let Some(edit) = &mut self.transition_edit { + if key == ESCAPE { + self.transition_edit = None; + } else if key == "enter" { + self.commit_transition(cx); + } else if key == "backspace" { + edit.backspace(); + } else if let Ok(digit) = key.parse::() { + edit.digit(digit); + } else { + return false; + } + return true; + } // The export card's custom bitrate field: the mouse door is the // steppers and the row's own click (`Player::edit_mbps`), but the // `0–9`/backspace/enter/esc chords `keymap::FIXED` already diff --git a/crates/app/src/player/export.rs b/crates/app/src/player/export.rs index db50050..37f56fa 100644 --- a/crates/app/src/player/export.rs +++ b/crates/app/src/player/export.rs @@ -1,6 +1,7 @@ //! What is cached about the media and the machine, and the export the //! two of them decide. +use crate::ui::widgets::NumberEdit; use crate::*; impl Player { @@ -579,7 +580,13 @@ impl Player { /// bitrate it had. pub(crate) fn edit_mbps(&mut self) { self.quality = Quality::Custom; - self.mbps_edit = Some(NumberEdit::new(self.custom_mbps)); + self.mbps_edit = Some(NumberEdit::new( + self.custom_mbps, + MBPS_MIN, + MBPS_MAX, + MBPS_DIGITS, + "Mbps", + )); } /// The card's Destination row: the desktop's save dialog, on a background diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 4559645..308d666 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1,6 +1,7 @@ //! Editing the timeline itself: the cuts, the clipboard, the drags, the //! snapping and the trims. +use crate::ui::widgets::NumberEdit; use crate::*; /// Whether a seam that just lost its drag owes a disk write -- exactly the @@ -1650,9 +1651,6 @@ impl Player { /// lands at the successor's length instead of asking for more than /// there is. pub(crate) fn nudge_transition(&mut self, lane: Lane, idx: usize, step: i32, cx: &mut Context) { - if self.exporting().is_some() { - return; - } let Some(session) = &mut self.session else { return; }; @@ -1662,6 +1660,27 @@ impl Player { _ => return, }; let frames = (current as i32 + step).max(0) as u32; + self.set_transition_frames(lane, idx, frames, cx); + } + + /// The transition's absolute length, through the same setter the stepper + /// dispatches to (`set_transition_out`/`crossfade`, each already clamping + /// to the shorter neighbour's frames) -- the typed field + /// ([`Player::commit_transition`]) and the ± steps land a number the same + /// way, so undo and persistence never tell the two doors apart. + pub(crate) fn set_transition_frames( + &mut self, + lane: Lane, + idx: usize, + frames: u32, + cx: &mut Context, + ) { + if self.exporting().is_some() { + return; + } + let Some(session) = &mut self.session else { + return; + }; let ok = match lane.kind { LaneKind::Video => session.set_transition_out(lane, idx, frames), LaneKind::Audio => session.crossfade(lane, idx, frames), @@ -1673,6 +1692,31 @@ impl Player { } } + /// Opens the transition duration's field on the frame count the row is + /// carrying -- the mouse door for DEBT #111's type-in, the row's own + /// click, `Player::edit_mbps`'s convention. `cap` is the same ceiling the + /// stepper is already clamped to (`transition_of`), so the field never + /// offers a number a step could not also land on. + pub(crate) fn edit_transition(&mut self, frames: u32, cap: u32) { + let digits = cap.to_string().chars().count() + 1; + self.transition_edit = Some(NumberEdit::new(frames, 0, cap, digits, "frames")); + } + + /// The typed field's enter: clamps into the anchor's own range + /// ([`NumberEdit::commit_clamped`]) rather than refusing, because the + /// stepper this field stands in for already clamps silently -- a typed + /// number stricter than the mouse door would be a rule the field alone + /// enforces. + pub(crate) fn commit_transition(&mut self, cx: &mut Context) { + let Some(mut edit) = self.transition_edit.take() else { + return; + }; + if let Some((lane, idx)) = self.selected.anchor() { + let frames = edit.commit_clamped(); + self.set_transition_frames(lane, idx, frames, cx); + } + } + /// The clip as the drag is showing it: an edge under the pointer moves its /// own box, and the boxes of everything linked to it, before anything is /// committed. Display only -- the project is not touched until the release. diff --git a/crates/app/src/tests/cards.rs b/crates/app/src/tests/cards.rs index 0dc6cf7..ac833c7 100644 --- a/crates/app/src/tests/cards.rs +++ b/crates/app/src/tests/cards.rs @@ -984,11 +984,17 @@ fn the_sound_row_carries_its_rate_into_both_kinds_of_file() { assert!(audio_rate_choices(7).iter().all(|(.., picked)| !picked)); } +/// The shape [`NumberEdit`] is opened in for a bitrate: `(value, MBPS_MIN, +/// MBPS_MAX, MBPS_DIGITS, "Mbps")`, same as `Player::edit_mbps`. +fn mbps_edit(value: u32) -> NumberEdit { + NumberEdit::new(value, MBPS_MIN, MBPS_MAX, MBPS_DIGITS, "Mbps") +} + #[test] fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { // It opens on the number in force, so backspace edits that number // rather than the field starting empty over a bitrate still being used. - let mut edit = NumberEdit::new(12); + let mut edit = mbps_edit(12); assert_eq!(edit.text, "12"); edit.backspace(); edit.digit(8); @@ -996,11 +1002,11 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { assert_eq!(edit.commit(), Some(18)); // A card nobody has typed a number into opens empty: zero is not a // bitrate anyone chose. - assert_eq!(NumberEdit::new(0).text, ""); + assert_eq!(mbps_edit(0).text, ""); // Out of range is refused *in words* and the digits stay put: clamping // 55 to 50 would write a bitrate the user never typed. - let mut edit = NumberEdit::new(0); + let mut edit = mbps_edit(0); for digit in [5, 5] { edit.digit(digit); } @@ -1022,7 +1028,7 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { assert_eq!(commit_mbps("1"), Ok(MBPS_MIN)); assert_eq!(commit_mbps("50"), Ok(MBPS_MAX)); assert!(commit_mbps("51").is_err()); - let mut edit = NumberEdit::new(0); + let mut edit = mbps_edit(0); for digit in [9, 9, 9, 9] { edit.digit(digit); } @@ -1035,18 +1041,18 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { // The arrows step inside the range and stop at both ends: a walk // through the legal numbers, never a way out of them. - let mut edit = NumberEdit::new(0); + let mut edit = mbps_edit(0); edit.step(1); assert_eq!(edit.text, MBPS_MIN.to_string(), "empty starts at the floor"); edit.step(-1); assert_eq!(edit.text, MBPS_MIN.to_string()); - let mut edit = NumberEdit::new(MBPS_MAX); + let mut edit = mbps_edit(MBPS_MAX); edit.step(1); assert_eq!(edit.text, MBPS_MAX.to_string()); edit.step(-1); assert_eq!(edit.text, (MBPS_MAX - 1).to_string()); // A step past a refused number clears the refusal with it. - let mut edit = NumberEdit::new(0); + let mut edit = mbps_edit(0); edit.digit(5); edit.digit(5); assert_eq!(edit.commit(), None); @@ -1056,7 +1062,7 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { // The hint the field shows when there is nothing to refuse names both // ways out of it. - let detail = NumberEdit::new(6).detail(); + let detail = mbps_edit(6).detail(); assert!( detail.contains("enter") && detail.contains("esc"), "{detail}" @@ -1064,6 +1070,40 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { assert!(detail.starts_with("6▏"), "{detail}"); } +/// DEBT #111: the transition duration row's field is the same [`NumberEdit`] +/// opened `(frames, 0, cap, ..)`, the shape [`Player::edit_transition`] +/// builds it in -- but it clamps on commit instead of refusing, because the +/// row's own stepper (`Player::nudge_transition`, backed by +/// `set_transition_out`/`crossfade`) already clamps to `cap` in silence, and +/// a typed number holding itself to a stricter rule than the mouse door +/// would be a field disagreeing with its own stepper. +#[test] +fn a_typed_transition_duration_clamps_like_its_own_stepper() { + let cap = 24; + let digits = cap.to_string().chars().count() + 1; + // Opens on the frame count already in force, same as the bitrate field. + let mut edit = NumberEdit::new(10, 0, cap, digits, "frames"); + assert_eq!(edit.text, "10"); + // A number past the cap clamps to it rather than being refused -- the + // stepper's own ceiling, reached by a different door. + edit.backspace(); + edit.backspace(); + for digit in [9, 9] { + edit.digit(digit); + } + assert_eq!(edit.text, "99"); + assert_eq!( + edit.commit_clamped(), + cap, + "clamped to the same cap the stepper stops at" + ); + assert_eq!(edit.refusal, None, "clamping is not a refusal"); + // Empty commits to the floor, not a parse failure -- there is no number + // typed that is not zero or more frames. + let mut edit = NumberEdit::new(0, 0, cap, digits, "frames"); + assert_eq!(edit.commit_clamped(), 0); +} + /// [`crate::ui::dock_stance::transition_of`]: the dock's duration row /// appears only for an anchor that actually carries a dissolve or a /// crossfade, and the room it offers is capped at what the successor clip diff --git a/crates/app/src/ui/dock_stance.rs b/crates/app/src/ui/dock_stance.rs index fcfb737..45b7750 100644 --- a/crates/app/src/ui/dock_stance.rs +++ b/crates/app/src/ui/dock_stance.rs @@ -1103,8 +1103,9 @@ pub(crate) fn transition_of( /// The transition duration row: `transition_of` picks the anchor's /// transition and its cap, [`Player::nudge_transition`] does the clamped -/// step -- only the paint is this tab's own (`ghost_verb`'s row height and -/// ink tokens). +/// step, [`Player::edit_transition`]/[`Player::commit_transition`] the +/// type-in (DEBT #111) -- only the paint is this tab's own (`ghost_verb`'s +/// row height and ink tokens). fn transition_row(player: &Player, cx: &mut Context) -> Option { let (lane, idx) = player.selected.anchor()?; let session = player.session.as_ref()?; @@ -1112,6 +1113,7 @@ fn transition_row(player: &Player, cx: &mut Context) -> Option| { div() .id(id) @@ -1140,11 +1142,25 @@ fn transition_row(player: &Player, cx: &mut Context) -> Option edit.detail(), + None => format!("{label_text} {frames}f (of {cap}f offered)"), + }), ) .child(step("transition-minus", "Shorten transition", "−", -1, cx)) .child(step("transition-plus", "Lengthen transition", "+", 1, cx)), diff --git a/crates/app/src/ui/widgets.rs b/crates/app/src/ui/widgets.rs index 9010cf4..0ebea0b 100644 --- a/crates/app/src/ui/widgets.rs +++ b/crates/app/src/ui/widgets.rs @@ -272,6 +272,143 @@ impl Render for OverlayTip { } } +/// A number being typed into a row that has no text field of its own: the +/// digits so far, the range and digit cap that row enforces, and once a +/// commit has been refused, why. Text-field semantics on a card that has no +/// text field -- typing, backspace, arrows that step, enter that commits and +/// escape that gives up -- held as state and driven by the root's key +/// handler, since nothing in these rows takes gpui focus. Shared by the +/// export card's custom bitrate field and the transition duration row +/// (DEBT #111) -- one implementation, each caller its own range and unit. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct NumberEdit { + pub(crate) text: String, + pub(crate) refusal: Option, + min: u32, + max: u32, + digits: usize, + unit: &'static str, +} + +impl NumberEdit { + /// Starts on the number the row already carries, so backspace edits it + /// rather than the field opening empty over a value that is still in + /// force. Zero is no number at all -- it is what the row opens at, before + /// anyone has typed one. + pub(crate) fn new(value: u32, min: u32, max: u32, digits: usize, unit: &'static str) -> Self { + NumberEdit { + text: match value { + 0 => String::new(), + v => v.to_string(), + }, + refusal: None, + min, + max, + digits, + unit, + } + } + + /// A digit against what is there. The one past the row's digit cap is + /// refused *out loud*: a keystroke dropped in silence is how the old + /// digit capture left a row showing a number the user had already typed + /// past. + pub(crate) fn digit(&mut self, digit: u32) { + if self.text.chars().count() >= self.digits { + self.refusal = Some(format!("{} digits is already past the ceiling", self.digits)); + return; + } + match char::from_digit(digit, 10) { + Some(c) => { + self.text.push(c); + self.refusal = None; + } + None => self.refusal = Some("digits only".into()), + } + } + + /// Erases the last digit, and the refusal with it: the number on screen has + /// changed, so the reason the old one was refused no longer describes it. + pub(crate) fn backspace(&mut self) { + self.text.pop(); + self.refusal = None; + } + + /// The arrows, which is how a number gets picked rather than typed. Steps + /// from what is in the field -- an empty one starts at the floor, so the + /// first press up is the row's minimum and not a jump to some remembered + /// value -- and stays inside the range, because a step is a walk through + /// the legal numbers rather than a way out of them. + pub(crate) fn step(&mut self, by: i32) { + let at = self + .text + .parse::() + .unwrap_or(self.min as i32 - by.signum()); + self.text = (at + by) + .clamp(self.min as i32, self.max as i32) + .to_string(); + self.refusal = None; + } + + /// The number, or `None` with the reason recorded where the row will read + /// it. Never clamped: a row that clamps a typed number to its ceiling + /// without saying so is writing a number the user never typed. For a row + /// whose own stepper already clamps silently, use [`Self::commit_clamped`] + /// instead -- refusing there would be a stricter rule than the mouse door + /// enforces. + pub(crate) fn commit(&mut self) -> Option { + match self.text.parse::() { + Ok(v) if (self.min..=self.max).contains(&v) => { + self.refusal = None; + Some(v) + } + Ok(0) => { + self.refusal = Some(format!( + "0 is not a value — {}–{} {}", + self.min, self.max, self.unit + )); + None + } + Ok(v) => { + self.refusal = Some(format!("{v} is past the {} {} ceiling", self.max, self.unit)); + None + } + Err(_) => { + self.refusal = Some(format!( + "type a number — {}–{} {}", + self.min, self.max, self.unit + )); + None + } + } + } + + /// The number, clamped into range instead of refused -- for a row whose + /// own stepper already clamps silently (the transition duration's setter + /// clamps to the clips' shared length), so a typed number lands exactly + /// where that same stepper would have walked it to. Always succeeds. + pub(crate) fn commit_clamped(&mut self) -> u32 { + let v = self.text.parse::().unwrap_or(self.min); + self.refusal = None; + v.clamp(self.min, self.max) + } + + /// What the row shows while it is being typed into: the digits, the caret + /// that says they are landing *here*, and either the refusal or the two + /// keys that end the edit. + pub(crate) fn detail(&self) -> String { + format!( + "{}▏ {} — {}", + self.text, + self.unit, + match &self.refusal { + Some(why) => why.as_str(), + None => "enter commits · esc cancels", + } + ) + } +} + /// What a window with no file open is waiting for. Both ways in are already /// built -- the whole window is the drop target and the Import chooser takes a /// project as readily as media -- so this only has to say so.