mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #3627 from dotCooCoo/fix/ruler-feet-inches
ui-canvas rulers: the units menu can offer Feet & Inches beside Feet
This commit is contained in:
4 files changed
+100
-14
No files matched your search
@@ -506,8 +506,10 @@ impl Component for Rulers<'_> {
|
||||
let numbers = cx.env.units();
|
||||
let words = cx.words::<CanvasWords>();
|
||||
let shown = self.published().x.kind;
|
||||
// A menu row's `&` marks its mnemonic, so the `&` of a unit's name ("Feet & Inches") is doubled to show as written.
|
||||
let row_name = |kind: UnitKind| Label::from(numbers.name(kind).as_str().replace('&', "&&"));
|
||||
let mut entries: Vec<Entry<MenuRow>> =
|
||||
units.iter().map(|&kind| Entry::new(MenuRow::Unit(kind), numbers.name(kind)).checked(kind == shown)).collect();
|
||||
units.iter().map(|&kind| Entry::new(MenuRow::Unit(kind), row_name(kind)).checked(kind == shown)).collect();
|
||||
entries.push(Entry::separator());
|
||||
entries.push(Entry::new(MenuRow::ResetZero, Label::from(words.ruler_reset_zero.as_str().to_owned())).enabled(!self.origin_locked));
|
||||
let name = Label::from(words.ruler_menu.as_str().to_owned());
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use craft_behavior::geometry::{SnapOptions, SnapTargets};
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use craft_behavior::{CancelReason, CanvasView, RulerUnits, TickLabels, Ticks};
|
||||
use craft_behavior::{CancelReason, CanvasView, Mixed, RulerUnits, TickLabels, Ticks};
|
||||
use craft_ui::canvas::{
|
||||
CENTIMETRE_LABEL_STEP_MIN, Canvas, CanvasTool, INCH_EIGHTHS, INCH_SIXTEENTHS, LABEL_STEP_MIN, PageFrame, Pasteboard, RulerText, Rulers, Snap,
|
||||
StepLadder, TickRule, ToolAction, ToolContext, ToolPath, ToolPointer,
|
||||
@@ -268,6 +268,43 @@ fn the_units_menu_names_each_unit() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A ruler counting Feet & Inches checks the Feet & Inches row of the units menu, not the Feet row beside it, and
|
||||
/// picking Feet & Inches reports that unit rather than Feet.
|
||||
#[trial]
|
||||
fn the_units_menu_tells_feet_and_inches_from_feet() {
|
||||
const UNITS: [UnitKind; 3] = [UnitKind::Inches, UnitKind::Feet, UnitKind::FeetInches];
|
||||
let text = RulerText::new(RulerUnits::new(1.0 / 864.0, Ticks::decimal(false)), UnitKind::FeetInches);
|
||||
let zero = Rc::new(RefCell::new(Point::ZERO));
|
||||
let shown_zero = Rc::clone(&zero);
|
||||
let room = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let shown_room = Rc::clone(&room);
|
||||
let picked = Rc::new(Cell::new(None));
|
||||
let seen = Rc::clone(&picked);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let at = ui.available_rect_before_wrap();
|
||||
shown_room.set(at);
|
||||
let view = CanvasView::new(at, Point::new(f64::from(at.center().x), f64::from(at.center().y)), 1.0);
|
||||
let mut zero = shown_zero.borrow_mut();
|
||||
let rulers = Rulers::new("Rulers", Bind::bound(&mut *zero)).units(&UNITS);
|
||||
let shown = Canvas::new("Document").view(view).with_rulers(Some(rulers)).rulers(text, text).show(ui, |_, _| {});
|
||||
if let Some(kind) = shown.output.rulers.and_then(|rulers| rulers.unit_picked) {
|
||||
seen.set(Some(kind));
|
||||
}
|
||||
});
|
||||
fixture.room(Vec2::new(500.0, 400.0)).settle();
|
||||
let at = egui::pos2(room.get().center().x, room.get().min.y + 4.0);
|
||||
fixture.frame(vec![craft_ui::testing::pointer::moved(at)]);
|
||||
fixture.frame(vec![craft_ui::testing::pointer::secondary(at, true)]);
|
||||
fixture.frame(vec![craft_ui::testing::pointer::secondary(at, false)]);
|
||||
fixture.settle();
|
||||
let feet_inches = fixture.by_label("Feet & Inches").expect("the menu lists Feet & Inches");
|
||||
let feet = fixture.by_label("Feet").expect("the menu lists Feet");
|
||||
assert_eq!(fixture.tree().toggled_of(feet_inches.node), Some(Mixed::Same(true)), "the ruler's unit is checked");
|
||||
assert_ne!(fixture.tree().toggled_of(feet.node), Some(Mixed::Same(true)), "Feet is not checked for a Feet & Inches ruler");
|
||||
fixture.click(&feet_inches).settle();
|
||||
assert_eq!(picked.get(), Some(UnitKind::FeetInches), "the pick names Feet & Inches");
|
||||
}
|
||||
|
||||
/// A drag of the rulers' corner with one move past the drag threshold, released there, moves the zero point to where it
|
||||
/// was released: the drag proposes its landing at its start and at its release, not only on the moves between.
|
||||
#[trial]
|
||||
|
||||
+28
-12
@@ -54,6 +54,7 @@ craft_ui::words! {
|
||||
lines_per_inch_name: "units.linesPerInchName" = "Lines/Inch";
|
||||
lines_per_centimetre_name: "units.linesPerCentimetreName" = "Lines/Centimetre";
|
||||
feet_name: "units.feetName" = "Feet";
|
||||
feet_inches_name: "units.feetInchesName" = "Feet & Inches";
|
||||
yards_name: "units.yardsName" = "Yards";
|
||||
metres_name: "units.metresName" = "Metres";
|
||||
q_name: "units.qName" = "Q";
|
||||
@@ -160,6 +161,10 @@ pub enum UnitKind {
|
||||
LinesPerCentimetre,
|
||||
/// `{n} ft`.
|
||||
Feet,
|
||||
/// `{n} ft`: the Feet & Inches unit of a document. Its numbers are feet, written and read as [`Feet`](Self::Feet)
|
||||
/// writes and reads them; only its name differs, so a list of units can offer both and check the one a document
|
||||
/// uses.
|
||||
FeetInches,
|
||||
/// `{n} yd`.
|
||||
Yards,
|
||||
/// `{n} m`.
|
||||
@@ -190,7 +195,7 @@ impl UnitKind {
|
||||
Self::ALL.get(tag as usize).copied()
|
||||
}
|
||||
|
||||
const ALL: [UnitKind; 24] = [
|
||||
const ALL: [UnitKind; 25] = [
|
||||
UnitKind::Pixels,
|
||||
UnitKind::Percent,
|
||||
UnitKind::Degrees,
|
||||
@@ -207,6 +212,7 @@ impl UnitKind {
|
||||
UnitKind::LinesPerInch,
|
||||
UnitKind::LinesPerCentimetre,
|
||||
UnitKind::Feet,
|
||||
UnitKind::FeetInches,
|
||||
UnitKind::Yards,
|
||||
UnitKind::Metres,
|
||||
UnitKind::Q,
|
||||
@@ -223,7 +229,7 @@ impl UnitKind {
|
||||
UnitKind::Pixels | UnitKind::Minutes | UnitKind::Letters | UnitKind::Hyphens | UnitKind::Milliseconds => 0..=0,
|
||||
UnitKind::Percent | UnitKind::Degrees | UnitKind::Points => 0..=1,
|
||||
UnitKind::Q | UnitKind::Ha => 0..=2,
|
||||
UnitKind::Feet | UnitKind::Yards | UnitKind::Metres => 0..=4,
|
||||
UnitKind::Feet | UnitKind::FeetInches | UnitKind::Yards | UnitKind::Metres => 0..=4,
|
||||
UnitKind::Times
|
||||
| UnitKind::Centimetres
|
||||
| UnitKind::Millimetres
|
||||
@@ -241,7 +247,13 @@ impl UnitKind {
|
||||
/// the kind steps by the kind's rule.
|
||||
pub const fn step(self) -> f64 {
|
||||
match self {
|
||||
UnitKind::Times | UnitKind::Inches | UnitKind::Centimetres | UnitKind::Feet | UnitKind::Yards | UnitKind::Metres => 0.1,
|
||||
UnitKind::Times
|
||||
| UnitKind::Inches
|
||||
| UnitKind::Centimetres
|
||||
| UnitKind::Feet
|
||||
| UnitKind::FeetInches
|
||||
| UnitKind::Yards
|
||||
| UnitKind::Metres => 0.1,
|
||||
_ => 1.0,
|
||||
}
|
||||
}
|
||||
@@ -258,7 +270,7 @@ impl UnitKind {
|
||||
UnitKind::Millimetres => Some(25.4),
|
||||
UnitKind::Points => Some(points_per_inch),
|
||||
UnitKind::Picas => Some(points_per_inch / 12.0),
|
||||
UnitKind::Feet => Some(1.0 / 12.0),
|
||||
UnitKind::Feet | UnitKind::FeetInches => Some(1.0 / 12.0),
|
||||
UnitKind::Yards => Some(1.0 / 36.0),
|
||||
UnitKind::Metres => Some(0.0254),
|
||||
UnitKind::Q | UnitKind::Ha => Some(101.6),
|
||||
@@ -473,7 +485,7 @@ impl Units {
|
||||
UnitKind::Columns => words.columns.as_str(),
|
||||
UnitKind::LinesPerInch => words.lines_per_inch.as_str(),
|
||||
UnitKind::LinesPerCentimetre => words.lines_per_centimetre.as_str(),
|
||||
UnitKind::Feet => words.feet.as_str(),
|
||||
UnitKind::Feet | UnitKind::FeetInches => words.feet.as_str(),
|
||||
UnitKind::Yards => words.yards.as_str(),
|
||||
UnitKind::Metres => words.metres.as_str(),
|
||||
UnitKind::Q => words.q.as_str(),
|
||||
@@ -505,6 +517,7 @@ impl Units {
|
||||
UnitKind::LinesPerInch => &words.lines_per_inch_name,
|
||||
UnitKind::LinesPerCentimetre => &words.lines_per_centimetre_name,
|
||||
UnitKind::Feet => &words.feet_name,
|
||||
UnitKind::FeetInches => &words.feet_inches_name,
|
||||
UnitKind::Yards => &words.yards_name,
|
||||
UnitKind::Metres => &words.metres_name,
|
||||
UnitKind::Q => &words.q_name,
|
||||
@@ -641,7 +654,7 @@ impl Units {
|
||||
pub fn length_reader(&self, lengths: LengthUnits) -> Reader {
|
||||
let Some(shown) = self.length_per_inch(lengths.shown, lengths.pixels_per_inch) else { return self.reader(None) };
|
||||
let factor = |other: UnitKind| Some(shown / self.length_per_inch(other, lengths.pixels_per_inch)?);
|
||||
let reader = self.reader_over(factor);
|
||||
let reader = self.reader_over(Some(lengths.shown), factor);
|
||||
match lengths.bare.and_then(factor) {
|
||||
Some(bare) => reader.bare(bare),
|
||||
None => reader,
|
||||
@@ -654,8 +667,8 @@ impl Units {
|
||||
}
|
||||
|
||||
fn build_reader(&self, kind: Option<UnitKind>, points_per_inch: f64) -> Reader {
|
||||
let Some(kind) = kind else { return self.reader_over(|_| None) };
|
||||
self.reader_over(|other| match (kind.per_inch(points_per_inch), other.per_inch(points_per_inch)) {
|
||||
let Some(kind) = kind else { return self.reader_over(None, |_| None) };
|
||||
self.reader_over(Some(kind), |other| match (kind.per_inch(points_per_inch), other.per_inch(points_per_inch)) {
|
||||
(Some(of), Some(per)) => Some(of / per),
|
||||
_ if other == kind => Some(1.0),
|
||||
_ => None,
|
||||
@@ -669,12 +682,13 @@ impl Units {
|
||||
pub fn document_reader(&self, ruler: UnitKind, extent: f64, pixels_per_inch: f64, points_per_inch: f64) -> Reader {
|
||||
let pixels = |kind: UnitKind| kind.to_pixels(1.0, extent, pixels_per_inch, points_per_inch);
|
||||
let one_ruler = pixels(ruler).filter(|one| *one != 0.0);
|
||||
self.reader_over(|other| Some(pixels(other)? / one_ruler?))
|
||||
self.reader_over(Some(ruler), |other| Some(pixels(other)? / one_ruler?))
|
||||
}
|
||||
|
||||
/// The locale's separators and, for every unit kind `factor` gives a factor to, its spellings (in the user's
|
||||
/// language and in English) reading as that many of the reader's own unit.
|
||||
fn reader_over(&self, factor: impl Fn(UnitKind) -> Option<f64>) -> Reader {
|
||||
/// language and in English) reading as that many of the reader's own unit. `own`'s spellings come first, so a
|
||||
/// spelling two kinds share (`ft` for Feet and Feet & Inches) names `own` when it is one of them.
|
||||
fn reader_over(&self, own: Option<UnitKind>, factor: impl Fn(UnitKind) -> Option<f64>) -> Reader {
|
||||
let (decimal, group) = self.format.separators();
|
||||
let mut reader = Reader::new().decimal(decimal);
|
||||
if let Some(group) = group {
|
||||
@@ -682,7 +696,9 @@ impl Units {
|
||||
}
|
||||
let english = Units::new(self.format.clone(), english_words(), Arc::new(Words::english()), self.point_size);
|
||||
let points = factor(UnitKind::Points);
|
||||
for (tag, other) in UnitKind::ALL.into_iter().enumerate() {
|
||||
let order = own.into_iter().chain(UnitKind::ALL.into_iter().filter(|other| Some(*other) != own));
|
||||
for other in order {
|
||||
let Some(tag) = UnitKind::ALL.iter().position(|kind| *kind == other) else { continue };
|
||||
let Some(factor) = factor(other) else { continue };
|
||||
for spelling in [self.symbol(other), english.symbol(other)] {
|
||||
if is_unit_spelling(&spelling) {
|
||||
|
||||
@@ -64,6 +64,37 @@ fn a_length_field_types_and_steps_in_the_shown_unit() {
|
||||
assert!((stepped - tenth_up).abs() < SAME, "an arrow moves a tenth of an inch: {stepped} against {tenth_up}");
|
||||
}
|
||||
|
||||
/// A Feet & Inches length is written, typed and stepped as feet, as a Feet length is, and only its name tells the two
|
||||
/// apart.
|
||||
#[trial]
|
||||
fn a_feet_and_inches_length_counts_feet() {
|
||||
let feet = LengthUnits::new(UnitKind::Points, UnitKind::Feet, START);
|
||||
let feet_inches = LengthUnits::new(UnitKind::Points, UnitKind::FeetInches, START);
|
||||
for text in ["1.5", "18 in", "1.5 ft"] {
|
||||
let plain = in_lengths(feet, START, type_and_commit(text));
|
||||
let both = in_lengths(feet_inches, START, type_and_commit(text));
|
||||
assert!((both - plain).abs() < SAME && both != START, "{text} reads as feet: {both} against {plain}");
|
||||
}
|
||||
let stepped = |lengths: LengthUnits| {
|
||||
in_lengths(lengths, START, |frames, show| {
|
||||
frames.frame(vec![key_press(Key::ArrowUp, Modifiers::NONE)], show);
|
||||
})
|
||||
};
|
||||
assert!((stepped(feet_inches) - stepped(feet)).abs() < SAME, "an arrow steps both by the same tenth of a foot");
|
||||
|
||||
let mut frames = Frames::new();
|
||||
frames.frame(vec![], |ui| {
|
||||
let units = Env::of(ui).units();
|
||||
assert_eq!(units.show(UnitKind::FeetInches, 1.5), units.show(UnitKind::Feet, 1.5), "both write feet");
|
||||
assert_ne!(units.name(UnitKind::FeetInches), units.name(UnitKind::Feet), "a list of units tells them apart");
|
||||
for kind in [UnitKind::Feet, UnitKind::FeetInches] {
|
||||
let reader = units.length_reader(LengthUnits::new(UnitKind::Points, kind, START));
|
||||
let named = reader.read_tagged("1.5 ft", None).ok().and_then(|(_, tag)| tag).and_then(UnitKind::from_tag);
|
||||
assert_eq!(named, Some(kind), "a typed ft names the field's own unit");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// With bare numbers in points, a bare number in a picas field reads as points; a spelled unit still reads as written.
|
||||
#[trial]
|
||||
fn a_bare_number_reads_in_the_lengths_bare_unit() {
|
||||
|
||||
Reference in new issue
Block a user