mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
fix(ui): fit complete zoom percentages in the status bar
This commit is contained in:
@@ -219,7 +219,7 @@ impl<'a> StatusReadout<'a> {
|
||||
};
|
||||
let field = Field::new(words.readout_zoom.clone())
|
||||
.side(SideLabel::new().kind(LabelKind::Hidden))
|
||||
.show(ui, |ui, _| ZoomField::new(words.readout_zoom.clone(), current, document).scrub(true).show(ui));
|
||||
.show(ui, |ui, _| ZoomField::new(words.readout_zoom.clone(), current, document).scrub(true).status_bar().show(ui));
|
||||
let field = field.inner.map(|zoom| {
|
||||
invalid_zoom = zoom.invalid_zoom;
|
||||
events = zoom.view;
|
||||
|
||||
@@ -22,8 +22,8 @@ use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps};
|
||||
use craft_behavior::{
|
||||
Codec, ColumnKey, HitLevel, HitPriority, Metrics, NavMove, NumberCodec, PartKey, PressPhase, Sense, ValueDrag, decimals_for_step, wheel_step,
|
||||
};
|
||||
use craft_theme::{Stateful, States};
|
||||
use egui::{Rect, Vec2};
|
||||
use craft_theme::{Rgba, Stateful, States, over};
|
||||
use egui::{CornerRadius, Rect, Vec2};
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
@@ -128,6 +128,8 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option<f64>> {
|
||||
/// Whether the unit is drawn inside the field or left to the host to draw after it.
|
||||
#[prop]
|
||||
unit_placement: UnitPlacement,
|
||||
#[style_key]
|
||||
status_zoom: bool,
|
||||
/// The rows of the list a chevron after the field opens; Alt+Down opens it too.
|
||||
#[prop]
|
||||
presets: Option<&'a [Entry<NumberPick>]>,
|
||||
@@ -200,6 +202,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
stepper: None,
|
||||
unit: None,
|
||||
unit_placement: UnitPlacement::Inside,
|
||||
status_zoom: false,
|
||||
presets: None,
|
||||
popup_slider: None,
|
||||
snap_typed: false,
|
||||
@@ -234,6 +237,13 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Keeps the entire zoom percentage together in an unframed well in the status bar.
|
||||
#[must_use]
|
||||
pub(super) fn status_zoom(mut self) -> Self {
|
||||
self.status_zoom = true;
|
||||
self.sizing(Sizing::Content)
|
||||
}
|
||||
|
||||
/// A drag on the field's side label scrubs the value too, with or without the focus in the field.
|
||||
#[must_use]
|
||||
pub fn scrub_by_label(mut self) -> Self {
|
||||
@@ -257,7 +267,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
/// The unit drawn inside the field after the number: the field's own, else the symbol of its unit kind; none when
|
||||
/// the host draws it after the field.
|
||||
fn shown_unit(&self, units: &Units) -> Option<Label> {
|
||||
if self.unit_placement == UnitPlacement::After {
|
||||
if self.status_zoom || self.unit_placement == UnitPlacement::After {
|
||||
return None;
|
||||
}
|
||||
self.unit.clone().or_else(|| self.unit_kind.map(|kind| units.symbol(kind).into()))
|
||||
@@ -330,6 +340,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
decimals: self.commit_decimals,
|
||||
mixed,
|
||||
names,
|
||||
suffix: self.status_zoom.then(|| units.symbol(UnitKind::Percent)),
|
||||
scale: self.scale,
|
||||
value: PhantomData,
|
||||
}
|
||||
@@ -399,12 +410,24 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
|
||||
type Style = NumberStyle;
|
||||
type Output = NumberOutput<V>;
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: NumberFieldStyleKey<V>) -> NumberStyle {
|
||||
fn style(cx: &StyleCx<'_>, key: NumberFieldStyleKey<V>) -> NumberStyle {
|
||||
let field = FieldStyle::at(cx);
|
||||
let mut surface = field.surface.clone();
|
||||
if key.status_zoom {
|
||||
surface.kind = None;
|
||||
surface.radius = CornerRadius::ZERO;
|
||||
// Recess it slightly into the current chrome. Its focused and invalid borders still take their usual colors.
|
||||
let chrome = cx.tokens.color.surface.chrome;
|
||||
surface.fill = Stateful::new(cx.color(over(Rgba::BLACK.with_alpha(16), chrome)));
|
||||
let clear = cx.color(Rgba::TRANSPARENT);
|
||||
if let Some(border) = &mut surface.border {
|
||||
border.off = States::new(clear, clear, clear, clear);
|
||||
}
|
||||
}
|
||||
let faint = cx.color(cx.tokens.color.text.faint);
|
||||
let muted = cx.color(cx.tokens.color.text.muted);
|
||||
NumberStyle {
|
||||
surface: field.surface.clone(),
|
||||
surface,
|
||||
// The width is the value area, at most a number cell, and the adornments beside it, at least a cell
|
||||
// (`measure`); the style's minimum is only the height.
|
||||
metrics: field.clone().sized_by_content().metrics,
|
||||
|
||||
@@ -110,6 +110,7 @@ pub(super) struct Ruled<V> {
|
||||
pub(super) decimals: Option<usize>,
|
||||
pub(super) mixed: bool,
|
||||
pub(super) names: Vec<(u32, String)>,
|
||||
pub(super) suffix: Option<String>,
|
||||
/// How many shown units make one stored unit (1 for a field that shows what it holds).
|
||||
pub(super) scale: f64,
|
||||
pub(super) value: PhantomData<fn() -> V>,
|
||||
@@ -124,6 +125,7 @@ impl<V> Clone for Ruled<V> {
|
||||
decimals: self.decimals,
|
||||
mixed: self.mixed,
|
||||
names: self.names.clone(),
|
||||
suffix: self.suffix.clone(),
|
||||
scale: self.scale,
|
||||
value: PhantomData,
|
||||
}
|
||||
@@ -141,6 +143,19 @@ impl<V: NumberValue> Ruled<V> {
|
||||
v.number().filter(|number| number.is_finite()).map(|number| number * self.scale)
|
||||
}
|
||||
|
||||
fn with_suffix(&self, mut text: String) -> String {
|
||||
if let Some(suffix) = &self.suffix
|
||||
&& !text.is_empty()
|
||||
{
|
||||
text.push_str(suffix);
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
fn without_suffix<'a>(&self, text: &'a str) -> &'a str {
|
||||
self.suffix.as_deref().and_then(|suffix| text.trim_end().strip_suffix(suffix)).unwrap_or(text)
|
||||
}
|
||||
|
||||
/// The name `text` spells, as a value of the field's type.
|
||||
fn name_of(&self, text: &str) -> Option<V> {
|
||||
let text = text.trim();
|
||||
@@ -158,7 +173,7 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
}
|
||||
match v.named().and_then(|id| self.names.iter().find(|(name, _)| *name == id)) {
|
||||
Some((_, label)) => label.clone(),
|
||||
None => self.inner.format(&self.shown(v)),
|
||||
None => self.with_suffix(self.inner.format(&self.shown(v))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +190,7 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
if text.trim().is_empty() && (self.mixed || V::blank().is_none()) {
|
||||
return Err(NotAValue);
|
||||
}
|
||||
let (read, mut note) = self.inner.parse_tagged(text, &self.shown(start))?;
|
||||
let (read, mut note) = self.inner.parse_tagged(self.without_suffix(text), &self.shown(start))?;
|
||||
match read {
|
||||
Some(number) => {
|
||||
note.held = (number < self.rule.low || number > self.rule.high).then_some(number);
|
||||
@@ -186,7 +201,7 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
}
|
||||
|
||||
fn applies_as_typed(&self, text: &str) -> bool {
|
||||
!self.snap && !self.mixed && is_plain(text) && matches!(self.inner.parse(text, &None), Ok(Some(_)))
|
||||
!self.snap && !self.mixed && is_plain(text) && matches!(self.inner.parse(self.without_suffix(text), &None), Ok(Some(_)))
|
||||
}
|
||||
|
||||
fn filter(&self, c: char) -> bool {
|
||||
@@ -196,7 +211,7 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
fn normalise(&self, text: &str, v: &V) -> String {
|
||||
match v.named() {
|
||||
Some(_) => self.format(v),
|
||||
None => self.inner.normalise(text, &self.shown(v)),
|
||||
None => self.with_suffix(self.inner.normalise(self.without_suffix(text), &self.shown(v))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +225,7 @@ impl<V: NumberValue> Codec for Ruled<V> {
|
||||
// A field that rounds what it commits shows no more places than that.
|
||||
None if self.rule.low.is_finite() && self.rule.high.is_finite() => {
|
||||
let codec = self.decimals.map_or_else(|| self.inner.clone(), |places| self.inner.clone().at_most(places));
|
||||
Cow::Owned(codec.widest(&(self.rule.low..=self.rule.high)))
|
||||
Cow::Owned(self.with_suffix(codec.widest(&(self.rule.low..=self.rule.high))))
|
||||
}
|
||||
None => Cow::Owned(self.format(current)),
|
||||
}
|
||||
|
||||
@@ -32,12 +32,13 @@ pub struct ZoomField {
|
||||
stepper: bool,
|
||||
scrub: bool,
|
||||
enabled: bool,
|
||||
status_bar: bool,
|
||||
}
|
||||
|
||||
impl ZoomField {
|
||||
/// A zoom field named `label` over `view` (`None` when no view is known) of a document of `document` units.
|
||||
pub fn new(label: impl Into<Label>, view: Option<CanvasView>, document: Size) -> Self {
|
||||
ZoomField { label: label.into(), view, document, stepper: false, scrub: false, enabled: true }
|
||||
ZoomField { label: label.into(), view, document, stepper: false, scrub: false, enabled: true, status_bar: false }
|
||||
}
|
||||
|
||||
/// The arrow keys and the wheel step along the zoom ladder.
|
||||
@@ -61,6 +62,13 @@ impl ZoomField {
|
||||
self
|
||||
}
|
||||
|
||||
/// Uses the status bar's space and field appearance.
|
||||
#[must_use]
|
||||
pub(crate) fn status_bar(mut self) -> Self {
|
||||
self.status_bar = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows the field and reports the view changes it made.
|
||||
pub fn show(self, ui: &mut Ui) -> ZoomFieldOutput {
|
||||
let env = Env::of(ui);
|
||||
@@ -83,6 +91,9 @@ impl ZoomField {
|
||||
if self.scrub {
|
||||
field = field.scrub_by_label();
|
||||
}
|
||||
if self.status_bar {
|
||||
field = field.status_zoom();
|
||||
}
|
||||
let shown = field.show(ui);
|
||||
let invalid_zoom = range.filter(|_| shown.cut).map(|range| InvalidZoom { low: *range.start(), high: *range.end() });
|
||||
let view = match self.view {
|
||||
|
||||
@@ -322,6 +322,81 @@ fn a_narrow_bar_gives_way_from_its_end_so_the_message_elides_before_the_readout(
|
||||
assert!(!cut(&narrow, "Doc"), "the readout's text was cut while the message after it could still give way");
|
||||
}
|
||||
|
||||
fn status_zoom(zoom: f64, comma: bool) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let size = Size::new(2000.0, 1500.0);
|
||||
let canvas = Rect::from_min_max(egui::Pos2::ZERO, egui::Pos2::new(800.0, 600.0));
|
||||
let scale = craft_ui::runtime::env::Env::of(ui).scale.0;
|
||||
let view = CanvasView::new(canvas, Point::new(1000.0, 750.0), zoom / f64::from(scale));
|
||||
Bar::new("Status", BarKind::Status).show(ui, |ui, _| {
|
||||
StatusReadout::new(Bind::given(view), size, StatusItem::DocumentSizes, &StatusValues::default()).show(ui);
|
||||
});
|
||||
});
|
||||
if comma {
|
||||
use craft_host::{AppInfo, Host};
|
||||
use std::sync::Arc;
|
||||
let (i18n, problems) = craft_i18n::I18n::from_owned([("de".to_owned(), String::new(), String::new())], &["de"]);
|
||||
assert!(problems.is_empty(), "{problems:?}");
|
||||
i18n.set_format(craft_i18n::Format::from_tag("de"));
|
||||
fixture.host(Arc::new(Host::builder(AppInfo { id: "trial", name: "Trial", version: "0" }).i18n(Arc::new(i18n)).build()));
|
||||
}
|
||||
fixture.room(Vec2::new(600.0, 80.0)).settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_status_zoom_shows_its_complete_value_and_percent() {
|
||||
for (zoom, comma, expected) in [(0.7969, false, "79.69%"), (0.7969, true, "79,69%"), (32.0, false, "3200%"), (31.9999, false, "3199.99%")] {
|
||||
let fixture = status_zoom(zoom, comma);
|
||||
let number = fixture.texts().iter().find(|text| text.text == expected).expect(expected);
|
||||
assert!(
|
||||
!number.elided && number.rect.min.x + SLACK >= number.clip.min.x && number.rect.max.x <= number.clip.max.x + SLACK,
|
||||
"{expected} is clipped: {:?} outside {:?}",
|
||||
number.rect,
|
||||
number.clip,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_status_zoom_still_scrubs_and_applies_edited_text() {
|
||||
use egui::{Event, Key, Modifiers};
|
||||
|
||||
let view = Rc::new(Cell::new(CanvasView::new(Rect::from_min_size(egui::Pos2::ZERO, Vec2::new(800.0, 600.0)), Point::ZERO, 1.0)));
|
||||
let shown = view.clone();
|
||||
let committed = Rc::new(Cell::new(None));
|
||||
let seen = committed.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut view = shown.get();
|
||||
Bar::new("Status", BarKind::Status).show(ui, |ui, _| {
|
||||
let output =
|
||||
StatusReadout::new(Bind::bound(&mut view), Size::new(2000.0, 1500.0), StatusItem::DocumentSizes, &StatusValues::default()).show(ui);
|
||||
if let Some(view) = output.view.committed() {
|
||||
seen.set(Some(view.zoom));
|
||||
}
|
||||
});
|
||||
shown.set(view);
|
||||
});
|
||||
fixture.room(Vec2::new(600.0, 80.0)).settle();
|
||||
let zoom = fixture.by_label("Zoom").expect("the status zoom field");
|
||||
fixture.drag(&zoom, Vec2::new(12.0, 0.0)).settle();
|
||||
let scrubbed = view.get().zoom;
|
||||
assert!(scrubbed > 1.0, "the status bar still scrubs the zoom to the right");
|
||||
|
||||
fixture.click(&zoom).settle();
|
||||
fixture.key(Key::A, Modifiers::COMMAND);
|
||||
fixture.frame(vec![Event::Paste("079.69%".to_owned())]).settle();
|
||||
let before_tab = view.get().zoom;
|
||||
committed.set(None);
|
||||
fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
assert_eq!(view.get().zoom, before_tab, "Tab does not change the current zoom");
|
||||
assert_eq!(committed.get(), None, "Tab does not commit the draft");
|
||||
let zoom = fixture.by_label("Zoom").expect("the status zoom field");
|
||||
assert_eq!(fixture.tree().value_of(zoom.node), Some("79.69%"), "Tab rewrites the draft with its suffix");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert!(committed.get().is_some_and(|value| (value - 0.7969).abs() < 1e-7), "Enter commits the percent-bearing zoom");
|
||||
}
|
||||
|
||||
/// A Row in a Bar under right-to-left measures in a room unbounded at its start edge, the left one: a labelled check
|
||||
/// box there lays out with finite rects inside the bar, after the button before it in reading order. Guards against a
|
||||
/// label row built from the room's start and width, which was NaN there.
|
||||
|
||||
Reference in New Issue
Block a user