forge
Explore
Log in

Don't you dare go Hollow

Type-in for the transition duration row (DEBT #111)

Browse files at this commit

1110ca4e6d89b218771b2ab68985bf05859eb200Ahmet Kaan GümüşParent: b05a649
  • feat
    • type-in for the transition duration row (DEBT #111)
Show full message
✨ feat(ui): type-in for the transition duration row (DEBT #111)

Generalizes export_ui.rs's NumberEdit into ui/widgets.rs with a caller-owned
range/digit-cap/unit instead of the hardcoded Mbps constants, adds a
commit_clamped() alongside the existing refuse-on-commit commit() for rows
whose stepper already clamps silently, and wires it into the Clip tab's
transition duration row (click to open, digits/backspace/enter/esc through
Player::param_card_key, commit through the same set_transition_frames the
± steppers now share).
crates/app/src/export_ui.rsmodified+0 -93
@@ -903,99 +903,6 @@ pub(crate) const MBPS_MAX: u32 = 50;
903 /// than being dropped keystroke by keystroke.903 /// than being dropped keystroke by keystroke.
904 pub(crate) const MBPS_DIGITS: usize = 3;904 pub(crate) const MBPS_DIGITS: usize = 3;
905 905
906-/// A number being typed into a card row: the digits so far and, once a commit
907-/// has been refused, why. Text-field semantics on a card that has no text field
908-/// -- typing, backspace, arrows that step, enter that commits and escape that
909-/// gives up -- held as state and driven by the root's key handler, since
910-/// nothing in these cards takes gpui focus.
911-#[derive(Clone, Debug, Default, PartialEq, Eq)]
912-pub(crate) struct NumberEdit {
913- pub(crate) text: String,
914- pub(crate) refusal: Option<String>,
915-}
916-
917-impl NumberEdit {
918- /// Starts on the number the row already carries, so backspace edits it
919- /// rather than the field opening empty over a value that is still in force.
920- /// Zero is no number at all -- it is what the card opens at, before anyone
921- /// has typed one.
922- pub(crate) fn new(value: u32) -> Self {
923- NumberEdit {
924- text: match value {
925- 0 => String::new(),
926- v => v.to_string(),
927- },
928- refusal: None,
929- }
930- }
931-
932- /// A digit against what is there. The one past [`MBPS_DIGITS`] is refused
933- /// *out loud*: a keystroke dropped in silence is how the old digit capture
934- /// left the card showing a number the user had already typed past.
935- pub(crate) fn digit(&mut self, digit: u32) {
936- if self.text.chars().count() >= MBPS_DIGITS {
937- self.refusal = Some(format!("{MBPS_DIGITS} digits is already past the ceiling"));
938- return;
939- }
940- match char::from_digit(digit, 10) {
941- Some(c) => {
942- self.text.push(c);
943- self.refusal = None;
944- }
945- None => self.refusal = Some("digits only".into()),
946- }
947- }
948-
949- /// Erases the last digit, and the refusal with it: the number on screen has
950- /// changed, so the reason the old one was refused no longer describes it.
951- pub(crate) fn backspace(&mut self) {
952- self.text.pop();
953- self.refusal = None;
954- }
955-
956- /// The arrows, which is how a number gets picked rather than typed. Steps
957- /// from what is in the field -- an empty one starts at the floor, so the
958- /// first press up is `MBPS_MIN` and not a jump to some remembered value --
959- /// and stays inside the range, because a step is a walk through the legal
960- /// numbers rather than a way out of them.
961- pub(crate) fn step(&mut self, by: i32) {
962- let at = self
963- .text
964- .parse::<i32>()
965- .unwrap_or(MBPS_MIN as i32 - by.signum());
966- self.text = (at + by)
967- .clamp(MBPS_MIN as i32, MBPS_MAX as i32)
968- .to_string();
969- self.refusal = None;
970- }
971-
972- /// The number, or `None` with the reason recorded where the row will read
973- /// it. Never clamped: 45 committed as 20 is a number the user did not type.
974- pub(crate) fn commit(&mut self) -> Option<u32> {
975- match commit_mbps(&self.text) {
976- Ok(mbps) => Some(mbps),
977- Err(why) => {
978- self.refusal = Some(why);
979- None
980- }
981- }
982- }
983-
984- /// What the row shows while it is being typed into: the digits, the caret
985- /// that says they are landing *here*, and either the refusal or the two
986- /// keys that end the edit.
987- pub(crate) fn detail(&self) -> String {
988- format!(
989- "{}▏ Mbps — {}",
990- self.text,
991- match &self.refusal {
992- Some(why) => why.as_str(),
993- None => "enter commits · esc cancels",
994- }
995- )
996- }
997-}
998-
999 /// A typed bitrate as the card takes it, or the reason it is not one. The words906 /// A typed bitrate as the card takes it, or the reason it is not one. The words
1000 /// are the row's: they are what the field shows in place of its hint.907 /// are the row's: they are what the field shows in place of its hint.
1001 pub(crate) fn commit_mbps(text: &str) -> Result<u32, String> {908 pub(crate) fn commit_mbps(text: &str) -> Result<u32, String> {
crates/app/src/main.rsmodified+7 -0
@@ -562,6 +562,12 @@ struct Player {
562 /// keyboard), so the field is a modal state on the player exactly as a562 /// keyboard), so the field is a modal state on the player exactly as a
563 /// waiting rebind row is, and the root's handler is what types into it.563 /// waiting rebind row is, and the root's handler is what types into it.
564 mbps_edit: Option<NumberEdit>,564 mbps_edit: Option<NumberEdit>,
565+ /// The Clip tab's transition duration row, the same way (DEBT #111):
566+ /// the anchor clip's frame count while it is being typed, `None` when
567+ /// nobody is. Opened on the row's own click, closed on the same field's
568+ /// enter/esc the export card's field already answers to
569+ /// ([`Player::param_card_key`]).
570+ transition_edit: Option<NumberEdit>,
565 /// What the *sound* is coded at, in kbps, for every format that encodes it571 /// What the *sound* is coded at, in kbps, for every format that encodes it
566 /// -- the AAC inside a video export as much as an MP3. Kept across closes572 /// -- the AAC inside a video export as much as an MP3. Kept across closes
567 /// like the picture's quality, and starts at the figure this program wrote573 /// like the picture's quality, and starts at the figure this program wrote
@@ -1024,6 +1030,7 @@ fn main() {
1024 quality: Quality::Medium,1030 quality: Quality::Medium,
1025 custom_mbps: 0,1031 custom_mbps: 0,
1026 mbps_edit: None,1032 mbps_edit: None,
1033+ transition_edit: None,
1027 // ...and the rate the sound has always been written at.1034 // ...and the rate the sound has always been written at.
1028 audio_kbps: DEFAULT_AUDIO_KBPS,1035 audio_kbps: DEFAULT_AUDIO_KBPS,
1029 // Picture and sound, which is what an export was before1036 // Picture and sound, which is what an export was before
crates/app/src/player/cards.rsmodified+18 -0
@@ -1739,6 +1739,24 @@ impl Player {
1739 }1739 }
1740 return true;1740 return true;
1741 }1741 }
1742+ // The Clip tab's transition duration field (DEBT #111): the same
1743+ // keyboard door as the export card's bitrate field, checked first
1744+ // since it can be open while no card is (the Clip tab is always on
1745+ // screen, not modal).
1746+ if let Some(edit) = &mut self.transition_edit {
1747+ if key == ESCAPE {
1748+ self.transition_edit = None;
1749+ } else if key == "enter" {
1750+ self.commit_transition(cx);
1751+ } else if key == "backspace" {
1752+ edit.backspace();
1753+ } else if let Ok(digit) = key.parse::<u32>() {
1754+ edit.digit(digit);
1755+ } else {
1756+ return false;
1757+ }
1758+ return true;
1759+ }
1742 // The export card's custom bitrate field: the mouse door is the1760 // The export card's custom bitrate field: the mouse door is the
1743 // steppers and the row's own click (`Player::edit_mbps`), but the1761 // steppers and the row's own click (`Player::edit_mbps`), but the
1744 // `0–9`/backspace/enter/esc chords `keymap::FIXED` already1762 // `0–9`/backspace/enter/esc chords `keymap::FIXED` already
crates/app/src/player/export.rsmodified+8 -1
@@ -1,6 +1,7 @@
1 //! What is cached about the media and the machine, and the export the1 //! What is cached about the media and the machine, and the export the
2 //! two of them decide.2 //! two of them decide.
3 3
4+use crate::ui::widgets::NumberEdit;
4 use crate::*;5 use crate::*;
5 6
6 impl Player {7 impl Player {
@@ -579,7 +580,13 @@ impl Player {
579 /// bitrate it had.580 /// bitrate it had.
580 pub(crate) fn edit_mbps(&mut self) {581 pub(crate) fn edit_mbps(&mut self) {
581 self.quality = Quality::Custom;582 self.quality = Quality::Custom;
582- self.mbps_edit = Some(NumberEdit::new(self.custom_mbps));583+ self.mbps_edit = Some(NumberEdit::new(
584+ self.custom_mbps,
585+ MBPS_MIN,
586+ MBPS_MAX,
587+ MBPS_DIGITS,
588+ "Mbps",
589+ ));
583 }590 }
584 591
585 /// The card's Destination row: the desktop's save dialog, on a background592 /// The card's Destination row: the desktop's save dialog, on a background
crates/app/src/player/timeline_edit.rsmodified+47 -3
@@ -1,6 +1,7 @@
1 //! Editing the timeline itself: the cuts, the clipboard, the drags, the1 //! Editing the timeline itself: the cuts, the clipboard, the drags, the
2 //! snapping and the trims.2 //! snapping and the trims.
3 3
4+use crate::ui::widgets::NumberEdit;
4 use crate::*;5 use crate::*;
5 6
6 /// Whether a seam that just lost its drag owes a disk write -- exactly the7 /// Whether a seam that just lost its drag owes a disk write -- exactly the
@@ -1650,9 +1651,6 @@ impl Player {
1650 /// lands at the successor's length instead of asking for more than1651 /// lands at the successor's length instead of asking for more than
1651 /// there is.1652 /// there is.
1652 pub(crate) fn nudge_transition(&mut self, lane: Lane, idx: usize, step: i32, cx: &mut Context<Self>) {1653 pub(crate) fn nudge_transition(&mut self, lane: Lane, idx: usize, step: i32, cx: &mut Context<Self>) {
1653- if self.exporting().is_some() {
1654- return;
1655- }
1656 let Some(session) = &mut self.session else {1654 let Some(session) = &mut self.session else {
1657 return;1655 return;
1658 };1656 };
@@ -1662,6 +1660,27 @@ impl Player {
1662 _ => return,1660 _ => return,
1663 };1661 };
1664 let frames = (current as i32 + step).max(0) as u32;1662 let frames = (current as i32 + step).max(0) as u32;
1663+ self.set_transition_frames(lane, idx, frames, cx);
1664+ }
1665+
1666+ /// The transition's absolute length, through the same setter the stepper
1667+ /// dispatches to (`set_transition_out`/`crossfade`, each already clamping
1668+ /// to the shorter neighbour's frames) -- the typed field
1669+ /// ([`Player::commit_transition`]) and the ± steps land a number the same
1670+ /// way, so undo and persistence never tell the two doors apart.
1671+ pub(crate) fn set_transition_frames(
1672+ &mut self,
1673+ lane: Lane,
1674+ idx: usize,
1675+ frames: u32,
1676+ cx: &mut Context<Self>,
1677+ ) {
1678+ if self.exporting().is_some() {
1679+ return;
1680+ }
1681+ let Some(session) = &mut self.session else {
1682+ return;
1683+ };
1665 let ok = match lane.kind {1684 let ok = match lane.kind {
1666 LaneKind::Video => session.set_transition_out(lane, idx, frames),1685 LaneKind::Video => session.set_transition_out(lane, idx, frames),
1667 LaneKind::Audio => session.crossfade(lane, idx, frames),1686 LaneKind::Audio => session.crossfade(lane, idx, frames),
@@ -1673,6 +1692,31 @@ impl Player {
1673 }1692 }
1674 }1693 }
1675 1694
1695+ /// Opens the transition duration's field on the frame count the row is
1696+ /// carrying -- the mouse door for DEBT #111's type-in, the row's own
1697+ /// click, `Player::edit_mbps`'s convention. `cap` is the same ceiling the
1698+ /// stepper is already clamped to (`transition_of`), so the field never
1699+ /// offers a number a step could not also land on.
1700+ pub(crate) fn edit_transition(&mut self, frames: u32, cap: u32) {
1701+ let digits = cap.to_string().chars().count() + 1;
1702+ self.transition_edit = Some(NumberEdit::new(frames, 0, cap, digits, "frames"));
1703+ }
1704+
1705+ /// The typed field's enter: clamps into the anchor's own range
1706+ /// ([`NumberEdit::commit_clamped`]) rather than refusing, because the
1707+ /// stepper this field stands in for already clamps silently -- a typed
1708+ /// number stricter than the mouse door would be a rule the field alone
1709+ /// enforces.
1710+ pub(crate) fn commit_transition(&mut self, cx: &mut Context<Self>) {
1711+ let Some(mut edit) = self.transition_edit.take() else {
1712+ return;
1713+ };
1714+ if let Some((lane, idx)) = self.selected.anchor() {
1715+ let frames = edit.commit_clamped();
1716+ self.set_transition_frames(lane, idx, frames, cx);
1717+ }
1718+ }
1719+
1676 /// The clip as the drag is showing it: an edge under the pointer moves its1720 /// The clip as the drag is showing it: an edge under the pointer moves its
1677 /// own box, and the boxes of everything linked to it, before anything is1721 /// own box, and the boxes of everything linked to it, before anything is
1678 /// committed. Display only -- the project is not touched until the release.1722 /// committed. Display only -- the project is not touched until the release.
crates/app/src/tests/cards.rsmodified+48 -8
@@ -984,11 +984,17 @@ fn the_sound_row_carries_its_rate_into_both_kinds_of_file() {
984 assert!(audio_rate_choices(7).iter().all(|(.., picked)| !picked));984 assert!(audio_rate_choices(7).iter().all(|(.., picked)| !picked));
985 }985 }
986 986
987+/// The shape [`NumberEdit`] is opened in for a bitrate: `(value, MBPS_MIN,
988+/// MBPS_MAX, MBPS_DIGITS, "Mbps")`, same as `Player::edit_mbps`.
989+fn mbps_edit(value: u32) -> NumberEdit {
990+ NumberEdit::new(value, MBPS_MIN, MBPS_MAX, MBPS_DIGITS, "Mbps")
991+}
992+
987 #[test]993 #[test]
988 fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {994 fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
989 // It opens on the number in force, so backspace edits that number995 // It opens on the number in force, so backspace edits that number
990 // rather than the field starting empty over a bitrate still being used.996 // rather than the field starting empty over a bitrate still being used.
991- let mut edit = NumberEdit::new(12);997+ let mut edit = mbps_edit(12);
992 assert_eq!(edit.text, "12");998 assert_eq!(edit.text, "12");
993 edit.backspace();999 edit.backspace();
994 edit.digit(8);1000 edit.digit(8);
@@ -996,11 +1002,11 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
996 assert_eq!(edit.commit(), Some(18));1002 assert_eq!(edit.commit(), Some(18));
997 // A card nobody has typed a number into opens empty: zero is not a1003 // A card nobody has typed a number into opens empty: zero is not a
998 // bitrate anyone chose.1004 // bitrate anyone chose.
999- assert_eq!(NumberEdit::new(0).text, "");1005+ assert_eq!(mbps_edit(0).text, "");
1000 1006
1001 // Out of range is refused *in words* and the digits stay put: clamping1007 // Out of range is refused *in words* and the digits stay put: clamping
1002 // 55 to 50 would write a bitrate the user never typed.1008 // 55 to 50 would write a bitrate the user never typed.
1003- let mut edit = NumberEdit::new(0);1009+ let mut edit = mbps_edit(0);
1004 for digit in [5, 5] {1010 for digit in [5, 5] {
1005 edit.digit(digit);1011 edit.digit(digit);
1006 }1012 }
@@ -1022,7 +1028,7 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
1022 assert_eq!(commit_mbps("1"), Ok(MBPS_MIN));1028 assert_eq!(commit_mbps("1"), Ok(MBPS_MIN));
1023 assert_eq!(commit_mbps("50"), Ok(MBPS_MAX));1029 assert_eq!(commit_mbps("50"), Ok(MBPS_MAX));
1024 assert!(commit_mbps("51").is_err());1030 assert!(commit_mbps("51").is_err());
1025- let mut edit = NumberEdit::new(0);1031+ let mut edit = mbps_edit(0);
1026 for digit in [9, 9, 9, 9] {1032 for digit in [9, 9, 9, 9] {
1027 edit.digit(digit);1033 edit.digit(digit);
1028 }1034 }
@@ -1035,18 +1041,18 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
1035 1041
1036 // The arrows step inside the range and stop at both ends: a walk1042 // The arrows step inside the range and stop at both ends: a walk
1037 // through the legal numbers, never a way out of them.1043 // through the legal numbers, never a way out of them.
1038- let mut edit = NumberEdit::new(0);1044+ let mut edit = mbps_edit(0);
1039 edit.step(1);1045 edit.step(1);
1040 assert_eq!(edit.text, MBPS_MIN.to_string(), "empty starts at the floor");1046 assert_eq!(edit.text, MBPS_MIN.to_string(), "empty starts at the floor");
1041 edit.step(-1);1047 edit.step(-1);
1042 assert_eq!(edit.text, MBPS_MIN.to_string());1048 assert_eq!(edit.text, MBPS_MIN.to_string());
1043- let mut edit = NumberEdit::new(MBPS_MAX);1049+ let mut edit = mbps_edit(MBPS_MAX);
1044 edit.step(1);1050 edit.step(1);
1045 assert_eq!(edit.text, MBPS_MAX.to_string());1051 assert_eq!(edit.text, MBPS_MAX.to_string());
1046 edit.step(-1);1052 edit.step(-1);
1047 assert_eq!(edit.text, (MBPS_MAX - 1).to_string());1053 assert_eq!(edit.text, (MBPS_MAX - 1).to_string());
1048 // A step past a refused number clears the refusal with it.1054 // A step past a refused number clears the refusal with it.
1049- let mut edit = NumberEdit::new(0);1055+ let mut edit = mbps_edit(0);
1050 edit.digit(5);1056 edit.digit(5);
1051 edit.digit(5);1057 edit.digit(5);
1052 assert_eq!(edit.commit(), None);1058 assert_eq!(edit.commit(), None);
@@ -1056,7 +1062,7 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
1056 1062
1057 // The hint the field shows when there is nothing to refuse names both1063 // The hint the field shows when there is nothing to refuse names both
1058 // ways out of it.1064 // ways out of it.
1059- let detail = NumberEdit::new(6).detail();1065+ let detail = mbps_edit(6).detail();
1060 assert!(1066 assert!(
1061 detail.contains("enter") && detail.contains("esc"),1067 detail.contains("enter") && detail.contains("esc"),
1062 "{detail}"1068 "{detail}"
@@ -1064,6 +1070,40 @@ fn a_typed_bitrate_is_a_field_and_not_a_key_capture() {
1064 assert!(detail.starts_with("6▏"), "{detail}");1070 assert!(detail.starts_with("6▏"), "{detail}");
1065 }1071 }
1066 1072
1073+/// DEBT #111: the transition duration row's field is the same [`NumberEdit`]
1074+/// opened `(frames, 0, cap, ..)`, the shape [`Player::edit_transition`]
1075+/// builds it in -- but it clamps on commit instead of refusing, because the
1076+/// row's own stepper (`Player::nudge_transition`, backed by
1077+/// `set_transition_out`/`crossfade`) already clamps to `cap` in silence, and
1078+/// a typed number holding itself to a stricter rule than the mouse door
1079+/// would be a field disagreeing with its own stepper.
1080+#[test]
1081+fn a_typed_transition_duration_clamps_like_its_own_stepper() {
1082+ let cap = 24;
1083+ let digits = cap.to_string().chars().count() + 1;
1084+ // Opens on the frame count already in force, same as the bitrate field.
1085+ let mut edit = NumberEdit::new(10, 0, cap, digits, "frames");
1086+ assert_eq!(edit.text, "10");
1087+ // A number past the cap clamps to it rather than being refused -- the
1088+ // stepper's own ceiling, reached by a different door.
1089+ edit.backspace();
1090+ edit.backspace();
1091+ for digit in [9, 9] {
1092+ edit.digit(digit);
1093+ }
1094+ assert_eq!(edit.text, "99");
1095+ assert_eq!(
1096+ edit.commit_clamped(),
1097+ cap,
1098+ "clamped to the same cap the stepper stops at"
1099+ );
1100+ assert_eq!(edit.refusal, None, "clamping is not a refusal");
1101+ // Empty commits to the floor, not a parse failure -- there is no number
1102+ // typed that is not zero or more frames.
1103+ let mut edit = NumberEdit::new(0, 0, cap, digits, "frames");
1104+ assert_eq!(edit.commit_clamped(), 0);
1105+}
1106+
1067 /// [`crate::ui::dock_stance::transition_of`]: the dock's duration row1107 /// [`crate::ui::dock_stance::transition_of`]: the dock's duration row
1068 /// appears only for an anchor that actually carries a dissolve or a1108 /// appears only for an anchor that actually carries a dissolve or a
1069 /// crossfade, and the room it offers is capped at what the successor clip1109 /// crossfade, and the room it offers is capped at what the successor clip
crates/app/src/ui/dock_stance.rsmodified+19 -3
@@ -1103,8 +1103,9 @@ pub(crate) fn transition_of(
1103 1103
1104 /// The transition duration row: `transition_of` picks the anchor's1104 /// The transition duration row: `transition_of` picks the anchor's
1105 /// transition and its cap, [`Player::nudge_transition`] does the clamped1105 /// transition and its cap, [`Player::nudge_transition`] does the clamped
1106-/// step -- only the paint is this tab's own (`ghost_verb`'s row height and1106+/// step, [`Player::edit_transition`]/[`Player::commit_transition`] the
1107-/// ink tokens).1107+/// type-in (DEBT #111) -- only the paint is this tab's own (`ghost_verb`'s
1108+/// row height and ink tokens).
1108 fn transition_row(player: &Player, cx: &mut Context<Player>) -> Option<impl IntoElement> {1109 fn transition_row(player: &Player, cx: &mut Context<Player>) -> Option<impl IntoElement> {
1109 let (lane, idx) = player.selected.anchor()?;1110 let (lane, idx) = player.selected.anchor()?;
1110 let session = player.session.as_ref()?;1111 let session = player.session.as_ref()?;
@@ -1112,6 +1113,7 @@ fn transition_row(player: &Player, cx: &mut Context<Player>) -> Option<impl Into
1112 let (label_text, frames, cap) =1113 let (label_text, frames, cap) =
1113 transition_of(lane.kind, clips.get(idx)?, clips.get(idx + 1))?;1114 transition_of(lane.kind, clips.get(idx)?, clips.get(idx + 1))?;
1114 let label_style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM);1115 let label_style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM);
1116+ let field = player.transition_edit.as_ref();
1115 let step = |id: &'static str, label: &'static str, glyph: &'static str, by: i32, cx: &mut Context<Player>| {1117 let step = |id: &'static str, label: &'static str, glyph: &'static str, by: i32, cx: &mut Context<Player>| {
1116 div()1118 div()
1117 .id(id)1119 .id(id)
@@ -1140,11 +1142,25 @@ fn transition_row(player: &Player, cx: &mut Context<Player>) -> Option<impl Into
1140 .px(px(8.))1142 .px(px(8.))
1141 .child(1143 .child(
1142 div()1144 div()
1145+ .id("transition-duration-field")
1143 .flex_1()1146 .flex_1()
1144 .font(label_style.font)1147 .font(label_style.font)
1145 .text_size(label_style.size)1148 .text_size(label_style.size)
1146 .text_color(rgb(INK2()))1149 .text_color(rgb(INK2()))
1147- .child(format!("{label_text} {frames}f (of {cap}f offered)")),1150+ .cursor_pointer()
1151+ .children(hitmap::control(
1152+ "transition-duration-field",
1153+ "Transition duration, type-in",
1154+ true,
1155+ ))
1156+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
1157+ this.edit_transition(frames, cap);
1158+ cx.notify();
1159+ }))
1160+ .child(match field {
1161+ Some(edit) => edit.detail(),
1162+ None => format!("{label_text} {frames}f (of {cap}f offered)"),
1163+ }),
1148 )1164 )
1149 .child(step("transition-minus", "Shorten transition", "−", -1, cx))1165 .child(step("transition-minus", "Shorten transition", "−", -1, cx))
1150 .child(step("transition-plus", "Lengthen transition", "+", 1, cx)),1166 .child(step("transition-plus", "Lengthen transition", "+", 1, cx)),
crates/app/src/ui/widgets.rsmodified+137 -0
@@ -272,6 +272,143 @@ impl Render for OverlayTip {
272 }272 }
273 }273 }
274 274
275+/// A number being typed into a row that has no text field of its own: the
276+/// digits so far, the range and digit cap that row enforces, and once a
277+/// commit has been refused, why. Text-field semantics on a card that has no
278+/// text field -- typing, backspace, arrows that step, enter that commits and
279+/// escape that gives up -- held as state and driven by the root's key
280+/// handler, since nothing in these rows takes gpui focus. Shared by the
281+/// export card's custom bitrate field and the transition duration row
282+/// (DEBT #111) -- one implementation, each caller its own range and unit.
283+#[derive(Clone, Debug, PartialEq, Eq)]
284+pub(crate) struct NumberEdit {
285+ pub(crate) text: String,
286+ pub(crate) refusal: Option<String>,
287+ min: u32,
288+ max: u32,
289+ digits: usize,
290+ unit: &'static str,
291+}
292+
293+impl NumberEdit {
294+ /// Starts on the number the row already carries, so backspace edits it
295+ /// rather than the field opening empty over a value that is still in
296+ /// force. Zero is no number at all -- it is what the row opens at, before
297+ /// anyone has typed one.
298+ pub(crate) fn new(value: u32, min: u32, max: u32, digits: usize, unit: &'static str) -> Self {
299+ NumberEdit {
300+ text: match value {
301+ 0 => String::new(),
302+ v => v.to_string(),
303+ },
304+ refusal: None,
305+ min,
306+ max,
307+ digits,
308+ unit,
309+ }
310+ }
311+
312+ /// A digit against what is there. The one past the row's digit cap is
313+ /// refused *out loud*: a keystroke dropped in silence is how the old
314+ /// digit capture left a row showing a number the user had already typed
315+ /// past.
316+ pub(crate) fn digit(&mut self, digit: u32) {
317+ if self.text.chars().count() >= self.digits {
318+ self.refusal = Some(format!("{} digits is already past the ceiling", self.digits));
319+ return;
320+ }
321+ match char::from_digit(digit, 10) {
322+ Some(c) => {
323+ self.text.push(c);
324+ self.refusal = None;
325+ }
326+ None => self.refusal = Some("digits only".into()),
327+ }
328+ }
329+
330+ /// Erases the last digit, and the refusal with it: the number on screen has
331+ /// changed, so the reason the old one was refused no longer describes it.
332+ pub(crate) fn backspace(&mut self) {
333+ self.text.pop();
334+ self.refusal = None;
335+ }
336+
337+ /// The arrows, which is how a number gets picked rather than typed. Steps
338+ /// from what is in the field -- an empty one starts at the floor, so the
339+ /// first press up is the row's minimum and not a jump to some remembered
340+ /// value -- and stays inside the range, because a step is a walk through
341+ /// the legal numbers rather than a way out of them.
342+ pub(crate) fn step(&mut self, by: i32) {
343+ let at = self
344+ .text
345+ .parse::<i32>()
346+ .unwrap_or(self.min as i32 - by.signum());
347+ self.text = (at + by)
348+ .clamp(self.min as i32, self.max as i32)
349+ .to_string();
350+ self.refusal = None;
351+ }
352+
353+ /// The number, or `None` with the reason recorded where the row will read
354+ /// it. Never clamped: a row that clamps a typed number to its ceiling
355+ /// without saying so is writing a number the user never typed. For a row
356+ /// whose own stepper already clamps silently, use [`Self::commit_clamped`]
357+ /// instead -- refusing there would be a stricter rule than the mouse door
358+ /// enforces.
359+ pub(crate) fn commit(&mut self) -> Option<u32> {
360+ match self.text.parse::<u32>() {
361+ Ok(v) if (self.min..=self.max).contains(&v) => {
362+ self.refusal = None;
363+ Some(v)
364+ }
365+ Ok(0) => {
366+ self.refusal = Some(format!(
367+ "0 is not a value — {}–{} {}",
368+ self.min, self.max, self.unit
369+ ));
370+ None
371+ }
372+ Ok(v) => {
373+ self.refusal = Some(format!("{v} is past the {} {} ceiling", self.max, self.unit));
374+ None
375+ }
376+ Err(_) => {
377+ self.refusal = Some(format!(
378+ "type a number — {}–{} {}",
379+ self.min, self.max, self.unit
380+ ));
381+ None
382+ }
383+ }
384+ }
385+
386+ /// The number, clamped into range instead of refused -- for a row whose
387+ /// own stepper already clamps silently (the transition duration's setter
388+ /// clamps to the clips' shared length), so a typed number lands exactly
389+ /// where that same stepper would have walked it to. Always succeeds.
390+ pub(crate) fn commit_clamped(&mut self) -> u32 {
391+ let v = self.text.parse::<u32>().unwrap_or(self.min);
392+ self.refusal = None;
393+ v.clamp(self.min, self.max)
394+ }
395+
396+ /// What the row shows while it is being typed into: the digits, the caret
397+ /// that says they are landing *here*, and either the refusal or the two
398+ /// keys that end the edit.
399+ pub(crate) fn detail(&self) -> String {
400+ format!(
401+ "{}▏ {} — {}",
402+ self.text,
403+ self.unit,
404+ match &self.refusal {
405+ Some(why) => why.as_str(),
406+ None => "enter commits · esc cancels",
407+ }
408+ )
409+ }
410+}
411+
275 /// What a window with no file open is waiting for. Both ways in are already412 /// What a window with no file open is waiting for. Both ways in are already
276 /// built -- the whole window is the drop target and the Import chooser takes a413 /// built -- the whole window is the drop target and the Import chooser takes a
277 /// project as readily as media -- so this only has to say so.414 /// project as readily as media -- so this only has to say so.