mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
A unit select names its unit once: the number field reads the unit's spellings without a suffix, and the unit lists show the units' names (Units::name)
This commit is contained in:
@@ -410,7 +410,7 @@ fn body(
|
||||
typed: &mut Option<RangeAlert>,
|
||||
) -> bool {
|
||||
let document = values.units.unwrap_or_else(pixels_only);
|
||||
let entries: Vec<Entry<UnitKind>> = length_units(values).map(|kind| Entry::new(kind, Label::from(numbers.symbol(kind)))).collect();
|
||||
let entries: Vec<Entry<UnitKind>> = length_units(values).map(|kind| Entry::new(kind, numbers.name(kind))).collect();
|
||||
let kind = values.kind;
|
||||
FieldGroup::new(title_of(kind, words)).show(ui, |ui, _| {
|
||||
let (width, height) = (Label::from(words.width.as_str().to_owned()), Label::from(words.height.as_str().to_owned()));
|
||||
|
||||
+41
-2
@@ -1,7 +1,7 @@
|
||||
//! `Units`: what typed number input and unit readouts need from the env (core 2.1): the locale's separators and the
|
||||
//! unit spellings of a number's kind in the user's language, and the file sizes and durations written with them.
|
||||
|
||||
use super::EnvValue;
|
||||
use super::{EnvValue, Label};
|
||||
use craft_behavior::NumberCodec;
|
||||
use craft_behavior::quantity::{Reader, is_unit_spelling};
|
||||
use craft_i18n::{FileSize, Format, WordSet as _, Words};
|
||||
@@ -10,7 +10,8 @@ use std::ops::RangeInclusive;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
craft_ui::words! {
|
||||
/// The unit words: `{n}` is where the number goes.
|
||||
/// The unit words: `{n}` is where the number goes. The `_name` words name a unit in a list of units (a unit
|
||||
/// select), without a number.
|
||||
pub struct UnitWords {
|
||||
pixels: unit "units.pixels" = "{n} px";
|
||||
percent: unit "units.percent" = "{n}%";
|
||||
@@ -28,6 +29,21 @@ craft_ui::words! {
|
||||
lines_per_centimetre: unit "units.linesPerCentimetre" = "{n} lpcm";
|
||||
minutes: unit "units.minutes" = "{n} min";
|
||||
columns: unit "units.columns" = "{n} col";
|
||||
pixels_name: "units.pixelsName" = "Pixels";
|
||||
percent_name: "units.percentName" = "Percent";
|
||||
degrees_name: "units.degreesName" = "Degrees";
|
||||
points_name: "units.pointsName" = "Points";
|
||||
times_name: "units.timesName" = "Times";
|
||||
inches_name: "units.inchesName" = "Inches";
|
||||
centimetres_name: "units.centimetresName" = "Centimetres";
|
||||
millimetres_name: "units.millimetresName" = "Millimetres";
|
||||
picas_name: "units.picasName" = "Picas";
|
||||
pixels_per_inch_name: "units.pixelsPerInchName" = "Pixels/Inch";
|
||||
pixels_per_centimetre_name: "units.pixelsPerCentimetreName" = "Pixels/Centimetre";
|
||||
minutes_name: "units.minutesName" = "Minutes";
|
||||
columns_name: "units.columnsName" = "Columns";
|
||||
lines_per_inch_name: "units.linesPerInchName" = "Lines/Inch";
|
||||
lines_per_centimetre_name: "units.linesPerCentimetreName" = "Lines/Centimetre";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,6 +358,29 @@ impl Units {
|
||||
}
|
||||
}
|
||||
|
||||
/// The unit's name in the user's language, as a list of units shows it: Pixels, Pixels/Inch.
|
||||
pub fn name(&self, kind: UnitKind) -> Label {
|
||||
let words = &*self.words;
|
||||
let name = match kind {
|
||||
UnitKind::Pixels => &words.pixels_name,
|
||||
UnitKind::Percent => &words.percent_name,
|
||||
UnitKind::Degrees => &words.degrees_name,
|
||||
UnitKind::Points => &words.points_name,
|
||||
UnitKind::Times => &words.times_name,
|
||||
UnitKind::Inches => &words.inches_name,
|
||||
UnitKind::Centimetres => &words.centimetres_name,
|
||||
UnitKind::Millimetres => &words.millimetres_name,
|
||||
UnitKind::Picas => &words.picas_name,
|
||||
UnitKind::PixelsPerInch => &words.pixels_per_inch_name,
|
||||
UnitKind::PixelsPerCentimetre => &words.pixels_per_centimetre_name,
|
||||
UnitKind::Minutes => &words.minutes_name,
|
||||
UnitKind::Columns => &words.columns_name,
|
||||
UnitKind::LinesPerInch => &words.lines_per_inch_name,
|
||||
UnitKind::LinesPerCentimetre => &words.lines_per_centimetre_name,
|
||||
};
|
||||
name.clone()
|
||||
}
|
||||
|
||||
/// The unit's spelling in the user's language without the number: `mm`, `px`.
|
||||
pub fn symbol(&self, kind: UnitKind) -> String {
|
||||
self.template(kind).replace("{n}", "").trim().to_owned()
|
||||
|
||||
@@ -80,9 +80,9 @@ fn apply_number(session: &mut Session, events: &EditEvents<Option<f64>>, set: im
|
||||
}
|
||||
}
|
||||
|
||||
/// The entries of a unit list, named by the units' symbols.
|
||||
/// The entries of a unit list, named by the units' names.
|
||||
fn unit_entries(read: &Read<'_>, kinds: &[UnitKind]) -> Vec<Entry<UnitKind>> {
|
||||
kinds.iter().map(|kind| Entry::new(*kind, read.units.symbol(*kind))).collect()
|
||||
kinds.iter().map(|kind| Entry::new(*kind, read.units.name(*kind))).collect()
|
||||
}
|
||||
|
||||
/// Shows the column.
|
||||
|
||||
@@ -333,8 +333,8 @@ fn metadata_row(ui: &mut Ui, words: &SaveForWebWords, options: &mut SaveForWebOp
|
||||
/// Image Size: Width and Height in pixels or percent, the Percent row and the Resample method.
|
||||
fn size_section(ui: &mut Ui, words: &SaveForWebWords, sizing: &SizingWords, units: &Units, link: &mut LinkedSize, resample: &mut Resample) {
|
||||
let kinds = [
|
||||
Entry::new(UnitKind::Pixels, Label::from(units.symbol(UnitKind::Pixels))),
|
||||
Entry::new(UnitKind::Percent, Label::from(units.symbol(UnitKind::Percent))),
|
||||
Entry::new(UnitKind::Pixels, units.name(UnitKind::Pixels)),
|
||||
Entry::new(UnitKind::Percent, units.name(UnitKind::Percent)),
|
||||
];
|
||||
Section::fixed(words.image_size.clone()).id_salt("size").show(ui, |ui, _| {
|
||||
row(ui, &words.width, "width", |ui| {
|
||||
|
||||
@@ -334,7 +334,7 @@ impl Kit {
|
||||
let env = Env::of(ui);
|
||||
let units = env.units();
|
||||
let entries =
|
||||
|kinds: &[UnitKind]| -> Vec<Entry<UnitKind>> { kinds.iter().map(|kind| Entry::new(*kind, Label::from(units.symbol(*kind)))).collect() };
|
||||
|kinds: &[UnitKind]| -> Vec<Entry<UnitKind>> { kinds.iter().map(|kind| Entry::new(*kind, units.name(*kind))).collect() };
|
||||
let lengths = entries(&LENGTH_UNITS);
|
||||
let mut widths = lengths.clone();
|
||||
widths.extend(entries(&[UnitKind::Columns]));
|
||||
|
||||
@@ -4,10 +4,10 @@ use super::select::Select;
|
||||
use crate::contract::{
|
||||
Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Entry, MeasureCx, Outcome, PaintUi, Props as _, Role, WordSetCx as _,
|
||||
};
|
||||
use crate::runtime::env::{Label, UnitKind};
|
||||
use crate::runtime::env::{Label, UnitKind, Units};
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use crate::text::{NumberField, RangeAlert};
|
||||
use craft_behavior::{EditEvents, Flow, Metrics, Mixed, Proposal, Sense};
|
||||
use craft_behavior::{EditEvents, Flow, Metrics, Mixed, NumberCodec, Proposal, Sense};
|
||||
use egui::{Rect, Response, Vec2, pos2};
|
||||
use std::ops::{Deref, RangeInclusive};
|
||||
|
||||
@@ -59,9 +59,10 @@ impl Outcome for UnitSelectOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// A number field with a select of units after it. The select picks the unit kind the field reads typed text in and
|
||||
/// shows its number with, and the kind sets the digits and the step of the number; the number itself is the field's value, and the unit goes to its own binding, so the app
|
||||
/// converts the number when the unit changes.
|
||||
/// A number field with a select of units after it. The select picks the unit kind the field reads typed text in
|
||||
/// (its spellings: "12 mm") and shows its number with, and the kind sets the digits and the step of the number; the
|
||||
/// select names the unit, so the field shows the number alone. The number itself is the field's value, and the unit
|
||||
/// goes to its own binding, so the app converts the number when the unit changes.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component]
|
||||
pub struct UnitSelect<'a> {
|
||||
@@ -102,7 +103,7 @@ impl<'a> UnitSelect<'a> {
|
||||
.label(label)
|
||||
}
|
||||
|
||||
fn number(&self, name: Label, value: Mixed<Option<f64>>, kind: Option<UnitKind>) -> NumberField<'static> {
|
||||
fn number(&self, name: Label, value: Mixed<Option<f64>>, kind: Option<(UnitKind, &Units)>) -> NumberField<'static> {
|
||||
let field = NumberField::new(name, Bind::given(value));
|
||||
let field = match self.number_enabled {
|
||||
Some(on) => field.enabled(on),
|
||||
@@ -113,7 +114,7 @@ impl<'a> UnitSelect<'a> {
|
||||
None => field,
|
||||
};
|
||||
match kind {
|
||||
Some(kind) => field.unit_kind(kind).step(kind.step()),
|
||||
Some((kind, units)) => field.codec(NumberCodec::new(units.reader(Some(kind)), *kind.decimals().end())).step(kind.step()),
|
||||
None => field,
|
||||
}
|
||||
}
|
||||
@@ -172,7 +173,8 @@ impl Component for UnitSelect<'_> {
|
||||
};
|
||||
let shown = shown_of(cx.value());
|
||||
let start = cx.value().and_then(|value| *value);
|
||||
let number = ui.child("number", place(number_rect), self.number(group.clone(), shown, kind));
|
||||
let units = cx.env.units();
|
||||
let number = ui.child("number", place(number_rect), self.number(group.clone(), shown, kind.map(|kind| (kind, &units))));
|
||||
cx.adopt(&number, &start, |value, mine| *mine = *value);
|
||||
self.out_of_range = number.out_of_range;
|
||||
let select = Select::new(self.select_name(&group), self.unit.reborrowed(), self.units);
|
||||
|
||||
@@ -8,7 +8,7 @@ use craft_behavior::Mixed;
|
||||
use egui::Ui;
|
||||
|
||||
fn units() -> [Entry<UnitKind>; 3] {
|
||||
[Entry::new(UnitKind::Millimetres, "mm"), Entry::new(UnitKind::Inches, "in"), Entry::new(UnitKind::Points, "pt")]
|
||||
[Entry::new(UnitKind::Millimetres, "Millimetres"), Entry::new(UnitKind::Inches, "Inches"), Entry::new(UnitKind::Points, "Points")]
|
||||
}
|
||||
|
||||
#[story(UnitSelect<'static>, covers = [Default], drive = Drive::Type("42"))]
|
||||
|
||||
Reference in New Issue
Block a user