Skip to main content

egui_pop_api/admin/
detail.rs

1//! A row's detail sheet ([`Records::detail`]): a window-modal sheet over
2//! the window it is opened from, listing the row's declared keys as
3//! label and value lines, multi-line text kept. Its Close button and
4//! Escape close it.
5
6use egui::Ui;
7use egui::accesskit::Role;
8use egui_pop_core::a11y;
9use egui_pop_core::theme::Tokens;
10use egui_pop_model::admin::{CellText, Records, cell_text};
11use serde_json::{Value as Json, json};
12
13use super::Admin;
14use super::shell::{builtin_text, cell_words};
15use crate::app::AppHandle;
16use crate::dialogs::{Custom, CustomPass, DIALOGS};
17
18/// A detail sheet's size, in points (capped to its window).
19const SIZE: [f32; 2] = [480.0, 360.0];
20
21/// What a line shows for an empty value.
22const NO_VALUE: &str = "—";
23
24impl Admin {
25    /// Opens the detail sheet of `row`, one of `records`' rows, over the
26    /// window drawn (or the one whose command opened it): the sheet
27    /// [`Records::detail`] declares, its title the row's, each line a
28    /// label over its value. Returns the dialog's id; refused when the
29    /// records declare no sheet or there are no dialogs.
30    pub fn open_detail(
31        &self,
32        app: &AppHandle,
33        records: &Records,
34        row: &Json,
35    ) -> Result<u64, String> {
36        if records.detail.is_none() {
37            return Err(format!(
38                "the records `{}` declare no detail sheet",
39                records.meta.id
40            ));
41        }
42        let dialogs = app
43            .service(&DIALOGS)
44            .ok_or("a detail sheet needs the dialogs plugin")?;
45        let title = records.detail_title(row);
46        let custom = Custom::closing(&title).size(SIZE[0], SIZE[1]);
47        let (records, row) = (records.clone(), row.clone());
48        dialogs.open_custom(app, custom, move |ui, app, pass| {
49            sheet(ui, app, &records, &row, pass);
50        })
51    }
52}
53
54/// A record's value as words: a keyed text in the interface's language,
55/// anything else written out ([`cell_text`]).
56fn value_words(ctx: &egui::Context, v: &Json) -> String {
57    match serde_json::from_value::<CellText>(v.clone()) {
58        Ok(t @ CellText::Key { .. }) if v.is_object() => cell_words(ctx, &t),
59        _ => cell_text(v),
60    }
61}
62
63/// The sheet's lines: each declared key's label and value.
64fn lines(
65    ctx: &egui::Context,
66    app: &AppHandle,
67    records: &Records,
68    row: &Json,
69) -> Vec<(String, String)> {
70    records
71        .detail
72        .iter()
73        .flat_map(|d| &d.lines)
74        .map(|l| {
75            let value = value_words(ctx, &row[l.key.as_str()]);
76            (builtin_text(ctx, app, &l.title), value)
77        })
78        .collect()
79}
80
81/// The sheet's body: each line its label (muted) over its value, which
82/// wraps, keeps its line breaks, and can be selected and copied. Each
83/// value is named "label: value" for assistive technology.
84fn sheet(ui: &mut Ui, app: &AppHandle, records: &Records, row: &Json, pass: &mut CustomPass) {
85    let ctx = ui.ctx().clone();
86    let t = Tokens::of(&ctx);
87    let lines = lines(&ctx, app, records, row);
88    egui::ScrollArea::vertical()
89        .id_salt(("egui_pop.admin.detail", &records.meta.id))
90        .auto_shrink([false, false])
91        .show(ui, |ui| {
92            for (title, value) in &lines {
93                ui.weak(title);
94                let shown = if value.is_empty() { NO_VALUE } else { value };
95                let resp = ui.add(egui::Label::new(shown).wrap().selectable(true));
96                a11y::name(&resp, Role::Label, &format!("{title}: {shown}"));
97                ui.add_space(t.space.s);
98            }
99        });
100    pass.describe(json!({
101        "records": records.meta.id,
102        "row": cell_text(&row[records.key.as_str()]),
103        "lines": lines
104            .iter()
105            .map(|(title, value)| json!({ "title": title, "value": value }))
106            .collect::<Vec<_>>(),
107    }));
108}