egui_pop_api/bus/panel_retitle.rs
1//! A panel instance retitled on the bus (ADR 0029): `pop.panel.retitle`
2//! names an instance's tab title, whoever asks (the application's code
3//! through [`AppHandle::set_instance_title`], an agent, a test).
4//!
5//! The ask names an open instance `<kind>#<n>` of a panel declared with
6//! several (`PanelSpec::instances`) and its title; `title: null` gives the
7//! generated one back (the kind's title and the number, "Gallery 2"). The
8//! workbench's listener ([`PANEL_RETITLE`]) keeps the title in the
9//! layout's instance entry (`Layout::instances`), so it is saved and
10//! restored with the layout; the tab, the panel menu's list of instances,
11//! the View menu and accessibility read it, and so does the
12//! [`Instance`](crate::workbench::instances::Instance) handed to the draw
13//! function. The journal says `panel.instance.retitled`. Anything else
14//! (the panel declared, a panel declared once, an instance not open) is
15//! refused with the reason.
16//!
17//! [`AppHandle::set_instance_title`]: crate::AppHandle::set_instance_title
18
19use bus_contract::{Bus, ListenOpts, Point, PointId, PointKind};
20use egui_pop_model::ids::PanelId;
21use serde::{Deserialize, Serialize};
22
23use super::{OWNER, asking};
24
25/// A panel instance asked retitled: which instance, its title (`None`:
26/// the generated one again).
27///
28/// ```
29/// use egui_pop_api::bus::PanelRetitleAsk;
30///
31/// let ask = PanelRetitleAsk::new("app.gallery#2", Some("Picks".into()));
32/// let json = serde_json::to_value(&ask).unwrap();
33/// assert_eq!(json["title"], "Picks");
34/// let back: PanelRetitleAsk = serde_json::from_value(serde_json::json!({
35/// "panel": "app.gallery#2", "title": null
36/// })).unwrap();
37/// assert_eq!(back.title, None);
38/// ```
39#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
40#[non_exhaustive]
41pub struct PanelRetitleAsk {
42 /// The instance's id (`<kind>#<n>`).
43 pub panel: String,
44 /// Its title; `None` (`null`) gives the generated title back.
45 #[serde(default)]
46 pub title: Option<String>,
47}
48
49impl PanelRetitleAsk {
50 pub fn new(panel: impl Into<String>, title: Option<String>) -> Self {
51 Self {
52 panel: panel.into(),
53 title,
54 }
55 }
56}
57
58/// A panel instance is asked retitled (`pop.panel.retitle`); its
59/// listeners say what they did, one line each, in the value.
60#[derive(Debug)]
61pub struct PanelRetitle;
62impl Point for PanelRetitle {
63 const KIND: PointKind = PointKind::Request;
64 const ID: PointId = PointId::new("pop.panel.retitle");
65 type Input = PanelRetitleAsk;
66 type Value = Vec<String>;
67}
68
69/// The panel retitling points, in the order [`super::order_dump`] prints
70/// them.
71pub const PANEL_RETITLE_POINTS: [&str; 1] = [PanelRetitle::ID.as_str()];
72
73/// The workbench's listener of `pop.panel.retitle`: keeps the title in
74/// the layout's instance entry.
75pub const PANEL_RETITLE: &str = "pop.workbench.panel_retitle";
76
77/// Registers the workbench's listener of `pop.panel.retitle`.
78pub(super) fn install(kernel: &dyn Bus) {
79 let listened =
80 kernel.listen::<PanelRetitle>(PANEL_RETITLE, ListenOpts::new().owner(OWNER), |ask, did| {
81 let Some(app) = asking() else {
82 return Ok(());
83 };
84 let panel = PanelId::new(&ask.panel).map_err(|e| e.to_string())?;
85 let changed = app.retitle_instance(&panel, ask.title.clone())?;
86 did.push(if changed {
87 format!("{PANEL_RETITLE}: `{panel}` retitled")
88 } else {
89 format!("{PANEL_RETITLE}: `{panel}` unchanged")
90 });
91 Ok(())
92 });
93 debug_assert!(listened.is_ok(), "{listened:?}");
94}