Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions crates/ui-egui/src/panels/detail/filmstrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ pub(crate) fn filmstrip(app: &mut LightcraftApp, ui: &mut egui::Ui, r: Rect) {
}
let resp = ui.interact(cr, egui::Id::unique(("film", id.0)), Sense::click());
let failed = app.renderer.thumb(*id).is_none() && app.renderer.failure(Slot::Thumb(*id)).is_some();
crate::widgets::announce(&resp, film_spoken(app.session.catalog.photo(*id).map_or("", |p| p.file_name.as_str()), failed));
register(ui, format!("film:{}", id.0), cr);
let state = app.session.selection.state_of(*id);
let selected = state != lightcraft_engine::SelectionState::NotSelected;
// the same name as the grid's cell for this photo (file, rating, flag, label), and whether it is selected
let name = app.session.catalog.photo(*id).map(|p| crate::panels::grid::photo_spoken(&app.session.catalog, p)).unwrap_or_default();
crate::widgets::announce_cell(&resp, film_spoken(&name, failed), selected);
register(ui, format!("film:{}", id.0), cr);
let sel = state == lightcraft_engine::SelectionState::Active;
let p = ui.painter();
let label = app.session.catalog.photo(*id).and_then(|ph| ph.label);
let selected = state != lightcraft_engine::SelectionState::NotSelected;
let base = if selected {
t.cell_selected
} else if resp.hovered() {
Expand Down Expand Up @@ -192,14 +194,15 @@ pub(crate) fn filmstrip(app: &mut LightcraftApp, ui: &mut egui::Ui, r: Rect) {
});
}

/// What a screen reader says for a filmstrip thumbnail: the file, and why there is no picture
/// What a screen reader says for a filmstrip thumbnail: the photo's name as the grid says it
/// ([`crate::panels::grid::photo_spoken`]: file, rating, flag, label), and why there is no picture
/// when the thumbnail failed (the painted `!` has no name of its own). The name is empty when the
/// catalog no longer has the photo (or its file name is blank); the reason then stands alone.
pub(crate) fn film_spoken(file_name: &str, failed: bool) -> String {
match (file_name.is_empty(), failed) {
(_, false) => file_name.to_string(),
/// catalog no longer has the photo; the reason then stands alone.
pub(crate) fn film_spoken(name: &str, failed: bool) -> String {
match (name.is_empty(), failed) {
(_, false) => name.to_string(),
(true, true) => crate::i18n::tr("This photo can't be opened").to_string(),
(false, true) => format!("{file_name}, {}", crate::i18n::tr("This photo can't be opened")),
(false, true) => format!("{name}, {}", crate::i18n::tr("This photo can't be opened")),
}
}

Expand Down
10 changes: 6 additions & 4 deletions crates/ui-egui/src/panels/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ fn cell(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, r: Rect, square
register(ui, format!("thumb:{}", id.0), r);
let state = app.session.selection.state_of(id);
let selected = state != lightcraft_engine::SelectionState::NotSelected;
let spoken = cell_description(app, &photo);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, selected, &spoken));
let spoken = photo_spoken(&app.session.catalog, &photo);
crate::widgets::announce_cell(&resp, spoken.as_str(), selected);
let active = state == lightcraft_engine::SelectionState::Active;
let p = ui.painter();
let img_rect = if square {
Expand All @@ -797,7 +797,9 @@ fn cell(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, r: Rect, square
cell_interaction(app, ui, &resp, id, &photo);
}

fn cell_description(app: &LightcraftApp, photo: &lightcraft_catalog::Photo) -> String {
/// What a screen reader says for a photo's thumbnail, in the grid and in the filmstrip: the file,
/// then rating / flag / label.
pub(crate) fn photo_spoken(catalog: &lightcraft_catalog::Catalog, photo: &lightcraft_catalog::Photo) -> String {
let mut spoken = photo.file_name.clone();
if photo.rating > 0 {
spoken.push_str(&crate::i18n::tr_format!(", {} star{}", photo.rating, if photo.rating == 1 { "" } else { "s" }));
Expand All @@ -808,7 +810,7 @@ fn cell_description(app: &LightcraftApp, photo: &lightcraft_catalog::Photo) -> S
lightcraft_catalog::Flag::None => {}
}
if let Some(l) = photo.label {
spoken.push_str(&crate::i18n::tr_format!(", {} label", crate::i18n::color_label(&app.session.catalog, l)));
spoken.push_str(&crate::i18n::tr_format!(", {} label", crate::i18n::color_label(catalog, l)));
}
spoken
}
Expand Down
12 changes: 12 additions & 0 deletions crates/ui-egui/src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,18 @@ pub(crate) fn announce_selected(resp: &egui::Response, name: impl Into<String>,
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, resp.enabled(), selected, name.clone()));
}

/// A photo's thumbnail (grid cell, filmstrip thumbnail) for screen readers: a selectable list
/// item that is, or is not, selected.
///
/// egui's `WidgetInfo::selected` publishes AccessKit's `Toggled`, which macOS turns into a check box
/// ("checked"). A list item with AccessKit's `selected` state is what macOS reads as "selected",
/// and it keeps the click action, so one helper gives the grid and the filmstrip the same role.
pub(crate) fn announce_cell(resp: &egui::Response, name: impl Into<String>, selected: bool) {
let name = name.into();
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListItem, resp.enabled(), name.clone()));
resp.ctx.accesskit_node_builder(resp.id, |node| node.set_selected(selected));
}

