mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
labels: GlyphColon::Form gives a glyph caption (GlyphLabel::caption) a form's colon; draw's X/Y/W/H are captions
This commit is contained in:
@@ -87,7 +87,7 @@ impl<'a> ArtboardProperties<'a> {
|
||||
});
|
||||
// A glyph-labelled number field; the pairs share a row, as W and H in the shape panel.
|
||||
let field = |ui: &mut Ui, glyph: &Label, name: &Label, value: f64, range: &RangeInclusive<f64>| {
|
||||
let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(glyph.clone()))).scrub(true);
|
||||
let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::caption(glyph.clone()))).scrub(true);
|
||||
Field::new(name.clone())
|
||||
.side(side)
|
||||
.show(ui, |ui, _| {
|
||||
|
||||
@@ -527,7 +527,7 @@ impl<'p, 'a> Cells<'p, 'a> {
|
||||
|
||||
/// The side label of a cell shown by a short glyph (W, H) instead of its caption, which scrubs it.
|
||||
pub(super) fn glyph_side(glyph: &Label) -> SideLabel {
|
||||
SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(glyph.clone()))).scrub(true)
|
||||
SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::caption(glyph.clone()))).scrub(true)
|
||||
}
|
||||
|
||||
/// The change a Sides value makes: a whole number of sides in range.
|
||||
|
||||
@@ -503,7 +503,7 @@ impl Cells<'_> {
|
||||
/// inside the field.
|
||||
fn side_and_name(&self, field: TransformField, form: Form) -> (SideLabel, Label) {
|
||||
let words = &self.words;
|
||||
let beside = |glyph: &Label| SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(glyph.clone())));
|
||||
let beside = |glyph: &Label| SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::caption(glyph.clone())));
|
||||
let (side, name) = match field {
|
||||
TransformField::X => (beside(&words.x_glyph), &words.x_name),
|
||||
TransformField::Y => (beside(&words.y_glyph), &words.y_name),
|
||||
|
||||
@@ -13,5 +13,5 @@ mod step;
|
||||
pub use leaf::colon_of;
|
||||
pub(crate) use place::{control_after, label_gap};
|
||||
pub(crate) use props::inside;
|
||||
pub use props::{CaptionColumn, CellWidth, FieldLayout, GlyphLabel, InsideLabel, LabelKind, LabelPress, LabelScheme, Scrub, SideLabel};
|
||||
pub use props::{CaptionColumn, CellWidth, FieldLayout, GlyphColon, GlyphLabel, InsideLabel, LabelKind, LabelPress, LabelScheme, Scrub, SideLabel};
|
||||
pub(crate) use step::{LabelIn, LeafLabel, Trailing, flow_leaf, in_column, leaf, leaf_under, report_after_reach, run, show_leaf_in_flow};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! The side label as a leaf component: its node, press state, reset dot and colon.
|
||||
|
||||
use super::props::{FieldLayout, Glyph, LabelKind, LabelScheme};
|
||||
use super::props::{FieldLayout, Glyph, GlyphColon, LabelKind, LabelScheme};
|
||||
use super::step::LabelIn;
|
||||
use crate::contract::InBar;
|
||||
use crate::contract::{Component, Props};
|
||||
@@ -411,18 +411,20 @@ impl Component for LabelLeaf {
|
||||
}
|
||||
|
||||
/// The colon a beside label draws after its shown text in a form (`LabelScheme::Form`), unless that text ends in one:
|
||||
/// a text label's, and in a bar a glyph text's as well ("X:"). An inspector's labels draw none. The label's node and
|
||||
/// the control keep the name as given.
|
||||
/// a text label's, in a bar a glyph text's as well ("X:"), and under `GlyphColon::Form` a glyph caption's. An
|
||||
/// inspector's labels draw none. The label's node and the control keep the name as given.
|
||||
pub(super) fn colon(env: &Env, input: &LabelIn<'_>) -> Option<String> {
|
||||
if input.side.layout != FieldLayout::Beside || env.values.get::<LabelScheme>() == LabelScheme::Inspector {
|
||||
return None;
|
||||
}
|
||||
let text = match &input.side.kind {
|
||||
LabelKind::Text | LabelKind::Short(_) => env.view().resolve(input.side.kind.shown(input.text)),
|
||||
LabelKind::Glyph(glyph) if env.values.get::<InBar>().0 => match glyph.resolved(env.view()) {
|
||||
Glyph::Text(text) => text,
|
||||
Glyph::Icon(_) => return None,
|
||||
},
|
||||
LabelKind::Glyph(glyph) if env.values.get::<InBar>().0 || (glyph.caption && env.values.get::<GlyphColon>() == GlyphColon::Form) => {
|
||||
match glyph.resolved(env.view()) {
|
||||
Glyph::Text(text) => text,
|
||||
Glyph::Icon(_) => return None,
|
||||
}
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
if text.as_str().is_empty() || text.as_str().ends_with([':', '\u{ff1a}']) {
|
||||
|
||||
@@ -48,17 +48,26 @@ pub(super) enum Glyph {
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct GlyphLabel {
|
||||
shown: Glyph,
|
||||
/// The text stands for a word caption ([`GlyphLabel::caption`]).
|
||||
pub(super) caption: bool,
|
||||
}
|
||||
|
||||
impl GlyphLabel {
|
||||
/// Shows `icon`, as large as the control's icons.
|
||||
pub fn icon(icon: impl Into<IconSpec<'static>>) -> Self {
|
||||
GlyphLabel { shown: Glyph::Icon(icon.into()) }
|
||||
GlyphLabel { shown: Glyph::Icon(icon.into()), caption: false }
|
||||
}
|
||||
|
||||
/// Shows `text`, resolved in the scope's language.
|
||||
pub fn text(text: impl Into<Label>) -> Self {
|
||||
GlyphLabel { shown: Glyph::Text(text.into()) }
|
||||
GlyphLabel { shown: Glyph::Text(text.into()), caption: false }
|
||||
}
|
||||
|
||||
/// Shows `text` that stands for a word caption: X for the horizontal position, W for the width. It is a glyph text
|
||||
/// in every way, and under [`GlyphColon::Form`] it takes a form's colon as a word label does ("X:"). A glyph that
|
||||
/// is a mark of its own ("#" before a hex value, a colour channel's letter) is [`GlyphLabel::text`].
|
||||
pub fn caption(text: impl Into<Label>) -> Self {
|
||||
GlyphLabel { caption: true, ..GlyphLabel::text(text) }
|
||||
}
|
||||
|
||||
/// What the label leaf draws: the icon, or the text resolved in `env`.
|
||||
@@ -76,7 +85,8 @@ impl GlyphLabel {
|
||||
#[non_exhaustive]
|
||||
pub enum LabelScheme {
|
||||
/// A dialog or a form: a beside label sits against its control and ends with a colon; the control keeps its own
|
||||
/// width. In a bar a glyph text takes the colon as well ("X:").
|
||||
/// width. In a bar a glyph text takes the colon as well ("X:"), and so does a glyph caption under
|
||||
/// [`GlyphColon::Form`].
|
||||
#[default]
|
||||
Form,
|
||||
/// An inspector: a beside label sits at the row's start with no colon, and its control fills the rest of the row.
|
||||
@@ -92,6 +102,25 @@ impl crate::runtime::env::EnvValue for LabelScheme {
|
||||
}
|
||||
}
|
||||
|
||||
/// Which glyph labels take the colon of a scheme whose word labels take one. An app sets it once at its root
|
||||
/// (`RootEnv::values`); the default is [`GlyphColon::Bar`].
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, crate::contract::MemoProps)]
|
||||
#[non_exhaustive]
|
||||
pub enum GlyphColon {
|
||||
/// A glyph text takes the colon in a bar only ("X:"), and shows bare elsewhere.
|
||||
#[default]
|
||||
Bar,
|
||||
/// A glyph caption ([`GlyphLabel::caption`]) takes the colon wherever a word label does, so a form's X/Y/W/H read
|
||||
/// "X:"; every other glyph text keeps the bar's rule. VectorCraft's panels.
|
||||
Form,
|
||||
}
|
||||
|
||||
impl crate::runtime::env::EnvValue for GlyphColon {
|
||||
fn root() -> Self {
|
||||
GlyphColon::Bar
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the beside captions of a [`LabelScheme::Inline`] form share one column. Off (the default), each caption is
|
||||
/// as wide as its own text and its control follows it at once. On, the captions keep to the row start in one column as
|
||||
/// wide as the widest, so the controls after them line up (production's Rotate Pages dialog: Direction:, Pages: and
|
||||
|
||||
@@ -93,7 +93,7 @@ pub use items::{measure_item, show_item, show_item_in_flow};
|
||||
#[doc(hidden)]
|
||||
pub use label::colon_of;
|
||||
pub use label::{
|
||||
CaptionColumn, CellWidth, FieldLabelRole, FieldLayout, GlyphLabel, InsideLabel, LabelKind, LabelPress, LabelScheme, Scrub, SideLabel,
|
||||
CaptionColumn, CellWidth, FieldLabelRole, FieldLayout, GlyphColon, GlyphLabel, InsideLabel, LabelKind, LabelPress, LabelScheme, Scrub, SideLabel,
|
||||
};
|
||||
pub use lazy::Lazy;
|
||||
#[doc(hidden)]
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
use super::Help;
|
||||
use super::{Field, FieldGroup};
|
||||
use crate::choice::Swatch;
|
||||
use crate::contract::{FieldLayout, GlyphLabel, LabelKind, LabelScheme, Show as _, ShowContainer as _, SideLabel, Validity};
|
||||
use crate::contract::{FieldLayout, GlyphColon, GlyphLabel, LabelKind, LabelScheme, Show as _, ShowContainer as _, SideLabel, Validity};
|
||||
use crate::layout_components::Row;
|
||||
use crate::runtime::env::Message;
|
||||
use crate::runtime::env::{Message, with_env};
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use crate::text::NumberField;
|
||||
use crate::text::{HexField, NumberField};
|
||||
use craft_behavior::IconId;
|
||||
use craft_theme::Srgba;
|
||||
use craft_theme::variant::Tone;
|
||||
@@ -102,6 +102,32 @@ fn field_group(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// A form under `GlyphColon::Form`: the glyph captions X, Y, W and H end with a colon as the word label does, and the
|
||||
/// "#" before the hex value, a mark of its own, stays bare.
|
||||
#[story(FieldGroup, covers = [], drive = Drive::Never)]
|
||||
fn field_glyph_captions_with_colons(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
const BLUE: craft_theme::Rgba = craft_theme::Rgba([0x33, 0x66, 0x99, 0xff]);
|
||||
with_env(
|
||||
ui,
|
||||
|overrides| {
|
||||
overrides.values.set(GlyphColon::Form);
|
||||
},
|
||||
|ui| {
|
||||
FieldGroup::new("Transform").labels(LabelScheme::Form).show(ui, |ui, _| {
|
||||
for (glyph, name, key, value) in
|
||||
[("X", "X position", "x", 24.0), ("Y", "Y position", "y", 36.0), ("W", "Width", "w", 640.0), ("H", "Height", "h", 480.0)]
|
||||
{
|
||||
let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::caption(glyph))).scrub(true);
|
||||
Field::new(name).side(side).show(ui, |ui, _| NumberField::new(name, data.bind(key, Some(value))).show(ui));
|
||||
}
|
||||
Field::new("Opacity").show(ui, |ui, _| NumberField::new("Opacity", data.bind("opacity", Some(100.0))).show(ui));
|
||||
Field::new("Hex color").side(HexField::hash_label()).show(ui, |ui, _| HexField::hex("Hex color", data.bind("hex", BLUE)).show(ui));
|
||||
})
|
||||
},
|
||||
)
|
||||
.into()
|
||||
}
|
||||
|
||||
/// The rows of an inspector: glyph labels W and H sharing a row, word labels at the row's start without a colon, and
|
||||
/// controls that fill the rest of the row.
|
||||
#[story(FieldGroup, covers = [], drive = Drive::Never)]
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
use craft_ui::bars::{Bar, BarKind};
|
||||
use craft_ui::choice::{Checkbox, Radio, RadioGroup};
|
||||
use craft_ui::contract::{Bind, FieldLayout, GlyphLabel, LabelKind, LabelScheme, Props as _, Show as _, ShowContainer as _, SideLabel};
|
||||
use craft_ui::contract::{Bind, FieldLayout, GlyphColon, GlyphLabel, LabelKind, LabelScheme, Props as _, Show as _, ShowContainer as _, SideLabel};
|
||||
use craft_ui::form::{Field, FieldGroup};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::NumberField;
|
||||
use craft_ui::text::{HexField, NumberField};
|
||||
use egui::{Rect, Ui};
|
||||
|
||||
const SHORT: &str = "Size";
|
||||
@@ -96,6 +96,38 @@ fn a_glyph_caption_takes_a_colon_in_a_bar_only() {
|
||||
assert_eq!(colons(&plain), 0, "a glyph caption outside a bar takes no colon");
|
||||
}
|
||||
|
||||
/// Under `GlyphColon::Form` a glyph caption takes a form's colon as a word label does ("X:"), and a glyph that is a mark
|
||||
/// of its own keeps the bar's rule: the "#" before a hex value stays bare. Without it, neither takes one.
|
||||
#[trial]
|
||||
fn a_glyph_caption_takes_a_forms_colon_under_the_form_rule_and_the_hex_hash_does_not() {
|
||||
let form = |rule: Option<GlyphColon>| {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
FieldGroup::new("Transform").labels(LabelScheme::Form).show(ui, |ui, _| {
|
||||
Field::new("Horizontal position").side(SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::caption("X")))).show(ui, |ui, _| {
|
||||
NumberField::new("Horizontal position", Bind::given(Some(10.0))).show(ui);
|
||||
});
|
||||
Field::new("Hex color").side(HexField::hash_label()).show(ui, |ui, _| {
|
||||
HexField::hex("Hex color", Bind::given(craft_theme::Rgba([0x33, 0x66, 0x99, 0xff]))).show(ui);
|
||||
});
|
||||
});
|
||||
});
|
||||
if let Some(rule) = rule {
|
||||
fixture.root_value(rule);
|
||||
}
|
||||
fixture.settle();
|
||||
fixture
|
||||
};
|
||||
assert_eq!(colons(&form(None)), 0, "by default a glyph takes no colon outside a bar");
|
||||
let opted = form(Some(GlyphColon::Form));
|
||||
assert_eq!(colons(&opted), 1, "only the caption takes the colon; the hash stays bare");
|
||||
let colon = opted.texts().iter().find(|run| run.text.trim() == ":").map(|run| run.rect).unwrap_or(Rect::NOTHING);
|
||||
let field = control(&opted, "Horizontal position");
|
||||
assert!(
|
||||
colon.min.x >= caption(&opted, "X").max.x - SLACK && colon.max.x <= field.min.x + SLACK && field.y_range().contains(colon.center().y),
|
||||
"the colon follows the X, clear of its field: {colon:?} {field:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// A check box row of a form starts its box in the control column, where the rows labelled beside it start their
|
||||
/// controls, whether the check box carries its label or a field's label follows it. In an inspector, in a form with no
|
||||
/// labelled row, and as a radio group's member, it stays at the row's start.
|
||||
|
||||
Reference in New Issue
Block a user