The custom bitrate is a field you type in, not digits caught from nowhere
Browse files at this commit10cdebcec8212ca57ff40893b2107a0ae420cfbbParent: 75bfb8e- feat
- the custom bitrate is a field you type in, not digits caught from nowhere
2 changed files +307 -58
- crates/app/src/keymap.rs +9 -4
- crates/app/src/main.rs +298 -54
crates/app/src/keymap.rsmodified+9 -4
| @@ -345,7 +345,7 @@ pub struct Fixed { | ||
| 345 | 345 | pub category: Category, |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | -pub const FIXED: [Fixed; 27] = [ | |
| 348 | +pub const FIXED: [Fixed; 28] = [ | |
| 349 | 349 | // Not a chord at all but a way of pressing one, and the only place the |
| 350 | 350 | // editor can say so: holding a key that moves a *value* runs it, and |
| 351 | 351 | // holding anything else still does what one press did. |
| @@ -359,9 +359,14 @@ pub const FIXED: [Fixed; 27] = [ | ||
| 359 | 359 | label: "Close this card or menu, or cancel a capture", |
| 360 | 360 | category: Category::View, |
| 361 | 361 | }, |
| 362 | + Fixed { | |
| 363 | + chord: "n", | |
| 364 | + label: "Open the custom export bitrate field — ↑↓ step it", | |
| 365 | + category: Category::File, | |
| 366 | + }, | |
| 362 | 367 | Fixed { |
| 363 | 368 | chord: "0–9", |
| 364 | - label: "Type a custom export bitrate", | |
| 369 | + label: "Type into that field — nothing outside it", | |
| 365 | 370 | category: Category::File, |
| 366 | 371 | }, |
| 367 | 372 | Fixed { |
| @@ -406,12 +411,12 @@ pub const FIXED: [Fixed; 27] = [ | ||
| 406 | 411 | }, |
| 407 | 412 | Fixed { |
| 408 | 413 | chord: "enter", |
| 409 | - label: "Start the export the card is set to", | |
| 414 | + label: "Start the export — or commit the bitrate field", | |
| 410 | 415 | category: Category::File, |
| 411 | 416 | }, |
| 412 | 417 | Fixed { |
| 413 | 418 | chord: "backspace", |
| 414 | - label: "Erase a bitrate digit", | |
| 419 | + label: "Erase a digit in the bitrate field", | |
| 415 | 420 | category: Category::File, |
| 416 | 421 | }, |
| 417 | 422 | // The equalizer card's own input, for the same reason the export card has |
crates/app/src/main.rsmodified+298 -54
| @@ -1062,6 +1062,14 @@ struct Player { | ||
| 1062 | 1062 | /// first one chose. |
| 1063 | 1063 | quality: Quality, |
| 1064 | 1064 | custom_mbps: u32, |
| 1065 | + /// The custom row's number *while it is being typed*, or `None` when nobody | |
| 1066 | + /// is typing one. A field with a caret in it and not a key capture: digits | |
| 1067 | + /// used to change the bitrate from anywhere in the card, with no caret to | |
| 1068 | + /// say where they were landing and nothing to look at before the number | |
| 1069 | + /// took effect. Nothing in this card takes gpui focus (the root keeps the | |
| 1070 | + /// keyboard), so the field is a modal state on the player exactly as a | |
| 1071 | + /// waiting rebind row is, and the root's handler is what types into it. | |
| 1072 | + mbps_edit: Option<NumberEdit>, | |
| 1065 | 1073 | /// What the *sound* is coded at, in kbps, for every format that encodes it |
| 1066 | 1074 | /// -- the AAC inside a video export as much as an MP3. Kept across closes |
| 1067 | 1075 | /// like the picture's quality, and starts at the figure this program wrote |
| @@ -2272,7 +2280,12 @@ impl Player { | ||
| 2272 | 2280 | /// exactly the ones that answer differently from the keys menu and the |
| 2273 | 2281 | /// export card. |
| 2274 | 2282 | fn repeat_scope(&self) -> Repeat { |
| 2275 | - if self.rebinding.is_some() | |
| 2283 | + // A number being typed is a value under the arrows, exactly as a card's | |
| 2284 | + // slider is -- so a held arrow runs it. Asked before the export card | |
| 2285 | + // below, which otherwise repeats nothing. | |
| 2286 | + if self.mbps_edit.is_some() { | |
| 2287 | + Repeat::Card | |
| 2288 | + } else if self.rebinding.is_some() | |
| 2276 | 2289 | || self.keys_open |
| 2277 | 2290 | || self.export_open |
| 2278 | 2291 | || self.exporting().is_some() |
| @@ -3903,9 +3916,11 @@ impl Player { | ||
| 3903 | 3916 | } |
| 3904 | 3917 | self.export_open = true; |
| 3905 | 3918 | // One card at a time, and a waiting row must not outlive the card it |
| 3906 | - // was waiting in. | |
| 3919 | + // was waiting in. Nor may a half-typed number: the card opens on the | |
| 3920 | + // bitrate it will write, never on digits left behind by a closed one. | |
| 3907 | 3921 | self.keys_open = false; |
| 3908 | 3922 | self.rebinding = None; |
| 3923 | + self.mbps_edit = None; | |
| 3909 | 3924 | cx.notify(); |
| 3910 | 3925 | } |
| 3911 | 3926 | |
| @@ -3978,8 +3993,19 @@ impl Player { | ||
| 3978 | 3993 | /// (the engine's own 1..20 Mbps), and picking the row is part of the step -- |
| 3979 | 3994 | /// a stepper that moves a number nobody is using would move nothing. |
| 3980 | 3995 | fn nudge_mbps(&mut self, step: i32) { |
| 3981 | - self.custom_mbps = (self.custom_mbps as i32 + step).clamp(1, 20) as u32; | |
| 3996 | + self.custom_mbps = | |
| 3997 | + (self.custom_mbps as i32 + step).clamp(MBPS_MIN as i32, MBPS_MAX as i32) as u32; | |
| 3998 | + self.quality = Quality::Custom; | |
| 3999 | + } | |
| 4000 | + | |
| 4001 | + /// Opens the custom bitrate's field on the number the row is carrying, and | |
| 4002 | + /// picks the row while it is at it: a field typed into is the row being | |
| 4003 | + /// chosen, and a number nobody is using would be a number typed at nothing. | |
| 4004 | + /// Nothing is committed here -- until enter, the card still exports at the | |
| 4005 | + /// bitrate it had. | |
| 4006 | + fn edit_mbps(&mut self) { | |
| 3982 | 4007 | self.quality = Quality::Custom; |
| 4008 | + self.mbps_edit = Some(NumberEdit::new(self.custom_mbps)); | |
| 3983 | 4009 | } |
| 3984 | 4010 | |
| 3985 | 4011 | /// The card's Destination row: the desktop's save dialog, on a background |
| @@ -4249,10 +4275,10 @@ impl Render for Player { | ||
| 4249 | 4275 | } |
| 4250 | 4276 | // The export card owns it the same way, and for the same |
| 4251 | 4277 | // reason. Escape closes it -- nothing has been written yet, so |
| 4252 | - // there is nothing here to cancel -- and a digit is the custom | |
| 4253 | - // bitrate being typed: the card has no text field (nothing in | |
| 4254 | - // it takes focus, ledger:182) so this listener is its input, | |
| 4255 | - // exactly as it is a waiting row's. | |
| 4278 | + // there is nothing here to cancel -- and the card's own letters | |
| 4279 | + // are its input: it has no widget that takes focus (nothing in | |
| 4280 | + // it does), so this listener is its keyboard, exactly as it is | |
| 4281 | + // a waiting row's. | |
| 4256 | 4282 | if this.export_open { |
| 4257 | 4283 | // A list open over the card is the innermost thing on |
| 4258 | 4284 | // screen, so it is what a stroke closes first -- the rule |
| @@ -4262,6 +4288,35 @@ impl Render for Player { | ||
| 4262 | 4288 | cx.notify(); |
| 4263 | 4289 | return; |
| 4264 | 4290 | } |
| 4291 | + // A number being typed is the next thing in: while the | |
| 4292 | + // field is open every stroke is text, which is what makes | |
| 4293 | + // it a field and not a capture -- the card's letters cannot | |
| 4294 | + // fire under it, and escape gives up the edit before it | |
| 4295 | + // touches the card. | |
| 4296 | + if let Some(edit) = &mut this.mbps_edit { | |
| 4297 | + if key == ESCAPE { | |
| 4298 | + this.mbps_edit = None; | |
| 4299 | + } else if key == "enter" { | |
| 4300 | + // Committed or refused in its own words; a refused | |
| 4301 | + // one stays open on what was typed, so the number | |
| 4302 | + // can be fixed rather than typed again. | |
| 4303 | + if let Some(mbps) = edit.commit() { | |
| 4304 | + this.custom_mbps = mbps; | |
| 4305 | + this.quality = Quality::Custom; | |
| 4306 | + this.mbps_edit = None; | |
| 4307 | + } | |
| 4308 | + } else if key == "backspace" { | |
| 4309 | + edit.backspace(); | |
| 4310 | + } else if key == "up" { | |
| 4311 | + edit.step(1); | |
| 4312 | + } else if key == "down" { | |
| 4313 | + edit.step(-1); | |
| 4314 | + } else if let Ok(digit) = key.parse::<u32>() { | |
| 4315 | + edit.digit(digit); | |
| 4316 | + } | |
| 4317 | + cx.notify(); | |
| 4318 | + return; | |
| 4319 | + } | |
| 4265 | 4320 | if key == ESCAPE { |
| 4266 | 4321 | this.export_open = false; |
| 4267 | 4322 | } else if key == "enter" { |
| @@ -4292,19 +4347,13 @@ impl Render for Player { | ||
| 4292 | 4347 | this.export_grouped = !this.export_grouped; |
| 4293 | 4348 | } else if key == "r" { |
| 4294 | 4349 | this.export_refusals_inline = !this.export_refusals_inline; |
| 4295 | - } else if let Ok(digit) = key.parse::<u32>() { | |
| 4296 | - let next = push_digit(this.custom_mbps, digit); | |
| 4297 | - // A refused third digit says so: it used to be dropped | |
| 4298 | - // in silence, which left the card showing a number the | |
| 4299 | - // user had already typed past. | |
| 4300 | - if next == this.custom_mbps && this.custom_mbps > 0 { | |
| 4301 | - this.notice = Some("1–20 Mbps — a third digit is refused".into()); | |
| 4302 | - } | |
| 4303 | - this.custom_mbps = next; | |
| 4304 | - this.quality = Quality::Custom; | |
| 4305 | - } else if key == "backspace" { | |
| 4306 | - this.custom_mbps /= 10; | |
| 4307 | - this.quality = Quality::Custom; | |
| 4350 | + } else if key == "n" { | |
| 4351 | + // The custom row's field, by keyboard. The digits used | |
| 4352 | + // to do this from anywhere in the card, which meant a | |
| 4353 | + // stray keystroke changed the bitrate with nothing on | |
| 4354 | + // screen to say it had: now a digit outside the field | |
| 4355 | + // means nothing at all, and this is the way in. | |
| 4356 | + this.edit_mbps(); | |
| 4308 | 4357 | } |
| 4309 | 4358 | cx.notify(); |
| 4310 | 4359 | return; |
| @@ -5794,7 +5843,7 @@ impl Player { | ||
| 5794 | 5843 | ) |
| 5795 | 5844 | // The search box: no focus and no text field -- the |
| 5796 | 5845 | // card's key handler is the field, exactly as the |
| 5797 | - // export card's digits are typed into it. Its own line | |
| 5846 | + // export card's bitrate is typed into it. Its own line | |
| 5798 | 5847 | // above the list, so the rows below it are always the |
| 5799 | 5848 | // answer to what it says. |
| 5800 | 5849 | .child( |
| @@ -5831,7 +5880,8 @@ impl Player { | ||
| 5831 | 5880 | /// two lines that state the file all of that adds up to. The same scrim and |
| 5832 | 5881 | /// row shape as the keybindings overlay (two cards of different builds over |
| 5833 | 5882 | /// one window read as two different programs) and the same plain divs, so |
| 5834 | - /// the root keeps the keyboard and a typed digit reaches the custom row. | |
| 5883 | + /// the root keeps the keyboard and the custom row's field is typed into | |
| 5884 | + /// through it ([`NumberEdit`]). | |
| 5835 | 5885 | /// |
| 5836 | 5886 | /// Every row carries the key that picks it, so the card is drivable end to |
| 5837 | 5887 | /// end without a pointer *and* without a legend to memorise -- and every |
| @@ -6026,22 +6076,31 @@ impl Player { | ||
| 6026 | 6076 | ), |
| 6027 | 6077 | None => { |
| 6028 | 6078 | for (i, quality) in Quality::ALL.into_iter().enumerate() { |
| 6079 | + // The custom row is a field: `n` opens it, a click in it | |
| 6080 | + // opens it too, and while it is open the row shows what is | |
| 6081 | + // being typed with a caret in it rather than the number in | |
| 6082 | + // force. The other four are picked whole. | |
| 6083 | + let field = self.mbps_edit.as_ref().filter(|_| quality == Quality::Custom); | |
| 6029 | 6084 | let mut r = entry( |
| 6030 | 6085 | ("quality", i), |
| 6031 | 6086 | match quality { |
| 6032 | - // The one row a key does not step to but types | |
| 6033 | - // into, which is what its key column says. | |
| 6034 | - Quality::Custom => "0–9", | |
| 6087 | + Quality::Custom => "n", | |
| 6035 | 6088 | _ => "q", |
| 6036 | 6089 | }, |
| 6037 | 6090 | quality.label().into(), |
| 6038 | - quality.detail(self.custom_mbps).into(), | |
| 6091 | + match field { | |
| 6092 | + Some(edit) => edit.detail().into(), | |
| 6093 | + None => quality.detail(self.custom_mbps).into(), | |
| 6094 | + }, | |
| 6039 | 6095 | self.quality == quality, |
| 6040 | 6096 | true, |
| 6041 | 6097 | ) |
| 6042 | 6098 | .on_click(cx.listener( |
| 6043 | 6099 | move |this, _: &ClickEvent, _, cx| { |
| 6044 | - this.quality = quality; | |
| 6100 | + match quality { | |
| 6101 | + Quality::Custom => this.edit_mbps(), | |
| 6102 | + _ => this.quality = quality, | |
| 6103 | + } | |
| 6045 | 6104 | cx.notify(); |
| 6046 | 6105 | }, |
| 6047 | 6106 | )); |
| @@ -6207,11 +6266,22 @@ impl Player { | ||
| 6207 | 6266 | .px(px(6.)) |
| 6208 | 6267 | .text_size(px(11.)) |
| 6209 | 6268 | .text_color(rgb(INK_DIM)) |
| 6210 | - .child(self.notice.clone().unwrap_or_else(|| { | |
| 6211 | - "the keys are on the rows · enter exports · esc closes · \ | |
| 6212 | - g/r change the layout" | |
| 6213 | - .into() | |
| 6214 | - })), | |
| 6269 | + .child(match (&self.mbps_edit, &self.notice) { | |
| 6270 | + // A field being typed into says so here as | |
| 6271 | + // well as in its row: this line is outside | |
| 6272 | + // the scrolling list and on screen at every | |
| 6273 | + // window size, and at 360 px the custom row | |
| 6274 | + // itself can be below the fold -- a number | |
| 6275 | + // typed where it cannot be seen is the | |
| 6276 | + // blind capture this field replaced. | |
| 6277 | + (Some(edit), _) => { | |
| 6278 | + SharedString::from(format!("Custom bitrate {}", edit.detail())) | |
| 6279 | + } | |
| 6280 | + (None, Some(notice)) => notice.clone(), | |
| 6281 | + (None, None) => "the keys are on the rows · enter exports · \ | |
| 6282 | + esc closes · g/r change the layout" | |
| 6283 | + .into(), | |
| 6284 | + }), | |
| 6215 | 6285 | ) |
| 6216 | 6286 | // Capped and scrolling like the keybindings list: the |
| 6217 | 6287 | // rows are more than a 360 px window has room for, and |
| @@ -9162,7 +9232,9 @@ impl Quality { | ||
| 9162 | 9232 | fn detail(self, custom_mbps: u32) -> String { |
| 9163 | 9233 | match self { |
| 9164 | 9234 | Quality::Auto => "from the picture size and frame rate".to_string(), |
| 9165 | - Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), | |
| 9235 | + Quality::Custom => { | |
| 9236 | + format!("{custom_mbps} Mbps — n types a number, {MBPS_MIN}–{MBPS_MAX}") | |
| 9237 | + } | |
| 9166 | 9238 | other => format!( |
| 9167 | 9239 | "{} Mbps", |
| 9168 | 9240 | export_settings(other, 0, Format::Mp4, DEFAULT_AUDIO_KBPS) |
| @@ -9228,7 +9300,7 @@ const FORMATS: [(&[Format], &str, &str, &str); 8] = [ | ||
| 9228 | 9300 | /// `a` after an mp4 gives AV1 *in mp4*, because a box decided once is not a |
| 9229 | 9301 | /// question again. A letter of the row's own name, spelled out in the table |
| 9230 | 9302 | /// rather than taken from the initial (`MP4` and `MP3` share one). Never a |
| 9231 | -/// digit -- those are the bitrate's. `None` for every other stroke, the rows | |
| 9303 | +/// digit -- those are the bitrate field's. `None` for every other stroke, the rows | |
| 9232 | 9304 | /// that cannot be picked included. |
| 9233 | 9305 | fn format_key(key: &str, current: Format) -> Option<Format> { |
| 9234 | 9306 | FORMATS |
| @@ -9629,13 +9701,118 @@ fn export_settings( | ||
| 9629 | 9701 | } |
| 9630 | 9702 | } |
| 9631 | 9703 | |
| 9632 | -/// A digit typed against the custom row, appended to what is already there. | |
| 9633 | -/// Refused rather than truncated past two digits: the engine's ceiling is | |
| 9634 | -/// 20 Mbps, so a third digit can only be a mistake, and a silently dropped one | |
| 9635 | -/// would leave the card showing a number nobody typed. | |
| 9636 | -fn push_digit(mbps: u32, digit: u32) -> u32 { | |
| 9637 | - let next = mbps * 10 + digit; | |
| 9638 | - if next <= 99 { next } else { mbps } | |
| 9704 | +/// What the engine will code an explicit bitrate at, in whole Mbps: outside | |
| 9705 | +/// this it clamps (`export.rs` `MIN_BITRATE`/`MAX_BITRATE`), so a number typed | |
| 9706 | +/// past either end would be written as a different one. The field refuses it | |
| 9707 | +/// instead of clamping quietly -- a card that changes the user's number without | |
| 9708 | +/// saying so is the one thing a field like this must never do. | |
| 9709 | +const MBPS_MIN: u32 = 1; | |
| 9710 | +const MBPS_MAX: u32 = 20; | |
| 9711 | + | |
| 9712 | +/// How many digits the field takes. Two reach the ceiling; the third is there so | |
| 9713 | +/// a number *past* it can be typed whole and refused in its own words, rather | |
| 9714 | +/// than being dropped keystroke by keystroke. | |
| 9715 | +const MBPS_DIGITS: usize = 3; | |
| 9716 | + | |
| 9717 | +/// A number being typed into a card row: the digits so far and, once a commit | |
| 9718 | +/// has been refused, why. Text-field semantics on a card that has no text field | |
| 9719 | +/// -- typing, backspace, arrows that step, enter that commits and escape that | |
| 9720 | +/// gives up -- held as state and driven by the root's key handler, since | |
| 9721 | +/// nothing in these cards takes gpui focus. | |
| 9722 | +#[derive(Clone, Debug, Default, PartialEq, Eq)] | |
| 9723 | +struct NumberEdit { | |
| 9724 | + text: String, | |
| 9725 | + refusal: Option<String>, | |
| 9726 | +} | |
| 9727 | + | |
| 9728 | +impl NumberEdit { | |
| 9729 | + /// Starts on the number the row already carries, so backspace edits it | |
| 9730 | + /// rather than the field opening empty over a value that is still in force. | |
| 9731 | + /// Zero is no number at all -- it is what the card opens at, before anyone | |
| 9732 | + /// has typed one. | |
| 9733 | + fn new(value: u32) -> Self { | |
| 9734 | + NumberEdit { | |
| 9735 | + text: match value { | |
| 9736 | + 0 => String::new(), | |
| 9737 | + v => v.to_string(), | |
| 9738 | + }, | |
| 9739 | + refusal: None, | |
| 9740 | + } | |
| 9741 | + } | |
| 9742 | + | |
| 9743 | + /// A digit against what is there. The one past [`MBPS_DIGITS`] is refused | |
| 9744 | + /// *out loud*: a keystroke dropped in silence is how the old digit capture | |
| 9745 | + /// left the card showing a number the user had already typed past. | |
| 9746 | + fn digit(&mut self, digit: u32) { | |
| 9747 | + if self.text.chars().count() >= MBPS_DIGITS { | |
| 9748 | + self.refusal = Some(format!("{MBPS_DIGITS} digits is already past the ceiling")); | |
| 9749 | + return; | |
| 9750 | + } | |
| 9751 | + match char::from_digit(digit, 10) { | |
| 9752 | + Some(c) => { | |
| 9753 | + self.text.push(c); | |
| 9754 | + self.refusal = None; | |
| 9755 | + } | |
| 9756 | + None => self.refusal = Some("digits only".into()), | |
| 9757 | + } | |
| 9758 | + } | |
| 9759 | + | |
| 9760 | + /// Erases the last digit, and the refusal with it: the number on screen has | |
| 9761 | + /// changed, so the reason the old one was refused no longer describes it. | |
| 9762 | + fn backspace(&mut self) { | |
| 9763 | + self.text.pop(); | |
| 9764 | + self.refusal = None; | |
| 9765 | + } | |
| 9766 | + | |
| 9767 | + /// The arrows, which is how a number gets picked rather than typed. Steps | |
| 9768 | + /// from what is in the field -- an empty one starts at the floor, so the | |
| 9769 | + /// first press up is `MBPS_MIN` and not a jump to some remembered value -- | |
| 9770 | + /// and stays inside the range, because a step is a walk through the legal | |
| 9771 | + /// numbers rather than a way out of them. | |
| 9772 | + fn step(&mut self, by: i32) { | |
| 9773 | + let at = self.text.parse::<i32>().unwrap_or(MBPS_MIN as i32 - by.signum()); | |
| 9774 | + self.text = (at + by) | |
| 9775 | + .clamp(MBPS_MIN as i32, MBPS_MAX as i32) | |
| 9776 | + .to_string(); | |
| 9777 | + self.refusal = None; | |
| 9778 | + } | |
| 9779 | + | |
| 9780 | + /// The number, or `None` with the reason recorded where the row will read | |
| 9781 | + /// it. Never clamped: 45 committed as 20 is a number the user did not type. | |
| 9782 | + fn commit(&mut self) -> Option<u32> { | |
| 9783 | + match commit_mbps(&self.text) { | |
| 9784 | + Ok(mbps) => Some(mbps), | |
| 9785 | + Err(why) => { | |
| 9786 | + self.refusal = Some(why); | |
| 9787 | + None | |
| 9788 | + } | |
| 9789 | + } | |
| 9790 | + } | |
| 9791 | + | |
| 9792 | + /// What the row shows while it is being typed into: the digits, the caret | |
| 9793 | + /// that says they are landing *here*, and either the refusal or the two | |
| 9794 | + /// keys that end the edit. | |
| 9795 | + fn detail(&self) -> String { | |
| 9796 | + format!( | |
| 9797 | + "{}▏ Mbps — {}", | |
| 9798 | + self.text, | |
| 9799 | + match &self.refusal { | |
| 9800 | + Some(why) => why.as_str(), | |
| 9801 | + None => "enter commits · esc cancels", | |
| 9802 | + } | |
| 9803 | + ) | |
| 9804 | + } | |
| 9805 | +} | |
| 9806 | + | |
| 9807 | +/// A typed bitrate as the card takes it, or the reason it is not one. The words | |
| 9808 | +/// are the row's: they are what the field shows in place of its hint. | |
| 9809 | +fn commit_mbps(text: &str) -> Result<u32, String> { | |
| 9810 | + match text.parse::<u32>() { | |
| 9811 | + Ok(mbps) if (MBPS_MIN..=MBPS_MAX).contains(&mbps) => Ok(mbps), | |
| 9812 | + Ok(0) => Err(format!("0 is not a rate — {MBPS_MIN}–{MBPS_MAX} Mbps")), | |
| 9813 | + Ok(mbps) => Err(format!("{mbps} is past the {MBPS_MAX} Mbps ceiling")), | |
| 9814 | + Err(_) => Err(format!("type a number — {MBPS_MIN}–{MBPS_MAX} Mbps")), | |
| 9815 | + } | |
| 9639 | 9816 | } |
| 9640 | 9817 | |
| 9641 | 9818 | /// Whether a stroke gets out of a running export. Escape does, whatever |
| @@ -10929,16 +11106,18 @@ mod tests { | ||
| 10929 | 11106 | Format, HEADER_GAP, HEADER_H, HEADER_W, HIST_BINS, HIST_H, HIST_SAMPLES, HIT_MIN, INK, |
| 10930 | 11107 | INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, KeyRow, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, |
| 10931 | 11108 | LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, |
| 10932 | - MENU_ROWS_H, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, | |
| 11109 | + MBPS_DIGITS, MBPS_MAX, MBPS_MIN, MENU_ROWS_H, MENU_W, NO_FILE, NumberEdit, PANEL_H, | |
| 11110 | + Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, | |
| 10933 | 11111 | SOURCE_TINTS, SPEED_PRESETS, SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, |
| 10934 | 11112 | Transport, VOLUME_W, Volume, WAVE_BPS, WAVE_COL, WAVE_COLS_MAX, Wave, band_label, |
| 10935 | - bitrate_refusal, can_add, cancels_export, clipboard_after_remove, color_snap, containers, | |
| 11113 | + bitrate_refusal, can_add, cancels_export, clipboard_after_remove, color_snap, commit_mbps, | |
| 11114 | + containers, | |
| 10936 | 11115 | enable, envelope, eq_card_w, eq_freq, eq_freq_label, eq_spectrum, eq_x, eq_y, |
| 10937 | 11116 | estimated_mb, export_path, export_settings, format_key, |
| 10938 | 11117 | format_line, format_refusal, fps_label, frac_along, frac_down, frame_at, histogram, |
| 10939 | 11118 | inserted_band, is_bare_modifier, is_project, keymap, keys_filter, keys_rows, lanes_h, |
| 10940 | 11119 | marked, menu_at, next_audio_kbps, next_container, normalise, nothing_to_play, panel_h, project_path, |
| 10941 | - push_digit, retarget, scrub_due, show_label, silence_rate, snap_cue, snap_marks, snapped, | |
| 11120 | + retarget, scrub_due, show_label, silence_rate, snap_cue, snap_marks, snapped, | |
| 10942 | 11121 | source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, transport, |
| 10943 | 11122 | typed, unseen_paths, unseen_sources, |
| 10944 | 11123 | whole_take, window_title, |
| @@ -13628,16 +13807,80 @@ mod tests { | ||
| 13628 | 13807 | } |
| 13629 | 13808 | |
| 13630 | 13809 | #[test] |
| 13631 | - fn a_typed_bitrate_stops_at_two_digits() { | |
| 13632 | - assert_eq!(push_digit(0, 8), 8); | |
| 13633 | - assert_eq!(push_digit(1, 2), 12); | |
| 13634 | - // A third digit is refused whole: the ceiling is 20 Mbps, so it can | |
| 13635 | - // only be a mistake, and dropping it silently would leave the card | |
| 13636 | - // showing a number nobody typed. | |
| 13637 | - assert_eq!(push_digit(12, 3), 12); | |
| 13638 | - assert_eq!(push_digit(99, 9), 99); | |
| 13639 | - // Never past what the clamp can take back to a real bitrate. | |
| 13640 | - assert!(u64::from(push_digit(99, 9)) * 1_000_000 < u64::from(u32::MAX)); | |
| 13810 | + fn a_typed_bitrate_is_a_field_and_not_a_key_capture() { | |
| 13811 | + // It opens on the number in force, so backspace edits that number | |
| 13812 | + // rather than the field starting empty over a bitrate still being used. | |
| 13813 | + let mut edit = NumberEdit::new(12); | |
| 13814 | + assert_eq!(edit.text, "12"); | |
| 13815 | + edit.backspace(); | |
| 13816 | + edit.digit(8); | |
| 13817 | + assert_eq!(edit.text, "18"); | |
| 13818 | + assert_eq!(edit.commit(), Some(18)); | |
| 13819 | + // A card nobody has typed a number into opens empty: zero is not a | |
| 13820 | + // bitrate anyone chose. | |
| 13821 | + assert_eq!(NumberEdit::new(0).text, ""); | |
| 13822 | + | |
| 13823 | + // Out of range is refused *in words* and the digits stay put: clamping | |
| 13824 | + // 45 to 20 would write a bitrate the user never typed. | |
| 13825 | + let mut edit = NumberEdit::new(0); | |
| 13826 | + for digit in [4, 5] { | |
| 13827 | + edit.digit(digit); | |
| 13828 | + } | |
| 13829 | + assert_eq!(edit.commit(), None); | |
| 13830 | + assert_eq!(edit.text, "45", "a refusal keeps what was typed"); | |
| 13831 | + let refusal = edit.refusal.clone().expect("a refusal says why"); | |
| 13832 | + assert!(refusal.contains("20"), "{refusal}"); | |
| 13833 | + assert!(edit.detail().starts_with("45▏"), "{}", edit.detail()); | |
| 13834 | + assert!(edit.detail().contains(&refusal)); | |
| 13835 | + // And is fixable in place, which is the whole point of a field. | |
| 13836 | + edit.backspace(); | |
| 13837 | + assert_eq!(edit.refusal, None, "the reason went with the digit"); | |
| 13838 | + assert_eq!(edit.commit(), Some(4)); | |
| 13839 | + | |
| 13840 | + // Empty, zero, and past the digit cap: each its own reason, none of | |
| 13841 | + // them silent. | |
| 13842 | + assert!(commit_mbps("").is_err()); | |
| 13843 | + assert!(commit_mbps("0").unwrap_err().contains("not a rate")); | |
| 13844 | + assert_eq!(commit_mbps("1"), Ok(MBPS_MIN)); | |
| 13845 | + assert_eq!(commit_mbps("20"), Ok(MBPS_MAX)); | |
| 13846 | + assert!(commit_mbps("21").is_err()); | |
| 13847 | + let mut edit = NumberEdit::new(0); | |
| 13848 | + for digit in [9, 9, 9, 9] { | |
| 13849 | + edit.digit(digit); | |
| 13850 | + } | |
| 13851 | + assert_eq!(edit.text, "999", "the cap holds"); | |
| 13852 | + assert!(edit.refusal.is_some(), "and says it is holding"); | |
| 13853 | + // Never past what a u64 bitrate can be built from -- the committed | |
| 13854 | + // number is the only one that reaches the engine, and it is bounded. | |
| 13855 | + assert!(u64::from(MBPS_MAX) * 1_000_000 < u64::from(u32::MAX)); | |
| 13856 | + assert_eq!(MBPS_DIGITS, 3); | |
| 13857 | + | |
| 13858 | + // The arrows step inside the range and stop at both ends: a walk | |
| 13859 | + // through the legal numbers, never a way out of them. | |
| 13860 | + let mut edit = NumberEdit::new(0); | |
| 13861 | + edit.step(1); | |
| 13862 | + assert_eq!(edit.text, MBPS_MIN.to_string(), "empty starts at the floor"); | |
| 13863 | + edit.step(-1); | |
| 13864 | + assert_eq!(edit.text, MBPS_MIN.to_string()); | |
| 13865 | + let mut edit = NumberEdit::new(MBPS_MAX); | |
| 13866 | + edit.step(1); | |
| 13867 | + assert_eq!(edit.text, MBPS_MAX.to_string()); | |
| 13868 | + edit.step(-1); | |
| 13869 | + assert_eq!(edit.text, (MBPS_MAX - 1).to_string()); | |
| 13870 | + // A step past a refused number clears the refusal with it. | |
| 13871 | + let mut edit = NumberEdit::new(0); | |
| 13872 | + edit.digit(4); | |
| 13873 | + edit.digit(5); | |
| 13874 | + assert_eq!(edit.commit(), None); | |
| 13875 | + edit.step(-1); | |
| 13876 | + assert_eq!(edit.refusal, None); | |
| 13877 | + assert_eq!(edit.text, MBPS_MAX.to_string(), "back inside the range"); | |
| 13878 | + | |
| 13879 | + // The hint the field shows when there is nothing to refuse names both | |
| 13880 | + // ways out of it. | |
| 13881 | + let detail = NumberEdit::new(6).detail(); | |
| 13882 | + assert!(detail.contains("enter") && detail.contains("esc"), "{detail}"); | |
| 13883 | + assert!(detail.starts_with("6▏"), "{detail}"); | |
| 13641 | 13884 | } |
| 13642 | 13885 | |
| 13643 | 13886 | #[test] |
| @@ -14895,6 +15138,7 @@ fn main() { | ||
| 14895 | 15138 | // bitrate the picture asks for. |
| 14896 | 15139 | quality: Quality::Auto, |
| 14897 | 15140 | custom_mbps: 0, |
| 15141 | + mbps_edit: None, | |
| 14898 | 15142 | // ...and the rate the sound has always been written at. |
| 14899 | 15143 | audio_kbps: DEFAULT_AUDIO_KBPS, |
| 14900 | 15144 | // Picture and sound, which is what an export was before |