/// `r`, named `name` for screen readers: a plain egui field or slider whose caption is drawn
/// beside it, or whose hint is all it shows.
pub(crate) fn named(r: egui::Response, name: &str) -> egui::Response {
Expand Down
63 changes: 63 additions & 0 deletions crates/ui-egui/tests/it/filmstrip_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! What a screen reader is told about a filmstrip thumbnail: the same name as the grid's cell for
//! the same photo (file, rating, flag) and whether it is selected, not just the file name.
use egui::accesskit::Role;
use egui::vec2;
use egui_kittest::{Harness, kittest::NodeT};
use lightcraft_catalog::Flag;
use lightcraft_ui_egui::{
LightcraftApp, Services,
state::{RightPanel, ViewMode},
};

fn detail() -> Harness<'static, LightcraftApp> {
let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
app.ui.left_panel = false;
app.ui.view = ViewMode::Detail;
app.ui.right = RightPanel::None;
let mut h = Harness::builder().with_size(vec2(1500.0, 950.0)).build_ui_state(
|ui, app: &mut LightcraftApp| {
app.logic(&ui.ctx().clone());
app.ui(ui);
},
app,
);
h.run_steps(10);
h
}

#[test]
fn a_filmstrip_thumbnail_is_named_with_its_rating_and_flag() {
let h = detail();
let cells: Vec<String> =
h.root().children_recursive().filter(|n| n.accesskit_node().role() == Role::ListItem).filter_map(|n| n.accesskit_node().label()).collect();
let mut rated = 0;
let mut flagged = 0;
for photo in h.state().session.catalog.photos() {
let Some(name) = cells.iter().find(|b| b.starts_with(photo.file_name.as_str())) else { continue };
if photo.rating > 0 {
rated += 1;
let stars = format!("{} star", photo.rating);
assert!(name.contains(&stars), "{name:?} says its {stars}");
}
if photo.flag == Flag::Reject {
flagged += 1;
assert!(name.contains("rejected"), "{name:?} says it is rejected");
}
}
assert!(rated >= 3, "the filmstrip shows several rated photos ({rated}): {cells:?}");
assert!(flagged >= 1, "and a rejected one ({flagged}): {cells:?}");
}

#[test]
fn the_active_filmstrip_thumbnail_is_announced_as_selected() {
let h = detail();
let active = h.state().session.selection.active.and_then(|id| h.state().session.catalog.photo(id).map(|p| p.file_name.clone()));
let active = active.expect("the demo library opens with an active photo");
let node = h
.root()
.children_recursive()
.find(|n| n.accesskit_node().role() == Role::ListItem && n.accesskit_node().label().is_some_and(|l| l.starts_with(active.as_str())))
.unwrap_or_else(|| panic!("a filmstrip thumbnail named {active:?}"));
// (a selectable cell with its selected state on, as the grid's cells are)
assert_eq!(node.accesskit_node().is_selected(), Some(true), "the active photo's thumbnail is selected: {:?}", node.accesskit_node().data());
}
2 changes: 2 additions & 0 deletions crates/ui-egui/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

