List Percent before Pixels in Image Size

This commit is contained in:
haranishi
2026-11-20 05:40:28 +09:00
parent 6bd20d7878
commit 299a9edfea
4 changed files with 81 additions and 3 deletions
@@ -352,10 +352,14 @@ pub(super) struct Kit {
resolutions: Vec<Entry<UnitKind>>,
}
/// The units the width and height are shown in.
/// The length units in Canvas Size order.
const LENGTH_UNITS: [UnitKind; 7] =
[UnitKind::Pixels, UnitKind::Percent, UnitKind::Inches, UnitKind::Centimetres, UnitKind::Millimetres, UnitKind::Points, UnitKind::Picas];
/// Image Size lists Percent first in both dimension menus.
const IMAGE_LENGTH_UNITS: [UnitKind; 7] =
[UnitKind::Percent, UnitKind::Pixels, UnitKind::Inches, UnitKind::Centimetres, UnitKind::Millimetres, UnitKind::Points, UnitKind::Picas];
/// The units the resolution is shown in.
const RESOLUTION_UNITS: [UnitKind; 2] = [UnitKind::PixelsPerInch, UnitKind::PixelsPerCentimetre];
@@ -368,11 +372,21 @@ enum Dimension {
}
impl Kit {
/// The rows of Image Size.
pub(super) fn new(ui: &Ui) -> Self {
Self::with_length_order(ui, &IMAGE_LENGTH_UNITS)
}
/// The rows of Canvas Size.
pub(super) fn for_canvas(ui: &Ui) -> Self {
Self::with_length_order(ui, &LENGTH_UNITS)
}
fn with_length_order(ui: &Ui, length_order: &[UnitKind]) -> Self {
let env = Env::of(ui);
let units = env.units();
let entries = |kinds: &[UnitKind]| -> Vec<Entry<UnitKind>> { kinds.iter().map(|kind| Entry::new(*kind, units.name(*kind))).collect() };
let lengths = entries(&LENGTH_UNITS);
let lengths = entries(length_order);
let mut widths = lengths.clone();
widths.extend(entries(&[UnitKind::Columns]));
Kit {
@@ -178,7 +178,7 @@ impl<'a> CanvasSize<'a> {
/// Shows the dialog.
pub fn show(self, ui: &mut Ui) -> CanvasSizeOutput {
let CanvasSize { values, open, bytes_per_pixel, hold } = self;
let kit = Kit::new(ui);
let kit = Kit::for_canvas(ui);
let words = kit.words();
let title = words.canvas_size_title.clone();
@@ -0,0 +1,63 @@
//! Image Size makes Percent easy to find in both dimension menus.
use craft_behavior::PixelSize;
use craft_ui::contract::Role;
use craft_ui::dialog::{DialogHost, DialogResult};
use craft_ui::patterns::form_dialogs::{CanvasSize, CanvasSizeValues, HostedCanvasSize, HostedImageSize, ImageSize, ImageSizeValues};
use craft_ui::runtime::env::{ColumnSize, UnitKind};
use craft_ui::testing::focus::AccessActionKind;
use craft_ui::testing::{Fixture, trial};
use egui::{Key, Modifiers, vec2};
use std::cell::RefCell;
use std::rc::Rc;
const SIZE: PixelSize = PixelSize::new(1200, 900);
const COLUMNS: ColumnSize = ColumnSize::new(180.0, 12.0);
const IMAGE_UNITS: [&str; 8] = ["Percent", "Pixels", "Inches", "Centimetres", "Millimetres", "Points", "Picas", "Columns"];
const CANVAS_UNITS: [&str; 8] = ["Pixels", "Percent", "Inches", "Centimetres", "Millimetres", "Points", "Picas", "Columns"];
fn fixture(host: &Rc<RefCell<DialogHost>>) -> Fixture {
let host = Rc::clone(host);
let mut fixture = Fixture::new(move |ui| host.borrow_mut().run(ui));
fixture.room(vec2(1400.0, 1000.0)).idle(10);
fixture
}
fn unit_order(fixture: &mut Fixture, axis: &str) -> Vec<String> {
let field = fixture.by_label(axis).expect("the dimension's unit combobox");
fixture.access(AccessActionKind::Focus, &field).idle(2);
fixture.key(Key::Space, Modifiers::NONE).idle(3);
let mut options = fixture.by_role(Role::LIST_BOX_OPTION);
options.sort_by(|a, b| a.center().y.total_cmp(&b.center().y));
options.iter().map(|option| fixture.tree().name_of(option.node).expect("the visible option has a name").to_owned()).collect()
}
fn image_host() -> Rc<RefCell<DialogHost>> {
let mut host = DialogHost::new();
host.open(HostedImageSize::new(ImageSizeValues::new(SIZE).column_size(COLUMNS), |values| ImageSize::new(values)));
Rc::new(RefCell::new(host))
}
#[trial]
fn image_size_dimension_menus_put_percent_before_pixels() {
let mut canvas = DialogHost::new();
canvas.open(HostedCanvasSize::new(CanvasSizeValues::new(SIZE).column_size(COLUMNS), |values| CanvasSize::new(values)));
let mut comparison = fixture(&Rc::new(RefCell::new(canvas)));
assert_eq!(unit_order(&mut comparison, "Width unit"), CANVAS_UNITS);
let host = image_host();
let mut width = fixture(&host);
let mut height = fixture(&image_host());
let width_order = unit_order(&mut width, "Width unit");
let height_order = unit_order(&mut height, "Height unit");
assert_eq!(width_order, IMAGE_UNITS, "Image Size Width puts Percent first and Columns last");
assert_eq!(height_order, &IMAGE_UNITS[..7], "Image Size Height puts Percent first and offers no Columns");
let percent = width.by_label("Percent").expect("Percent is a selectable unit");
width.click(&percent).idle(4);
let confirm = width.by_label("OK").expect("the Image Size confirmation");
width.access(AccessActionKind::Click, &confirm).idle(4);
let accepted = host.borrow_mut().take::<HostedImageSize>().and_then(DialogResult::ok).expect("Image Size answers after choosing Percent");
assert_eq!(accepted.unit, UnitKind::Percent);
assert_eq!(accepted.size, SIZE, "switching units preserves the image size");
}
@@ -6,6 +6,7 @@ mod dialog;
mod export;
mod files;
mod hosted;
mod image_unit_order;
mod new_document;
mod params;
mod password;