mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge pull request #19140
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
d29206cd2a
2 files changed
+117
-6
No files matched your search
@@ -398,7 +398,7 @@ pub(super) fn menu_tint(name: &str) -> Option<egui::Color32> {
|
||||
}
|
||||
|
||||
/// Translates the titles of menus and submenus, each translated one keeping its English title.
|
||||
fn translate_titles(bar: &mut MenuBar, lang: crate::i18n::Lang) {
|
||||
fn translate_titles(bar: &mut MenuBar, lang: crate::i18n::Lang, align_to_selection: bool) {
|
||||
/// `title` as shown in `lang`, and its English name when the two differ.
|
||||
fn translate(title: &mut String, english: &mut Option<String>, lang: crate::i18n::Lang) {
|
||||
let shown = crate::i18n::tr(lang, title).to_string();
|
||||
@@ -406,19 +406,26 @@ fn translate_titles(bar: &mut MenuBar, lang: crate::i18n::Lang) {
|
||||
*english = Some(std::mem::replace(title, shown));
|
||||
}
|
||||
}
|
||||
fn level(nodes: &mut [Node], lang: crate::i18n::Lang) {
|
||||
fn level(nodes: &mut [Node], lang: crate::i18n::Lang, align_to_selection: bool) {
|
||||
for node in nodes {
|
||||
if let Node::Submenu { label, english, children, .. } = node {
|
||||
translate(label, english, lang);
|
||||
level(children, lang);
|
||||
if align_to_selection && label.as_str() == "Align" {
|
||||
let shown = tl!("Align Layers to Selection");
|
||||
*label = shown.into();
|
||||
*english = (shown != "Align Layers to Selection").then(|| "Align Layers to Selection".into());
|
||||
} else {
|
||||
translate(label, english, lang);
|
||||
}
|
||||
level(children, lang, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
for menu in &mut bar.menus {
|
||||
let layer = menu.title == "Layer";
|
||||
if menu.role != MenuRole::App {
|
||||
translate(&mut menu.title, &mut menu.english, lang);
|
||||
}
|
||||
level(&mut menu.children, lang);
|
||||
level(&mut menu.children, lang, layer && align_to_selection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -477,7 +484,8 @@ fn build(app: &PhotocraftApp) -> Built {
|
||||
let label = |s: &'static str| crate::i18n::tr(lang, s).to_string();
|
||||
let keys = |chord: &Chord| craft::ui::runtime::commands::holder(app.ctx(), chord);
|
||||
let options = LayoutOptions::new(crate::native_menu::APP_NAME).label(&label).keys(&keys);
|
||||
translate_titles(&mut bar, lang);
|
||||
let align_to_selection = app.session.active().and_then(|state| state.doc.selection.as_ref()).is_some_and(|mask| !mask.content_bounds().is_empty());
|
||||
translate_titles(&mut bar, lang, align_to_selection);
|
||||
let laid = layout(&bar, target(app.ctx()), &options).bar;
|
||||
Built { rows, source: bar, bar: laid }
|
||||
}
|
||||
@@ -486,6 +494,10 @@ fn build(app: &PhotocraftApp) -> Built {
|
||||
#[path = "background_entry_swap_tests.rs"]
|
||||
mod background_entry_swap_tests;
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
#[path = "layer_align_selection_label_tests.rs"]
|
||||
mod layer_align_selection_label_tests;
|
||||
|
||||
#[cfg(all(test, not(target_arch = "wasm32")))]
|
||||
#[path = "quick_mask_shortcut_text_tests.rs"]
|
||||
mod quick_mask_shortcut_text_tests;
|
||||
@@ -0,0 +1,99 @@
|
||||
use craft::menu::{MenuBar, Node};
|
||||
use craft::window::policy::{CaptionFamily, Captions, Decision, Desktop, DesktopKind, Frame};
|
||||
use egui::accesskit::Role;
|
||||
use egui::{Event, Key, Modifiers, PointerButton};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use photocraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::shortcut_dispatch::tests::app_harness;
|
||||
|
||||
use super::fresh_bar;
|
||||
|
||||
const ALIGN_COMMANDS: [&str; 7] = [
|
||||
"layer.align.topEdges",
|
||||
"layer.align.verticalCenters",
|
||||
"layer.align.bottomEdges",
|
||||
"---",
|
||||
"layer.align.leftEdges",
|
||||
"layer.align.horizontalCenters",
|
||||
"layer.align.rightEdges",
|
||||
];
|
||||
|
||||
fn align_rows<'a>(bar: &'a MenuBar, title: &str) -> Result<Vec<(&'a str, bool)>, String> {
|
||||
let layer = bar.menus.iter().find(|menu| menu.title == "Layer").ok_or("Layer menu missing")?;
|
||||
let children = layer
|
||||
.children
|
||||
.iter()
|
||||
.find_map(|node| match node {
|
||||
Node::Submenu { label, children, .. } if label == title => Some(children),
|
||||
_ => None,
|
||||
})
|
||||
.ok_or_else(|| format!("{title} submenu missing"))?;
|
||||
children
|
||||
.iter()
|
||||
.map(|node| match node {
|
||||
Node::Item(item) => Ok((item.command.id.as_str(), item.enabled)),
|
||||
Node::Separator => Ok(("---", false)),
|
||||
_ => Err(format!("unexpected row in {title}: {node:?}")),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_title_changes_without_changing_the_alignment_commands() -> Result<(), String> {
|
||||
let mut app = PhotocraftApp::new(Session::new(), Default::default());
|
||||
let bar = fresh_bar(&app);
|
||||
let rows = align_rows(&bar, "Align")?;
|
||||
assert_eq!(rows.iter().map(|(id, _)| *id).collect::<Vec<_>>(), ALIGN_COMMANDS);
|
||||
assert!(rows.iter().all(|(_, enabled)| !enabled), "no document: all commands are greyed");
|
||||
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
app.run("select.rect", json!({"x": 4, "y": 4, "width": 20, "height": 16})).unwrap();
|
||||
let bar = fresh_bar(&app);
|
||||
let rows = align_rows(&bar, "Align Layers to Selection")?;
|
||||
assert_eq!(rows.iter().map(|(id, _)| *id).collect::<Vec<_>>(), ALIGN_COMMANDS);
|
||||
assert!(rows.iter().filter(|(id, _)| *id != "---").all(|(_, enabled)| *enabled));
|
||||
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
let bar = fresh_bar(&app);
|
||||
assert_eq!(align_rows(&bar, "Align")?.iter().map(|(id, _)| *id).collect::<Vec<_>>(), ALIGN_COMMANDS);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
type AppHarness = Harness<'static, Box<PhotocraftApp>>;
|
||||
|
||||
fn assert_accessible_title(h: &mut AppHarness, title: &str) {
|
||||
let at = h.get_by_role_and_label(Role::MenuItem, "Layer").rect().center();
|
||||
h.event(Event::PointerMoved(at));
|
||||
h.step();
|
||||
for pressed in [true, false] {
|
||||
h.event(Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
h.step();
|
||||
}
|
||||
h.run_steps(6);
|
||||
assert_eq!(h.get_by_role_and_label(Role::MenuItem, title).accesskit_node().role(), Role::MenuItem);
|
||||
h.key_press(Key::Escape);
|
||||
h.run_steps(4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_rendered_menu_exposes_the_current_title_to_accessibility() {
|
||||
let ctx = egui::Context::default();
|
||||
let frame = Frame::Integrated(Captions { family: CaptionFamily::Windows, layout: craft::window::Layout::default() });
|
||||
let desktop = Desktop { kind: DesktopKind::Other, ssd: None, gtk_theme: None, icon_theme: None };
|
||||
PhotocraftApp::setup_window(&ctx, Default::default(), Decision { frame, overridden: None, desktop }, None);
|
||||
let mut app = PhotocraftApp::with_context(ctx, Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
|
||||
let mut h = app_harness(app);
|
||||
|
||||
assert_accessible_title(&mut h, "Align");
|
||||
h.state_mut().run("select.rect", json!({"x": 4, "y": 4, "width": 20, "height": 16})).unwrap();
|
||||
h.run_steps(4);
|
||||
assert_accessible_title(&mut h, "Align Layers to Selection");
|
||||
h.state_mut().run("select.deselect", json!({})).unwrap();
|
||||
h.run_steps(4);
|
||||
assert_accessible_title(&mut h, "Align");
|
||||
}
|
||||
Reference in new issue
Block a user