mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 18:54:31 +01:00
Icons: draw every tool icon's outline at the same width
The app icon set declared a 1.6-unit stroke, but its own dc-* icons draw 1.5-unit outlines and its Lucide copies draw 2-unit ones. The framework scales each icon's strokes by the drawn width over the declared width, so the dc-* icons drew about 6% thinner than the catalog's icons and the Lucide copies (Pen, Paintbrush, Add and Delete Anchor Point and others) about 25% heavier, which made the tool rail's outlines uneven. - Split the table: the dc-* icons stay in the "vectorcraft" set, now declared at 1.5, and the Lucide copies move to a "vectorcraft-lucide" set declared at 2. icons::register() registers both. - dc-free-distort drew a 2-unit outline; it now draws 1.5 like the rest of its set. - A test checks that every app icon's outline width equals its set's declared width.
This commit is contained in:
1 parent
703252de65
commit
183c365eff
8 files changed
+97
-50
No files matched your search
@@ -1,4 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path d="M2 4.8L18.7 1.4L22.6 21.6L4.8 18.2Z"/>
|
||||
<rect x="0.8" y="3.6" width="2.4" height="2.4" fill="currentColor"/>
|
||||
<rect x="17.5" y="0.2" width="2.4" height="2.4" fill="currentColor"/>
|
||||
|
||||
|
Before Width: | Height: | Size: 525 B After Width: | Height: | Size: 527 B |
@@ -709,7 +709,7 @@ mod cancel_tests {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use craft::behavior::IconId;
|
||||
use craft::ui::primitives::{has_icon, register_app_icons};
|
||||
use craft::ui::primitives::has_icon;
|
||||
|
||||
use super::icon_for_shape;
|
||||
|
||||
@@ -717,7 +717,7 @@ mod tests {
|
||||
/// would show the placeholder instead of the toolbar's icon.
|
||||
#[test]
|
||||
fn every_shape_button_shows_its_tools_icon() {
|
||||
register_app_icons(&crate::icons::APP_ICONS);
|
||||
crate::icons::register();
|
||||
for id in ["box3d", "rectangle", "ellipse", "polygon", "star", "spiral"] {
|
||||
let tool = vectorcraft_tools::tool_info(id).unwrap_or_else(|| panic!("{id} is a tool"));
|
||||
assert_eq!(icon_for_shape(id), crate::icons::tool_icon(tool.icon), "{id}");
|
||||
|
||||
@@ -67,7 +67,7 @@ impl VectorcraftApp {
|
||||
/// once, gives Data Recovery the host's store and makes a view for each open document.
|
||||
pub fn new(mut session: Session, mut services: Services) -> Self {
|
||||
// Every entry point (desktop, web, tests) makes the app before its first pass.
|
||||
craft::ui::primitives::register_app_icons(&super::icons::APP_ICONS);
|
||||
super::icons::register();
|
||||
craft::ui::primitives::register_app_icons(&super::cursors::CURSOR_ICONS);
|
||||
// The font menus and the first file opened need the installed fonts: catalog them now.
|
||||
vectorcraft_text::FontDb::global().scan_in_background();
|
||||
|
||||
@@ -17,7 +17,7 @@ fn every_tool_cursors_hotspot_is_on_its_glyph() {
|
||||
use craft::ui::primitives::{painted_image, register_app_icons};
|
||||
use craft::ui::runtime::env::Env;
|
||||
use craft::ui::style::rgba_color;
|
||||
register_app_icons(&crate::icons::APP_ICONS);
|
||||
crate::icons::register();
|
||||
register_app_icons(&CURSOR_ICONS);
|
||||
let mut missed = Vec::new();
|
||||
crate::testing::ui_context()
|
||||
|
||||
@@ -4,31 +4,8 @@
|
||||
/// The Selection and Direct Selection arrows' fill, painted black or white under their outline.
|
||||
pub const DC_ARROW_FILL: &[u8] = include_bytes!("../../../assets/icons/dc-arrow-fill.svg");
|
||||
|
||||
/// VectorCraft's own `dc-*` icons, drawn with 1.5-unit strokes in a 24-unit view box.
|
||||
pub static ICONS: &[(&str, &[u8])] = &[
|
||||
("align-center-horizontal", include_bytes!("../../../assets/icons/align-center-horizontal.svg")),
|
||||
("align-center-vertical", include_bytes!("../../../assets/icons/align-center-vertical.svg")),
|
||||
("align-end-horizontal", include_bytes!("../../../assets/icons/align-end-horizontal.svg")),
|
||||
("align-end-vertical", include_bytes!("../../../assets/icons/align-end-vertical.svg")),
|
||||
("align-horizontal-justify-center", include_bytes!("../../../assets/icons/align-horizontal-justify-center.svg")),
|
||||
("align-horizontal-justify-end", include_bytes!("../../../assets/icons/align-horizontal-justify-end.svg")),
|
||||
("align-horizontal-justify-start", include_bytes!("../../../assets/icons/align-horizontal-justify-start.svg")),
|
||||
("align-start-horizontal", include_bytes!("../../../assets/icons/align-start-horizontal.svg")),
|
||||
("align-start-vertical", include_bytes!("../../../assets/icons/align-start-vertical.svg")),
|
||||
("align-vertical-justify-center", include_bytes!("../../../assets/icons/align-vertical-justify-center.svg")),
|
||||
("align-vertical-justify-end", include_bytes!("../../../assets/icons/align-vertical-justify-end.svg")),
|
||||
("align-vertical-justify-start", include_bytes!("../../../assets/icons/align-vertical-justify-start.svg")),
|
||||
("arrow-left-right", include_bytes!("../../../assets/icons/arrow-left-right.svg")),
|
||||
("arrow-up-down", include_bytes!("../../../assets/icons/arrow-up-down.svg")),
|
||||
("chart-column", include_bytes!("../../../assets/icons/chart-column.svg")),
|
||||
("chevron-down", include_bytes!("../../../assets/icons/chevron-down.svg")),
|
||||
("chevron-left", include_bytes!("../../../assets/icons/chevron-left.svg")),
|
||||
("chevron-right", include_bytes!("../../../assets/icons/chevron-right.svg")),
|
||||
("chevrons-left", include_bytes!("../../../assets/icons/chevrons-left.svg")),
|
||||
("chevrons-right", include_bytes!("../../../assets/icons/chevrons-right.svg")),
|
||||
// PLAN: circle-off.svg is plugins-w11's file; the row waits for it.
|
||||
("circle-off", include_bytes!("../../../assets/icons/circle-off.svg")),
|
||||
("clipboard-paste", include_bytes!("../../../assets/icons/clipboard-paste.svg")),
|
||||
("combine", include_bytes!("../../../assets/icons/combine.svg")),
|
||||
("dc-actions", include_bytes!("../../../assets/icons/dc-actions.svg")),
|
||||
("dc-al-bottom", include_bytes!("../../../assets/icons/dc-al-bottom.svg")),
|
||||
("dc-al-center", include_bytes!("../../../assets/icons/dc-al-center.svg")),
|
||||
@@ -244,6 +221,34 @@ pub static ICONS: &[(&str, &[u8])] = &[
|
||||
("dc-wrinkle", include_bytes!("../../../assets/icons/dc-wrinkle.svg")),
|
||||
("dc-zoom-large", include_bytes!("../../../assets/icons/dc-zoom-large.svg")),
|
||||
("dc-zoom-small", include_bytes!("../../../assets/icons/dc-zoom-small.svg")),
|
||||
];
|
||||
|
||||
/// Lucide icons the built-in catalog lacks, drawn with Lucide's 2-unit strokes in a 24-unit view box.
|
||||
pub static LUCIDE_ICONS: &[(&str, &[u8])] = &[
|
||||
("align-center-horizontal", include_bytes!("../../../assets/icons/align-center-horizontal.svg")),
|
||||
("align-center-vertical", include_bytes!("../../../assets/icons/align-center-vertical.svg")),
|
||||
("align-end-horizontal", include_bytes!("../../../assets/icons/align-end-horizontal.svg")),
|
||||
("align-end-vertical", include_bytes!("../../../assets/icons/align-end-vertical.svg")),
|
||||
("align-horizontal-justify-center", include_bytes!("../../../assets/icons/align-horizontal-justify-center.svg")),
|
||||
("align-horizontal-justify-end", include_bytes!("../../../assets/icons/align-horizontal-justify-end.svg")),
|
||||
("align-horizontal-justify-start", include_bytes!("../../../assets/icons/align-horizontal-justify-start.svg")),
|
||||
("align-start-horizontal", include_bytes!("../../../assets/icons/align-start-horizontal.svg")),
|
||||
("align-start-vertical", include_bytes!("../../../assets/icons/align-start-vertical.svg")),
|
||||
("align-vertical-justify-center", include_bytes!("../../../assets/icons/align-vertical-justify-center.svg")),
|
||||
("align-vertical-justify-end", include_bytes!("../../../assets/icons/align-vertical-justify-end.svg")),
|
||||
("align-vertical-justify-start", include_bytes!("../../../assets/icons/align-vertical-justify-start.svg")),
|
||||
("arrow-left-right", include_bytes!("../../../assets/icons/arrow-left-right.svg")),
|
||||
("arrow-up-down", include_bytes!("../../../assets/icons/arrow-up-down.svg")),
|
||||
("chart-column", include_bytes!("../../../assets/icons/chart-column.svg")),
|
||||
("chevron-down", include_bytes!("../../../assets/icons/chevron-down.svg")),
|
||||
("chevron-left", include_bytes!("../../../assets/icons/chevron-left.svg")),
|
||||
("chevron-right", include_bytes!("../../../assets/icons/chevron-right.svg")),
|
||||
("chevrons-left", include_bytes!("../../../assets/icons/chevrons-left.svg")),
|
||||
("chevrons-right", include_bytes!("../../../assets/icons/chevrons-right.svg")),
|
||||
// PLAN: circle-off.svg is plugins-w11's file; the row waits for it.
|
||||
("circle-off", include_bytes!("../../../assets/icons/circle-off.svg")),
|
||||
("clipboard-paste", include_bytes!("../../../assets/icons/clipboard-paste.svg")),
|
||||
("combine", include_bytes!("../../../assets/icons/combine.svg")),
|
||||
("ellipsis", include_bytes!("../../../assets/icons/ellipsis.svg")),
|
||||
("eye-off", include_bytes!("../../../assets/icons/eye-off.svg")),
|
||||
("file-plus", include_bytes!("../../../assets/icons/file-plus.svg")),
|
||||
|
||||
+60
-18
@@ -2,14 +2,24 @@
|
||||
|
||||
use craft::ui::primitives::{AppIcons, icon_names};
|
||||
|
||||
use crate::icon_data::{DC_ARROW_FILL, ICONS};
|
||||
use crate::icon_data::{DC_ARROW_FILL, ICONS, LUCIDE_ICONS};
|
||||
|
||||
/// The fills of the Selection and Direct Selection arrows: black and white whatever the theme, as users
|
||||
/// know them, under an outline in the theme's colour.
|
||||
const FILLS: &[(&str, &[u8], [u8; 4])] = &[("dc-selection", DC_ARROW_FILL, [0, 0, 0, 255]), ("dc-direct", DC_ARROW_FILL, [255, 255, 255, 255])];
|
||||
|
||||
/// The app's icons, for `register_app_icons`.
|
||||
pub static APP_ICONS: AppIcons = AppIcons::new("vectorcraft", Some(1.6), ICONS).with_fills(FILLS);
|
||||
/// The app's own icons, for `register_app_icons`. The stroke width is the documents' own, so the framework redraws
|
||||
/// their strokes at the width it draws the catalog's.
|
||||
pub static APP_ICONS: AppIcons = AppIcons::new("vectorcraft", Some(1.5), ICONS).with_fills(FILLS);
|
||||
|
||||
/// The Lucide icons the catalog lacks, with Lucide's own stroke width.
|
||||
pub static LUCIDE_APP_ICONS: AppIcons = AppIcons::new("vectorcraft-lucide", Some(2.0), LUCIDE_ICONS);
|
||||
|
||||
/// Registers both of the app's icon sets beside the framework's catalog.
|
||||
pub fn register() {
|
||||
craft::ui::primitives::register_app_icons(&APP_ICONS);
|
||||
craft::ui::primitives::register_app_icons(&LUCIDE_APP_ICONS);
|
||||
}
|
||||
|
||||
/// Map tool-catalogue icon names to the names of icons that resolve.
|
||||
pub fn tool_icon(name: &str) -> &'static str {
|
||||
@@ -114,23 +124,63 @@ pub fn tool_icon(name: &str) -> &'static str {
|
||||
"tool-print-tiling" => "dc-print-tiling",
|
||||
"tool-zoom" => "zoom_in",
|
||||
// A name that is already an icon maps to itself, as the `'static` name of the set that has it.
|
||||
other => icon_names().iter().copied().chain(APP_ICONS.icons.iter().map(|(n, _)| *n)).find(|n| *n == other).unwrap_or("rect_dashed"),
|
||||
other => icon_names()
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(APP_ICONS.icons.iter().chain(LUCIDE_APP_ICONS.icons).map(|(n, _)| *n))
|
||||
.find(|n| *n == other)
|
||||
.unwrap_or("rect_dashed"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use craft::ui::primitives::{has_icon, register_app_icons};
|
||||
use craft::ui::primitives::has_icon;
|
||||
// PLAN: assumed the `craft` facade is a dependency of vectorcraft-ui-egui (Cargo.toml is not in this set's files; craft-ui does not re-export IconId)
|
||||
use craft::behavior::IconId;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn resolves(name: &'static str) -> bool {
|
||||
register_app_icons(&APP_ICONS);
|
||||
register();
|
||||
has_icon(&IconId::new(name))
|
||||
}
|
||||
|
||||
/// The SVG document of the app icon `name`, from either set.
|
||||
fn app_svg(name: &str) -> String {
|
||||
let (_, bytes) = ICONS.iter().chain(LUCIDE_ICONS).find(|(n, _)| *n == name).unwrap_or_else(|| panic!("{name} isn't an app icon"));
|
||||
String::from_utf8_lossy(bytes).into_owned()
|
||||
}
|
||||
|
||||
/// The `stroke-width` the document's root `<svg>` element sets, the width of every stroke that sets none of its
|
||||
/// own; `None` for a document drawn with fills.
|
||||
fn root_stroke_width(svg: &str) -> Option<f32> {
|
||||
let root = svg.split("<svg").nth(1)?.split('>').next()?;
|
||||
Some(root.split("stroke-width=\"").nth(1)?.split('"').next()?.parse().unwrap_or(f32::NAN))
|
||||
}
|
||||
|
||||
// Regression test: the tool rail's icons drew uneven outlines. The app set declared a 1.6-unit stroke while its own icons drew 1.5 and
|
||||
// its Lucide copies drew 2, so the framework drew them 6% thinner and 25% heavier than the catalog's icons.
|
||||
/// The framework scales every stroke in a set by the drawn width over the set's declared width, so an icon's
|
||||
/// outline matches the catalog's only when its root stroke width equals its set's declared width. Details with
|
||||
/// a width of their own keep their ratio to the outline.
|
||||
#[test]
|
||||
fn every_app_icon_draws_its_outline_at_the_catalog_width() {
|
||||
let mut off = Vec::new();
|
||||
for set in [&APP_ICONS, &LUCIDE_APP_ICONS] {
|
||||
let declared = set.stroke_width.unwrap_or_else(|| panic!("{} declares its stroke width", set.name));
|
||||
for (name, bytes) in set.icons {
|
||||
let Some(width) = root_stroke_width(&String::from_utf8_lossy(bytes)) else { continue };
|
||||
// The outline's drawn width relative to the catalog's.
|
||||
let relative = width / declared;
|
||||
if !(0.99..=1.01).contains(&relative) {
|
||||
off.push(format!("{name} in {}: {width} draws at {relative:.3} of the catalog width", set.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(off.is_empty(), "app icons whose outlines do not draw at the catalog width: {off:#?}");
|
||||
}
|
||||
|
||||
/// The ways a source line writes an icon id as a literal; a `const` that holds one is read by `const_icon`.
|
||||
const FORMS: [&str; 3] = ["IconId::new(\"", "icon(\"", "icon = \""];
|
||||
|
||||
@@ -153,7 +203,7 @@ mod tests {
|
||||
/// app's set. Other ids built in match arms and tuple tables are not scanned.
|
||||
#[test]
|
||||
fn every_icon_id_literal_and_panel_icon_resolves() {
|
||||
register_app_icons(&APP_ICONS);
|
||||
register();
|
||||
let resolves = |id: &str| has_icon(&IconId::from(std::borrow::Cow::Owned(id.to_owned())));
|
||||
let (mut seen, mut unknown) = (0, Vec::new());
|
||||
let mut stack = vec![std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src")];
|
||||
@@ -212,10 +262,7 @@ mod tests {
|
||||
// as opaque as the line, so the line vanished where it crossed the bar.
|
||||
#[test]
|
||||
fn the_cap_icons_mark_the_path_end_in_one_place_over_a_lighter_stroke() {
|
||||
let svg = |name: &str| {
|
||||
let (_, bytes) = crate::icon_data::ICONS.iter().find(|(n, _)| *n == name).unwrap_or_else(|| panic!("{name} isn't an icon"));
|
||||
String::from_utf8_lossy(bytes).into_owned()
|
||||
};
|
||||
let svg = app_svg;
|
||||
// The end-of-path line is the icon's last shape, a vertical line `M{x} …V…`.
|
||||
let end_line = |icon: &str| -> String {
|
||||
let last = icon.rsplit("<path").next().unwrap_or_default();
|
||||
@@ -274,12 +321,7 @@ mod tests {
|
||||
// plus or a minus beside it.
|
||||
#[test]
|
||||
fn the_add_and_delete_anchor_tool_icons_are_the_pen_icon_with_a_badge() {
|
||||
let svg = |tool: &str| {
|
||||
let name = tool_icon(tool);
|
||||
let (_, bytes) =
|
||||
crate::icon_data::ICONS.iter().find(|(n, _)| *n == name).unwrap_or_else(|| panic!("{tool} maps to {name}, which isn't an icon"));
|
||||
String::from_utf8_lossy(bytes).into_owned()
|
||||
};
|
||||
let svg = |tool: &str| app_svg(tool_icon(tool));
|
||||
let shapes =
|
||||
|svg: &str| svg.lines().map(|l| l.trim().to_string()).filter(|l| l.starts_with("<path") || l.starts_with("<circle")).collect::<Vec<_>>();
|
||||
let pen = shapes(&svg("tool-pen"));
|
||||
@@ -331,7 +373,7 @@ mod tests {
|
||||
assert!(resolves(name), "{} maps to {name}, which no icon set has", tool.icon);
|
||||
}
|
||||
let catalog = icon_names();
|
||||
for (name, _) in APP_ICONS.icons {
|
||||
for (name, _) in APP_ICONS.icons.iter().chain(LUCIDE_APP_ICONS.icons) {
|
||||
assert!(!catalog.contains(name), "{name} is in the catalog: the app's copy is refused");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,7 +341,7 @@ fn rows_list_by_depth_and_ruler_guide_nodes_get_no_object_row() {
|
||||
/// guide kind's ruler, and both glyphs are in the app's icon set.
|
||||
#[test]
|
||||
fn horizontal_and_vertical_guide_rows_show_their_own_drawn_glyphs() {
|
||||
use craft::ui::primitives::{has_icon, register_app_icons};
|
||||
use craft::ui::primitives::has_icon;
|
||||
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
|
||||
@@ -355,7 +355,7 @@ fn horizontal_and_vertical_guide_rows_show_their_own_drawn_glyphs() {
|
||||
};
|
||||
let (across, down) = (icon_of(false), icon_of(true));
|
||||
assert_ne!(across, down, "the two kinds of guide rows look alike");
|
||||
register_app_icons(&crate::icons::APP_ICONS);
|
||||
crate::icons::register();
|
||||
for icon in [&across, &down] {
|
||||
assert!(has_icon(icon), "{icon:?} is in no icon set, so the row draws nothing");
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ fn the_cut_tools_have_their_own_icons_and_toolbar_slot() {
|
||||
let info = vectorcraft_tools::tool_info(tool).unwrap();
|
||||
assert_eq!(icons::tool_icon(info.icon), icon);
|
||||
// The app registers its icons at start; a test run alone in its process must too.
|
||||
craft::ui::primitives::register_app_icons(&icons::APP_ICONS);
|
||||
icons::register();
|
||||
assert!(craft::ui::primitives::has_icon(&IconId::new(icon)), "{icon}");
|
||||
assert!(crate::toolbar::BASIC.iter().flat_map(|(_, slots)| slots.iter()).any(|s| s.contains(&tool) && s.contains(&"knife")));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user