mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Draw the tool icon in Tool Presets rows (#17813)
This commit is contained in:
1 parent
c5ca44cd2f
commit
44deb98ff0
2 files changed
+63
-3
No files matched your search
@@ -16,7 +16,7 @@ use craft::ui::patterns::presets::{
|
||||
PresetFooterOptions, PresetMenuAction, PresetPanelMenu, PresetPicker, PresetReorder, PresetRow, PresetSource, PresetState, PresetView, ThumbFn, ThumbInfo,
|
||||
};
|
||||
use craft::ui::primitives::textures::TextureCache;
|
||||
use craft::ui::primitives::{ContentPainterCx, FacePaint};
|
||||
use craft::ui::primitives::{ContentPainterCx, FacePaint, IconSpec};
|
||||
use craft::ui::runtime::access::PopupKind;
|
||||
use craft::ui::runtime::commands::{CommandSet as _, CommandState};
|
||||
use craft::ui::runtime::env::Env;
|
||||
@@ -834,8 +834,19 @@ fn tool_preset_rows(presets: &&[(String, String)], _: &Env) -> Vec<PresetRow<Pre
|
||||
presets
|
||||
.iter()
|
||||
.map(|(name, tool)| {
|
||||
let icon = Tool::from_name(tool).map_or_else(|| craft::behavior::IconId::new("settings"), crate::icons::tool_icon);
|
||||
PresetRow::item(PresetKey::Item(name.clone()), name.clone(), 0).icon(icon)
|
||||
let tool = Tool::from_name(tool);
|
||||
let icon = tool.map_or_else(|| IconId::new("settings"), crate::icons::tool_icon);
|
||||
let mut key = DefaultHasher::new();
|
||||
icon.hash(&mut key);
|
||||
let face = FacePaint::new(key.finish(), move |cx, rect| {
|
||||
cx.with_draw(|draw| draw.icon(rect.shrink(rect.height() * 0.18), &IconSpec::svg(icon.clone())));
|
||||
});
|
||||
// The preset browser draws list thumbnails from their preview face.
|
||||
let row = PresetRow::item(PresetKey::Item(name.clone()), name.clone(), 0).preview_face(ThumbAspect::SQUARE, face);
|
||||
match tool {
|
||||
Some(tool) => row.accessible_name(crate::i18n::fmt(tl!("{preset}, {tool}"), &[("preset", name), ("tool", tl!(tool.label()))])),
|
||||
None => row,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -1141,3 +1152,7 @@ pub(crate) fn gradient_picker_at(app: &mut PhotocraftApp, ui: &mut egui::Ui, at:
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "tool_presets_row_icon_tests.rs"]
|
||||
mod tool_presets_row_icon_tests;
|
||||
@@ -0,0 +1,45 @@
|
||||
use egui::{Rect, Shape, TextureId};
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use photocraft_engine::presets::{PresetsExt, tools::ToolPreset};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::input::shortcut_dispatch::tests::app_harness;
|
||||
use crate::{PhotocraftApp, Services};
|
||||
|
||||
fn icon_texture(shape: &Shape, row: Rect) -> Option<TextureId> {
|
||||
match shape {
|
||||
Shape::Vec(shapes) => shapes.iter().find_map(|shape| icon_texture(shape, row)),
|
||||
Shape::PaintChunk(shapes) => shapes.iter().find_map(|shape| icon_texture(&shape.shape, row)),
|
||||
Shape::Mesh(mesh) if mesh.texture_id != TextureId::default() && row.contains(mesh.calc_bounds().center()) => Some(mesh.texture_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_preset_rows_paint_distinct_tool_glyphs_and_a_fallback() {
|
||||
let names = ["Test crop preset", "Test brush preset", "Test unknown preset"];
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
app.session.presets_mut().tool_presets = names
|
||||
.into_iter()
|
||||
.zip(["crop", "brush", "unrecognised-test-tool"])
|
||||
.map(|(name, tool)| ToolPreset { name: name.into(), tool: tool.into(), options: json!({}) })
|
||||
.collect();
|
||||
let mut h = app_harness(app);
|
||||
let ctx = h.ctx.clone();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.toolPresets", json!({})).unwrap();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, "window.panel.window", json!({"panel": "toolPresets", "native": false})).unwrap();
|
||||
h.run_steps(6);
|
||||
|
||||
let textures = names.map(|name| {
|
||||
let row = h.query_by_label_contains(name).expect("the preset is exposed as a row in the panel");
|
||||
h.output().shapes.iter().find_map(|shape| icon_texture(&shape.shape, row.rect())).unwrap_or_else(|| panic!("{name} draws an icon in its row"))
|
||||
});
|
||||
assert_ne!(textures[0], textures[1], "different tools draw different icons");
|
||||
assert_ne!(textures[0], textures[2], "the unknown tool uses the fallback icon");
|
||||
assert_ne!(textures[1], textures[2], "the unknown tool uses the fallback icon");
|
||||
for (name, tool) in [(names[0], "Crop Tool"), (names[1], "Brush Tool")] {
|
||||
let label = h.query_by_label_contains(name).unwrap().accesskit_node().label().unwrap();
|
||||
assert!(label.starts_with(name) && label.contains(tool), "the row announces its preset name and tool: {label}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user