mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
color-ui: ColorPickerDialog::working_rgb puts the host's working RGB in place of the built-in RGB in the Full and tabbed layouts
This commit is contained in:
@@ -15,7 +15,7 @@ use craft_host::Host;
|
||||
use craft_host::target::Caps;
|
||||
use craft_settings::Remembered;
|
||||
use craft_theme::variant::Tone;
|
||||
use craft_theme::{ColorModel, Srgba, builtin_models};
|
||||
use craft_theme::{ColorModel, DefaultOn, RgbModel, Srgba, builtin_models};
|
||||
use craft_ui::choice::{SwatchArrangement, SwatchSpacing, SwatchTable};
|
||||
use craft_ui::contract::{
|
||||
Bind, CanvasGesture, CollectionEvent, Entry, FooterSlot, Gap, Sampler as _, Show as _, ShowCollection as _, ShowContainer as _,
|
||||
@@ -240,6 +240,7 @@ pub struct ColorPickerDialog {
|
||||
remember: bool,
|
||||
add_to_swatches: bool,
|
||||
cmyk: Option<CmykConverter>,
|
||||
working_rgb: Option<Box<dyn ColorModel + Send + Sync>>,
|
||||
gamut: Option<Gamut<'static>>,
|
||||
swatches: Option<PickerSwatches>,
|
||||
eyedropper: bool,
|
||||
@@ -268,11 +269,18 @@ pub struct ColorPickerDialog {
|
||||
/// What the tabbed body shows besides the memory and the colour it edits.
|
||||
struct TabbedBody<'a> {
|
||||
title: &'a Label,
|
||||
working_rgb: Option<&'a dyn ColorModel>,
|
||||
words: &'a ColorPickerDialogWords,
|
||||
standard: Option<&'a StandardColors>,
|
||||
compare: Option<Mixed<PickedColor>>,
|
||||
}
|
||||
|
||||
/// The built-in models `on` takes, the host's working RGB in place of the built-in RGB.
|
||||
fn models_with(working_rgb: Option<&dyn ColorModel>, on: impl Fn(DefaultOn) -> bool) -> Vec<&dyn ColorModel> {
|
||||
let models = builtin_models().into_iter().filter(|model| on(model.default_on()));
|
||||
models.map(|model| working_rgb.filter(|_| model.id() == RgbModel.id()).unwrap_or(model)).collect()
|
||||
}
|
||||
|
||||
/// The selection of the entry whose colour is exactly `current`.
|
||||
fn chosen(entries: &[(Label, Srgba)], current: Option<Srgba>) -> Selection<usize> {
|
||||
current.and_then(|color| entries.iter().position(|(_, entry)| *entry == color)).map_or_else(Selection::default, Selection::single)
|
||||
@@ -335,10 +343,12 @@ impl TabbedBody<'_> {
|
||||
|
||||
/// The compact picker over the colour, the compare swatch beside its area; the model switch is the memory's.
|
||||
fn custom_page(&self, ui: &mut Ui, memory: &mut ColorPickerMemory, held: &mut Mixed<PickedColor>) {
|
||||
let models: Vec<&dyn ColorModel> = builtin_models().into_iter().filter(|model| model.default_on().compact).collect();
|
||||
let models = models_with(self.working_rgb, |on| on.compact);
|
||||
let mut model: Option<&'static str> = memory.model.as_deref().and_then(|id| models.iter().map(|known| known.id()).find(|known| *known == id));
|
||||
let mut picker =
|
||||
ColorPicker::new(self.title.clone(), Bind::bound_mixed(held)).layout(ColorPickerLayout::Compact).shown_model(Bind::bound(&mut model));
|
||||
let mut picker = ColorPicker::new(self.title.clone(), Bind::bound_mixed(held))
|
||||
.layout(ColorPickerLayout::Compact)
|
||||
.models(models)
|
||||
.shown_model(Bind::bound(&mut model));
|
||||
if let Some(compare) = self.compare {
|
||||
picker = picker.compare(compare);
|
||||
}
|
||||
@@ -386,6 +396,7 @@ impl ColorPickerDialog {
|
||||
remember: true,
|
||||
add_to_swatches: true,
|
||||
cmyk: None,
|
||||
working_rgb: None,
|
||||
gamut: None,
|
||||
swatches: None,
|
||||
eyedropper: true,
|
||||
@@ -495,6 +506,14 @@ impl ColorPickerDialog {
|
||||
self
|
||||
}
|
||||
|
||||
/// The host's working-RGB colour model, whose channels replace the built-in RGB fields in both layouts: a wide
|
||||
/// working space shows its own numbers, and [`kept`](Self::kept) values in it stay as given.
|
||||
#[must_use]
|
||||
pub fn working_rgb(mut self, model: impl ColorModel + Send + Sync + 'static) -> ColorPickerDialog {
|
||||
self.working_rgb = Some(Box::new(model));
|
||||
self
|
||||
}
|
||||
|
||||
/// The screen eyedropper button; on by default, and shown only where the platform can capture the screen.
|
||||
#[must_use]
|
||||
pub fn eyedropper(mut self, on: bool) -> ColorPickerDialog {
|
||||
@@ -734,7 +753,10 @@ impl StackDialog for ColorPickerDialog {
|
||||
// The Full picker keeps a cell for the buttons; the tabbed body has none, so they stand in the side column, as in
|
||||
// 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, .. } = self;
|
||||
let Self {
|
||||
current, opening, compare, 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);
|
||||
let (sampled, busy, compare_with) = (sample.take(), *screen_busy, compare.then(|| opening.map_or(Mixed::Mixed, Mixed::Same)));
|
||||
let color = current.map(|current| current.color);
|
||||
@@ -754,7 +776,7 @@ impl StackDialog for ColorPickerDialog {
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
|
||||
}
|
||||
if *layout == ColorPickerDialogLayout::Tabbed {
|
||||
let body = TabbedBody { title: &title, 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 };
|
||||
body.show(ui, &mut *memory, &mut held);
|
||||
return;
|
||||
}
|
||||
@@ -764,7 +786,7 @@ impl StackDialog for ColorPickerDialog {
|
||||
show_swatches(ui, view, &mut memory.library, &mut held, &words.library);
|
||||
return;
|
||||
}
|
||||
let mut models: Vec<&dyn ColorModel> = builtin_models().into_iter().filter(|model| model.default_on().full).collect();
|
||||
let mut models = models_with(working_rgb, |on| on.full);
|
||||
models.push(&cmyk);
|
||||
let mut warnings = Vec::new();
|
||||
if let Some(color) = color {
|
||||
|
||||
@@ -124,6 +124,59 @@ fn a_dialog_reopened_keeps_only_web_colors_and_recalls_the_values_kept_on_ok() {
|
||||
assert_eq!(edited(&dialogs), Some(values), "Reset returns the typed values");
|
||||
}
|
||||
|
||||
/// A working RGB twice as wide as sRGB: 255 is sRGB's half.
|
||||
struct WideRgb;
|
||||
|
||||
impl ColorModel for WideRgb {
|
||||
fn id(&self) -> &'static str {
|
||||
RgbModel.id()
|
||||
}
|
||||
fn name(&self) -> &'static str {
|
||||
RgbModel.name()
|
||||
}
|
||||
fn channels(&self) -> &[craft_theme::Channel] {
|
||||
static RGB: RgbModel = RgbModel;
|
||||
RGB.channels()
|
||||
}
|
||||
fn from_srgb(&self, c: Srgba) -> ModelValues {
|
||||
ModelValues::new(self.id(), &[c.r * 510.0, c.g * 510.0, c.b * 510.0])
|
||||
}
|
||||
fn to_srgb(&self, v: &[f32; craft_theme::MAX_CHANNELS]) -> Srgba {
|
||||
Srgba::rgb(v[0] / 510.0, v[1] / 510.0, v[2] / 510.0)
|
||||
}
|
||||
fn default_on(&self) -> craft_theme::DefaultOn {
|
||||
RgbModel.default_on()
|
||||
}
|
||||
}
|
||||
|
||||
/// The dialog's RGB fields are the host's working RGB, in both layouts: the values `kept` gives in it stand, where the
|
||||
/// built-in sRGB would show its own numbers.
|
||||
#[trial]
|
||||
fn the_dialogs_rgb_fields_show_the_hosts_working_values() {
|
||||
let wide = Srgba::rgb(0.3, 0.2, 0.1);
|
||||
let kept = WideRgb.from_srgb(wide);
|
||||
for layout in [ColorPickerDialogLayout::Full, ColorPickerDialogLayout::Tabbed] {
|
||||
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));
|
||||
let dialog = ColorPickerDialog::new("fill", "Fill", wide).layout(layout).tab(ColorPickerTab::Custom).working_rgb(WideRgb).kept(kept);
|
||||
dialogs.borrow_mut().open(dialog);
|
||||
fixture.idle(6);
|
||||
if layout == ColorPickerDialogLayout::Tabbed {
|
||||
let ctx = fixture.ctx().clone();
|
||||
let set = dialogs.borrow_mut().set_json_field(&ctx, dialog_id(("color-picker", "fill")), "model", serde_json::json!("rgb"));
|
||||
assert_eq!(set, Ok(()), "the compact picker shows RGB");
|
||||
fixture.idle(4);
|
||||
}
|
||||
let tree = fixture.tree();
|
||||
let red = tree.with_role(Role::SPIN_BUTTON).into_iter().find(|node| tree.name_of(*node) == Some("Red"));
|
||||
let shown = red.and_then(|node| tree.numeric_of(node)).and_then(|numeric| numeric.value);
|
||||
let shown = shown.unwrap_or_else(|| panic!("{layout:?}: the Red field"));
|
||||
assert!((shown - f64::from(kept.values[0])).abs() < 0.5, "{layout:?}: Red shows the working value {kept:?}, not {shown}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The densities and themes every layout trial runs in: compact and comfortable, a Pro and a Studio theme.
|
||||
const LOOKS: [(Size, &str); 4] =
|
||||
[(Size::Compact, "craft.pro"), (Size::Comfortable, "craft.pro"), (Size::Compact, "craft.studio"), (Size::Comfortable, "craft.studio")];
|
||||
|
||||
Reference in New Issue
Block a user