Skip to main content

egui_pop_api/bus/
panel_describe.rs

1//! A panel described on the bus (ADR 0029): `pop.panel.describe` answers
2//! what a declared panel is, its kind first ([`PanelKind`]: whether it
3//! edits), for agents, tests and the application's own parts, without a
4//! screenshot.
5//!
6//! The workbench's listener ([`PANEL_DESCRIBE`]) fills the description
7//! from the panel's declaration; a listener after it may add to it. It is
8//! asked on the interface's thread ([`AppHandle::describe_panel`]):
9//! elsewhere nobody answers and the description stays `None`.
10//!
11//! [`AppHandle::describe_panel`]: crate::AppHandle::describe_panel
12
13use bus_contract::{Bus, ListenOpts, Point, PointId, PointKind};
14use egui_pop_model::ids::PanelId;
15use egui_pop_model::panels::PanelKind;
16use serde::{Deserialize, Serialize};
17
18use super::{OWNER, asking};
19
20/// A declared panel, as the bus describes it.
21#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
22#[non_exhaustive]
23pub struct PanelDescription {
24    /// The panel's id.
25    pub panel: String,
26    /// Its title, as declared (English).
27    pub title: String,
28    /// What it does to what the application edits.
29    pub kind: PanelKind,
30    /// Whether it is a palette (it floats when it opens).
31    pub palette: bool,
32}
33
34/// A panel is described (`pop.panel.describe`): the input is its id, the
35/// value its description, `None` while nobody knows it.
36#[derive(Debug)]
37pub struct PanelDescribe;
38impl Point for PanelDescribe {
39    const KIND: PointKind = PointKind::Alter;
40    const ID: PointId = PointId::new("pop.panel.describe");
41    type Input = String;
42    type Value = Option<PanelDescription>;
43}
44
45/// The panel description points, in the order [`super::order_dump`]
46/// prints them.
47pub const PANEL_DESCRIBE_POINTS: [&str; 1] = [PanelDescribe::ID.as_str()];
48
49/// The workbench's listener of `pop.panel.describe`: describes a declared
50/// panel from its declaration.
51pub const PANEL_DESCRIBE: &str = "pop.workbench.panel_describe";
52
53/// Registers the workbench's listener of `pop.panel.describe`.
54pub(super) fn install(kernel: &dyn Bus) {
55    let listened = kernel.listen::<PanelDescribe>(
56        PANEL_DESCRIBE,
57        ListenOpts::new().owner(OWNER),
58        |panel, value| {
59            let (Some(app), Ok(id)) = (asking(), PanelId::new(panel)) else {
60                return Ok(());
61            };
62            let Some(wb) = app.try_workbench() else {
63                return Err(format!("`{panel}`: the workbench is busy").into());
64            };
65            if let Some(spec) = wb.spec(&id) {
66                *value = Some(PanelDescription {
67                    panel: spec.id.to_string(),
68                    title: spec.title.clone(),
69                    kind: spec.kind,
70                    palette: spec.palette,
71                });
72            }
73            Ok(())
74        },
75    );
76    debug_assert!(listened.is_ok(), "{listened:?}");
77}