mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
look-7b: a text field and a text form row can ask for a width in characters (TextField::width_chars, Param::field_chars: n times the body face's '0', as CSS's ch, plus the field padding) over the field length; a row that lacks room still narrows it; gallery stories and trials
This commit is contained in:
1 parent
37b862c663
commit
4b4abd000e
8 files changed
+110
-2
No files matched your search
@@ -476,6 +476,8 @@ pub(crate) struct RowOptions {
|
||||
max_chars: Option<usize>,
|
||||
/// Whether a text row shows its text in the monospace face ([`Param::mono`]).
|
||||
mono: bool,
|
||||
/// The width a text row's field asks for, in characters ([`Param::field_chars`]).
|
||||
field_chars: Option<u16>,
|
||||
units: Option<UnitChoices>,
|
||||
/// The ids of the options a choice row shows disabled.
|
||||
disabled_options: Vec<String>,
|
||||
@@ -782,6 +784,16 @@ impl Param {
|
||||
self
|
||||
}
|
||||
|
||||
/// The width of a [`ParamKind::Text`] row's field, in characters (`TextField::width_chars`): the field asks for
|
||||
/// `chars` times the width of a `0` in its face instead of the field length, so one row of a form can be wider or
|
||||
/// narrower than the others (a watermark's text against a short name). A row that lacks room still narrows it.
|
||||
/// `0` keeps the field length; other kinds, and a text row with a Browse button, are unchanged.
|
||||
#[must_use]
|
||||
pub fn field_chars(mut self, chars: u16) -> Self {
|
||||
self.options.get_or_insert_default().field_chars = (chars > 0).then_some(chars);
|
||||
self
|
||||
}
|
||||
|
||||
/// Hover text for the whole row, label and control, of any kind.
|
||||
#[must_use]
|
||||
pub fn tooltip(mut self, text: impl Into<Label>) -> Self {
|
||||
@@ -1021,6 +1033,11 @@ impl RowOptions {
|
||||
self.mono
|
||||
}
|
||||
|
||||
/// The width a text row's field asks for, in characters; `None` for the field length.
|
||||
pub(crate) fn field_chars(&self) -> Option<u16> {
|
||||
self.field_chars
|
||||
}
|
||||
|
||||
/// Whether a choice row's select has a search field.
|
||||
pub(crate) fn searchable(&self) -> bool {
|
||||
self.searchable
|
||||
|
||||
@@ -870,6 +870,9 @@ impl<'p> Pass<'p> {
|
||||
if options.mono() {
|
||||
field = field.mono();
|
||||
}
|
||||
if let Some(chars) = options.field_chars() {
|
||||
field = field.width_chars(chars);
|
||||
}
|
||||
if let Some(hint) = line.param.hint_text() {
|
||||
field = field.placeholder(hint.clone());
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ fn patterns() -> Vec<PresetRow<String>> {
|
||||
}
|
||||
|
||||
/// One row of every kind the rows show, a section, a row that waits for a check box, a row with a unit menu, a
|
||||
/// multi-line text row, a searchable choice, a radio row with a disabled option, a radio row with its options in a row,
|
||||
/// multi-line text row, a text row forty characters wide, a searchable choice, a radio row with a disabled option, a radio row with its options in a row,
|
||||
/// a blend row, a pattern row, a files row in folder mode and a section of read-only rows.
|
||||
fn params() -> Vec<Param> {
|
||||
let mut params = Param::parse_list(SPEC);
|
||||
@@ -61,6 +61,7 @@ fn params() -> Vec<Param> {
|
||||
params.push(Param::new("sharpen", "Sharpen", ParamKind::bool(false)));
|
||||
params.push(Param::new("amount", "Amount", ParamKind::range(0.0, 100.0, 50.0, Default::default())).enabled_when("sharpen", true));
|
||||
params.push(Param::new("notes", "Notes", ParamKind::text("")).multiline(3));
|
||||
params.push(Param::new("caption", "Caption", ParamKind::text("CONFIDENTIAL")).field_chars(40));
|
||||
params.push(Param::section("Camera Data"));
|
||||
params.push(Param::read_only_text("make", "Make", "Canon"));
|
||||
params.push(Param::read_only_text("exposure", "Exposure", "1/250 s at f/8"));
|
||||
|
||||
@@ -153,6 +153,23 @@ fn a_radio_row_in_a_row_stands_its_options_side_by_side_without_a_caption() {
|
||||
assert_eq!(values.borrow().text("layout"), Some("horizontal"));
|
||||
}
|
||||
|
||||
// A text row with `field_chars` asks for that many characters instead of the field length: a wide row's field is wider
|
||||
// than a plain row's, and `0` keeps the field length.
|
||||
#[trial]
|
||||
fn a_text_row_takes_the_width_in_characters_it_asks_for() {
|
||||
let params = vec![
|
||||
Param::new("name", "Name", ParamKind::text("")),
|
||||
Param::new("text", "Text", ParamKind::text("CONFIDENTIAL")).field_chars(48),
|
||||
Param::new("plain", "Plain", ParamKind::text("")).field_chars(0),
|
||||
];
|
||||
let values = Rc::new(RefCell::new(DialogFields::new()));
|
||||
let fixture = rows(params, &values);
|
||||
let width = |label: &str| fixture.by_label(label).unwrap_or_else(|| panic!("the {label} field")).rect.width();
|
||||
let (name, text, plain) = (width("Name"), width("Text"), width("Plain"));
|
||||
assert!(text > name, "the wide row's field is wider than the field length: {text} against {name}");
|
||||
assert!((plain - name).abs() < 0.5, "0 characters keep the field length: {plain} against {name}");
|
||||
}
|
||||
|
||||
// A read-only row shows its text, selectable, and keeps no value.
|
||||
#[trial]
|
||||
fn a_read_only_row_shows_its_text_and_keeps_no_value() {
|
||||
|
||||
@@ -92,6 +92,14 @@ impl FieldStyle {
|
||||
self
|
||||
}
|
||||
|
||||
/// The width of a field that holds `chars` characters: `chars` times the width of a `0` in the body face (CSS's
|
||||
/// `ch`), plus the field's padding on both sides. [`held_at`](Self::held_at) takes it as the field's length.
|
||||
pub fn chars_width(cx: &StyleCx<'_>, chars: u16) -> f32 {
|
||||
let digit = cx.text_width(crate::style::TypeRole::Body, "0");
|
||||
let digit = if digit.is_finite() && digit > 0.0 { digit } else { 0.0 };
|
||||
f32::from(chars) * digit + 2.0 * cx.control().pad_x
|
||||
}
|
||||
|
||||
/// The field's padding, gap and minimum size.
|
||||
pub fn metrics(&self) -> Metrics {
|
||||
self.metrics
|
||||
|
||||
@@ -76,6 +76,10 @@ pub struct TextField<'a, C: Codec + Clone + 'static = PlainText> {
|
||||
caret: Option<Bind<'a, usize>>,
|
||||
/// The most lines a multi-line field grows to before its text scrolls inside it.
|
||||
max_lines: Option<usize>,
|
||||
/// The width the field asks for, in characters ([`width_chars`](Self::width_chars)); `None` asks for the field
|
||||
/// length (`size.length.field`).
|
||||
#[style_key]
|
||||
width_chars: Option<u16>,
|
||||
}
|
||||
|
||||
impl<'a> TextField<'a, PlainText> {
|
||||
@@ -112,6 +116,7 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
|
||||
drives: None,
|
||||
caret: None,
|
||||
max_lines: None,
|
||||
width_chars: None,
|
||||
}
|
||||
.label(label)
|
||||
}
|
||||
@@ -185,6 +190,17 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The field asks for the width of `chars` characters instead of the field length (`size.length.field`): `chars`
|
||||
/// times the width of a `0` in the field's body face, as CSS's `ch` unit, plus the field's padding. The width
|
||||
/// follows the text size and the face, as the field length does. It is the width the field asks for, not a fixed
|
||||
/// one: a row that lacks room still narrows it, and a field sized `Sizing::Content` or `Sizing::FillWidth` takes
|
||||
/// its own width as before. `0` keeps the field length.
|
||||
#[must_use]
|
||||
pub fn width_chars(mut self, chars: u16) -> Self {
|
||||
self.width_chars = (chars > 0).then_some(chars);
|
||||
self
|
||||
}
|
||||
|
||||
/// Binds the caret, as a byte offset into the text: a value the app writes places the caret at the character
|
||||
/// boundary at or before it, collapses the selection and scrolls the caret into view; the user moving the caret
|
||||
/// writes its offset back.
|
||||
@@ -298,8 +314,11 @@ impl<C: Codec + Clone + 'static> Component for TextField<'_, C> {
|
||||
type Output = EditResponse<C::Value>;
|
||||
|
||||
/// The field's shortest width (`size.length.short`) is `measure`'s, so a content-sized field can go without it.
|
||||
fn style(cx: &crate::style::StyleCx<'_>, _: TextFieldStyleKey<C>) -> FieldStyle {
|
||||
fn style(cx: &crate::style::StyleCx<'_>, key: TextFieldStyleKey<C>) -> FieldStyle {
|
||||
let mut style = FieldStyle::at(cx);
|
||||
if let Some(chars) = key.width_chars {
|
||||
style = style.held_at(FieldStyle::chars_width(cx, chars));
|
||||
}
|
||||
style.metrics = style.metrics.min_size(Vec2::new(0.0, style.metrics.min_size.y));
|
||||
style
|
||||
}
|
||||
|
||||
@@ -136,6 +136,21 @@ fn text_field_adorned(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Fields that ask for a width in characters over the field length: a short one, the field length, and a wide one
|
||||
/// (a watermark's text), one over the other so their ends show the difference.
|
||||
#[story(TextField<'static, PlainText>, covers = [], drive = Drive::Never)]
|
||||
fn text_field_width_chars(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let mut text = data.value("text", "CONFIDENTIAL".to_owned()).clone();
|
||||
let shown = Stack::new().show(ui, |ui, _| {
|
||||
let short = TextField::new("Short, 12 characters", Bind::bound(&mut text)).width_chars(12).show(ui);
|
||||
TextField::new("Field length", Bind::bound(&mut text)).show(ui);
|
||||
TextField::new("Wide, 40 characters", Bind::bound(&mut text)).width_chars(40).show(ui);
|
||||
short
|
||||
});
|
||||
*data.value("text", String::new()) = text;
|
||||
shown.inner.map(StoryOutput::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
const NOTES: &str = "Shot list\nGolden hour, wide\nDetail of the hands\nThe crowd at the gate\nClosing frame on the sign";
|
||||
|
||||
/// A multi-line field that grows to four lines and then scrolls, and one that fills its room, both over one text and
|
||||
|
||||
@@ -31,6 +31,34 @@ fn a_small_field_is_narrower_than_a_standard_one() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A field asked for a width in characters takes it over the field length: 48 characters are wider than the field
|
||||
/// length, the width grows with the count, and `0` keeps the field length.
|
||||
#[trial]
|
||||
fn a_field_takes_the_width_of_the_characters_it_asks_for() {
|
||||
let mut frames = Frames::new();
|
||||
let shown = Cell::new([0.0; 4]);
|
||||
let mut text = String::from("CONFIDENTIAL");
|
||||
frames.idle(3, |ui| {
|
||||
let width = |ui: &mut egui::Ui, text: &mut String, salt: &str, chars: Option<u16>| {
|
||||
let field = TextField::new("Text", Bind::bound(text)).id_salt(salt);
|
||||
let field = match chars {
|
||||
Some(chars) => field.width_chars(chars),
|
||||
None => field,
|
||||
};
|
||||
field.show(ui).response.rect.width()
|
||||
};
|
||||
let plain = width(ui, &mut text, "plain", None);
|
||||
let zero = width(ui, &mut text, "zero", Some(0));
|
||||
let wide = width(ui, &mut text, "wide", Some(48));
|
||||
let wider = width(ui, &mut text, "wider", Some(64));
|
||||
shown.set([plain, zero, wide, wider]);
|
||||
});
|
||||
let [plain, zero, wide, wider] = shown.get();
|
||||
assert!((zero - plain).abs() < 0.01, "0 characters keep the field length: {zero} against {plain}");
|
||||
assert!(wide > plain, "48 characters are wider than the field length: {wide} against {plain}");
|
||||
assert!(wider > wide, "the width grows with the count: {wider} against {wide}");
|
||||
}
|
||||
|
||||
/// A small number field with a pop-up slider shows "100%" whole before its chevron, and is no wider than its content
|
||||
/// needs: under a number cell, the chevron and a quarter cell. A field over a long range keeps one cell.
|
||||
#[trial]
|
||||
|
||||
Reference in new issue
Block a user