mod accessibility;
mod docking_workflows;
mod filmstrip_names;
mod thumb_cells;
80 changes: 80 additions & 0 deletions crates/ui-egui/tests/it/thumb_cells.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! A photo's thumbnail, in the grid and in the filmstrip, is a selectable cell: a screen reader
//! (macOS VoiceOver) reads "selected" for it, not a check box that is "checked". AccessKit's macOS
//! adapter turns a node with a toggled state into a check box, and says "selected" for an item-like
//! node (a list item) that carries a selected state, so the cell is a `ListItem` with `selected`.
use egui::accesskit::{Role, Toggled};
use egui::vec2;
use egui_kittest::{Harness, kittest::NodeT};
use lightcraft_engine::Selection;
use lightcraft_ui_egui::{
LightcraftApp, Services,
state::{RightPanel, ViewMode},
};

fn harness(view: ViewMode) -> Harness<'static, LightcraftApp> {
let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
app.ui.left_panel = false;
app.ui.view = view;
app.ui.right = RightPanel::None;
let mut h = Harness::builder().with_size(vec2(1500.0, 950.0)).build_ui_state(
|ui, app: &mut LightcraftApp| {
app.logic(&ui.ctx().clone());
app.ui(ui);
},
app,
);
h.run_steps(10);
h
}

/// What the accessibility tree says about the cell named `file`: (role, selected, toggled).
fn cell(h: &Harness<'_, LightcraftApp>, file: &str) -> Option<(Role, Option<bool>, Option<Toggled>)> {
h.root()
.children_recursive()
.filter(|n| n.accesskit_node().label().is_some_and(|l| l.starts_with(file)))
.map(|n| {
let a = n.accesskit_node();
(a.role(), a.is_selected(), a.toggled())
})
.find(|(role, _, _)| matches!(role, Role::ListItem | Role::Button | Role::CheckBox))
}

fn two_photos(h: &mut Harness<'_, LightcraftApp>) -> (lightcraft_catalog::PhotoId, String, String) {
let mut shown = h.state_mut().session.visible_cloned().into_iter();
let cat = &h.state().session.catalog;
let (a, b) = (shown.next().expect("the demo library has photos"), shown.next().expect("and a second one"));
let name = |id| cat.photo(id).map(|p| p.file_name.clone()).expect("a demo photo");
(a, name(a), name(b))
}

fn assert_selectable_cells(view: ViewMode) {
let mut h = harness(view);
let (first, first_name, second_name) = two_photos(&mut h);
h.state_mut().session.selection = Selection::single(first);
h.run_steps(5);
let (role, selected, toggled) = cell(&h, &first_name).expect("the selected photo has a cell");
assert_eq!(role, Role::ListItem, "{first_name}: a selectable cell, not a button that macOS reads as a check box");
assert_eq!(selected, Some(true), "{first_name} reads as selected");
assert_eq!(toggled, None, "{first_name} has no toggled state (macOS would call it a check box)");
let (role, selected, toggled) = cell(&h, &second_name).expect("the other photo has a cell");
assert_eq!(role, Role::ListItem);
assert_eq!(selected, Some(false), "{second_name} is a selectable cell that is not selected");
assert_eq!(toggled, None);

// a selection change reaches the tree
let second = h.state_mut().session.visible_cloned().get(1).copied().expect("a second photo");
h.state_mut().session.selection = Selection::single(second);
h.run_steps(5);
assert_eq!(cell(&h, &first_name).map(|c| c.1), Some(Some(false)), "{first_name} is deselected");
assert_eq!(cell(&h, &second_name).map(|c| c.1), Some(Some(true)), "{second_name} is selected");
}

#[test]
fn a_grid_cell_is_a_selectable_cell() {
assert_selectable_cells(ViewMode::PhotoGrid);
}

#[test]
fn a_filmstrip_thumbnail_is_a_selectable_cell() {
assert_selectable_cells(ViewMode::Detail);
}
Loading
Loading