fix(swatches): fit more swatches at the default zoom

This commit is contained in:
haranishi
2026-11-19 23:00:30 +09:00
parent b2456f91c6
commit e9d4a5d3f5
4 changed files with 58 additions and 9 deletions
+6
View File
@@ -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;
+4 -2
View File
@@ -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 }
}
}
+2 -1
View File
@@ -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()))
+46 -6
View File
@@ -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<SwatchGroup>) -> (Fixture, Rc<RefCell<Vec<SwatchEdit>>>) {
fn swatches_rig(groups: Vec<SwatchGroup>, state: SwatchesState) -> (Fixture, Rc<RefCell<Vec<SwatchEdit>>>) {
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<SwatchGroup>) -> (Fixture, Rc<RefCell<Vec<SwatchEdit
(fixture, edits)
}
#[trial]
fn default_swatches_fit_the_ten_grays_in_a_narrow_dock_and_still_zoom() {
let grays = SwatchLibrary::default().groups.into_iter().find(|group| group.name == "Grays").expect("the factory Grays group");
let groups = vec![grays];
let names: Vec<_> = 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::<Vec<_>>();
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();