mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Swatches: a Swatch Strip panel of one-click color chips
Window › Swatch Strip shows a None chip and then the document's color
swatches as a row of chips that wraps to the panel's room and scrolls
when the chips do not fit. Docked on the window's bottom edge the chips
run across in one line; in a dock column or a narrow floating window
they run down it. Gradient and pattern swatches are left out.
A click on a chip sets the fill (paint.setFill) and a right-click or an
Alt-click sets the stroke (paint.setStroke). Both pass focus: false, so
the active Fill or Stroke proxy stays as it was.
- window.panel {panel: "swatchStrip"} shows the panel, and
window.panel.dock with placement {"Edge": "Bottom"} beside the canvas
docks it along the bottom edge.
- The Swatches panel's shown() is shared so the strip draws a spot
color the way its art shows it.
Docs: docs/control-protocol.md, ROADMAP.
Fixes #10951
This commit is contained in:
@@ -124,6 +124,7 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
|
||||
## Progress log
|
||||
|
||||
- 2026-12-05: Window › Swatch Strip shows a None chip and the document's color swatches as a strip of chips that wraps to the panel's room: docked on the window's bottom edge it runs across in one line, and in a dock column or a narrow floating window it runs down. A click sets the fill and a right-click or Alt-click sets the stroke (#10951). A VectorCraft addition: headline numbers unchanged.
|
||||
- 2026-12-04: Raster Settings OK edits the objects selected when the dialog opened. It keeps the dialog open with the reason if its document is not active, was reverted or had an Undo, a Redo or a canceled edit that put back an earlier state, if a target was changed, deleted, locked or hidden, if another edit is in progress, or if the change is refused. Headline numbers unchanged.
|
||||
- 2026-12-04: Paste from CAD apps: on Windows, Paste reads the enhanced metafile CAD and office apps copy as vector art, and a `.dxf`, `.dwg` or HP-GL plot file copied in a file manager pastes as its drawing through the new command `clipboard.importCad` (#10819). Headline numbers unchanged.
|
||||
|
||||
|
||||
@@ -798,6 +798,7 @@ fn built_in_tree(english_names: bool, listed: &Listed) -> Vec<(&'static str, Vec
|
||||
panel("Separations Preview", "separations"),
|
||||
panel("Stroke", "stroke"),
|
||||
panel("SVG Interactivity", crate::panels::svg_interactivity::ID),
|
||||
panel("Swatch Strip", crate::panels::swatch_strip::ID),
|
||||
panel("Swatches", "swatches"),
|
||||
panel("Symbols", "symbols"),
|
||||
panel("Transform", "transform"),
|
||||
|
||||
@@ -46,6 +46,7 @@ pub mod script_console;
|
||||
pub mod separations;
|
||||
pub mod stroke;
|
||||
pub mod svg_interactivity;
|
||||
pub mod swatch_strip;
|
||||
pub mod swatches;
|
||||
pub mod symbols;
|
||||
pub mod tabs;
|
||||
@@ -568,6 +569,7 @@ pub(crate) fn set_part_hidden(app: &mut VectorcraftApp, key: &'static str, on: b
|
||||
pub fn show_icon_panel(app: &mut VectorcraftApp, ui: &mut Ui, id: &str) {
|
||||
match id {
|
||||
"swatches" => swatches::show(app, ui),
|
||||
swatch_strip::ID => swatch_strip::show(app, ui),
|
||||
"color" => color::show(app, ui),
|
||||
"colorGuide" => color_guide::show(app, ui),
|
||||
"stroke" => stroke::show(app, ui),
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
//! Swatch Strip panel (Window › Swatch Strip): a None chip, then the document's color swatches, in a row of chips
|
||||
//! that wraps to the room the panel has. Docked along the bottom of the window (`window.panel.dock {panel:
|
||||
//! "swatchStrip", anchor: "canvas", placement: {"Edge": "Bottom"}}`, or a drop on the bottom edge) the chips run
|
||||
//! across in one line; in a dock column or a narrow floating window they wrap down the column, and scroll when they
|
||||
//! do not fit.
|
||||
//!
|
||||
//! A click on a chip sets the fill (`paint.setFill`); a right-click or an Alt-click sets the stroke
|
||||
//! (`paint.setStroke`). Neither changes which of the Fill and Stroke proxies is active.
|
||||
|
||||
use craft::theme::Srgba;
|
||||
use craft::ui::choice::Swatch;
|
||||
use craft::ui::contract::{Props as _, Scroll, Show as _, ShowContainer as _};
|
||||
use craft::ui::layout_components::{Row, Stack};
|
||||
use egui::{PointerButton, Ui};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::Paint;
|
||||
|
||||
use super::srgba;
|
||||
use crate::VectorcraftApp;
|
||||
|
||||
/// The panel's id, as `window.panel` takes it.
|
||||
pub const ID: &str = "swatchStrip";
|
||||
|
||||
/// One chip: its name, the color it shows (`None`: the None chip) and the paint params a pick sends.
|
||||
struct Chip {
|
||||
name: String,
|
||||
color: Option<Srgba>,
|
||||
params: Value,
|
||||
}
|
||||
|
||||
/// The None chip, then each color swatch of the active document in the Swatches panel's order (the swatches outside
|
||||
/// any color group, then each group's), shown in the color its art shows. Gradient and pattern swatches are left out.
|
||||
fn chips(app: &VectorcraftApp) -> Vec<Chip> {
|
||||
let Some(st) = app.session.active() else { return Vec::new() };
|
||||
let d = &st.doc;
|
||||
let none = Chip { name: tl!("None").to_owned(), color: None, params: json!({"none": true}) };
|
||||
let swatches = d.swatches_iter().filter_map(|s| match super::swatches::shown(d, s).0 {
|
||||
Paint::Solid { color, .. } => Some(Chip { name: s.name.clone(), color: Some(srgba(&color)), params: json!({"swatch": s.name}) }),
|
||||
_ => None,
|
||||
});
|
||||
std::iter::once(none).chain(swatches).collect()
|
||||
}
|
||||
|
||||
/// Sets the fill (`stroke`: the stroke) to the chip's paint, leaving the active proxy as it is.
|
||||
fn apply(app: &mut VectorcraftApp, chip: &Chip, stroke: bool) {
|
||||
let mut params = chip.params.clone();
|
||||
params["focus"] = json!(false);
|
||||
app.run(if stroke { "paint.setStroke" } else { "paint.setFill" }, params).ok();
|
||||
}
|
||||
|
||||
/// The panel's body.
|
||||
pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
if app.session.active().is_none() {
|
||||
super::empty_state(ui, "swatch-book", tl!("No document"), tl!("Open a document to see its swatches."));
|
||||
return;
|
||||
}
|
||||
let chips = chips(app);
|
||||
let mut picked = None;
|
||||
Stack::new().id_salt(ID).fill().scroll(Scroll::vertical()).show(ui, |ui, _| {
|
||||
Row::new().fill_width().wrap(true).show(ui, |ui, _| {
|
||||
for (at, chip) in chips.iter().enumerate() {
|
||||
let swatch = match chip.color {
|
||||
Some(color) => Swatch::new(chip.name.clone(), color),
|
||||
None => Swatch::optional(chip.name.clone(), None::<Srgba>),
|
||||
};
|
||||
let out = swatch.id_salt(at).tooltip(chip.name.clone()).show(ui);
|
||||
// A right-click ends on the chip, as the framework's swatch palette reads one.
|
||||
let secondary = out.response.contains_pointer() && ui.input(|i| i.pointer.button_released(PointerButton::Secondary));
|
||||
if out.clicked || secondary {
|
||||
picked = Some((at, secondary || out.activation.is_some_and(|a| a.mods.alt)));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if let Some((at, stroke)) = picked
|
||||
&& let Some(chip) = chips.get(at)
|
||||
{
|
||||
apply(app, chip, stroke);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use craft::behavior::Axis;
|
||||
use craft::behavior::dock::Node;
|
||||
use egui::{Modifiers, vec2};
|
||||
use serde_json::json;
|
||||
use vectorcraft_color::Paint;
|
||||
use vectorcraft_engine::Session;
|
||||
|
||||
use super::show;
|
||||
use crate::VectorcraftApp;
|
||||
use crate::tests::panel_harness::Panel;
|
||||
|
||||
/// A document with three global color swatches (art painted with one stays linked to it) and a gradient swatch,
|
||||
/// and one selected rectangle.
|
||||
fn app() -> VectorcraftApp {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
for (name, color) in [("Strip Red", "#cc2222"), ("Strip Green", "#22aa44"), ("Strip Blue", "#2244cc")] {
|
||||
app.run("swatch.new", json!({"name": name, "color": color, "global": true})).unwrap();
|
||||
}
|
||||
let ramp = json!({"kind": "linear", "stops": [{"offset": 0, "color": "#000"}, {"offset": 1, "color": "#fff"}]});
|
||||
app.run("swatch.new", json!({"name": "Strip Ramp", "gradient": ramp})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
/// The fill and stroke the Fill and Stroke proxies show: the selected rectangle's.
|
||||
fn paints(panel: &Panel) -> (Paint, Paint) {
|
||||
crate::panels::current_paints(&panel.app())
|
||||
}
|
||||
|
||||
/// The swatch a solid paint is linked to.
|
||||
fn swatch_of(paint: &Paint) -> Option<String> {
|
||||
match paint {
|
||||
Paint::Solid { swatch, .. } => swatch.clone(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_sets_the_fill_and_a_right_click_or_alt_click_the_stroke() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
let fill_active = panel.app().session.fill_active;
|
||||
|
||||
panel.click("Strip Red");
|
||||
let (fill, stroke) = paints(&panel);
|
||||
assert_eq!(swatch_of(&fill).as_deref(), Some("Strip Red"), "a click sets the fill");
|
||||
assert_ne!(swatch_of(&stroke).as_deref(), Some("Strip Red"), "a click leaves the stroke");
|
||||
|
||||
panel.secondary_click("Strip Green");
|
||||
let (fill, stroke) = paints(&panel);
|
||||
assert_eq!(swatch_of(&stroke).as_deref(), Some("Strip Green"), "a right-click sets the stroke");
|
||||
assert_eq!(swatch_of(&fill).as_deref(), Some("Strip Red"), "a right-click leaves the fill");
|
||||
|
||||
panel.click_with("Strip Blue", Modifiers::ALT);
|
||||
let (fill, stroke) = paints(&panel);
|
||||
assert_eq!(swatch_of(&stroke).as_deref(), Some("Strip Blue"), "an Alt-click sets the stroke");
|
||||
assert_eq!(swatch_of(&fill).as_deref(), Some("Strip Red"), "an Alt-click leaves the fill");
|
||||
|
||||
assert_eq!(panel.app().session.fill_active, fill_active, "the active proxy stays as it was");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_none_chip_comes_first_and_takes_the_paint_off() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
let none = panel.rect("None");
|
||||
let red = panel.rect("Strip Red");
|
||||
assert!(none.left() < red.left() || none.top() < red.top(), "the None chip comes before the swatches: {none:?} {red:?}");
|
||||
|
||||
panel.click("Strip Red");
|
||||
panel.click("None");
|
||||
assert!(paints(&panel).0.is_none(), "a click on None takes the fill off");
|
||||
panel.secondary_click("None");
|
||||
assert!(paints(&panel).1.is_none(), "a right-click on None takes the stroke off");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_color_swatches_get_chips() {
|
||||
let panel = Panel::new(app(), show);
|
||||
assert!(panel.shows("Strip Red"), "a color swatch has a chip");
|
||||
assert!(!panel.shows("Strip Ramp"), "a gradient swatch has none");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_chips_run_along_a_wide_strip_and_wrap_down_a_narrow_one() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
let names = ["None", "Strip Red", "Strip Green", "Strip Blue"];
|
||||
|
||||
panel.room(vec2(4000.0, 200.0)).frame();
|
||||
panel.frame();
|
||||
let rects: Vec<_> = names.iter().map(|n| panel.rect(n)).collect();
|
||||
for pair in rects.windows(2) {
|
||||
assert!((pair[0].center().y - pair[1].center().y).abs() < 1.0, "the chips share one line: {pair:?}");
|
||||
assert!(pair[0].right() <= pair[1].left(), "each chip lies after the one before it without overlapping: {pair:?}");
|
||||
}
|
||||
|
||||
let one_chip = rects[0].width();
|
||||
panel.room(vec2(one_chip * 2.5, 2000.0)).frame();
|
||||
panel.frame();
|
||||
let first = panel.rect("None");
|
||||
let last = panel.rect("Strip Blue");
|
||||
assert!(last.top() >= first.bottom(), "in a narrow column a later chip wraps below the first: {first:?} {last:?}");
|
||||
assert!(last.right() <= one_chip * 2.5 + 0.5, "the chips stay inside the column: {last:?}");
|
||||
}
|
||||
|
||||
/// Whether `node` holds panel `panel`.
|
||||
fn holds(node: &Node<String>, panel: &str) -> bool {
|
||||
match node {
|
||||
Node::Split { first, second, .. } => holds(first, panel) || holds(second, panel),
|
||||
Node::Tabs { panels, .. } => panels.iter().any(|p| p == panel),
|
||||
Node::Stack { entries, .. } => entries.iter().any(|e| e.panel == panel),
|
||||
Node::Dock { columns, .. } => columns.iter().any(|c| c.panels().any(|p| p == panel)),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `node` has a split of rows with `upper` in its top part and `lower` in its bottom part.
|
||||
fn above(node: &Node<String>, upper: &str, lower: &str) -> bool {
|
||||
match node {
|
||||
Node::Split { axis: Axis::Y, first, second, .. } if holds(first, upper) && holds(second, lower) => true,
|
||||
Node::Split { first, second, .. } => above(first, upper, lower) || above(second, upper, lower),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_strip_docks_along_the_bottom_edge_below_the_canvas() {
|
||||
let mut app = app();
|
||||
app.run("window.panel.dock", json!({"panel": super::ID, "anchor": "canvas", "placement": {"Edge": "Bottom"}})).unwrap();
|
||||
let layout = crate::panel_docking::layout_of(&app.ui);
|
||||
let root = layout.root.as_ref().expect("a docked layout");
|
||||
assert!(above(root, crate::panel_docking::CANVAS, super::ID), "the strip lies in a row below the canvas: {root:?}");
|
||||
}
|
||||
}
|
||||
@@ -284,7 +284,7 @@ fn highlighted<'a>(d: &'a Document, sel: &'a [String]) -> Vec<&'a str> {
|
||||
|
||||
/// The `swatch`'s paint as the panel shows it, and whether it is global and spot. A tint swatch shows its base's kind;
|
||||
/// a spot colour shows the colour its art shows (a Lab one's CMYK equivalent when the Spot Colors options use those).
|
||||
fn shown(d: &Document, s: &Swatch) -> (Paint, bool, bool) {
|
||||
pub(crate) fn shown(d: &Document, s: &Swatch) -> (Paint, bool, bool) {
|
||||
let (global, spot) = s.tint_of().and_then(|(base, _)| d.swatch(base)).map_or((s.global, s.spot), |b| (b.global, b.spot));
|
||||
let paint = match s.paint {
|
||||
Paint::Solid { color, swatch: None, .. } if spot => Paint::solid(d.linked_color(color, true)),
|
||||
|
||||
@@ -308,6 +308,7 @@ pub const ICON_PANELS: &[(&str, &str, &str)] = &[
|
||||
("color", "Color", "palette"),
|
||||
("colorGuide", "Color Guide", "dc-color-guide"),
|
||||
("swatches", "Swatches", "swatch-book"),
|
||||
(crate::panels::swatch_strip::ID, "Swatch Strip", "swatch-book"),
|
||||
("brushes", "Brushes", "paintbrush"),
|
||||
("symbols", "Symbols", "spray-can"),
|
||||
("patternOptions", "Pattern Options", "grid-3x3"),
|
||||
|
||||
@@ -289,6 +289,13 @@ or `rgbPercent` value that is not true or false. The desktop app keeps the four
|
||||
switches R, G and B between percent and 0 to 255 and answers true when they show in percent; a parameter answers an
|
||||
error and changes nothing.
|
||||
|
||||
Swatch Strip panel (`window.panel {panel: "swatchStrip"}`): a None chip, then the document's color swatches (the
|
||||
swatches outside any color group, then each group's; gradient and pattern swatches are left out), in a row of chips
|
||||
that wraps to the panel's room and scrolls when they do not fit. `window.panel.dock {panel: "swatchStrip", anchor:
|
||||
"canvas", placement: {"Edge": "Bottom"}}` docks it along the window's bottom edge, where the chips run across in one
|
||||
line. A click on a chip runs `paint.setFill {swatch}` (`{none: true}` for None) and a right-click or an Alt-click
|
||||
runs `paint.setStroke`, both with `focus: false`, so the active proxy stays as it was.
|
||||
|
||||
Color Themes panel (`window.panel {panel: "colorThemes"}`): its Create tab makes a theme on a harmony wheel and its
|
||||
My Themes tab lists the saved themes; every action runs a command (`colorTheme.save`, `colorTheme.delete`,
|
||||
`colorTheme.addToSwatches`, `swatch.newGroup` for the theme being made), and `colorTheme.list` reads the library.
|
||||
|
||||
Reference in New Issue
Block a user