mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
HarmonyPanel: strip and grid span the panel's width; the centre column is marked
Swatch::marked draws a thin text-colour edge inside the outline. SwatchTable::span divides the width among a row's cells with per_row; SwatchTable::marked marks cells by index.
This commit is contained in:
@@ -281,7 +281,8 @@ impl GuideCache {
|
||||
|
||||
/// Color Guide: the base swatch (a click sets the base to the current colour) and the rule `Select`; the harmony strip
|
||||
/// (one cell per colour, tooltip the hex); the variation grid, one row per harmony colour from shades to tints (or
|
||||
/// cool to warm, muted to vivid); the grid's end labels. A click on any cell chooses its colour; Shift or Cmd selects
|
||||
/// cool to warm, muted to vivid), the harmony colour itself in the marked centre column; the grid's end labels. The
|
||||
/// strip's and the grid's cells share the panel's width. A click on any cell chooses its colour; Shift or Cmd selects
|
||||
/// several for Save Colors as Swatches.
|
||||
#[must_use = "show the panel with `show`"]
|
||||
#[derive(Pattern)]
|
||||
@@ -330,16 +331,27 @@ impl<'a> HarmonyPanel<'a> {
|
||||
let strip: Vec<(Label, Srgba)> = guide.colors.iter().map(|c| (Label::from(c.to_hex()), *c)).collect();
|
||||
let mut strip_selection = Selection::default();
|
||||
let out =
|
||||
SwatchTable::new(words.harmony.clone(), &strip).per_row(strip.len().max(1)).selection(Bind::bound(&mut strip_selection)).show_all(ui);
|
||||
SwatchTable::new(words.harmony.clone(), &strip)
|
||||
.per_row(strip.len().max(1))
|
||||
.span(true)
|
||||
.selection(Bind::bound(&mut strip_selection))
|
||||
.show_all(ui);
|
||||
if let Some(i) = out.selected {
|
||||
chosen = strip.get(i).map(|(_, c)| *c);
|
||||
}
|
||||
|
||||
let columns = guide.grid.first().map_or(1, Vec::len);
|
||||
let columns = guide.grid.first().map_or(1, Vec::len).max(1);
|
||||
let centre = guide.centre();
|
||||
let flat: Vec<(Label, Srgba)> = guide.grid.iter().flatten().map(|c| (Label::from(c.to_hex()), *c)).collect();
|
||||
let mut selection = if before_picked.guide == stamp { before_picked.selection.clone() } else { Selection::default() };
|
||||
let SwatchTableOutput { selected, chosen: cells, .. } =
|
||||
SwatchTable::new(words.variations.clone(), &flat).multiple(true).per_row(columns).selection(Bind::bound(&mut selection)).show_all(ui);
|
||||
SwatchTable::new(words.variations.clone(), &flat)
|
||||
.multiple(true)
|
||||
.per_row(columns)
|
||||
.span(true)
|
||||
.marked(move |&at| at % columns == centre)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.show_all(ui);
|
||||
if let Some(i) = selected
|
||||
&& cells.len() == 1
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
use craft_behavior::{DisplayOrder, EditEvent, ItemSource, Metrics, Mixed, Selection, TileLayout};
|
||||
use craft_theme::Srgba;
|
||||
use craft_ui::choice::{Swatch, SwatchMetrics};
|
||||
use craft_ui::list::RowTest;
|
||||
use craft_ui::contract::{
|
||||
ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, Common, Component, ContainerProps, Cx, Held, NoValue, Outcome, Pattern,
|
||||
Props, Role, SelectMode, ShowCollection as _, ShowContainer as _, Sizing,
|
||||
@@ -145,7 +146,8 @@ impl Outcome for SwatchTableOutput {
|
||||
}
|
||||
|
||||
/// A table of touching colour cells, one per entry, wrapping to the width or to a fixed count per row. Entries are
|
||||
/// told apart by index, so duplicates select one cell.
|
||||
/// told apart by index, so duplicates select one cell. With [`span`](Self::span) and a count per row, the cells share
|
||||
/// the table's width edge to edge; [`marked`](Self::marked) cells draw the swatch mark.
|
||||
///
|
||||
/// - A click or Space selects the cell; a double click or Enter reports `open` and the Enter is the table's, so a
|
||||
/// dialog around it does not also answer OK.
|
||||
@@ -168,6 +170,12 @@ pub struct SwatchTable<'a> {
|
||||
/// A fixed number of cells to a row instead of wrapping to the width.
|
||||
#[prop]
|
||||
per_row: Option<usize>,
|
||||
/// With `per_row`, the cells of a row divide the table's width among them, at the cell height.
|
||||
#[prop]
|
||||
span: bool,
|
||||
/// The cells that draw the swatch mark, by index.
|
||||
#[prop(func = "Fn(&usize) -> bool")]
|
||||
marked: Option<RowTest<'a, usize>>,
|
||||
/// Whether each cell's name is its tooltip.
|
||||
#[prop]
|
||||
tips: bool,
|
||||
@@ -186,6 +194,8 @@ impl<'a> SwatchTable<'a> {
|
||||
swatches,
|
||||
selection: None,
|
||||
per_row: None,
|
||||
span: false,
|
||||
marked: None,
|
||||
tips: false,
|
||||
wells: false,
|
||||
}
|
||||
@@ -200,12 +210,14 @@ impl<'a> SwatchTable<'a> {
|
||||
}
|
||||
|
||||
/// Shows the table.
|
||||
pub fn show_all(self, ui: &mut Ui) -> SwatchTableOutput {
|
||||
pub fn show_all(mut self, ui: &mut Ui) -> SwatchTableOutput {
|
||||
let (swatches, tips, wells) = (self.swatches, self.tips, self.wells);
|
||||
let marked = self.marked.take();
|
||||
self.show_items(ui, &SwatchRows::new(swatches), &mut |key, _| {
|
||||
let (name, color) = &swatches[*key];
|
||||
let cell = Swatch::new(name.clone(), *color).cell();
|
||||
let cell = if wells { cell.well() } else { cell };
|
||||
let cell = if marked.as_ref().is_some_and(|test| test.passes(key)) { cell.marked() } else { cell };
|
||||
if tips { cell.tooltip(name.clone()) } else { cell }
|
||||
})
|
||||
}
|
||||
@@ -261,10 +273,14 @@ impl Collection for SwatchTable<'_> {
|
||||
fn item_layout(&self, source: &dyn ItemSource<usize>, width: f32, cx: &StyleCx<'_>) -> TileLayout {
|
||||
let metrics = SwatchMetrics::of(cx);
|
||||
let cell = if self.wells { metrics.side } else { metrics.cell };
|
||||
let layout = TileLayout::new(source, width, Vec2::splat(cell), 0.0);
|
||||
match self.per_row {
|
||||
Some(columns) => layout.in_columns(columns),
|
||||
None => layout,
|
||||
Some(columns) => {
|
||||
let columns = columns.max(1);
|
||||
let spans = self.span && width.is_finite() && width > 0.0;
|
||||
let tile = if spans { Vec2::new(width / columns as f32, cell) } else { Vec2::splat(cell) };
|
||||
TileLayout::new(source, width, tile, 0.0).in_columns(columns)
|
||||
}
|
||||
None => TileLayout::new(source, width, Vec2::splat(cell), 0.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +168,27 @@ fn a_swatch_table_runs_as_many_components_for_many_cells_as_for_few() {
|
||||
assert_eq!(few, many, "the runs grow with the cells: {few} for 4 cells, {many} for 64");
|
||||
}
|
||||
|
||||
/// A spanning table of 4 cells to a row: the cells of a row cover the table's width edge to edge, each next to the one
|
||||
/// before without overlap, and the second row starts under the first.
|
||||
#[trial]
|
||||
fn a_spanning_swatch_table_divides_its_width_among_a_rows_cells() {
|
||||
let entries = table_entries(8);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ = SwatchTable::new("Color Table", &entries).per_row(4).span(true).show_all(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(400.0, 200.0)).settle();
|
||||
let table = fixture.by_role(Role::LIST_BOX).first().expect("the table").rect;
|
||||
let cell = |i: usize| fixture.by_label(&format!("Index {i}")).unwrap_or_else(|| panic!("cell {i}")).rect;
|
||||
let row: Vec<_> = (0..4).map(cell).collect();
|
||||
assert!((row[0].left() - table.left()).abs() < 0.5, "the first cell starts at the table's edge: {row:?} in {table:?}");
|
||||
assert!((row[3].right() - table.right()).abs() < 0.5, "the last cell ends at the table's edge: {row:?} in {table:?}");
|
||||
for pair in row.windows(2) {
|
||||
assert!(pair[1].left() >= pair[0].right() - 0.5 && pair[1].left() <= pair[0].right() + 0.5, "cells touch without overlap: {pair:?}");
|
||||
}
|
||||
assert!(row[0].width() > row[0].height(), "a spanning cell is wider than the square cell: {:?}", row[0]);
|
||||
assert!(cell(4).top() >= row[0].bottom() - 0.5 && (cell(4).left() - row[0].left()).abs() < 0.5, "the fifth cell starts the second row");
|
||||
}
|
||||
|
||||
/// A table's cell is a list box option named by its entry with the colour as its value; a click selects it, and the
|
||||
/// pointer's rest shows its name.
|
||||
#[trial]
|
||||
|
||||
@@ -56,6 +56,8 @@ pub struct SwatchStyle {
|
||||
edge: Stateful<StyleColor>,
|
||||
/// The ring inside the outline of a selected or checked well; clear while it is off.
|
||||
ring: Stateful<StyleColor>,
|
||||
/// The thin edge just inside the outline of a marked well.
|
||||
mark: StyleColor,
|
||||
/// The well of a mixed colour.
|
||||
mixed_fill: StyleColor,
|
||||
/// The dash on a mixed well.
|
||||
@@ -124,7 +126,8 @@ impl<E> crate::contract::ItemOutput<E> for SwatchOutput {
|
||||
/// translucent) and whose colour value is the colour; a mixed well has the value "Mixed" and no colour value.
|
||||
/// `member` makes the swatch a member of the enclosing `RadioGroup`: a radio button, checked when the group holds its
|
||||
/// value, whose keys and focus are the group's. `cell` makes it a cell of a table of touching swatches, a list box
|
||||
/// option. The selected ring is `selected` or a checked radio.
|
||||
/// option. The selected ring is `selected` or a checked radio. `marked` draws a thin edge in the text colour just inside
|
||||
/// the outline: a cell that stands out from its row, such as the harmony colour among its variations.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(content_painter, probe(Swatch<u8>))]
|
||||
pub struct Swatch<T: Value + 'static = ()> {
|
||||
@@ -144,6 +147,9 @@ pub struct Swatch<T: Value + 'static = ()> {
|
||||
/// Takes the width the layout offers, at the control height.
|
||||
#[prop(flag)]
|
||||
fill: bool,
|
||||
/// A thin edge in the text colour just inside the outline.
|
||||
#[prop(flag)]
|
||||
marked: bool,
|
||||
/// The key of the `ColorRequest` a click files.
|
||||
#[prop(into)]
|
||||
picker: Option<String>,
|
||||
@@ -161,6 +167,7 @@ impl Swatch<()> {
|
||||
cell: false,
|
||||
well: false,
|
||||
fill: false,
|
||||
marked: false,
|
||||
picker: None,
|
||||
checked: false,
|
||||
activation: None,
|
||||
@@ -179,6 +186,7 @@ impl<T: Value + 'static> Swatch<T> {
|
||||
cell: self.cell,
|
||||
well: self.well,
|
||||
fill: self.fill,
|
||||
marked: self.marked,
|
||||
picker: self.picker,
|
||||
checked: false,
|
||||
activation: None,
|
||||
@@ -208,6 +216,7 @@ impl<T: Value + 'static> Component for Swatch<T> {
|
||||
radius: if key.cell && !key.well { CornerRadius::ZERO } else { metrics.radius },
|
||||
edge: Stateful::new(rest).off(States::new(rest, rest, rest, disabled)),
|
||||
ring: Stateful::new(clear).on(States::new(accent, accent, accent, disabled)),
|
||||
mark: cx.color(colors.text.normal),
|
||||
mixed_fill: cx.color(colors.surface.sunken),
|
||||
mixed_mark: cx.color(colors.text.muted),
|
||||
checker: [colors.overlay.checker_light, colors.overlay.checker_dark],
|
||||
@@ -264,7 +273,7 @@ impl<T: Value + 'static> Component for Swatch<T> {
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
paint_face(&ui.draw(), cx.rect, cx.style, cx.state(), self.color);
|
||||
paint_face(&ui.draw(), cx.rect, cx.style, cx.state(), self.color, self.marked);
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> SwatchOutput {
|
||||
@@ -275,8 +284,8 @@ impl<T: Value + 'static> Component for Swatch<T> {
|
||||
}
|
||||
|
||||
/// The one swatch face painter: the well of `color` in `well` (a checkerboard behind a translucent colour, the dash
|
||||
/// of a mixed one), then the outline ring and the selection ring, in `state`.
|
||||
fn paint_face(draw: &Draw<'_>, well: Rect, style: &SwatchStyle, state: &StateSet, color: Mixed<Srgba>) {
|
||||
/// of a mixed one), then the outline ring, the mark when `marked`, and the selection ring, in `state`.
|
||||
fn paint_face(draw: &Draw<'_>, well: Rect, style: &SwatchStyle, state: &StateSet, color: Mixed<Srgba>, marked: bool) {
|
||||
let shape = PaintShape::Rect(well, style.radius);
|
||||
match color.same() {
|
||||
None => {
|
||||
@@ -291,6 +300,10 @@ fn paint_face(draw: &Draw<'_>, well: Rect, style: &SwatchStyle, state: &StateSet
|
||||
}
|
||||
}
|
||||
draw.stroke(&shape, StrokeWidth::Thin, state.resolve(&style.edge));
|
||||
if marked {
|
||||
let inside = PaintShape::Rect(well.shrink(draw.width(StrokeWidth::Thin)), style.radius);
|
||||
draw.stroke(&inside, StrokeWidth::Thin, style.mark);
|
||||
}
|
||||
draw.stroke(&shape, StrokeWidth::Thick, state.resolve(&style.ring));
|
||||
}
|
||||
|
||||
@@ -323,6 +336,6 @@ impl SwatchFace {
|
||||
let style = resolve_style(&scope, key, cx.painter.ctx(), Swatch::<()>::style);
|
||||
let (flags, on) = if self.selected { (Flags::SELECTED, 1.0) } else { (Flags::empty(), 0.0) };
|
||||
let state = StateSet::new(State::Rest, flags, StateMix::new(0.0, 0.0, on));
|
||||
cx.with_draw(|draw| paint_face(draw, rect, &style, &state, self.color));
|
||||
cx.with_draw(|draw| paint_face(draw, rect, &style, &state, self.color, false));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user