Skip to main content

egui_pop/
mounts.rs

1//! The mount contract across the crates enabled (ADR 0013): every
2//! component and host declared, in `docs/hosts.md`'s order, the page
3//! written from them, and [`MountsPlugin`], which declares the
4//! components and hosts of the component crates to the application so
5//! that `AppHandle::where_can` and the `mounts` collector know them.
6//!
7//! ```
8//! let page = egui_pop::mounts::page();
9//! assert!(page.starts_with("# Hosts and components"));
10//! assert!(egui_pop::mounts::components().iter().any(|m| m.id == "jobs-centre"));
11//! ```
12
13use egui_pop_api::{Ctx, Plugin, PluginError};
14use egui_pop_model::ids::PluginId;
15use egui_pop_model::mount::{HostOffer, Mountable, page};
16
17/// Every component of the enabled crates: the viewport's, the views',
18/// the editors', then `egui-pop-api`'s and `egui-pop-core`'s.
19pub fn components() -> Vec<Mountable> {
20    let mut out = crates();
21    out.extend(egui_pop_api::mounts::components());
22    out
23}
24
25/// The components of the component crates (not `egui-pop-api`'s, which
26/// the application knows already).
27fn crates() -> Vec<Mountable> {
28    #[allow(
29        unused_mut,
30        reason = "without the component features nothing is added to it"
31    )]
32    let mut out = Vec::new();
33    #[cfg(feature = "viewport")]
34    out.extend(egui_pop_viewport::mounts::components());
35    #[cfg(feature = "views")]
36    out.extend(egui_pop_views::mounts::components());
37    #[cfg(feature = "editors")]
38    out.extend(egui_pop_editors::mounts::components());
39    out
40}
41
42/// Every host, in `docs/hosts.md`'s column order: `egui-pop-api`'s, the
43/// viewport's overlays before the gallery's story.
44pub fn hosts() -> Vec<HostOffer> {
45    #[allow(
46        unused_mut,
47        reason = "without the component features nothing is added to it"
48    )]
49    let mut out = egui_pop_api::mounts::hosts();
50    #[cfg(feature = "viewport")]
51    {
52        let at = out
53            .iter()
54            .position(|h| h.id == egui_pop_api::mounts::STORY)
55            .unwrap_or(out.len());
56        out.insert(at, egui_pop_viewport::mounts::overlay_host());
57    }
58    out
59}
60
61/// `docs/hosts.md`, as the declarations write it.
62pub fn page() -> String {
63    page::render(&components(), &hosts())
64}
65
66/// Declares the components and hosts of the component crates enabled
67/// (the viewport, the views, the editors) to the application.
68#[derive(Debug, Default)]
69pub struct MountsPlugin;
70
71impl Plugin for MountsPlugin {
72    fn id(&self) -> PluginId {
73        PluginId::from_static("egui_pop.mounts")
74    }
75
76    fn title(&self) -> String {
77        "Mount declarations".into()
78    }
79
80    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
81        for m in crates() {
82            ctx.mountable(m);
83        }
84        #[cfg(feature = "viewport")]
85        ctx.host_offer(egui_pop_viewport::mounts::overlay_host());
86        Ok(())
87    }
88}