From e9d4a5d3f5478ecb0be4aa181fe0fd6860a1ac0f Mon Sep 17 00:00:00 2001 From: haranishi <223786623+haranishi@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:00:30 +0900 Subject: [PATCH] fix(swatches): fit more swatches at the default zoom --- crates/color-ui/src/ui/swatches.rs | 6 +++ crates/color-ui/src/ui/swatches/library.rs | 6 ++- crates/color-ui/src/ui/swatches/panel.rs | 3 +- crates/color-ui/tests/it/panels.rs | 52 +++++++++++++++++++--- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/crates/color-ui/src/ui/swatches.rs b/crates/color-ui/src/ui/swatches.rs index 6293e937f..14d9b3cd0 100644 --- a/crates/color-ui/src/ui/swatches.rs +++ b/crates/color-ui/src/ui/swatches.rs @@ -3,6 +3,8 @@ //! change as a resolved [`SwatchEdit`] (new, rename, delete, move, group edits, import) that the host runs, with //! [`SwatchLibrary::apply`] for an in-memory host. +use craft_ui::tile_grid::TileZoom; + mod library; mod menu; mod panel; @@ -13,6 +15,10 @@ pub use library::{SwatchAt, SwatchEdit, SwatchKey, SwatchLibrary, SwatchesState, pub use menu::{SwatchesCommand, SwatchesFileOutput, SwatchesMenu, SwatchesMenuOutput}; pub use panel::{SwatchesPanel, SwatchesPanelOutput}; +/// Compact colour tiles at the default zoom, with the same upper limit as the preset browser. +const SWATCH_MIN_ZOOM: f64 = 1.0 / 3.0; +const SWATCH_ZOOM: TileZoom = TileZoom::bounded(SWATCH_MIN_ZOOM, 5.0, 1.0 / 12.0, SWATCH_MIN_ZOOM); + /// The most swatches a library holds (PhotoCraft's `MAX_SWATCHES`). pub const MAX_SWATCHES: usize = 100_000; diff --git a/crates/color-ui/src/ui/swatches/library.rs b/crates/color-ui/src/ui/swatches/library.rs index 3653c61b9..937fd9885 100644 --- a/crates/color-ui/src/ui/swatches/library.rs +++ b/crates/color-ui/src/ui/swatches/library.rs @@ -5,7 +5,7 @@ use std::collections::HashSet; use craft_ui::contract::ReorderMove; use craft_ui::patterns::presets::{PresetRow, PresetState}; -use super::{MAX_GROUPS, MAX_SWATCHES, SwatchesWords}; +use super::{MAX_GROUPS, MAX_SWATCHES, SWATCH_ZOOM, SwatchesWords}; use crate::palette::{SwatchColor, SwatchEntry, SwatchGroup, SwatchReport}; use crate::ui::PickedColor; @@ -37,7 +37,9 @@ pub struct SwatchesState { impl Default for SwatchesState { fn default() -> Self { - SwatchesState { browser: PresetState::new(), selected: None, rename: None, export_notes: None } + let mut browser = PresetState::new(); + browser.zoom = SWATCH_ZOOM.default_value(); + SwatchesState { browser, selected: None, rename: None, export_notes: None } } } diff --git a/crates/color-ui/src/ui/swatches/panel.rs b/crates/color-ui/src/ui/swatches/panel.rs index 11c161748..f0bef8ce9 100644 --- a/crates/color-ui/src/ui/swatches/panel.rs +++ b/crates/color-ui/src/ui/swatches/panel.rs @@ -17,7 +17,7 @@ use egui::{Rect, Response, Ui}; use super::library::{find_swatch, has}; use super::menu::event_edits; -use super::{SwatchAt, SwatchEdit, SwatchKey, SwatchesFileOutput, SwatchesState, SwatchesWords, swatch_rows}; +use super::{SWATCH_ZOOM, SwatchAt, SwatchEdit, SwatchKey, SwatchesFileOutput, SwatchesState, SwatchesWords, swatch_rows}; use crate::palette::{SwatchColor, SwatchGroup, SwatchNote, SwatchReport}; /// What [`SwatchesPanel::show`] reports. @@ -103,6 +103,7 @@ impl<'a> SwatchesPanel<'a> { let out = { let colors: HashMap<&str, &SwatchColor> = groups.iter().flat_map(|g| g.entries.iter().map(|e| (e.name.as_str(), &e.color))).collect(); let mut browser = PresetBrowser::new(words.title.clone(), Bind::bound(&mut st.browser), &rows) + .thumb_range(SWATCH_ZOOM) .toolbar(true) .reorder(PresetReorder::items_and_groups()) .footer(PresetFooterOptions::new().can_delete(st.selected.is_some())) diff --git a/crates/color-ui/tests/it/panels.rs b/crates/color-ui/tests/it/panels.rs index 03a294af3..a3055754a 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -8,15 +8,17 @@ use craft_behavior::{EditEvent, Selection}; use craft_color_ui::palette::{SwatchColor, SwatchGroup}; use craft_color_ui::ui::{ ColorPanel, ColorPanelMode, ColorPanelState, PanelTarget, PickedColor, SwatchEdit, SwatchKey, SwatchLibrary, SwatchPalette, SwatchesCommand, - SwatchesMenu, SwatchesPanel, SwatchesState, + SwatchesMenu, SwatchesPanel, SwatchesState, SwatchesWords, swatch_rows, }; +use craft_host::target::Os; use craft_theme::Srgba; use craft_ui::choice::SwatchTable; use craft_ui::contract::Role; use craft_ui::contract::tooltip::DELAY; use craft_ui::contract::{Bind, Show as _}; -use craft_ui::runtime::env::Label; +use craft_ui::runtime::env::{Label, Scale}; use craft_ui::runtime::requests::ColorRequest; +use craft_ui::testing::focus::AccessActionKind; use craft_ui::testing::pointer::moved; use craft_ui::testing::{Fixture, trial}; use egui::{Key, Modifiers, Vec2}; @@ -243,9 +245,9 @@ fn the_palette_arrows_walk_the_wrapped_grid() { } /// The Swatches panel over two groups, collecting the edits it reports and never applying them. -fn swatches_rig(groups: Vec) -> (Fixture, Rc>>) { +fn swatches_rig(groups: Vec, state: SwatchesState) -> (Fixture, Rc>>) { let edits = Rc::new(RefCell::new(Vec::new())); - let state = Rc::new(RefCell::new(SwatchesState::default())); + let state = Rc::new(RefCell::new(state)); let (seen, held) = (Rc::clone(&edits), state); let mut fixture = Fixture::new(move |ui| { let mut current = held.borrow().clone(); @@ -257,6 +259,44 @@ fn swatches_rig(groups: Vec) -> (Fixture, Rc = swatch_rows(&groups, &SwatchesWords::default()).into_iter().filter_map(|row| row.accessible_name).collect(); + assert_eq!(names.len(), 10); + + let (mut fixture, _) = swatches_rig(groups.clone(), SwatchesState::default()); + fixture.os(Os::Windows).scale(Scale::new(1.25)).room(Vec2::new(220.0, 150.0)).settle(); + let visible_grays = + |fixture: &Fixture| names.iter().filter_map(|name| fixture.by_label(name).and_then(|tile| fixture.visible(&tile))).collect::>(); + let defaults = visible_grays(&fixture); + assert_eq!(defaults.len(), 10, "all ten Grays are visible in the default dock"); + let first = defaults[0].rect; + assert!(defaults.iter().all(|tile| tile.rect.top() == first.top()), "all ten Grays fit on one row"); + + let larger = fixture.by_label("Larger Thumbnails").expect("the larger thumbnail button"); + fixture.click(&larger).settle(); + let after_click = visible_grays(&fixture)[0].rect.width(); + assert!(after_click > first.width(), "the zoom button enlarges the tiles"); + + let slider = fixture.by_role(Role::SLIDER).into_iter().next().expect("the thumbnail slider"); + fixture.access(AccessActionKind::Focus, &slider).settle(); + fixture.key(Key::ArrowRight, Modifiers::NONE).settle(); + assert!(visible_grays(&fixture)[0].rect.width() > after_click, "the slider enlarges the tiles with the keyboard"); + fixture.click(&slider).click(&slider).settle(); + assert_eq!(visible_grays(&fixture)[0].rect.width(), first.width(), "double-click resets to the compact default"); + + let mut larger_state = SwatchesState::default(); + larger_state.browser.zoom = 1.75; + let (mut fixture, _) = swatches_rig(groups, larger_state); + fixture.room(Vec2::new(220.0, 310.0)).settle(); + let slider = fixture.by_role(Role::SLIDER).into_iter().next().expect("the reopened thumbnail slider"); + let shown = fixture.tree().numeric_of(slider.node).expect("the slider's accessible value and range"); + assert_eq!(shown.value, Some(1.75), "a previously chosen larger zoom is still displayed"); + assert_eq!(*shown.range.end(), 5.0, "the larger end of the zoom range stays available"); +} + /// Through the panel, a selected swatch deleted reports one `Delete`, and a swatch dragged onto another group's header /// reports one `MoveTo` the host can run; New Swatch from the menu reports an `Add` in the selected swatch's group under /// the next free numbered name. The slots of multi-swatch moves are checked in library.rs. @@ -269,14 +309,14 @@ fn swatches_panel_reports_each_edit_and_leaves_the_library() { SwatchGroup { name: "Basics".to_owned(), entries: vec![entry("Red", red), entry("Color Swatch 1", blue)] }, SwatchGroup { name: "Other".to_owned(), entries: vec![entry("Blue", blue)] }, ]; - let (mut fixture, edits) = swatches_rig(groups.clone()); + let (mut fixture, edits) = swatches_rig(groups.clone(), SwatchesState::default()); let row = fixture.by_label("Red, #ff0000").expect("the Red swatch is named with its value"); fixture.click(&row).settle(); assert!(edits.borrow().is_empty(), "a click selects and uses, it edits nothing: {:?}", edits.borrow()); fixture.key(Key::Delete, Modifiers::NONE).settle(); assert_eq!(*edits.borrow(), [SwatchEdit::delete("Red".to_owned())], "one resolved edit, once"); - let (mut fixture, edits) = swatches_rig(groups.clone()); + let (mut fixture, edits) = swatches_rig(groups.clone(), SwatchesState::default()); let row = fixture.by_label("Red, #ff0000").expect("the Red swatch"); let header = fixture.by_label("Other").expect("the Other group's header").center(); fixture.drag(&row, header - row.center()).settle();