Hidden labels with content show the content alone; Layers Panel Options shows each thumbnail size by its sample

This commit is contained in:
aiongg
2026-11-12 14:32:47 +00:00
parent 79c7732c40
commit 4aa4976fab
7 changed files with 69 additions and 13 deletions
+5 -4
View File
@@ -7,7 +7,7 @@
mod stories;
use craft_ui::choice::{Checkbox, Radio, RadioGroup};
use craft_ui::contract::{Bind, FieldLayout, Props as _, Show as _, ShowContainer as _, SideLabel};
use craft_ui::contract::{Bind, FieldLayout, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel};
use craft_ui::dialog::{DialogId, DialogResult, DialogStack, StackDialog, dialog_id};
use craft_ui::display::{Picture, PictureSize};
use craft_ui::layout_components::Stack;
@@ -268,7 +268,7 @@ fn sample_image() -> ColorImage {
/// ```
///
/// - **Layout**: a group box "Thumbnail Size" with a radio group of None, then Small, Medium and
/// Large, each with a sample picture of its size beside its label; a group box "Thumbnail
/// Large, each shown by a sample picture of its size in place of its name; a group box "Thumbnail
/// Contents" with Layer Bounds and Entire Document; then the five switches as check boxes, in
/// PhotoCraft's order.
/// - **Keyboard**: the radio groups take arrows; Tab walks the groups and check boxes; Enter is
@@ -304,14 +304,15 @@ impl StackDialog for LayersOptionsDialog {
GroupBox::new(words.thumbnail_size.clone(), None).show(ui, |ui, _| {
RadioGroup::new(words.thumbnail_size.clone(), Bind::bound(&mut options.thumbnail_size)).show(ui, |ui, cx| {
Stack::new().show(ui, |ui, _| {
// Each size's sample sits inside its option: a click on it picks the size.
// Each size's sample is its option's label: a click on it picks the size, and the size's
// name names the radio and is the sample's tooltip.
for size in ThumbSize::ALL {
let side = SideLabel::new().layout(FieldLayout::After);
if size == ThumbSize::None {
cx.labelled(ui, size, size.label(&list), side, Radio::new(size));
} else {
let sample = Picture::new(words.sample.clone(), SAMPLE_KEY, &sample_image).extent(PictureSize::Thumb(size));
cx.labelled_with(ui, size, size.label(&list), side, Radio::new(size), sample);
cx.labelled_with(ui, size, size.label(&list), side.kind(LabelKind::Hidden), Radio::new(size), sample);
}
}
});
+2 -1
View File
@@ -124,7 +124,8 @@ impl<'a> BodyCx<'a> {
/// `labelled`, with `content` shown inside the label after its text (a sample of what the option picks). The
/// content belongs to the label: for a child that draws only its mark (a radio button, a check box) a press on it
/// is the child's, as a press on the label is, so the content takes none itself.
/// is the child's, as a press on the label is, so the content takes none itself. With a `LabelKind::Hidden` label
/// the content is the whole label: a picture in place of the option's name.
pub fn labelled_with<C: Show, D: Show>(
&mut self,
ui: &mut Ui,
@@ -84,6 +84,8 @@ pub(super) struct LabelLeaf {
pub(super) claimed: bool,
/// The size of the content the label step shows after the text, inside the label: 0 without one.
pub(super) trailing: Vec2,
/// The name shows; false for a label that shows only its content (`SideLabel::content_only`).
pub(super) named: bool,
double_click: bool,
dot_click: bool,
activated: bool,
@@ -120,6 +122,7 @@ impl LabelLeaf {
wraps: false,
claimed: false,
trailing: Vec2::ZERO,
named: true,
double_click: false,
dot_click: false,
activated: false,
@@ -180,6 +183,12 @@ impl Component for LabelLeaf {
self.first_line.set(shown.y);
return Vec2::new(shown.x + self.slot, shown.y);
}
if !self.named {
let height = self.trailing.y.max(name.y);
self.baseline.set(line.first_baseline + (height - name.y) / 2.0);
self.first_line.set(height);
return Vec2::new(self.trailing.x + self.slot, height);
}
let room = match cx.proposal {
craft_behavior::Proposal::Width(width) => Some(width),
_ => cx.room,
@@ -280,6 +289,13 @@ impl Component for LabelLeaf {
if self.trailing.x > 0.0 {
rest = rest.take_end(self.trailing_width(cx.style.gap)).1;
}
if !self.named {
// The content is the label: the step shows it in the label's rect.
if self.slot > 0.0 {
draw.reset_mark(&cx.style.reset, slot.center(), self.differs);
}
return;
}
let centred = |rect: Rect, height: f32| Rect::from_min_size(egui::pos2(rect.min.x, middle - height / 2.0), Vec2::new(rect.width(), height));
if let Some(glyph) = &self.glyph {
match glyph {
@@ -151,14 +151,17 @@ pub(crate) fn label_gap(env: &Env) -> f32 {
pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Placed {
let ctx = ui.ctx().clone();
let flow = Flow::new(input.row, env.direction.is_rtl());
if input.side.shows_no_leaf() {
let content = input.trailing.x > 0.0;
if input.side.shows_no_leaf(content) {
return Placed { label: Rect::NOTHING, control: hidden_control(&ctx, input, flow, label_gap(env)), trailing: Rect::NOTHING };
}
let reset = scope::last_reset(&ctx, input.scope);
let slot = if reset.is_some() && input.side.reset_dot { env.tokens.size.handle.sm } else { 0.0 };
let mut leaf = LabelLeaf::new(input.side.kind.shown(input.text).clone(), slot);
leaf.resettable = reset.is_some();
leaf.colon = colon(env, input);
leaf.trailing = input.trailing;
leaf.named = !input.side.content_only(content);
leaf.colon = colon(env, input).filter(|_| leaf.named);
match &input.side.kind {
LabelKind::Glyph(glyph) => leaf.glyph = Some(glyph.resolved(env.view())),
_ => {
+12 -4
View File
@@ -15,7 +15,8 @@ pub enum LabelKind {
#[default]
Text,
/// No visible label: the text still names the control. In a body whose labelled rows share a label column, the
/// row keeps the column, so its control starts in the control column.
/// row keeps the column, so its control starts in the control column. A label given content
/// (`BodyCx::labelled_with`) shows the content alone, with the text as its tooltip.
Hidden,
/// An icon or a short text shown in place of the label; the text still names the control. Glyph labels share
/// a column of their own, apart from the text labels'. The label's detail joins the description and is not shown.
@@ -180,9 +181,16 @@ pub struct SideLabel {
}
impl SideLabel {
/// The label step shows no label leaf: a hidden label, or one the control shows inside its box.
pub(super) fn shows_no_leaf(&self) -> bool {
self.kind == LabelKind::Hidden || self.layout == FieldLayout::Inside
/// The label step shows no label leaf: a hidden label without content, or one the control shows inside its box.
/// A hidden label with `content` (`BodyCx::labelled_with`) shows the content alone.
pub(super) fn shows_no_leaf(&self, content: bool) -> bool {
(self.kind == LabelKind::Hidden && !content) || self.layout == FieldLayout::Inside
}
/// The label shows only its content: a hidden label with content. The text still names the control and is the
/// content's tooltip.
pub(super) fn content_only(&self, content: bool) -> bool {
self.kind == LabelKind::Hidden && content
}
/// A text label beside its control that names it.
@@ -144,7 +144,8 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option<
let reset = scope::last_reset(&ctx, input.scope);
let flow = Flow::new(input.row, rtl);
let label_id = sub(input.scope, "label");
if input.side.shows_no_leaf() {
let content = input.trailing.x > 0.0;
if input.side.shows_no_leaf(content) {
scope::begin(&ctx, input.scope, input.owner, scope::Kind::Label(field_label(env, input, label_id, Rect::NOTHING)));
let control = hidden_control(&ctx, input, flow, gap);
scope::place_controls(&ctx, input.scope, control.top());
@@ -155,12 +156,15 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option<
leaf.scrub = input.enabled && (input.side.scrub || scope::last_scrub(&ctx, input.scope));
leaf.resettable = reset.is_some();
leaf.differs = reset.is_some_and(|fact| fact.differs);
leaf.colon = colon(env, input);
leaf.named = !input.side.content_only(content);
leaf.colon = colon(env, input).filter(|_| leaf.named);
leaf.wraps = input.side.layout == FieldLayout::After;
leaf.claimed = input.claims;
leaf.trailing = input.trailing;
if let Some(tooltip) = input.side.tooltip.clone() {
leaf = leaf.tooltip(tooltip);
} else if !leaf.named {
leaf = leaf.tooltip(input.text.clone());
}
match &input.side.kind {
LabelKind::Glyph(glyph) => leaf.glyph = Some(glyph.resolved(env.view())),
+23
View File
@@ -25,3 +25,26 @@ fn a_short_caption_shows_in_place_of_the_label_and_the_label_names_the_control()
let control = fixture.by_label(NAME).expect("the long label names the control");
assert!(control.rect.min.x >= caption.rect.max.x, "the control follows its caption");
}
/// A hidden label given content shows the content alone in place of the label (a picture for an option), and the
/// label's text still names the control.
#[trial]
fn a_hidden_label_with_content_shows_only_the_content() {
use craft_ui::choice::{Radio, RadioGroup};
use craft_ui::contract::FieldLayout;
use craft_ui::display::Text;
const SIZE: &str = "Small";
let mut fixture = Fixture::new(|ui| {
let mut picked = 0u8;
RadioGroup::new("Size", Bind::bound(&mut picked)).show(ui, |ui, cx| {
let side = SideLabel::new().layout(FieldLayout::After).kind(LabelKind::Hidden);
cx.labelled_with(ui, 1u8, SIZE.into(), side, Radio::new(1u8), Text::new("sample"));
});
});
fixture.settle();
let texts = fixture.texts();
let sample = texts.iter().find(|run| run.text == "sample").expect("the content is shown");
assert!(texts.iter().all(|run| run.text != SIZE), "the label's text is not shown");
let radio = fixture.by_label(SIZE).expect("the label's text names the control");
assert!(radio.rect.max.x <= sample.rect.min.x, "the content follows its radio, as a label after it does");
}