1use egui::Ui;
16use egui_pop_core::a11y;
17use egui_pop_core::icon::{Icon, Size};
18use egui_pop_core::indicator::{Anchor, Indicator, IndicatorExt as _, Tone};
19use egui_pop_core::note::Note;
20use egui_pop_core::state::Slot;
21use egui_pop_core::strings;
22use egui_pop_model::ids::{BlockId, CommandId, PanelId, PluginId};
23use egui_pop_model::panels::{Area, PanelSpec};
24
25pub use crate::bus::NoticeBadge;
26pub use egui_pop_model::commands::CommandError;
27pub use egui_pop_model::notice::route::{
28 DefaultRouting, NoticeRouting, Route, RouteCx, Surface, default_route,
29};
30
31use crate::app::AppHandle;
32use crate::strings as txt;
33use crate::zones::{self, Block};
34use crate::{Ctx, Plugin, PluginError};
35
36pub const SITE_SHOWN: usize = 3;
38
39pub fn notice_site(ui: &mut Ui, app: &AppHandle, place: &str) {
50 let ctx = ui.ctx().clone();
51 let hub = &app.0.bus.notices;
52 hub.site_drawn(place, ctx.viewport_id(), ctx.cumulative_pass_nr());
53 let kept: Vec<_> = hub
54 .at(place)
55 .into_iter()
56 .filter(|k| !k.resolved)
57 .rev()
58 .take(SITE_SHOWN)
59 .collect();
60 for k in kept {
61 let n = &k.notice;
62 if !k.seen {
63 app.see_notice(n.id.clone());
64 }
65 let mut close = false;
66 let r = Note::from_notice(n).show_with(ui, |ui| {
67 let name = strings::tr_args(&ctx, &txt::NOTICE_CLOSE, &[("text", &n.headline)]);
68 close = ui
69 .add(Icon::Close.size(Size::Inline).label(&name).sense_click())
70 .clicked();
71 });
72 if let Some(a) = r.action.and_then(|i| n.actions.get(i))
73 && let Ok(command) = CommandId::new(&a.id)
74 {
75 app.run(command, a.args.clone());
76 }
77 if close {
78 app.dismiss_notice(n.id.clone());
79 }
80 }
81}
82
83mod view;
84
85pub use view::{Filter, copy_all_text, listed};
86
87pub const PLUGIN: PluginId = PluginId::from_static("egui_pop.notices");
89
90pub const PANEL_NOTICES: PanelId = PanelId::from_static("egui_pop.notices");
92
93pub const BELL: BlockId = BlockId::from_static("egui_pop.notices.bell");
95
96const BELL_COUNT: Slot<usize> = Slot::new("egui_pop.notices.bell_count");
98
99#[derive(Clone, Debug)]
108pub struct NoticesPlugin {
109 bell: bool,
110 panel: bool,
111}
112
113impl Default for NoticesPlugin {
114 fn default() -> Self {
115 Self {
116 bell: true,
117 panel: true,
118 }
119 }
120}
121
122impl NoticesPlugin {
123 pub fn without_bell(mut self) -> Self {
126 self.bell = false;
127 self
128 }
129
130 pub fn without_panel(mut self) -> Self {
133 self.panel = false;
134 self
135 }
136}
137
138impl Plugin for NoticesPlugin {
139 fn id(&self) -> PluginId {
140 PLUGIN
141 }
142
143 fn title(&self) -> String {
144 "Notices".into()
145 }
146
147 fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
148 if self.bell {
149 ctx.block(zones::STATUS_BAR, Block::add(BELL, 1100, bell));
150 }
151 if self.panel {
152 ctx.panel(
153 PanelSpec::new(PANEL_NOTICES, "Notices", Area::Bottom)
154 .min_size(120.0)
155 .default_size(260.0)
156 .icon("notification")
157 .closed(),
158 panel,
159 )?;
160 }
161 Ok(())
162 }
163}
164
165pub fn bell_text(ctx: &egui::Context, badge: &NoticeBadge) -> String {
168 let count = |n: usize, one: &strings::StrKey, many: &strings::StrKey| match n {
169 0 => None,
170 1 => Some(strings::tr(ctx, one)),
171 n => Some(strings::tr_with(ctx, many, &[("count", n.into())])),
172 };
173 let parts: Vec<String> = [
174 count(badge.errors, &txt::NOTICES_ERRORS_ONE, &txt::NOTICES_ERRORS),
175 count(
176 badge.warnings,
177 &txt::NOTICES_WARNINGS_ONE,
178 &txt::NOTICES_WARNINGS,
179 ),
180 ]
181 .into_iter()
182 .flatten()
183 .collect();
184 if parts.is_empty() {
185 return strings::tr(ctx, &txt::NOTICES_NONE_UNSEEN);
186 }
187 strings::tr_args(ctx, &txt::NOTICES_UNSEEN, &[("counts", &parts.join(", "))])
188}
189
190pub fn bell(ui: &mut Ui, app: &AppHandle) {
199 let ctx = ui.ctx().clone();
200 let badge = app.notice_badge();
201 let count = badge.errors + badge.warnings;
202 let tip = bell_text(&ctx, &badge);
203 if count > BELL_COUNT.get(&ctx).unwrap_or(0) {
204 a11y::announce(&ctx, tip.clone(), a11y::Politeness::Polite);
205 }
206 BELL_COUNT.set(&ctx, count);
207 let tone = if badge.errors > 0 {
208 Tone::Danger
209 } else {
210 Tone::Warning
211 };
212 let name = strings::tr(&ctx, &txt::NOTICES_TITLE);
213 let r = ui
214 .add(Icon::Notification.label(&name).sense_click())
215 .with_indicator(
216 ui,
217 Indicator::count(count as u64).tone(tone).id("notices.bell"),
218 Anchor::TopEnd,
219 )
220 .on_hover_text(&tip);
221 if r.clicked() && app.panels().iter().any(|p| p.id == PANEL_NOTICES) {
222 app.focus_panel(&PANEL_NOTICES);
223 }
224}
225
226pub fn panel(ui: &mut Ui, app: &AppHandle) {
234 view::show(ui, app, true);
235}
236
237pub fn panel_preview(ui: &mut Ui, app: &AppHandle) {
244 view::show(ui, app, false);
245}