mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
color-w1: net change of 1 commits
- color-w1: written from its sheet
This commit is contained in:
@@ -13,6 +13,9 @@ use std::sync::Arc;
|
||||
#[cfg(feature = "screen-testing")]
|
||||
mod stories;
|
||||
|
||||
/// The table's drag mapper, type-erased so the palette needs no payload parameter.
|
||||
type TableMap<'a> = Arc<dyn Fn(SwatchTable<'a>) -> SwatchTable<'a> + 'a>;
|
||||
|
||||
/// What a [`SwatchPalette`] reports.
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
@@ -49,38 +52,46 @@ pub struct SwatchPalette<'a> {
|
||||
label: Label,
|
||||
color: Bind<'a, Srgba>,
|
||||
swatches: &'a [(Label, Srgba)],
|
||||
spacing: SwatchSpacing,
|
||||
drag: Option<TableMap<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> SwatchPalette<'a> {
|
||||
/// A palette named `label` of `swatches`, each a name and a colour, choosing the colour bound to `color`.
|
||||
pub fn new(label: impl Into<Label>, color: Bind<'a, Srgba>, swatches: &'a [(Label, Srgba)]) -> Self {
|
||||
SwatchPalette { label: label.into(), color, swatches }
|
||||
SwatchPalette { label: label.into(), color, swatches, spacing: SwatchSpacing::Touching, drag: None }
|
||||
}
|
||||
|
||||
/// The gap between the cells, passed on to its `SwatchTable`.
|
||||
#[must_use]
|
||||
pub fn spacing(self, spacing: SwatchSpacing) -> Self {
|
||||
let _ = spacing;
|
||||
todo!("co.K7")
|
||||
pub fn spacing(mut self, spacing: SwatchSpacing) -> Self {
|
||||
self.spacing = spacing;
|
||||
self
|
||||
}
|
||||
|
||||
/// The payload a cell drags, passed on to its `SwatchTable`.
|
||||
#[must_use]
|
||||
pub fn drag_payload<P: Send + Sync + 'static>(self, map: impl Fn(&ListDrag<usize>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
let _ = map;
|
||||
todo!("co.K7")
|
||||
pub fn drag_payload<P: Send + Sync + 'static>(mut self, map: impl Fn(&ListDrag<usize>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
let map = Arc::new(map);
|
||||
self.drag = Some(Arc::new(move |table| {
|
||||
let map = Arc::clone(&map);
|
||||
table.drag_payload(move |drag| map(drag))
|
||||
}));
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows the palette.
|
||||
pub fn show(self, ui: &mut Ui) -> SwatchPaletteOutput {
|
||||
let SwatchPalette { label, color, swatches } = self;
|
||||
let SwatchPalette { label, color, swatches, spacing, drag } = self;
|
||||
let mut held = Held::new(color);
|
||||
let name = label.clone();
|
||||
let mut shown = Stack::new().show(ui, |ui, cx| {
|
||||
let before = held.read(cx)?;
|
||||
let selected = swatches.iter().position(|(_, c)| c.to_rgba() == before.to_rgba());
|
||||
let mut current = selected.map_or_else(Selection::default, Selection::single);
|
||||
let table = SwatchTable::new(label.clone(), swatches).tips(true).wells(true).selection(Bind::bound(&mut current)).show_all(ui);
|
||||
let table = SwatchTable::new(label.clone(), swatches).tips(true).wells(true).spacing(spacing).selection(Bind::bound(&mut current));
|
||||
let table = drag.as_ref().map_or(table, |map| map(table));
|
||||
let table = table.show_all(ui);
|
||||
let picked = table.selected.or(table.open)?;
|
||||
let after = swatches.get(picked)?.1;
|
||||
if after.to_rgba() == before.to_rgba() {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use super::SwatchPalette;
|
||||
use craft_theme::Srgba;
|
||||
use craft_ui::choice::SwatchSpacing;
|
||||
use craft_ui::contract::Bind;
|
||||
use craft_ui::runtime::env::Label;
|
||||
use craft_ui::testing::{Drive, StoryData, StoryOutput, story};
|
||||
@@ -27,10 +28,10 @@ fn named(entries: impl IntoIterator<Item = (String, Srgba)>) -> Vec<(Label, Srgb
|
||||
entries.into_iter().map(|(name, color)| (Label::from(name), color)).collect()
|
||||
}
|
||||
|
||||
#[story(SwatchPalette<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_palette(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
/// The palette of the twelve colours, set up by `options`, with the readout of its latest event.
|
||||
fn shown_palette(ui: &mut Ui, data: &mut StoryData, options: impl for<'a> FnOnce(SwatchPalette<'a>) -> SwatchPalette<'a>) -> StoryOutput {
|
||||
let swatches = named(PALETTE.iter().map(|(name, color)| ((*name).to_owned(), *color)));
|
||||
let shown = SwatchPalette::new("Swatches", Bind::bound(data.value("color", PALETTE[8].1)), &swatches).show(ui);
|
||||
let shown = options(SwatchPalette::new("Swatches", Bind::bound(data.value("color", PALETTE[8].1)), &swatches)).show(ui);
|
||||
let last = data.note("last", String::new());
|
||||
if let Some(event) = &shown.edit {
|
||||
*last = format!("{event:?}");
|
||||
@@ -38,3 +39,18 @@ fn swatch_palette(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let readout = format!("Last event: {last}");
|
||||
StoryOutput::from(shown).readout(readout)
|
||||
}
|
||||
|
||||
#[story(SwatchPalette<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_palette(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
shown_palette(ui, data, |palette| palette)
|
||||
}
|
||||
|
||||
#[story(SwatchPalette<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_palette_spaced(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
shown_palette(ui, data, |palette| palette.spacing(SwatchSpacing::Spaced))
|
||||
}
|
||||
|
||||
#[story(SwatchPalette<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_palette_spaced_draggable(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
shown_palette(ui, data, |palette| palette.spacing(SwatchSpacing::Spaced).drag_payload(|_| Some(String::from("swatch"))))
|
||||
}
|
||||
|
||||
@@ -108,15 +108,19 @@ impl<'a> Gamut<'a> {
|
||||
/// The gamut of a CMYK working space: the nearest in-gamut colour by [`CmykConverter::nearest_in_gamut`] with
|
||||
/// [`CmykConverter::PICKER_GAMUT_DE`], the colour itself inside.
|
||||
pub fn cmyk(converter: CmykConverter) -> Gamut<'static> {
|
||||
let _ = converter;
|
||||
todo!("co.K2")
|
||||
Gamut::new(move |c| match converter.nearest_in_gamut([c.r, c.g, c.b], CmykConverter::PICKER_GAMUT_DE) {
|
||||
Some([r, g, b]) => Srgba::new(r, g, b, c.a),
|
||||
None => c,
|
||||
})
|
||||
}
|
||||
|
||||
/// The mapped colour when it differs from `color` by more than one 8-bit step, else `None`: the one out-of-gamut
|
||||
/// test every warning chip and `ColorArea`'s shading use.
|
||||
pub fn nearest(&self, color: Srgba) -> Option<Srgba> {
|
||||
let _ = color;
|
||||
todo!("co.K3")
|
||||
let mapped = (self.0)(color);
|
||||
let step = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as i32;
|
||||
let (a, b) = ([color.r, color.g, color.b, color.a].map(step), [mapped.r, mapped.g, mapped.b, mapped.a].map(step));
|
||||
a.iter().zip(&b).any(|(x, y)| (x - y).abs() > 1).then_some(mapped)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +134,13 @@ pub struct RampRecolor {
|
||||
pub other: bool,
|
||||
}
|
||||
|
||||
impl RampRecolor {
|
||||
/// A recolour request with `invert` when Shift was held and `other` when Alt was.
|
||||
pub const fn new(invert: bool, other: bool) -> Self {
|
||||
RampRecolor { invert, other }
|
||||
}
|
||||
}
|
||||
|
||||
/// What [`ColorRamp`] returns.
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
@@ -163,6 +174,7 @@ impl Outcome for ColorRampOutput {
|
||||
struct Held {
|
||||
shift: bool,
|
||||
alt: bool,
|
||||
cmd: bool,
|
||||
/// The ends of a Between ramp at the press.
|
||||
ends: Option<[Srgba; 2]>,
|
||||
}
|
||||
@@ -190,7 +202,8 @@ struct Cache {
|
||||
/// moves, also outside the bar, where the pick stays at the edge, and commits at the release. A
|
||||
/// [`Between`](RampKind::Between) ramp keeps the ends it had at the press for the whole gesture. Alt at the press
|
||||
/// sets [`other`](RampPick::other) on every event. A click with Shift starts no gesture and sets
|
||||
/// [`cycle`](ColorRampOutput::cycle). A secondary click on the bar is reported as
|
||||
/// [`cycle`](ColorRampOutput::cycle). With [`recolor_clicks`](ColorRamp::recolor_clicks), a click with Cmd or Ctrl
|
||||
/// starts no gesture, picks nothing and sets [`recolor`](ColorRampOutput::recolor). A secondary click on the bar is reported as
|
||||
/// [`menu_at`](ColorRampOutput::menu_at).
|
||||
/// - Boxes: a click on the white or the black box (also by keyboard) is a `Set` of that colour, with
|
||||
/// [`other`](RampPick::other) when Alt is held; they are Tab stops of their own.
|
||||
@@ -212,7 +225,6 @@ pub struct ColorRamp<'a> {
|
||||
ends: Option<[Srgba; 2]>,
|
||||
/// Maps each colour of a spectrum ramp to the host's gamut (a CMYK round trip). It runs for the cached colours
|
||||
/// only, and again for a pick.
|
||||
// P3(color-w1): co.K1
|
||||
#[prop]
|
||||
gamut: Option<Gamut<'a>>,
|
||||
/// The host's counter for the working space `gamut` maps into; a change recomputes the colours.
|
||||
@@ -236,6 +248,7 @@ pub struct ColorRamp<'a> {
|
||||
cursor: (usize, usize),
|
||||
cycle: bool,
|
||||
menu_at: Option<Pos2>,
|
||||
recolor: Option<RampRecolor>,
|
||||
}
|
||||
|
||||
impl<'a> ColorRamp<'a> {
|
||||
@@ -258,6 +271,7 @@ impl<'a> ColorRamp<'a> {
|
||||
cursor: (0, CELLS_Y / 2),
|
||||
cycle: false,
|
||||
menu_at: None,
|
||||
recolor: None,
|
||||
}
|
||||
.label(label)
|
||||
.ltr_axis()
|
||||
@@ -292,7 +306,6 @@ impl<'a> ColorRamp<'a> {
|
||||
}
|
||||
|
||||
fn gamut_fn(&self) -> Option<&dyn Fn(Srgba) -> Srgba> {
|
||||
// P3(color-w1): co.K1
|
||||
self.gamut.as_ref().map(|gamut| &*gamut.0 as &dyn Fn(Srgba) -> Srgba)
|
||||
}
|
||||
|
||||
@@ -426,14 +439,18 @@ impl Component for ColorRamp<'_> {
|
||||
let configured = self.ends.unwrap_or_else(Self::default_ends);
|
||||
let press = cx.part(BAR).press;
|
||||
if primary(&press) && matches!(press.phase, PressPhase::Down { .. }) {
|
||||
*cx.memory::<Held>() = Held { shift: press.mods.shift, alt: press.mods.alt, ends: Some(configured) };
|
||||
*cx.memory::<Held>() = Held { shift: press.mods.shift, alt: press.mods.alt, cmd: press.mods.cmd, ends: Some(configured) };
|
||||
}
|
||||
let held = *cx.memory::<Held>();
|
||||
let running = cx.gesture().is_some();
|
||||
let ends = if running || press.phase.is_press() { held.ends.unwrap_or(configured) } else { configured };
|
||||
self.shown_ends = ends;
|
||||
let mut cursor = cx.memory::<Cursor>().0.unwrap_or((0, CELLS_Y / 2));
|
||||
if primary(&press) && !held.shift {
|
||||
if primary(&press) && self.recolor_clicks && held.cmd {
|
||||
if matches!(press.phase, PressPhase::Release { click: true, .. }) {
|
||||
self.recolor = Some(RampRecolor::new(held.shift, held.alt));
|
||||
}
|
||||
} else if primary(&press) && !held.shift {
|
||||
let mut picked = None;
|
||||
cx.follow_press(BAR, |pos| {
|
||||
let (fx, fy) = fractions(bar, pos);
|
||||
@@ -512,8 +529,8 @@ impl Component for ColorRamp<'_> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> ColorRampOutput {
|
||||
let (cycle, menu_at) = (self.cycle, self.menu_at);
|
||||
ColorRampOutput { edit: cx.edit_response(), cycle, menu_at, recolor: None }
|
||||
let (cycle, menu_at, recolor) = (self.cycle, self.menu_at, self.recolor);
|
||||
ColorRampOutput { edit: cx.edit_response(), cycle, menu_at, recolor }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,17 @@ fn color_ramp_web_safe(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
ColorRamp::new("Color ramp", RampKind::Spectrum, Bind::bound(data.value("pick", pick()))).web_safe(true).kind_name("Web colors").show(ui).into()
|
||||
}
|
||||
|
||||
#[story(ColorRamp<'static>, covers = [], drive = Drive::Never)]
|
||||
fn color_ramp_recolor_clicks(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let shown = ColorRamp::new("Color ramp", RampKind::Spectrum, Bind::bound(data.value("pick", pick()))).recolor_clicks(true).kind_name("RGB Spectrum").show(ui);
|
||||
let last = data.note("last", String::new());
|
||||
if let Some(recolor) = shown.recolor {
|
||||
*last = format!("{recolor:?}");
|
||||
}
|
||||
let readout = format!("Last recolor: {last}");
|
||||
StoryOutput::from(shown).readout(readout)
|
||||
}
|
||||
|
||||
#[story(ColorRamp<'static>, covers = [Invalid], drive = Drive::Never)]
|
||||
fn color_ramp_invalid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let refuse_dark = |pick: &RampPick| if pick.color.b < 0.5 { Validity::Refuse(Message::new("Too dark")) } else { Validity::Ok };
|
||||
|
||||
@@ -5,7 +5,6 @@ use crate::contract::{
|
||||
ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, Common, Component, ContainerProps, Cx, ListDrag, NoValue, Outcome,
|
||||
Props, Role, SelectMode, ShowCollection as _, Sizing,
|
||||
};
|
||||
use crate::list::RowTest;
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use craft_behavior::{DisplayOrder, ItemSource, Metrics, Selection, TileLayout};
|
||||
@@ -58,13 +57,18 @@ pub enum SwatchSpacing {
|
||||
}
|
||||
|
||||
/// The marks of the cell of a key: `SwatchTable::marks`.
|
||||
pub struct RowMarks<'a, K>(#[allow(dead_code)] Box<dyn Fn(&K) -> CellMarks + 'a>); // P3(color-w1): ex.E20
|
||||
pub struct RowMarks<'a, K>(Box<dyn Fn(&K) -> CellMarks + 'a>);
|
||||
|
||||
impl<'a, K> RowMarks<'a, K> {
|
||||
/// The marks `marks` gives.
|
||||
pub fn new(marks: impl Fn(&K) -> CellMarks + 'a) -> Self {
|
||||
RowMarks(Box::new(marks))
|
||||
}
|
||||
|
||||
/// The marks of the cell of `key`.
|
||||
pub fn of(&self, key: &K) -> CellMarks {
|
||||
(self.0)(key)
|
||||
}
|
||||
}
|
||||
|
||||
/// What a [`SwatchTable`] reports.
|
||||
@@ -94,7 +98,7 @@ 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. 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.
|
||||
/// the table's width edge to edge; [`marks`](Self::marks) gives each cell its marks.
|
||||
///
|
||||
/// - 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.
|
||||
@@ -120,9 +124,6 @@ pub struct SwatchTable<'a> {
|
||||
/// 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,
|
||||
@@ -131,12 +132,13 @@ pub struct SwatchTable<'a> {
|
||||
wells: bool,
|
||||
/// How the cells sit: touching, or the theme's item gap apart.
|
||||
#[prop]
|
||||
#[allow(dead_code)] // P3(color-w1): co.K5
|
||||
spacing: SwatchSpacing,
|
||||
/// The marks of each cell, by index, given to the cell's `Swatch::marks`; in place of `marked`.
|
||||
/// The marks of each cell, by index, given to the cell's `Swatch::marks`.
|
||||
#[prop(func = "Fn(&usize) -> CellMarks")]
|
||||
#[allow(dead_code)] // P3(color-w1): ex.E20
|
||||
marks: Option<RowMarks<'a, usize>>,
|
||||
/// Only the cells of the first `n` rows at the laid-out column count are items; the rest are not shown.
|
||||
#[prop]
|
||||
max_rows: Option<usize>,
|
||||
}
|
||||
|
||||
impl<'a> SwatchTable<'a> {
|
||||
@@ -151,16 +153,21 @@ impl<'a> SwatchTable<'a> {
|
||||
SwatchTable {
|
||||
common: Common::new(),
|
||||
props: ContainerProps::new().sizing(Sizing::FillWidth),
|
||||
items: CollectionProps::new().select(SelectMode::Single).activate(ActivateOn::DoubleClick).arrows_select(false).natural_height(true),
|
||||
items: CollectionProps::new()
|
||||
.select(SelectMode::Single)
|
||||
.activate(ActivateOn::DoubleClick)
|
||||
.arrows_select(false)
|
||||
.natural_height(true)
|
||||
.deletable(true),
|
||||
swatches,
|
||||
selection: None,
|
||||
per_row: None,
|
||||
span: false,
|
||||
marked: None,
|
||||
tips: false,
|
||||
wells: false,
|
||||
spacing: SwatchSpacing::Touching,
|
||||
marks: None,
|
||||
max_rows: None,
|
||||
}
|
||||
.label(label)
|
||||
}
|
||||
@@ -168,8 +175,37 @@ impl<'a> SwatchTable<'a> {
|
||||
/// Hands the drag of the cells to the host as `map` gives its payload, as `TileGrid::drag_payload` does.
|
||||
#[must_use]
|
||||
pub fn drag_payload<P: Send + Sync + 'static>(self, map: impl Fn(&ListDrag<usize>) -> Option<P> + Send + Sync + 'static) -> Self {
|
||||
let _ = map;
|
||||
todo!("co.K6")
|
||||
self.items = self.items.drag_payload(map);
|
||||
self
|
||||
}
|
||||
|
||||
/// What activates a cell: a double click by default.
|
||||
#[must_use]
|
||||
pub fn activate(mut self, on: ActivateOn) -> Self {
|
||||
self.items = self.items.activate(on);
|
||||
self
|
||||
}
|
||||
|
||||
/// The gap between two cells.
|
||||
fn gap(&self, cx: &StyleCx<'_>) -> f32 {
|
||||
match self.spacing {
|
||||
SwatchSpacing::Spaced => cx.tokens.space.gap.item,
|
||||
_ => 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// The layout of the cells of `source` in `width`.
|
||||
fn tiles(&self, source: &dyn ItemSource<usize>, width: f32, cx: &StyleCx<'_>) -> TileLayout {
|
||||
let (cell, gap) = (self.cell(cx), self.gap(cx));
|
||||
match self.per_row {
|
||||
Some(columns) => {
|
||||
let columns = columns.max(1);
|
||||
let spans = self.span && width.is_finite() && width > 0.0;
|
||||
let tile = if spans { Vec2::new((width - gap * (columns - 1) as f32) / columns as f32, cell) } else { Vec2::splat(cell) };
|
||||
TileLayout::new(source, width, tile, gap).in_columns(columns)
|
||||
}
|
||||
None => TileLayout::new(source, width, Vec2::splat(cell), gap),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether Shift or Cmd adds cells to the selection.
|
||||
@@ -182,12 +218,15 @@ impl<'a> SwatchTable<'a> {
|
||||
/// Shows the table.
|
||||
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();
|
||||
let marks = self.marks.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 };
|
||||
let cell = match &marks {
|
||||
Some(marks) => cell.marks(marks.of(key)),
|
||||
None => cell,
|
||||
};
|
||||
if tips { cell.tooltip(name.clone()) } else { cell }
|
||||
})
|
||||
}
|
||||
@@ -220,13 +259,16 @@ impl Component for SwatchTable<'_> {
|
||||
_ => None,
|
||||
});
|
||||
let changed = response.events.iter().any(|event| matches!(event, CollectionEvent::Selection { .. })); // P3(mechanical): dp.C12
|
||||
SwatchTableOutput {
|
||||
response: response.response,
|
||||
selected: selection.lead.filter(|_| changed),
|
||||
chosen: selection.items,
|
||||
open,
|
||||
delete: Vec::new(),
|
||||
} // P3(color-w1): ex.E20
|
||||
let delete = response
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
CollectionEvent::Delete(keys) => Some(keys.iter().copied()),
|
||||
_ => None,
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
SwatchTableOutput { response: response.response, selected: selection.lead.filter(|_| changed), chosen: selection.items, open, delete }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,19 +293,14 @@ impl Collection for SwatchTable<'_> {
|
||||
fn ideal_width(&self, _item: Option<Vec2>, cx: &StyleCx<'_>) -> f32 {
|
||||
let columns = self.per_row.map_or(self.swatches.len(), |columns| columns.max(1).min(self.swatches.len()));
|
||||
// Half a point over, so the division that counts the columns back never rounds below `columns`.
|
||||
columns as f32 * self.cell(cx) + 0.5
|
||||
columns as f32 * self.cell(cx) + columns.saturating_sub(1) as f32 * self.gap(cx) + 0.5
|
||||
}
|
||||
|
||||
fn item_layout(&self, source: &dyn ItemSource<usize>, width: f32, cx: &StyleCx<'_>) -> TileLayout {
|
||||
let cell = self.cell(cx);
|
||||
match self.per_row {
|
||||
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),
|
||||
}
|
||||
let layout = self.tiles(source, width, cx);
|
||||
let Some(rows) = self.max_rows else { return layout };
|
||||
// The cap is a count of the cells the room lays out in `rows` rows, so it follows the layout's columns.
|
||||
let keep = rows.saturating_mul(layout.columns()).min(self.swatches.len());
|
||||
self.tiles(&SwatchRows::new(&self.swatches[..keep]), width, cx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Stories of `SwatchTable`.
|
||||
|
||||
use super::{SwatchTable, SwatchTableOutput};
|
||||
use crate::contract::Bind;
|
||||
use super::{CellMarks, SwatchSpacing, SwatchTable, SwatchTableOutput};
|
||||
use crate::contract::{ActivateOn, Bind};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use craft_behavior::Selection;
|
||||
@@ -50,6 +50,60 @@ fn swatch_table_multiple(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
with_readout(data, shown)
|
||||
}
|
||||
|
||||
#[story(SwatchTable<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_table_spaced(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = table_entries();
|
||||
let shown = SwatchTable::new("Color Table", &entries)
|
||||
.spacing(SwatchSpacing::Spaced)
|
||||
.selection(Bind::bound(data.value("selected", Selection::single(5usize))))
|
||||
.per_row(16)
|
||||
.show_all(ui);
|
||||
with_readout(data, shown)
|
||||
}
|
||||
|
||||
#[story(SwatchTable<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_table_spaced_draggable(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = table_entries();
|
||||
let shown = SwatchTable::new("Color Table", &entries)
|
||||
.spacing(SwatchSpacing::Spaced)
|
||||
.drag_payload(|_| Some(String::from("swatch")))
|
||||
.selection(Bind::bound(data.value("selected", Selection::single(5usize))))
|
||||
.per_row(16)
|
||||
.show_all(ui);
|
||||
with_readout(data, shown)
|
||||
}
|
||||
|
||||
#[story(SwatchTable<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_table_marks(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = table_entries();
|
||||
let start = Selection::new(vec![2usize, 3], Some(3), Some(2));
|
||||
let shown = SwatchTable::new("Color Table", &entries)
|
||||
.multiple(true)
|
||||
.marks(|&at| match at {
|
||||
1 => CellMarks::default().edge(true),
|
||||
3 => CellMarks::default().locked(true),
|
||||
5 => CellMarks::default().web_safe(true),
|
||||
7 => CellMarks::default().transparent(true),
|
||||
9 => CellMarks::default().source(Srgba::rgb(0.9, 0.2, 0.1)),
|
||||
_ => CellMarks::default(),
|
||||
})
|
||||
.selection(Bind::bound(data.value("selected", start)))
|
||||
.per_row(16)
|
||||
.show_all(ui);
|
||||
with_readout(data, shown)
|
||||
}
|
||||
|
||||
#[story(SwatchTable<'static>, covers = [], drive = Drive::Never)]
|
||||
fn swatch_table_one_row_click(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = table_entries();
|
||||
let shown = SwatchTable::new("Color Table", &entries)
|
||||
.max_rows(1)
|
||||
.activate(ActivateOn::Click)
|
||||
.selection(Bind::bound(data.value("selected", Selection::single(5usize))))
|
||||
.show_all(ui);
|
||||
with_readout(data, shown)
|
||||
}
|
||||
|
||||
#[story(SwatchTable<'static>, covers = [Empty], drive = Drive::Never)]
|
||||
fn swatch_table_empty(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
SwatchTable::new("Color Table", &[]).empty("No colors").show_all(ui).into()
|
||||
|
||||
Reference in New Issue
Block a user