egui_pop_api/tips/
browser.rs1use egui::Ui;
7use egui_pop_core::strings;
8
9use super::{TIPS, Tip, markup};
10use crate::app::AppHandle;
11use crate::strings as txt;
12
13pub fn groups(tips: &[Tip]) -> Vec<(String, Vec<Tip>)> {
16 let mut out: Vec<(String, Vec<Tip>)> = Vec::new();
17 for t in tips {
18 let origin = t.origin_name();
19 match out.iter_mut().find(|(o, _)| o == origin) {
20 Some((_, list)) => list.push(t.clone()),
21 None => out.push((origin.to_string(), vec![t.clone()])),
22 }
23 }
24 out
25}
26
27fn origin_title(app: &AppHandle, origin: &str) -> String {
30 app.plugins()
31 .into_iter()
32 .find(|p| p.id.as_str() == origin)
33 .filter(|p| !p.title.is_empty())
34 .map(|p| {
35 strings::declared(
36 &app.egui(),
37 strings::kind::PLUGIN,
38 origin,
39 Some("title"),
40 &p.title,
41 )
42 })
43 .unwrap_or_else(|| origin.to_string())
44}
45
46pub fn browser(ui: &mut Ui, app: &AppHandle) {
48 let Some(tips) = app.service(&TIPS) else {
49 return;
50 };
51 let ctx = ui.ctx().clone();
52 let all = tips.all();
53 if all.is_empty() {
54 ui.weak(strings::tr(&ctx, &txt::TIPS_NONE));
55 return;
56 }
57 let dismissed = tips.dismissed(app);
58 let used = tips.used(app);
59 let off = tips.off(app);
60 for (origin, list) in groups(&all) {
61 let title = origin_title(app, &origin);
62 ui.horizontal(|ui| {
63 ui.strong(&title);
64 let mut on = !off.contains(&origin);
65 let label = strings::tr(&ctx, &txt::TIPS_ORIGIN_ON);
66 let r = ui.checkbox(&mut on, &label);
67 let name = strings::tr_args(&ctx, &txt::TIPS_ORIGIN_ON_NAME, &[("origin", &title)]);
68 egui_pop_core::a11y::name(&r, egui::Role::CheckBox, &name);
69 if r.changed()
70 && let Err(e) = tips.set_origin_on(app, &origin, on)
71 {
72 log::warn!("tips: {e}");
73 }
74 });
75 ui.indent(("egui_pop.tips.browser", &origin), |ui| {
76 for tip in &list {
77 let tip_title = tip.title_in(&ctx);
78 ui.horizontal(|ui| {
79 ui.label(egui::RichText::new(&tip_title).strong());
80 let state = if dismissed.contains(&tip.id) {
81 Some(&txt::TIPS_STATE_DISMISSED)
82 } else if tip
83 .command
84 .as_ref()
85 .is_some_and(|c| used.contains(c.as_str()))
86 {
87 Some(&txt::TIPS_STATE_USED)
88 } else {
89 None
90 };
91 if let Some(s) = state {
92 ui.weak(strings::tr(&ctx, s));
93 }
94 let r = ui.small_button(strings::tr(&ctx, &txt::TIPS_SHOW));
95 let name = strings::tr_args(&ctx, &txt::TIPS_SHOW_NAME, &[("tip", &tip_title)]);
96 egui_pop_core::a11y::name(&r, egui::Role::Button, &name);
97 if r.clicked()
98 && let Err(e) = tips.show(app, &tip.id)
99 {
100 log::warn!("tips: {e}");
101 }
102 });
103 let text = tip.text_shown(&ctx, app);
104 let job = markup::job(&text, ui.style(), ui.available_width());
105 ui.label(job);
106 ui.add_space(ui.spacing().item_spacing.y);
107 }
108 });
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115
116 #[test]
117 fn tips_group_by_origin_in_the_order_registered() {
118 let tips = [
119 Tip::new("photos.rate", "Rate", "R."),
120 Tip::new("maps.zoom", "Zoom", "Z."),
121 Tip::new("photos.compare", "Compare", "C."),
122 Tip::new("any.thing", "Thing", "T.").origin("maps"),
123 ];
124 let g: Vec<(String, usize)> = groups(&tips)
125 .into_iter()
126 .map(|(o, l)| (o, l.len()))
127 .collect();
128 assert_eq!(g, [("photos".to_string(), 2), ("maps".to_string(), 2)]);
129 }
130}