Merge main into fix/ui-perf-3-tool-rail-memo

This commit is contained in:
FredApps committed 2026-11-28 23:48:34 +01:00
commit 0104a098cb
6 files changed
+117 -11

No files matched your search

+28 -6
View File
@@ -4,7 +4,7 @@ use craft_behavior::{Metrics, Mixed, PartKey, Sense};
use craft_theme::Srgba;
use craft_ui::contract::{Access, Common, Component, Cx, MeasureCx, Name, Outcome, PaintUi, PartsCx, Props as _, Role};
use craft_ui::display::{Shade, Text};
use craft_ui::primitives::{PaintShape, StrokeWidth, TextRun};
use craft_ui::primitives::{PaintShape, StrokeWidth, TextRun, WellFill};
use craft_ui::runtime::env::Label;
use craft_ui::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TypeRole};
use egui::{CornerRadius, Rect, Response, Vec2, pos2};
@@ -83,6 +83,7 @@ pub struct CompareSwatch<'a> {
new: Mixed<Srgba>,
current: Mixed<Srgba>,
warnings: &'a [ColorWarning],
no_current: bool,
restore: bool,
warning: Option<usize>,
}
@@ -90,7 +91,23 @@ pub struct CompareSwatch<'a> {
impl<'a> CompareSwatch<'a> {
/// A block named `label` ("New color", the new half's name) showing `new` over `current`.
pub fn new(label: impl Into<Label>, new: impl Into<Mixed<Srgba>>, current: impl Into<Mixed<Srgba>>) -> Self {
CompareSwatch { common: Common::new(), new: new.into(), current: current.into(), warnings: &[], restore: false, warning: None }.label(label)
CompareSwatch {
common: Common::new(),
new: new.into(),
current: current.into(),
warnings: &[],
no_current: false,
restore: false,
warning: None,
}
.label(label)
}
/// The current half has no colour to show or restore (the target is unset or Automatic): it draws the sunken
/// "no colour" diagonal, not a colour or the mixed mark, and is not a button.
pub fn no_current(mut self, on: bool) -> Self {
self.no_current = on;
self
}
/// The warnings in the column after the block, top to bottom.
@@ -157,18 +174,22 @@ impl Component for CompareSwatch<'_> {
let restore = cx.env.words::<ColorPickerWords>().restore.as_str().to_owned();
let flow = craft_behavior::Flow::new(rect, cx.flow.is_rtl());
let (_, _, current) = halves(rect, cx.style, caption_of(rect.height(), cx.style));
cx.part(CURRENT, flow.to_physical(current)).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(restore);
if !self.no_current {
cx.part(CURRENT, flow.to_physical(current)).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(restore);
}
}
fn update(&mut self, cx: &mut Cx<'_, Self>) {
self.restore = cx.part_activated(CURRENT).is_some();
self.restore = !self.no_current && cx.part_activated(CURRENT).is_some();
}
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
if let Some(color) = self.new.same() {
access.value(color.to_hex());
}
access.node(CURRENT, Role::BUTTON).name = Name::Text(cx.env.words::<ColorPickerWords>().restore.as_str().to_owned());
if !self.no_current {
access.node(CURRENT, Role::BUTTON).name = Name::Text(cx.env.words::<ColorPickerWords>().restore.as_str().to_owned());
}
}
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
@@ -184,7 +205,8 @@ impl Component for CompareSwatch<'_> {
(flow.to_physical(block), flow.to_physical(new), flow.to_physical(current), flow.to_physical(above), flow.to_physical(under));
let _ = ui.child("new_caption", above, caption_text(words.new_caption.as_str()));
ui.draw().swatch_well(new, CornerRadius::ZERO, self.new.same().copied());
ui.draw().swatch_well(current, CornerRadius::ZERO, self.current.same().copied());
let current_fill = if self.no_current { WellFill::None } else { self.current.same().copied().into() };
ui.draw().swatch_well(current, CornerRadius::ZERO, current_fill);
let _ = ui.child("current_caption", under, caption_text(words.current_caption.as_str()));
ui.draw().stroke(&PaintShape::Rect(block, CornerRadius::ZERO), StrokeWidth::Thin, style.edge);
// The warnings column: each chip its icon over its swatch, the first level with the block's top.
+2
View File
@@ -26,6 +26,8 @@ pub(super) struct Body<'a> {
/// Compact only: the host gave a compare colour, so the compare block stands beside the area (Word's Colors
/// dialog's New over Current). The Full layout always shows it.
pub compare: bool,
/// The compare block's current half shows no colour.
pub no_current: bool,
pub alpha: bool,
pub dynamic_tracks: bool,
pub eyedropper: bool,
+4 -1
View File
@@ -92,7 +92,10 @@ impl<'a> Body<'a> {
Mixed::Same(current) => Mixed::Same(current.color),
Mixed::Mixed => Mixed::Mixed,
};
let shown = CompareSwatch::new(words.new_color.as_str().to_owned(), pass.shows(pass.shown.color), current).warnings(&self.warnings).show(ui);
let shown = CompareSwatch::new(words.new_color.as_str().to_owned(), pass.shows(pass.shown.color), current)
.warnings(&self.warnings)
.no_current(self.no_current)
.show(ui);
if shown.restore
&& let Mixed::Same(current) = pass.compare
{
+7
View File
@@ -303,6 +303,10 @@ pub struct ColorPicker<'a> {
/// picker's first show.
#[prop(into)]
compare: Option<Mixed<PickedColor>>,
/// The compare block's current half shows no colour (the "no colour" diagonal) and is not a restore button; the
/// host has no colour to go back to.
#[prop]
compare_none: bool,
warnings: Vec<ColorWarning>,
/// Applied to every colour before it is reported, after Only Web Colors' snap.
#[prop(func = "Fn(Srgba) -> Srgba")]
@@ -330,6 +334,7 @@ impl<'a> ColorPicker<'a> {
eyedropper: false,
eyedropper_busy: false,
compare: None,
compare_none: false,
warnings: Vec::new(),
constrain: None,
sampled: None,
@@ -388,6 +393,7 @@ impl<'a> ColorPicker<'a> {
eyedropper,
eyedropper_busy,
compare,
compare_none,
warnings,
constrain,
sampled,
@@ -401,6 +407,7 @@ impl<'a> ColorPicker<'a> {
models: models.clone(),
warnings,
compare: compare.is_some(),
no_current: compare_none,
alpha,
dynamic_tracks,
eyedropper,
+48 -4
View File
@@ -23,6 +23,7 @@ use craft_ui::contract::{
use craft_ui::dialog::{
ButtonPlace, Dialog, DialogId, DialogResult, DialogStack, FieldRefusal, StackDialog, dialog_id, serde_fields, set_serde_field,
};
use craft_ui::display::Text;
use craft_ui::layout::{CellAlign, GridFit};
use craft_ui::layout_components::{Grid, Row, Stack};
use craft_ui::notices::{Notice, NoticeLayout};
@@ -65,6 +66,7 @@ craft_ui::words! {
custom_tab: "colorPickerDialog.customTab" = "Custom";
standard_colors: "colorPickerDialog.standardColors" = "Standard colors";
greys: "colorPickerDialog.greys" = "Shades of gray";
colors_heading: "colorPickerDialog.colorsHeading" = "Colors:";
}
}
@@ -237,6 +239,8 @@ pub struct ColorPickerDialog {
/// The colour as it stands; `None` until the first pick when the dialog opened without one.
current: Option<PickedColor>,
compare: bool,
/// [`no_current`](Self::no_current).
no_current: bool,
remember: bool,
add_to_swatches: bool,
cmyk: Option<CmykConverter>,
@@ -273,6 +277,8 @@ struct TabbedBody<'a> {
words: &'a ColorPickerDialogWords,
standard: Option<&'a StandardColors>,
compare: Option<Mixed<PickedColor>>,
/// The compare block's current half shows no colour and restores nothing.
no_current: bool,
}
/// The built-in models `on` takes, the host's working RGB in place of the built-in RGB.
@@ -322,6 +328,7 @@ impl TabbedBody<'_> {
// The compare block stands beside the honeycomb, as it does beside the Custom page's area.
Row::new().show(ui, |ui, _| {
Stack::new().show(ui, |ui, _| {
Text::new(self.words.colors_heading.clone()).show(ui);
let honeycomb = SwatchTable::new(self.words.standard_colors.clone(), cells)
.arrangement(SwatchArrangement::Honeycomb)
.tips(true)
@@ -346,7 +353,7 @@ impl TabbedBody<'_> {
Mixed::Mixed => Mixed::Mixed,
};
let label = Env::of(ui).words::<ColorPickerWords>().new_color.clone();
if CompareSwatch::new(label, new, current).show(ui).restore
if CompareSwatch::new(label, new, current).no_current(self.no_current).show(ui).restore
&& let Mixed::Same(current) = compare
{
restore = Some(current);
@@ -370,8 +377,9 @@ impl TabbedBody<'_> {
.models(models)
.shown_model(Bind::bound(&mut model));
if let Some(compare) = self.compare {
picker = picker.compare(compare);
picker = picker.compare(compare).compare_none(self.no_current);
}
Text::new(self.words.colors_heading.clone()).show(ui);
let _ = picker.show(ui);
memory.model = model.map(str::to_owned);
}
@@ -413,6 +421,7 @@ impl ColorPickerDialog {
opening,
current: opening,
compare: true,
no_current: false,
remember: true,
add_to_swatches: true,
cmyk: None,
@@ -502,6 +511,20 @@ impl ColorPickerDialog {
self
}
/// The target has no colour to return to (unset or Automatic): the compare block's current half shows the "no
/// colour" diagonal and is not a restore button, while the dialog still opens on the colour it was given and
/// Reset returns to it.
#[must_use]
pub fn no_current(mut self) -> ColorPickerDialog {
self.no_current = true;
self
}
/// Whether the compare block has no current colour ([`no_current`](Self::no_current)).
pub fn has_no_current(&self) -> bool {
self.no_current
}
/// Whether the field's component and Only Web Colors are recalled on open and stored on OK
/// ([`ColorPickerMemory`]); on by default. A picker opened by a feature that wants its own start turns it off.
#[must_use]
@@ -774,7 +797,20 @@ impl StackDialog for ColorPickerDialog {
// Word's Colors dialog.
let place = if full { ButtonPlace::Body } else { ButtonPlace::Side };
let Self {
current, opening, compare, memory, screen_busy, sample, pending_screen, open, layout, standard, swatches: view, working_rgb, ..
current,
opening,
compare,
no_current,
memory,
screen_busy,
sample,
pending_screen,
open,
layout,
standard,
swatches: view,
working_rgb,
..
} = self;
let working_rgb = working_rgb.as_deref().map(|model| model as &dyn ColorModel);
let mut held: Mixed<PickedColor> = current.map_or(Mixed::Mixed, Mixed::Same);
@@ -796,7 +832,14 @@ impl StackDialog for ColorPickerDialog {
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
}
if *layout == ColorPickerDialogLayout::Tabbed {
let body = TabbedBody { title: &title, working_rgb, words: &words, standard: standard.as_ref(), compare: compare_with };
let body = TabbedBody {
title: &title,
working_rgb,
words: &words,
standard: standard.as_ref(),
compare: compare_with,
no_current: *no_current,
};
body.show(ui, &mut *memory, &mut held);
return;
}
@@ -835,6 +878,7 @@ impl StackDialog for ColorPickerDialog {
if let Some(compare) = compare_with {
picker = picker.compare(compare);
}
picker = picker.compare_none(*no_current);
if let Some(gesture) = picker.show(ui).screen_pick {
*pending_screen = Some(gesture);
}
+28
View File
@@ -543,6 +543,33 @@ fn dialog_field(dialogs: &Rc<RefCell<DialogHost>>, name: &str) -> Option<serde_j
dialogs.borrow().get::<ColorPickerDialog>().and_then(|dialog| dialog.json_fields()).and_then(|fields| fields.get(name).cloned())
}
/// #7984: a dialog with no current colour shows the Colors heading and the compare block, its current half is not a
/// restore button, and the dialog still opens on the colour it was given.
#[trial]
fn the_standard_page_without_a_current_colour_offers_no_restore() {
let dialogs = Rc::new(RefCell::new(DialogHost::new()));
let shown = Rc::clone(&dialogs);
let mut fixture = Fixture::new(move |ui| shown.borrow_mut().run(ui));
fixture.room(vec2(900.0, 700.0));
fixture.host(std::sync::Arc::new(Host::builder(AppInfo { id: "picker-test", name: "Picker test", version: "0" }).build()));
let cells = vec![(Label::from("Standard 0"), Srgba::from_hex("#ff0000").expect("a colour"))];
dialogs.borrow_mut().open(
ColorPickerDialog::new("fill", "Fill", Srgba::from_hex("#000000").expect("a colour"))
.layout(ColorPickerDialogLayout::Tabbed)
.standard(cells, Vec::new())
.no_current()
.web_colors_remembered(false),
);
fixture.idle(6);
assert!(fixture.by_label("Colors:").is_some(), "the honeycomb has its heading");
assert!(fixture.by_label("New color").is_some(), "the compare block shows without a current colour");
assert!(fixture.by_label("Restore current color").is_none(), "there is nothing to restore");
assert_eq!(dialog_color(&dialogs).as_deref(), Some("#000000"), "the dialog opens on the colour it was given");
fixture.click(&fixture.by_label("Standard 0").expect("a cell"));
fixture.idle(4);
assert_eq!(dialog_color(&dialogs).as_deref(), Some("#ff0000"));
}
/// The current colour of the open dialog as `#rrggbb`.
fn dialog_color(dialogs: &Rc<RefCell<DialogHost>>) -> Option<String> {
dialogs.borrow().get::<ColorPickerDialog>().and_then(|dialog| dialog.current()).map(|picked| picked.color.to_hex())
@@ -577,6 +604,7 @@ fn the_tabbed_colors_dialog_opens_on_its_tab_and_answers_from_either_page() {
fixture.idle(6);
assert_eq!(dialog_field(&dialogs, "tab"), Some(serde_json::json!("custom")), "the dialog opens on the tab it was given");
assert!(fixture.by_label("Hex color").is_some(), "the Custom page shows the compact picker");
assert!(fixture.by_label("Colors:").is_some(), "and the Colors heading over it");
assert!(fixture.by_label("Standard 0").is_none(), "and not the honeycomb");
assert_eq!(dialog_field(&dialogs, "model"), Some(serde_json::Value::Null), "no model is picked yet");
assert!(fixture.by_label("Red").is_none(), "the first model (HSB) shows no red channel");