egui_pop_api/admin/
detail.rs1use 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
18const SIZE: [f32; 2] = [480.0, 360.0];
20
21const NO_VALUE: &str = "—";
23
24impl Admin {
25 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
54fn 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
63fn 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
81fn 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}