diff --git a/crates/color/src/ui/panel.rs b/crates/color/src/ui/panel.rs index bc758edd0..35f2873a0 100644 --- a/crates/color/src/ui/panel.rs +++ b/crates/color/src/ui/panel.rs @@ -2,6 +2,7 @@ //! (two cubes, a wheel, five slider sets), the out-of-gamut and web-colour warnings, and the colour ramp under the //! sliders; the panel menu chooses the view and the ramp and copies the colour. +use std::cell::RefCell; use std::hash::{DefaultHasher, Hash, Hasher}; use craft_behavior::{EditEvent, GesturePhase, IconId, Mixed}; @@ -12,6 +13,7 @@ use craft_ui::contract::{ Bind, CommandSet, Gap, Held, LabelScheme, NoValue, Outcome, Pattern, Props as _, Remember as _, Scroll, Show as _, ShowContainer as _, Sizing, clicked, }; +use craft_ui::form::FieldGroup; use craft_ui::layout::RowAlign; use craft_ui::layout_components::{Row, Stack}; use craft_ui::menu::{Menu, MenuItem, PanelRow}; @@ -328,7 +330,7 @@ impl<'a> ColorPanel<'a> { let mut shown = Stack::new().label(words.title.clone()).fill().show(ui, |ui, cx| { let before = held.read(cx)?; let target = if before.background { PanelTarget::Background } else { PanelTarget::Foreground }; - let mut body = Body { + let body = Body { target, colors, converter: cmyk.unwrap_or_else(|| CmykConverter::coated()), @@ -341,7 +343,7 @@ impl<'a> ColorPanel<'a> { pick_color: None, add_to_swatches: None, }; - body.show(ui); + let mut body = body.show(ui); *cx.memory::() = std::mem::take(&mut body.kept); edits = body.events; pick_color = body.pick_color; @@ -540,43 +542,58 @@ impl Body<'_> { } /// The cubes take the height the readout leaves them, as Photoshop's field fills its dock group; the slider modes and - /// the wheel scroll where the group is shorter than they are. - fn show(&mut self, ui: &mut Ui) { + /// the wheel scroll where the group is shorter than they are. The slider modes' ramp is the body's footer, outside the + /// scroll: it stays whole under the rows, as Photoshop's ramp does. + fn show(self, ui: &mut Ui) -> Self { let mode = self.now.mode; let cube = mode.is_cube(); - let body = if cube { Stack::new().fill() } else { Stack::new().fill().scroll(Scroll::vertical()) }; - body.show(ui, |ui, _| { - let top = if cube { Row::new().fill() } else { Row::new() }; - top.show(ui, |ui, _| { - Stack::new().show(ui, |ui, _| { - self.wells(ui); - // The cubes show the out-of-gamut chip only: the web-colour chip is for Web Color. - if mode.is_sliders() || mode.is_cube() { - self.warnings(ui, mode == ColorPanelMode::WebColorSliders); - } - }); - match mode { - ColorPanelMode::HueCube => self.cube(ui, AreaMode::Hue), - ColorPanelMode::BrightnessCube => self.cube(ui, AreaMode::Brightness), - _ => { - // The channel rows stand as close as Photoshop's. - Stack::new().fill_width().gap(Gap::Tight).labels(LabelScheme::Inspector).show(ui, |ui, _| match mode { - ColorPanelMode::GrayscaleSlider => self.rows(ui, &GrayModel, false), - ColorPanelMode::RgbSliders => self.rows(ui, &RgbModel, false), - ColorPanelMode::CmykSliders => self.rows(ui, &CmykModel::new(self.converter.clone()), false), - ColorPanelMode::LabSliders => self.rows(ui, &LabModel, false), - ColorPanelMode::WebColorSliders => self.rows(ui, &RgbModel, true), - _ => self.rows(ui, &HsbModel, false), - }); - } + // The body and the footer both draw into the panel's pass state. + let this = RefCell::new(self); + let mut body = Stack::new().fill(); + if !cube { + body = body.scroll(Scroll::vertical()); + } + if mode.is_sliders() { + body = body.footer(|ui, _| this.borrow_mut().ramp(ui)); + } + body.show(ui, |ui, _| this.borrow_mut().content(ui)); + this.into_inner() + } + + /// The wells with their warnings beside the view of the mode; under the cubes their readout, under the wheel its + /// button. + fn content(&mut self, ui: &mut Ui) { + let mode = self.now.mode; + let top = if mode.is_cube() { Row::new().fill() } else { Row::new() }; + top.show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + self.wells(ui); + // The cubes show the out-of-gamut chip only: the web-colour chip is for Web Color. + if mode.is_sliders() || mode.is_cube() { + self.warnings(ui, mode == ColorPanelMode::WebColorSliders); } }); match mode { - ColorPanelMode::HueCube | ColorPanelMode::BrightnessCube => self.readout(ui), - ColorPanelMode::ColorWheel => self.wheel(ui), - _ => self.ramp(ui), + ColorPanelMode::HueCube => self.cube(ui, AreaMode::Hue), + ColorPanelMode::BrightnessCube => self.cube(ui, AreaMode::Brightness), + _ => { + // The channel rows stand as close as Photoshop's, their letters, fields and units in shared columns. + FieldGroup::new(mode.label(self.words)).labels(LabelScheme::Inspector).gap(Gap::Tight).show(ui, |ui, _| match mode { + ColorPanelMode::GrayscaleSlider => self.rows(ui, &GrayModel, false), + ColorPanelMode::RgbSliders => self.rows(ui, &RgbModel, false), + ColorPanelMode::CmykSliders => self.rows(ui, &CmykModel::new(self.converter.clone()), false), + ColorPanelMode::LabSliders => self.rows(ui, &LabModel, false), + ColorPanelMode::WebColorSliders => self.rows(ui, &RgbModel, true), + _ => self.rows(ui, &HsbModel, false), + }); + } } }); + match mode { + ColorPanelMode::HueCube | ColorPanelMode::BrightnessCube => self.readout(ui), + ColorPanelMode::ColorWheel => self.wheel(ui), + _ => {} + } } /// The foreground over the background: a click on the other well edits it, a click on the edited one asks for the diff --git a/crates/color/tests/it/panels.rs b/crates/color/tests/it/panels.rs index 75100d942..6fd59029f 100644 --- a/crates/color/tests/it/panels.rs +++ b/crates/color/tests/it/panels.rs @@ -129,20 +129,33 @@ fn the_cubes_take_the_room_the_readout_and_the_strip_leave() { assert!(blue.rect.right() <= narrow.x + 0.5, "the folded readout fits the width ({:?})", blue.rect); } -/// The slider modes show each channel on one line: its slider and its number field side by side. +/// The slider modes show each channel on one line: its slider and its number field side by side, every slider starting +/// at one x past the channels' letters. The ramp is the footer under the rows: whole and in view +/// in a short group too, where the rows scroll above it. #[trial] fn the_slider_modes_show_a_channel_on_one_line() { - for (mode, channels) in [(ColorPanelMode::RgbSliders, 3), (ColorPanelMode::CmykSliders, 4), (ColorPanelMode::HsbSliders, 3)] { - let rig = PanelRig::new(mode); - let sliders = rig.fixture.by_role(Role::SLIDER); - let fields = rig.fixture.by_role(Role::SPIN_BUTTON); - assert_eq!((sliders.len(), fields.len()), (channels, channels), "{mode:?}"); - for (slider, field) in sliders.iter().zip(&fields) { - let line = slider.rect.y_range(); - assert!( - line.contains(field.rect.center().y) && slider.rect.right() <= field.rect.left(), - "{mode:?}: {slider:?} then {field:?} on one line" - ); + for room in [COLOR_ROOM, Vec2::new(280.0, 96.0)] { + for (mode, channels) in [(ColorPanelMode::RgbSliders, 3), (ColorPanelMode::CmykSliders, 4), (ColorPanelMode::HsbSliders, 3)] { + let case = format!("{mode:?} in {room:?}"); + let rig = PanelRig::in_room(mode, room); + let sliders = rig.fixture.by_role(Role::SLIDER); + let fields = rig.fixture.by_role(Role::SPIN_BUTTON); + assert_eq!((sliders.len(), fields.len()), (channels, channels), "{case}"); + for (slider, field) in sliders.iter().zip(&fields) { + let line = slider.rect.y_range(); + assert!( + line.contains(field.rect.center().y) && slider.rect.right() <= field.rect.left(), + "{case}: {slider:?} then {field:?} on one line" + ); + assert!( + (slider.rect.left() - sliders[0].rect.left()).abs() < 0.5, + "{case}: the sliders start at one x ({slider:?}, {:?})", + sliders[0] + ); + } + let ramp = rig.fixture.by_label("Color Ramp").expect("the ramp"); + assert!(ramp.rect.top() >= -0.5 && ramp.rect.bottom() <= room.y + 0.5, "{case}: the ramp whole and in view ({:?})", ramp.rect); + assert!(sliders[0].rect.top() < ramp.rect.top(), "{case}: the ramp under the rows ({:?}, {:?})", sliders[0].rect, ramp.rect); } } } diff --git a/crates/ui-widgets/src/form/group.rs b/crates/ui-widgets/src/form/group.rs index 715146bc4..5b90e8ae2 100644 --- a/crates/ui-widgets/src/form/group.rs +++ b/crates/ui-widgets/src/form/group.rs @@ -1,6 +1,6 @@ //! `FieldGroup` (16.3): a container whose `Field`s share one label column. -use crate::contract::{Common, Component, Container, ContainerProps, Cx, LabelScheme, Props as _, Role, Sizing}; +use crate::contract::{Common, Component, Container, ContainerProps, Cx, Gap, LabelScheme, Props as _, Role, Sizing}; use crate::runtime::env::Label; use crate::style::{ComponentStyle, StyleCx}; use craft_behavior::{Metrics, Sense}; @@ -39,6 +39,13 @@ impl FieldGroup { self.props = self.props.labels(scheme); self } + + /// The spacing between the fields: `Gap::Row` by default, `Gap::Tight` for a dense panel's channel rows. + #[must_use] + pub fn gap(mut self, gap: Gap) -> Self { + self.props = self.props.body_gap(gap); + self + } } impl Component for FieldGroup { diff --git a/crates/ui-widgets/src/text/text_field.rs b/crates/ui-widgets/src/text/text_field.rs index f39fab8c4..7fa465bf2 100644 --- a/crates/ui-widgets/src/text/text_field.rs +++ b/crates/ui-widgets/src/text/text_field.rs @@ -6,7 +6,7 @@ mod stories; use super::adorn::{self, Adornment}; use super::{FieldStyle, completions, text_part, words}; use crate::contract::{ - Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, OverlayUi, PaintUi, PartsCx, Pointer as _, Props as _, Role, + Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, MeasureCx, OverlayUi, PaintUi, PartsCx, Pointer as _, Props as _, Role, Sizing, Styled as _, WordSetCx as _, }; use crate::primitives::{Completer, IconSpec, TextOptions, TextRun}; @@ -237,8 +237,11 @@ impl Component for TextField<'_, C> { type Style = FieldStyle; type Output = EditResponse; + /// The field's shortest width (`size.length.short`) is `measure`'s, so a content-sized field can go without it. fn style(cx: &crate::style::StyleCx<'_>, _: TextFieldStyleKey) -> FieldStyle { - FieldStyle::at(cx) + let mut style = FieldStyle::at(cx); + style.metrics = style.metrics.min_size(Vec2::new(0.0, style.metrics.min_size.y)); + style } fn sense(&self) -> Sense { @@ -266,6 +269,12 @@ impl Component for TextField<'_, C> { let clear = self.search.then(|| Self::clear_name(&cx.words::())); self.leading = std::mem::take(&mut self.leading).into_iter().map(|adornment| adornment.measured(cx)).collect(); size.x += adorn::reserve(cx.style, &self.leading, &self.trailing(clear)); + // Short text keeps a field at `size.length.short`, so a column of fields ends at one edge; a content-sized field + // takes its text and adornments only, as a content-sized number field does. + if cx.proposal != Proposal::Min && cx.resolved.sizing() != Some(Sizing::Content) { + let padding = cx.style.metrics.padding; + size.x = size.x.max(cx.tokens.size.length.short - padding.start - padding.end); + } size }