Pattern fill: the Foreground and Background openers show their colour

The pattern's colour openers read Color with no swatch, so the picked
colours could not be seen. They now show the current colour as a chip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dylan M. Taylor
2026-11-29 22:26:52 +00:00
co-authored by Claude Opus 5.5
parent 9877e57f7e
commit f50ee30a7b
3 changed files with 58 additions and 5 deletions
+38 -1
View File
@@ -1,11 +1,13 @@
//! Colour rows share the same conversion and delayed eyedropper route as ribbon menus.
use craft::color_ui::ui::{ColorPick, ThemeColorMenu};
use craft::theme::Srgba;
use craft::ui::buttons::Button;
use craft::ui::contract::{Bind, BodyCx, FieldLayout, Props as _, Show as _, ShowContainer as _, Trigger};
use craft::ui::controls::disclosure::DisclosureGlyph;
use craft::ui::form::Field;
use craft::ui::popover::Popover;
use craft::ui::primitives::IconSpec;
use craft::ui::runtime::env::{Env, Label, Word};
use deckcraft_model::ColorRef;
use egui::Ui;
@@ -61,6 +63,19 @@ pub(crate) fn color_row(
color_row_named(app, ui, key, label, label == "Color", none, on_pick);
}
/// Like [`color_row`], with the opener showing `current` as a chip in place of its word: a colour that is always set
/// (a pattern's foreground and background).
pub(crate) fn color_row_showing(
app: &mut SlideApp,
ui: &mut Ui,
key: &str,
label: &str,
current: Srgba,
on_pick: impl FnOnce(&mut SlideApp, Option<ColorRef>),
) {
color_opener(app, ui, key, label, Face::Chip(current), on_pick);
}
/// Like [`color_row`], with the opener named by its label alone (`plain`) or as "{label} color".
pub(crate) fn color_row_named(
app: &mut SlideApp,
@@ -71,12 +86,34 @@ pub(crate) fn color_row_named(
none: Option<&str>,
on_pick: impl FnOnce(&mut SlideApp, Option<ColorRef>),
) {
color_opener(app, ui, key, label, Face::Word { plain, none }, on_pick);
}
/// What a colour opener shows.
#[derive(Clone, Copy)]
enum Face<'a> {
/// The word "Color", the opener named by its label alone (`plain`) or as "{label} color"; the menu offers `none`.
Word { plain: bool, none: Option<&'a str> },
/// The current colour as a chip, the opener named "{label} color".
Chip(Srgba),
}
fn color_opener(app: &mut SlideApp, ui: &mut Ui, key: &str, label: &str, face: Face<'_>, on_pick: impl FnOnce(&mut SlideApp, Option<ColorRef>)) {
let Some(uid) = app.session.active().map(|st| st.uid) else { return };
let key = format!("fmt-color:{uid}:{key}");
let (plain, none) = match face {
Face::Word { plain, none } => (plain, none),
Face::Chip(_) => (false, None),
};
let name =
if plain { Label::from(Word::plain("Color")) } else { Label::from(Env::of(ui).words::<ColorRowWords>().named.fill(&[("label", label)])) };
let mut field = Field::new(label.to_owned()).layout(FieldLayout::Beside).id_salt(&key).show(ui, |ui, _| {
Button::new(name.clone()).painted(Word::plain("Color")).keeps_name(true).trailing_icon(DisclosureGlyph::dropdown()).show(ui)
let button = Button::new(name.clone()).keeps_name(true).trailing_icon(DisclosureGlyph::dropdown());
match face {
Face::Chip(color) => button.icon(IconSpec::swatch(color)).painted(Label::Empty),
Face::Word { .. } => button.painted(Word::plain("Color")),
}
.show(ui)
});
field.settle(|_, output| {
let mut popup = Popover::new(Word::plain("Color"), Bind::kept().reset(false))
+5 -4
View File
@@ -5,7 +5,7 @@
use craft::color_ui::gradient::{ColorStop, GradientStops, StopRows, StopRules};
use craft::color_ui::ui::gradient::{GradientEditor, StopFields};
use craft::i18n::Word;
use craft::theme::Srgba;
use craft::theme::{Rgba, Srgba};
use craft::ui::buttons::IconButton;
use craft::ui::contract::overlay::Anchor;
use craft::ui::contract::{Bind, Entry, FieldLayout, Props as _, Show as _, ShowContainer as _, clicked};
@@ -25,7 +25,7 @@ use deckcraft_model::style::{Gradient, GradientFlip, GradientShape, GradientStop
use egui::{Color32, CornerRadius, Rect, Ui, vec2};
use serde_json::{Value, json};
use super::color_row::{color_menu, color_row, take_color_answer};
use super::color_row::{color_menu, color_row_showing, take_color_answer};
use super::preset_grid::preset_grid;
use crate::ribbon::cref_param;
use crate::theme;
@@ -456,8 +456,9 @@ pub(crate) fn pattern_editor(app: &mut crate::SlideApp, ui: &mut Ui, p: &Pattern
if let Some(preset) = grid.settle(|_, picked| picked.take()).flatten().filter(|preset| preset != &pt.preset) {
out = Some(PatternFill { preset, ..pt.clone() });
}
for (label, foreground) in [("Foreground", true), ("Background", false)] {
color_row(app, ui, if foreground { "pattern-foreground" } else { "pattern-background" }, label, None, |_, picked| {
for (label, foreground, current) in [("Foreground", true, fg), ("Background", false, bg)] {
let chip = Srgba::from(Rgba(current.to_srgba_unmultiplied()));
color_row_showing(app, ui, if foreground { "pattern-foreground" } else { "pattern-background" }, label, chip, |_, picked| {
if let Some(color) = picked {
let pattern = out.get_or_insert_with(|| pt.clone());
if foreground {
@@ -99,5 +99,20 @@ fn shape_pattern_fill_keeps_preset_and_system_colors() {
assert_eq!(recolored.fg, ColorRef::rgb(Rgba::from_hex("00FF00").unwrap()), "the hex submission reached the foreground");
assert_eq!(recolored.bg, before.bg, "a foreground edit keeps the system background");
assert_eq!(recolored.preset, picked.preset, "a foreground edit keeps the chosen pattern");
assert!(paints_color(&h, "Foreground color", egui::Color32::from_rgb(0, 255, 0)), "the opener shows the colour picked");
}
}
/// Whether the button `label` paints `color` inside itself.
fn paints_color(h: &Harness<'_, SlideApp>, label: &str, color: egui::Color32) -> bool {
fn any(shape: &egui::Shape, within: egui::Rect, color: egui::Color32) -> bool {
match shape {
egui::Shape::Vec(shapes) => shapes.iter().any(|shape| any(shape, within, color)),
egui::Shape::Rect(rect) => rect.fill == color && within.contains_rect(rect.rect),
egui::Shape::Mesh(mesh) => mesh.vertices.iter().any(|vertex| vertex.color == color && within.contains(vertex.pos)),
_ => false,
}
}
let button = h.get_by_role_and_label(Role::Button, label).rect().expand(0.5);
h.output().shapes.iter().any(|shape| any(&shape.shape, button, color))
}