PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Color panel: the spectrum field in every theme (#4367) (#4399)
F4LCONf5d64a66bdb2
diff --git a/crates/ui-egui/src/color_tab_theme_tests.rs b/crates/ui-egui/src/color_tab_theme_tests.rsnew file mode 100644index 000000000..b3566277e--- /dev/null+++ b/crates/ui-egui/src/color_tab_theme_tests.rs@@ -0,0 +1,55 @@+//! The Color panel tab draws Photoshop's spectrum field in every theme (#4367): the light themes+//! used to show HSB sliders only. Those themes keep the sliders, under the field.++use egui::{Rect, pos2};+use egui_kittest::kittest::Queryable;++use crate::PhotocraftApp;+use crate::dock::Group;+use crate::theme::{ThemeKind, Tokens};++fn left_top() -> egui::Id {+ egui::Id::new("color-tab-test-origin")+}++/// The Color tab of `kind`, alone in a 300 pt wide panel, starting from a red foreground.+fn color_tab(kind: ThemeKind) -> egui_kittest::Harness<'static, PhotocraftApp> {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.tools.foreground = [1.0, 0.0, 0.0, 1.0];+ let tab = Group::Color.tabs(Tokens::for_kind(kind).pro).iter().position(|name| *name == "Color").unwrap();+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(300.0, 400.0)).build_ui_state(+ move |ui, app: &mut PhotocraftApp| {+ if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {+ let origin = ui.cursor().min;+ ui.data_mut(|d| d.insert_temp(left_top(), Rect::from_min_size(origin, egui::Vec2::ZERO)));+ crate::panels::dock_body(app, ui, Group::Color, tab);+ }+ },+ app,+ );+ PhotocraftApp::setup_context(&h.ctx, kind);+ h.run_steps(3);+ h+}++#[test]+fn every_theme_shows_the_spectrum_field() {+ for kind in ThemeKind::ALL {+ let mut h = color_tab(kind);+ let origin = h.ctx.data(|d| d.get_temp::<Rect>(left_top())).unwrap().min;+ // Left edge of the saturation/brightness field (after the 38 pt chips and the 8 pt gap),+ // halfway down its 120 pt: no saturation, half brightness.+ let p = pos2(origin.x + 38.0 + 8.0 + 1.0, origin.y + 60.0);+ h.hover_at(p);+ h.run_steps(1);+ h.drag_at(p);+ h.run_steps(1);+ h.drop_at(p);+ h.run_steps(2);+ let [r, g, b, _] = h.state().session.tools.foreground;+ assert!((r - g).abs() < 0.02 && (g - b).abs() < 0.02 && (0.3..0.7).contains(&r), "{kind:?}: the field picks a mid grey, got {r} {g} {b}");+ if !Tokens::for_kind(kind).pro {+ assert!(h.query_by_label("Saturation").is_some(), "{kind:?}: the sliders stay under the field");+ }+ }+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex a0aa5d4e9..d03885937 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2369,6 +2369,9 @@ mod move_auto_select_tests;#[cfg(test)]mod new_group_button_tests;+#[cfg(test)]+mod color_tab_theme_tests;+#[cfg(test)]mod new_layer_button_tests;diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 30a3bb400..7ee7be084 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1963,7 +1963,16 @@ fn empty(ui: &mut egui::Ui, s: &str) {ui.add_space(6.0);}+/// The Color tab of the themes other than Photoshop's: the same spectrum field (chips, saturation+/// and brightness field, hue strip and readout) with HSB sliders under it (#4367).fn color_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui) {+ color_field(app, ui);+ ui.add_space(8.0);+ hsb_sliders(app, ui);+}++/// Hue, Saturation and Brightness sliders for the foreground colour.+fn hsb_sliders(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let fg = app.session.tools.foreground;// Keep the last edited HSB while it still gives the foreground: black and greys have no hue or// saturation of their own, so recomputing them from RGB would reset what was just typed to 0.@@ -1990,16 +1999,6 @@ fn color_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui) {ui.data_mut(|d| d.insert_temp(key, hsva));crate::type_tool::foreground_changed(app);}- ui.horizontal(|ui| {- let (sw, _) = ui.allocate_exact_size(vec2(26.0, 26.0), Sense::hover());- ui.painter().rect_filled(sw, 6.0, hsva_color32(hsva));- let mut c = app.session.tools.foreground;- if color_readout(ui, ui.id().with("color-panel"), &mut c) {- app.session.tools.foreground = c;- ui.data_mut(|d| d.insert_temp(key, srgb_hsva(c)));- crate::type_tool::foreground_changed(app);- }- });}// ----------------------------------------------------------------------------- layers@@ -3358,7 +3357,9 @@ fn color_field(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let strip_w = 14.0;// Photoshop's Color panel field fills the panel: take the height the dock offers, less the// hex/RGB readout (which folds onto a second line in a narrow dock). Without a dock, 120 pt.- let h = color_field_height(ui.data(|d| d.get_temp::<f32>(color_field_fill_id())), w);+ // Only Photoshop's themes stretch it over the group: the other themes put sliders under it.+ let fill = if t.pro { ui.data(|d| d.get_temp::<f32>(color_field_fill_id())) } else { None };+ let h = color_field_height(fill, w);ui.horizontal(|ui| {ui.spacing_mut().item_spacing.x = 8.0;let (chips, _) = ui.allocate_exact_size(vec2(38.0, h), Sense::hover());@@ -4123,7 +4124,7 @@ mod color_tests {let mut h = egui_kittest::Harness::builder().with_size(vec2(300.0, 300.0)).build_ui_state(|ui, app: &mut PhotocraftApp| {if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {- color_picker(app, ui);+ hsb_sliders(app, ui);}},app,