Color panel look: a leaf with Sizing::Fill fills both axes; the Hue and Brightness Cubes take the height the readout leaves; ChannelRow is one line with the channel's letter and a triangle thumb (SliderField::thumb)

This commit is contained in:
aiongg
2026-11-13 19:26:50 +00:00
parent cc4b4ec895
commit 622f18768c
9 changed files with 100 additions and 34 deletions
+9 -1
View File
@@ -5,7 +5,8 @@ use crate::AreaMode;
use craft_behavior::{Metrics, Mixed, Sense};
use craft_theme::ModelValues;
use craft_ui::contract::{
Access, Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, MeasureCx, PaintUi, Props as _, Role, WordSetCx as _,
Access, Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, MeasureCx, PaintUi, Props as _, Role, Sizing,
WordSetCx as _,
};
use craft_ui::primitives::ContentPainterCx;
use craft_ui::runtime::env::Label;
@@ -38,6 +39,9 @@ pub struct ColorAreaStyle {
/// changed, so a host that keeps the values it was sent keeps the hue of a grey and the hue and saturation of black.
/// The area keeps its axes right to left: across still grows to the right. Its value text names both components
/// ("Saturation 40 %, Brightness 70 %", from [`names`](ColorArea::names)).
///
/// It is at least `size.plot` and fills the width its row leaves; with `Props::sizing(Sizing::Fill)` it takes the room
/// on both axes, however small (the Color panel's field, as tall as its dock group leaves it).
#[derive(craft_ui::contract::Component, craft_ui::contract::Props)]
pub struct ColorArea<'a> {
#[common]
@@ -116,6 +120,10 @@ impl Component for ColorArea<'_> {
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
// An area that fills its room (the Color panel's, as tall as its group leaves it) keeps no plot size of its own.
if cx.resolved.sizing() == Some(Sizing::Fill) {
return Vec2::splat(cx.tokens.size.hit.target);
}
let mut picker = self.picker(cx.value(), false);
cx.child("picker", &mut picker, cx.proposal)
}
+35 -19
View File
@@ -8,7 +8,10 @@ use craft_behavior::{EditEvent, GesturePhase, IconId, Mixed};
use craft_cms::convert::CmykConverter;
use craft_theme::{ColorModel, HsbModel, LabModel, ModelValues, RgbModel, Srgba};
use craft_ui::buttons::IconButton;
use craft_ui::contract::{Bind, CommandSet, Held, NoValue, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _, clicked};
use craft_ui::contract::{
Bind, CommandSet, Held, LabelScheme, NoValue, Outcome, Pattern, Props as _, Remember as _, Scroll, Show as _, ShowContainer as _, Sizing,
clicked,
};
use craft_ui::layout::RowAlign;
use craft_ui::layout_components::{Row, Stack};
use craft_ui::menu::{Menu, MenuItem, PanelRow};
@@ -18,7 +21,7 @@ use craft_ui::runtime::requests::ColorRequest;
use egui::{Response, Ui};
use super::channels::finite_values;
use super::picker::{ChannelRow, ChannelRule, channel_name};
use super::picker::{ChannelRow, ChannelRule, ChannelUnit, channel_name};
use super::{
AreaMode, ColorArea, ColorPickerDialogWords, ColorRamp, ColorReadout, ColorStrip, ColorWarning, ColorWarningChip, ColorWheel, PairWell,
PickedColor, RampKind, SwatchPair, Well,
@@ -322,7 +325,7 @@ impl<'a> ColorPanel<'a> {
let (words, dialog) = (env.words::<ColorPanelWords>(), env.words::<ColorPickerDialogWords>());
let mut held = Held::new(state);
let (mut edits, mut pick_color, mut add_to_swatches) = (Vec::new(), None, None);
let mut shown = Stack::new().label(words.title.clone()).show(ui, |ui, cx| {
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 {
@@ -536,10 +539,15 @@ impl Body<'_> {
self.events.push((target, event));
}
/// 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) {
let mode = self.now.mode;
Stack::new().show(ui, |ui, _| {
Row::new().show(ui, |ui, _| {
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.
@@ -547,17 +555,20 @@ impl Body<'_> {
self.warnings(ui, mode == ColorPanelMode::WebColorSliders);
}
});
Stack::new().fill_width().show(ui, |ui, _| match mode {
match mode {
ColorPanelMode::HueCube => self.cube(ui, AreaMode::Hue),
ColorPanelMode::BrightnessCube => self.cube(ui, AreaMode::Brightness),
ColorPanelMode::ColorWheel => self.rows(ui, &HsbModel, false),
ColorPanelMode::GrayscaleSlider => self.rows(ui, &GrayModel, false),
ColorPanelMode::RgbSliders => self.rows(ui, &RgbModel, false),
ColorPanelMode::HsbSliders => self.rows(ui, &HsbModel, 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),
});
_ => {
Stack::new().fill_width().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),
});
}
}
});
match mode {
ColorPanelMode::HueCube | ColorPanelMode::BrightnessCube => self.readout(ui),
@@ -674,8 +685,8 @@ impl Body<'_> {
);
}
/// The channel rows of `model`: a label that scrubs the number field at the row's end, and the slider over the
/// channel's colours. Web Color has sliders that step by the web levels.
/// The channel rows of `model`, one line each: the channel's letter, which scrubs the value, the slider over the
/// channel's colours, and the number field with its unit. Web Color has sliders that step by the web levels.
fn rows(&mut self, ui: &mut Ui, model: &dyn ColorModel, web: bool) {
let current = self.values(model);
// Dynamic Color Sliders off: the tracks keep the colours they had when a drag began.
@@ -698,14 +709,18 @@ impl Body<'_> {
track_values.values[2] = 100.0;
}
let track = model.track(i, &track_values);
let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(f64::from(current.values[i])), rule).track(track).show(ui);
let row = ChannelRow::new(channel_name(model, i, &[model]), Bind::given(f64::from(current.values[i])), rule)
.short(ch.label)
.unit(ChannelUnit::of(ch.unit))
.track(track)
.show(ui);
for event in row.into_iter().flat_map(|row| row.events) {
self.channel_event(model, current, i, event);
}
}
}
/// Hue Cube or Brightness Cube: the area and the strip of `mode`.
/// Hue Cube or Brightness Cube: the area and the strip of `mode`, in the room the wells and the readout leave.
fn cube(&mut self, ui: &mut Ui, mode: AreaMode) {
let model = mode.model();
let values = self.values(model);
@@ -713,8 +728,9 @@ impl Body<'_> {
let names = [channel_name(model, across, &[model]), channel_name(model, down, &[model])];
let (area_name, strip_name) = (Label::from(mode.name()), channel_name(model, mode.strip(), &[model]));
let (area, strip) = Row::new()
.fill()
.show(ui, |ui, _| {
let area = ColorArea::new(area_name.clone(), mode, Bind::given(values)).names(names.clone()).show(ui);
let area = ColorArea::new(area_name.clone(), mode, Bind::given(values)).names(names.clone()).sizing(Sizing::Fill).show(ui);
let strip = ColorStrip::new(strip_name.clone(), mode, Bind::given(values)).show(ui);
(area, strip)
})
+33 -9
View File
@@ -1,16 +1,18 @@
//! The channel row: a label that scrubs a slider whose track shows the channel's colours, and a number field after
//! it. Shared with the panels that show channels.
//! The channel row: on one line, a label that scrubs, a slider whose track shows the channel's colours, and a number
//! field after it. Shared with the panels that show channels.
use std::ops::RangeInclusive;
use craft_behavior::{Axis, Sense};
use craft_theme::{Channel, ColorModel, Srgba};
use craft_ui::contract::{Bind, Common, Component, Cx, DisabledLook, EditResponse, MeasureCx, Role, Show as _};
use craft_ui::form::SliderField;
use craft_ui::contract::{
Bind, Common, Component, Cx, DisabledLook, EditResponse, GlyphLabel, LabelKind, MeasureCx, Role, Show as _, ShowContainer as _, SideLabel,
};
use craft_ui::form::{Field, SliderField, SliderFieldLayout};
use craft_ui::primitives::{PaintShape, StrokeWidth};
use craft_ui::runtime::env::{Label, UnitKind};
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx};
use craft_ui::value_drag::TrackFace;
use craft_ui::value_drag::{SliderThumb, TrackFace};
use egui::{Color32, CornerRadius, Ui, Vec2};
/// How a channel's value steps and settles: a channel's or the alpha's.
@@ -153,10 +155,14 @@ pub(crate) fn channel_slider<'a>(
}
}
/// One channel: the word label and number field on a line over the slider across the channel's colours.
/// One channel on one line, as Photoshop's Color panel draws it: the label at the start (the channel's short label
/// where one is given, "C"), which scrubs the value, the slider over the channel's colours with the pointer under its
/// track, and the number field with its unit at the end.
#[derive(craft_ui::contract::Pattern)]
pub(crate) struct ChannelRow<'a> {
label: Label,
/// The text shown in place of the label: the channel's short label.
short: Option<&'static str>,
value: Bind<'a, f64>,
rule: ChannelRule,
track: Option<Vec<Srgba>>,
@@ -167,7 +173,13 @@ pub(crate) struct ChannelRow<'a> {
impl<'a> ChannelRow<'a> {
pub(crate) fn new(label: impl Into<Label>, value: Bind<'a, f64>, rule: ChannelRule) -> Self {
ChannelRow { label: label.into(), value, rule, track: None, checker: false, unit: ChannelUnit::None }
ChannelRow { label: label.into(), short: None, value, rule, track: None, checker: false, unit: ChannelUnit::None }
}
/// Shows `short` at the line's start in place of the label, which stays the row's name.
pub(crate) fn short(mut self, short: &'static str) -> Self {
self.short = Some(short).filter(|short| !short.is_empty());
self
}
/// The colours of the track, from the channel's start to its end.
@@ -189,7 +201,19 @@ impl<'a> ChannelRow<'a> {
/// Shows the row; the edit of the slider or the field, `None` when the row did not run.
pub(crate) fn show(self, ui: &mut Ui) -> Option<EditResponse<f64>> {
let ChannelRow { label, mut value, rule, track, checker, unit } = self;
Some(channel_slider(label, value.reborrowed(), &rule, unit, track.map(|stops| (stops, checker))).show(ui))
let ChannelRow { label, short, mut value, rule, track, checker, unit } = self;
let side = match short {
Some(short) => SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(Label::from(short)))),
None => SideLabel::new(),
};
Field::new(label.clone())
.side(side.scrub(true))
.show(ui, |ui, _| {
channel_slider(label.clone(), value.reborrowed(), &rule, unit, track.clone().map(|stops| (stops, checker)))
.layout(SliderFieldLayout::Inline)
.thumb(SliderThumb::Triangle)
.show(ui)
})
.inner
}
}
+1
View File
@@ -56,6 +56,7 @@ impl<'a> Body<'a> {
Bind::given(pass.shows(f64::from(values.values[i]))),
ChannelRule::of(channel),
)
.short(channel.label)
.unit(ChannelUnit::of(channel.unit));
if self.dynamic_tracks {
row = row.track(model.track(i, &values));
+1 -1
View File
@@ -20,7 +20,7 @@ use craft_ui::runtime::env::Label;
use egui::{Pos2, Response, Ui};
use super::AreaMode;
pub(crate) use channel::{ChannelRow, ChannelRule, channel_name};
pub(crate) use channel::{ChannelRow, ChannelRule, ChannelUnit, channel_name};
pub use full::{ColorWarning, ColorWarningChip, ColorWarningChipOutput};
pub use value::ColorConstraint;
use value::Rules;
+6 -1
View File
@@ -146,8 +146,13 @@ impl Component for ColorStrip<'_> {
}
}
/// As tall as the line of its row, the area beside it: a strip stands beside an area, in a picker or a panel.
fn fills(&self) -> Option<Axis> {
Some(Axis::Y)
}
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
Vec2::new(cx.tokens.size.icon.lg, cx.tokens.size.plot.height)
Vec2::new(cx.tokens.size.icon.lg, cx.tokens.size.hit.target)
}
fn sense(&self) -> Sense {
+2
View File
@@ -115,6 +115,8 @@ pub trait Props: Meta + Sized {
/// A leaf with `Sizing::Content` is as wide as its content, without the ideal width its style asks for where the
/// room allows (a field's `size.length.field`); a leaf otherwise keeps that width, and with `Sizing::FillWidth` it
/// takes the room its row leaves after the other members, or the width its column offers (`Component::fills`).
/// A leaf with `Sizing::Fill` fills both axes: along an axis its body shares, its share of the room; across one,
/// the room offered there (a plot that takes the height its panel leaves).
fn sizing(mut self, sizing: Sizing) -> Self {
self.set_sizing(sizing, Internal::new());
self
+5 -1
View File
@@ -326,7 +326,11 @@ impl<C: Component> ShapeHooks<C> for LeafHooks {
};
// The control beside an inspector's label fills the cell the label leaves, unless its caller sized it to its
// content.
let in_cell = env.cell_fills && comp.common(super::Internal::new()).sizing != Some(super::Sizing::Content);
let sizing = comp.common(super::Internal::new()).sizing;
if sizing == Some(super::Sizing::Fill) {
return Vec2::new(fill(size.x, room.x, content.0, craft_behavior::Axis::X), fill(size.y, room.y, content.1, craft_behavior::Axis::Y));
}
let in_cell = env.cell_fills && sizing != Some(super::Sizing::Content);
match comp.fills().or(in_cell.then_some(craft_behavior::Axis::X)) {
Some(craft_behavior::Axis::X) => Vec2::new(fill(size.x, room.x, content.0, craft_behavior::Axis::X), size.y),
Some(craft_behavior::Axis::Y) => Vec2::new(size.x, fill(size.y, room.y, content.1, craft_behavior::Axis::Y)),
+8 -2
View File
@@ -10,7 +10,7 @@ use crate::contract::{
use crate::runtime::env::{Label, UnitKind};
use crate::style::{ComponentStyle, StyleCx};
use crate::text::NumberField;
use crate::value_drag::{Slider, SliderScale, TrackFace};
use crate::value_drag::{Slider, SliderScale, SliderThumb, TrackFace};
use craft_behavior::{Flow, Metrics, Mixed, Proposal, Sense, decimals_for_step};
use egui::{Rect, Vec2, pos2};
use std::ops::RangeInclusive;
@@ -87,6 +87,9 @@ pub struct SliderField<'a> {
/// The look of the track in place of the groove and the fill (a colour gradient).
#[prop]
track_face: Option<TrackFace<'a>>,
/// The shape of the slider's thumb: a knob, or the pointer under the track of a colour channel.
#[prop]
thumb: SliderThumb,
}
impl<'a> SliderField<'a> {
@@ -104,6 +107,7 @@ impl<'a> SliderField<'a> {
unit: None,
fill: true,
track_face: None,
thumb: SliderThumb::Round,
}
.label(label)
}
@@ -118,6 +122,7 @@ impl<'a> SliderField<'a> {
unit_kind: self.unit_kind,
unit: self.unit.clone(),
fill: self.fill,
thumb: self.thumb,
reset: self.value.reset_value().copied(),
}
}
@@ -132,6 +137,7 @@ struct Look {
unit_kind: Option<UnitKind>,
unit: Option<Label>,
fill: bool,
thumb: SliderThumb,
/// The reset value of the composite's own binding, which both children restore on a double click.
reset: Option<f64>,
}
@@ -159,7 +165,7 @@ impl Look {
if let Some(check) = check {
bind = bind.check(check);
}
let slider = Slider::new(bind, self.range.clone()).scale(self.scale).fill(self.fill).label(name.clone());
let slider = Slider::new(bind, self.range.clone()).scale(self.scale).fill(self.fill).thumb(self.thumb).label(name.clone());
let slider = match face {
Some(face) => slider.track_face(face),
None => slider,