mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 21:29:54 +00:00
Color Picker channel rows: radio, letter and number field in one grid, no words beside the radios
This commit is contained in:
@@ -4,11 +4,10 @@ use craft_behavior::{ColumnWidth, IconId, Metrics, Mixed, NavShape, Sense};
|
||||
use craft_theme::{ColorModel, HsbModel, OklchModel, RgbModel, Srgba};
|
||||
use craft_ui::choice::{Checkbox, Radio, RadioGroup, Swatch};
|
||||
use craft_ui::contract::{
|
||||
Bind, Common, Component, Cx, GlyphLabel, LabelKind, LabelScheme, MeasureCx, Outcome, PaintUi, Props as _, Role, Show as _, ShowContainer as _,
|
||||
Bind, BodyCx, Common, Component, Cx, FieldLayout, LabelKind, MeasureCx, Outcome, PaintUi, Props as _, Role, Show as _, ShowContainer as _,
|
||||
SideLabel, Sizing,
|
||||
};
|
||||
use craft_ui::display::{Icon, Shade, Text};
|
||||
use craft_ui::form::{Field, FieldGroup};
|
||||
use craft_ui::layout_components::{Grid, Row, Stack};
|
||||
use craft_ui::primitives::IconSpec;
|
||||
use craft_ui::runtime::env::{Env, Label};
|
||||
@@ -36,6 +35,7 @@ pub struct ColorWarning {
|
||||
struct ModelChoice<'m> {
|
||||
visible: &'m [&'m dyn ColorModel],
|
||||
offered: &'m [AreaMode],
|
||||
words: &'m ColorPickerWords,
|
||||
}
|
||||
|
||||
impl ColorWarning {
|
||||
@@ -256,14 +256,16 @@ impl<'a> Body<'a> {
|
||||
};
|
||||
Grid::new(widths).show(ui, |ui, _| {
|
||||
for (column, models) in columns.iter().enumerate() {
|
||||
FieldGroup::new(words.channels.as_str().to_owned()).labels(LabelScheme::Inspector).show(ui, |ui, _| {
|
||||
// Each model column is its own grid of radio, letter and field cells, so the fields of a
|
||||
// column share one x whether or not their row has a radio.
|
||||
Grid::new(THREE_COLUMNS).show(ui, |ui, cx| {
|
||||
for model in models {
|
||||
self.model_fields(ui, pass, *model, ModelChoice { visible, offered: &offered }, words, got);
|
||||
self.model_fields(ui, cx, pass, *model, ModelChoice { visible, offered: &offered, words }, got);
|
||||
}
|
||||
if column == 0 {
|
||||
Field::new(words.hex.as_str().to_owned())
|
||||
.side(HexField::hash_label())
|
||||
.show(ui, |ui, _| self.hex_field(ui, pass, words, got));
|
||||
Text::new(Label::from("")).show(ui);
|
||||
Text::new(Label::from(HexField::HASH)).shade(Shade::Muted).show(ui);
|
||||
self.hex_field(ui, pass, words, got);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -275,53 +277,49 @@ impl<'a> Body<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The rows of one model's channels.
|
||||
/// The rows of one model's channels, three cells each: the radio that chooses the area mode (an empty cell where the
|
||||
/// channel offers none), the channel's letter and its number field. The radio's name stays its accessible name.
|
||||
fn model_fields(
|
||||
&self,
|
||||
ui: &mut Ui,
|
||||
cx: &mut BodyCx<'_>,
|
||||
pass: &Pass<'a>,
|
||||
model: &'a dyn ColorModel,
|
||||
choice: ModelChoice<'_>,
|
||||
words: &ColorPickerWords,
|
||||
got: &mut Collected,
|
||||
) {
|
||||
let ModelChoice { visible, offered } = choice;
|
||||
let ModelChoice { visible, offered, words } = choice;
|
||||
let values = pass.shown.values_in(model);
|
||||
for (i, channel) in model.channels().iter().enumerate() {
|
||||
let name = channel_name(model, i, visible);
|
||||
let short = words.grid_label.fill(&[("label", channel.label)]);
|
||||
let short: &str = &short;
|
||||
let label = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(Label::from(short.to_owned()))));
|
||||
let letter = words.grid_label.fill(&[("label", channel.label)]);
|
||||
let letter: &str = &letter;
|
||||
let mode = offered.iter().find(|mode| mode.model().id() == model.id() && mode.strip() == i).copied();
|
||||
match mode {
|
||||
Some(mode) => {
|
||||
let side = SideLabel::new().layout(FieldLayout::After).kind(LabelKind::Hidden);
|
||||
cx.labelled(ui, (model.id(), i), name.clone(), side, Radio::new(mode));
|
||||
}
|
||||
None => {
|
||||
Text::new(Label::from("")).show(ui);
|
||||
}
|
||||
}
|
||||
Text::new(Label::from(letter.to_owned())).shade(Shade::Muted).show(ui);
|
||||
let rule = ChannelRule::of(channel);
|
||||
let unit = ChannelUnit::of(channel.unit);
|
||||
let value = f64::from(values.values[i]);
|
||||
let edit = Field::new(name.clone())
|
||||
.side(label)
|
||||
.show(ui, |ui, _| {
|
||||
Row::new()
|
||||
.show(ui, |ui, _| {
|
||||
if let Some(mode) = mode {
|
||||
Radio::new(mode).label(name.clone()).show(ui);
|
||||
}
|
||||
let field = NumberField::new(name.clone(), Bind::given(pass.shows(Some(value))))
|
||||
.range(rule.range.clone())
|
||||
.step(rule.step)
|
||||
.snap_typed(true)
|
||||
.commit_decimals(usize::from(rule.decimals));
|
||||
let field = match unit {
|
||||
ChannelUnit::None => field,
|
||||
ChannelUnit::Percent => field.unit_kind(craft_ui::runtime::env::UnitKind::Percent),
|
||||
ChannelUnit::Degrees => field.unit_kind(craft_ui::runtime::env::UnitKind::Degrees),
|
||||
ChannelUnit::Text(text) => field.unit(Label::from(text)),
|
||||
};
|
||||
field.show(ui).edit
|
||||
})
|
||||
.inner
|
||||
})
|
||||
.inner
|
||||
.flatten();
|
||||
if let Some(edit) = edit {
|
||||
let field = NumberField::new(name, Bind::given(pass.shows(Some(value))))
|
||||
.range(rule.range.clone())
|
||||
.step(rule.step)
|
||||
.snap_typed(true)
|
||||
.commit_decimals(usize::from(rule.decimals));
|
||||
let field = match unit {
|
||||
ChannelUnit::None => field,
|
||||
ChannelUnit::Percent => field.unit_kind(craft_ui::runtime::env::UnitKind::Percent),
|
||||
ChannelUnit::Degrees => field.unit_kind(craft_ui::runtime::env::UnitKind::Degrees),
|
||||
ChannelUnit::Text(text) => field.unit(Label::from(text)),
|
||||
};
|
||||
if let Some(edit) = field.show(ui).edit {
|
||||
got.adopt(edit, pass.shown, |v, mine| {
|
||||
if let Some(v) = v {
|
||||
*mine = mine.with_channel(model, i, *v, &pass.rules);
|
||||
|
||||
@@ -4,10 +4,10 @@ use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft_behavior::Mixed;
|
||||
use craft_color_ui::ui::{ColorPickerDialog, ColorReadout, PickedColor};
|
||||
use craft_color_ui::ui::{ColorPicker, ColorPickerDialog, ColorPickerLayout, ColorReadout, PickedColor};
|
||||
use craft_host::{AppInfo, Host};
|
||||
use craft_theme::{ModelValues, Rgba, Srgba};
|
||||
use craft_ui::contract::Bind;
|
||||
use craft_theme::{ColorModel, HsbModel, LabModel, ModelValues, RgbModel, Rgba, Srgba};
|
||||
use craft_ui::contract::{Bind, Role, Show as _};
|
||||
use craft_ui::dialog::{DialogHost, DialogResult, dialog_id};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, vec2};
|
||||
@@ -117,3 +117,32 @@ fn a_dialog_reopened_keeps_only_web_colors_and_recalls_the_values_kept_on_ok() {
|
||||
fixture.hold(Modifiers::NONE).idle(2);
|
||||
assert_eq!(edited(&dialogs), Some(values), "Reset returns the typed values");
|
||||
}
|
||||
|
||||
/// The Full picker's channel rows show a letter after each radio and no channel words; the radios keep their names, and
|
||||
/// the number fields of a column, the hex field included, start at one x whether or not their row has a radio.
|
||||
#[trial]
|
||||
fn the_full_pickers_channel_rows_show_letters_and_align_their_fields() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let models: [&dyn ColorModel; 3] = [&HsbModel, &RgbModel, &LabModel];
|
||||
let mut color = PickedColor::rgb(Srgba::rgb(0.2, 0.4, 0.6));
|
||||
let _ = ColorPicker::new("Color", Bind::bound(&mut color)).layout(ColorPickerLayout::Full).models(models).show(ui);
|
||||
});
|
||||
fixture.room(vec2(900.0, 600.0));
|
||||
fixture.settle();
|
||||
let texts = fixture.texts();
|
||||
for word in ["Hue", "Saturation", "Brightness", "Red", "Lightness"] {
|
||||
assert!(texts.iter().all(|run| run.text != word), "{word} is not shown beside its radio");
|
||||
}
|
||||
for letter in ["H:", "S:", "R:", "L:"] {
|
||||
assert!(texts.iter().any(|run| run.text == letter), "{letter} is shown");
|
||||
}
|
||||
assert_eq!(fixture.by_role(Role::RADIO_BUTTON).len(), 9, "every channel keeps its radio");
|
||||
let fields = fixture.by_role(Role::SPIN_BUTTON);
|
||||
assert_eq!(fields.len(), 9);
|
||||
let first = fields[0].rect.min.x;
|
||||
assert!(fields[..6].iter().all(|field| (field.rect.min.x - first).abs() < 0.5), "HSB and RGB fields share a column");
|
||||
let hex = fixture.by_label("Hex color").expect("the hex field");
|
||||
assert!((hex.rect.min.x - first).abs() < 0.5, "the hex field, which has no radio, starts in the same column");
|
||||
let lab = fields[6].rect.min.x;
|
||||
assert!(fields[6..].iter().all(|field| (field.rect.min.x - lab).abs() < 0.5), "Lab fields share a column");
|
||||
}
|
||||
|
||||
@@ -119,9 +119,12 @@ impl<'a> TextField<'a, HexCodec> {
|
||||
field
|
||||
}
|
||||
|
||||
/// The text before a hex field.
|
||||
pub const HASH: &'static str = "#";
|
||||
|
||||
/// The `#` before a hex field: the glyph side label of the `Field` that holds it, in every place a hex field shows.
|
||||
pub fn hash_label() -> SideLabel {
|
||||
SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text("#")))
|
||||
SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text(Self::HASH)))
|
||||
}
|
||||
|
||||
/// Digits per channel: 2 (`RRGGBB`, the default) or 1 (`RGB`, a web colour).
|
||||
|
||||
Reference in New Issue
Block a user