Skip to main content

perspective_viewer/components/
main_panel.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13//! `MainPanel`: the multi-panel layout host. Owns the `<regular-layout>`
14//! element, the per-panel `<PanelTab>`s, the shared status bar, and the panel
15//! context menu. The `Component` impl below is thin — its handlers live in the
16//! submodules:
17//!
18//! - [`update`] — the small `MainPanelMsg` handlers (pointer, close detection,
19//!   tab/active sync, context menu).
20//! - [`presize`] — the `BeforeResize` pre-size-every-plugin algorithm.
21//! - [`reconcile`] — the `rendered` layout reconcile + per-panel theme stamp.
22//! - [`frame_theme`] — the `rendered` frame-background mirror (each
23//!   `<regular-layout-frame>`'s panel-theme background var).
24//! - [`render`] — the `view` (status bar, cells, tabs, menu).
25
26mod frame_theme;
27mod presize;
28mod reconcile;
29mod render;
30mod update;
31
32pub mod msg;
33
34use std::collections::{HashMap, HashSet};
35
36use perspective_js::utils::JsValueSerdeExt;
37use wasm_bindgen::prelude::*;
38use yew::prelude::*;
39
40pub use self::msg::MainPanelMsg;
41use super::panel_menu::PanelCommand;
42use crate::presentation::{Presentation, PresentationProps};
43use crate::renderer::*;
44use crate::session::{Session, SessionProps};
45use crate::tasks::PanelResizeObserverHandle;
46use crate::workspace::{PanelId, Workspace};
47
48#[derive(Clone, Properties)]
49pub struct MainPanelProps {
50    /// Toggle the settings sidebar open. Fired by a `PanelTab`'s open-settings
51    /// button (after selecting + activating that panel) — the tabs are the
52    /// only open-settings affordance.
53    pub on_settings: Callback<()>,
54
55    /// Reset callback forwarded from the root component.  Fired when the user
56    /// clicks the reset button; `bool` is `true` for a full reset (expressions
57    /// + column configs), `false` for config-only.
58    pub on_reset: Callback<bool>,
59
60    /// Fired with a panel id when its frame titlebar is pressed, to make it the
61    /// active panel.
62    pub on_activate_panel: Callback<String>,
63
64    /// Fired with a panel id from a tab × (an app-initiated close): the root
65    /// removes it from the model at once and parks its engines.
66    pub on_close_panel: Callback<String>,
67
68    /// Fired with a panel id once the `regular-layout` tree no longer holds
69    /// it, so the root can eject the parked panel.
70    pub on_panel_closed: Callback<String>,
71
72    /// Fired with `(panel id, command)` when the panel context menu selects a
73    /// command the root executes (New/Duplicate/Reset/ToggleMaster/Close).
74    /// Maximize/Restore are handled HERE (this component owns the layout
75    /// element), and Export/Copy end-to-end by
76    /// [`PanelMenu`](super::panel_menu::PanelMenu) itself.
77    pub on_panel_command: Callback<(String, PanelCommand)>,
78
79    /// Snapshots threaded from root.  Read for `has_table`, `title` here in
80    /// the panel itself; threaded wholesale to `StatusBar`/`StatusIndicator`.
81    pub session_props: SessionProps,
82    pub renderer_props: RendererProps,
83    pub presentation_props: PresentationProps,
84
85    /// Derived from root: `settings_open && has_table_loaded`.
86    pub is_settings_open: bool,
87
88    /// Root-managed in-flight render counter (not engine state).
89    pub update_count: u32,
90
91    /// Ids of every layout panel, in order; one `<regular-layout>` cell is
92    /// rendered per id. Drives re-render when panels are added/removed.
93    pub panel_ids: Vec<PanelId>,
94
95    /// `(panel id, session title)` for every panel. Rendered into the
96    /// `<regular-layout>` `style` as `--regular-layout-<id>--title` custom
97    /// properties, which regular-layout's tabs display via `::before` content.
98    pub panel_titles: Vec<(String, Option<String>)>,
99
100    /// `(panel id, per-panel theme)` for every panel. A snapshot so a
101    /// per-panel theme change re-renders MainPanel — `renderer.theme()` is
102    /// interior-mutable and not otherwise observed by `eq`. Each frame inlines
103    /// its theme's `--psp-*` block only when it diverges from the host theme.
104    pub panel_themes: Vec<(String, Option<String>)>,
105
106    /// The master (filter-source) panel ids, sorted. A snapshot so a master
107    /// toggle re-renders MainPanel — the role set is interior-mutable on
108    /// `Workspace` and not otherwise observed by `eq`. Drives each tab's
109    /// broadcast badge.
110    pub panel_masters: Vec<PanelId>,
111
112    /// Element-level global filters (fed by master/detail selection), threaded
113    /// to the `StatusBar` where the global-filter chips are rendered.
114    pub global_filters: Vec<perspective_client::config::Filter>,
115
116    /// Remove the global filter at this index (a chip's × in the `StatusBar`).
117    pub on_remove_global_filter: Callback<usize>,
118
119    /// Clear all global filters (the "Clear" affordance in the `StatusBar`).
120    pub on_clear_global_filters: Callback<()>,
121
122    /// The multi-panel model, for per-panel `Renderer`/`Session` access when
123    /// reconciling `insertPanel`/`removePanel`.
124    pub workspace: Workspace,
125
126    /// State (the *active* panel's handles — for the shared status bar).
127    pub session: Session,
128    pub renderer: Renderer,
129    pub presentation: Presentation,
130}
131
132impl PartialEq for MainPanelProps {
133    fn eq(&self, rhs: &Self) -> bool {
134        self.session_props == rhs.session_props
135            && self.renderer_props == rhs.renderer_props
136            && self.presentation_props == rhs.presentation_props
137            && self.is_settings_open == rhs.is_settings_open
138            && self.update_count == rhs.update_count
139            && self.panel_ids == rhs.panel_ids
140            && self.panel_titles == rhs.panel_titles
141            && self.panel_themes == rhs.panel_themes
142            && self.panel_masters == rhs.panel_masters
143            && self.global_filters == rhs.global_filters
144            && self.workspace == rhs.workspace
145            && self.session == rhs.session
146            && self.renderer == rhs.renderer
147            && self.presentation == rhs.presentation
148    }
149}
150
151impl MainPanelProps {
152    fn is_title(&self) -> bool {
153        self.session_props.title.is_some()
154    }
155
156    pub(super) fn effective_panel_theme(&self, id: &str) -> Option<String> {
157        self.panel_themes
158            .iter()
159            .find(|(pid, _)| pid == id)
160            .and_then(|(_, theme)| theme.clone())
161            .or_else(|| self.presentation_props.available_themes.first().cloned())
162    }
163}
164
165pub struct MainPanel {
166    main_panel_ref: NodeRef,
167
168    /// Ref to the `<regular-layout>` element hosting the panel cells.
169    layout_ref: NodeRef,
170
171    /// Panel slots currently placed in `layout_ref`'s grid, reconciled against
172    /// `panel_ids` on each render so we `insertPanel`/`removePanel` exactly
173    /// once per add/remove.
174    inserted: Vec<String>,
175
176    /// `regular-layout-update` listener (close detection); kept alive here and
177    /// attached to the layout element once in `rendered`.
178    _layout_update_listener: Closure<dyn FnMut(web_sys::Event)>,
179
180    /// `regular-layout-select` listener (active-panel sync); kept alive here
181    /// and attached alongside the update listener.
182    _layout_select_listener: Closure<dyn FnMut(web_sys::Event)>,
183
184    /// `regular-layout-before-resize` listener (presize hook); kept alive here
185    /// and attached alongside the others.
186    _layout_before_resize_listener: Closure<dyn FnMut(web_sys::Event)>,
187
188    /// `contextmenu` listener on the viewer HOST element — one stable attach
189    /// point covering every surface (panels, status bar, sidebars),
190    /// independent of the layout reconcile; attached once in `rendered` and
191    /// REMOVED in `destroy`, since the host outlives this component's Yew
192    /// app. Imperative — not a Yew `oncontextmenu` — because the plugin body
193    /// is light-DOM attached by the renderer (its DOM parent is the host,
194    /// not the frame), so Yew's delegated handler never matches a
195    /// right-click there; a native listener sees every surface — shadow
196    /// chrome and slotted light DOM alike — on the composed path.
197    _contextmenu_listener: Closure<dyn FnMut(web_sys::Event)>,
198
199    /// The `<regular-layout>` ELEMENT the layout listeners are attached to.
200    /// Compared by identity in `reconcile` — if the element is ever a
201    /// different instance (it should never be: the render keys it into a
202    /// fully-keyed sibling list precisely so Yew reuses it), the listeners
203    /// are re-attached and `inserted` is reset so the fresh (empty) layout
204    /// is repopulated instead of silently orphaning every panel. A latched
205    /// `bool` here once turned an unkeyed-diff element replacement into
206    /// "Duplicate commits into an EMPTY tree with no `before-resize`
207    /// listener": the old element left the DOM with the committed tree and
208    /// every listener, and nothing ever noticed.
209    listener_target: Option<web_sys::HtmlElement>,
210
211    /// Ids gone from the model whose `removePanel` has been issued to the
212    /// layout and not yet committed.
213    pending_removals: HashSet<String>,
214
215    /// Ids observed in a committed layout tree, the only model-held panels a
216    /// later commit can report closed.
217    on_screen: HashSet<String>,
218
219    /// Per-panel `ResizeObserver`s, keyed by panel id, each observing that
220    /// panel's slotted plugin element and resizing only that panel's
221    /// `Renderer`. Bound in `BeforeResize` for the DRAGGED panel only (it is
222    /// excluded from the presize paths, and the `overlay` presize is disabled);
223    /// dropped on `LayoutUpdated` (the drop).
224    panel_resize_observers: HashMap<String, PanelResizeObserverHandle>,
225
226    /// Panels currently *hidden* behind an unselected index of a tab stack,
227    /// recomputed from the layout tree on every `regular-layout-update`.
228    /// Drives each tab's `visible` prop (`PanelTab` marks the front tab of
229    /// every stack, not just the active panel). Empty until the first layout
230    /// update — every panel starts visible.
231    hidden_tabs: HashSet<String>,
232
233    /// Open context menu as `(client x, client y, target panel id)`; outer
234    /// `None` when closed. A `None` *panel id* is the target-less menu (no
235    /// active panel to resolve to — "New" only). Rendered as a
236    /// cursor-anchored [`PanelMenu`](super::panel_menu::PanelMenu) overlay.
237    context_menu: Option<(f64, f64, Option<String>)>,
238
239    /// Id of the currently maximized panel (via `regular-layout.maximize`), or
240    /// `None`. Transient (regular-layout doesn't persist it); drives the
241    /// Maximize/Restore menu label. Cleared when the panel leaves the layout.
242    maximized: Option<String>,
243
244    /// Theme-name-keyed cache of backgrounds read off stamped plugin
245    /// elements, the mirror source for frames whose own plugin is unreadable
246    /// (see [`frame_theme`]). Cleared when the theme registry changes.
247    theme_backgrounds: HashMap<String, String>,
248
249    /// The inputs `stamp_frame_themes` last mirrored from; unchanged inputs
250    /// skip the pass (and its forced style recalcs) on unrelated re-renders.
251    /// `None` until the first *fully-resolved* pass — an unresolved frame
252    /// leaves it unlatched so the mirror retries each render.
253    stamped_frame_themes: Option<frame_theme::FrameThemeSnapshot>,
254
255    /// `Workspace::staged_changed` → [`MainPanelMsg::StagedChanged`] on THIS
256    /// component's scope (see the message doc for why not the root's).
257    _staged_sub: crate::utils::Subscription,
258
259    /// `Workspace::layout_staged` → [`MainPanelMsg::LayoutStaged`].
260    _layout_staged_sub: crate::utils::Subscription,
261}
262
263impl Component for MainPanel {
264    type Message = MainPanelMsg;
265    type Properties = MainPanelProps;
266
267    fn create(ctx: &Context<Self>) -> Self {
268        let cb = ctx.link().callback(|_: ()| MainPanelMsg::LayoutUpdated);
269        let listener = Closure::wrap(
270            Box::new(move |_: web_sys::Event| cb.emit(())) as Box<dyn FnMut(web_sys::Event)>
271        );
272
273        let select_cb = ctx.link().callback(MainPanelMsg::TabSelected);
274        let select_listener = Closure::wrap(Box::new(move |event: web_sys::Event| {
275            #[derive(serde::Deserialize)]
276            struct SelectDetail {
277                name: String,
278            }
279
280            if let Some(custom) = event.dyn_ref::<web_sys::CustomEvent>()
281                && let Ok(SelectDetail { name }) = custom.detail().into_serde_ext()
282            {
283                select_cb.emit(name);
284            }
285        }) as Box<dyn FnMut(web_sys::Event)>);
286
287        // `preventDefault()` synchronously suspends the layout's resize commit
288        // (the event is cancelable); the component then pre-sizes each panel
289        // and calls `resumeResize` to release it (see
290        // `MainPanelMsg::BeforeResize`).
291        let before_resize_cb = ctx.link().callback(MainPanelMsg::BeforeResize);
292        let before_resize_listener = Closure::wrap(Box::new(move |event: web_sys::Event| {
293            event.prevent_default();
294            before_resize_cb.emit(event);
295        }) as Box<dyn FnMut(web_sys::Event)>);
296
297        // Imperative `contextmenu` listener: a right-click anywhere in the
298        // element opens the panel menu — targeting the frame on the event's
299        // composed path, or `None` (resolved to the ACTIVE panel, else the
300        // stage menu, in `on_context_menu`, where props live) from any
301        // other surface. Two pass-throughs keep the NATIVE menu: editable
302        // targets (inputs, textareas, contenteditable — cut/copy/paste),
303        // and anything inside an open `PortalModal` (every modal host is a
304        // light-DOM viewer child in the modal slot — this menu included, so
305        // without the carve-out a right-click in any open dropdown would
306        // re-open the menu underneath it).
307        let contextmenu_cb = ctx
308            .link()
309            .callback(|(id, x, y)| MainPanelMsg::ContextMenu(id, x, y));
310        let viewer_elem = ctx.props().presentation.viewer_elem().clone();
311        let contextmenu_listener = Closure::wrap(Box::new(move |event: web_sys::Event| {
312            // Shift+right-click passes through to the native browser menu.
313            if event.unchecked_ref::<web_sys::MouseEvent>().shift_key() {
314                return;
315            }
316
317            let path = event.composed_path();
318            let is_editable = path.get(0).dyn_ref::<web_sys::HtmlElement>().is_some_and(
319                |el: &web_sys::HtmlElement| {
320                    el.tag_name().eq_ignore_ascii_case("input")
321                        || el.tag_name().eq_ignore_ascii_case("textarea")
322                        || el.is_content_editable()
323                },
324            );
325
326            if is_editable {
327                return;
328            }
329
330            let mut panel_id = None;
331            for i in 0..path.length() {
332                let node = path.get(i);
333                let Some(el) = node.dyn_ref::<web_sys::Element>() else {
334                    continue;
335                };
336
337                if el == viewer_elem.unchecked_ref::<web_sys::Element>() {
338                    break;
339                }
340
341                if el.slot() == crate::ui::MODAL_SLOT {
342                    return;
343                }
344
345                if el.tag_name().eq_ignore_ascii_case("regular-layout-frame")
346                    && let Some(name) = el.get_attribute("name")
347                {
348                    panel_id = Some(name);
349                    break;
350                }
351            }
352
353            event.prevent_default();
354            let mouse = event.unchecked_ref::<web_sys::MouseEvent>();
355            contextmenu_cb.emit((panel_id, mouse.client_x() as f64, mouse.client_y() as f64));
356        }) as Box<dyn FnMut(web_sys::Event)>);
357
358        let staged_sub = {
359            use crate::utils::AddListener;
360            let cb = ctx.link().callback(|_: ()| MainPanelMsg::StagedChanged);
361            ctx.props()
362                .workspace
363                .staged_changed()
364                .add_listener(move |()| cb.emit(()))
365        };
366
367        let layout_staged_sub = {
368            use crate::utils::AddListener;
369            let cb = ctx.link().callback(|_: ()| MainPanelMsg::LayoutStaged);
370            ctx.props()
371                .workspace
372                .layout_staged()
373                .add_listener(move |()| cb.emit(()))
374        };
375
376        Self {
377            main_panel_ref: NodeRef::default(),
378            layout_ref: NodeRef::default(),
379            inserted: Vec::new(),
380            pending_removals: HashSet::new(),
381            on_screen: HashSet::new(),
382            _layout_update_listener: listener,
383            _layout_select_listener: select_listener,
384            _layout_before_resize_listener: before_resize_listener,
385            _contextmenu_listener: contextmenu_listener,
386            listener_target: None,
387            panel_resize_observers: HashMap::new(),
388            hidden_tabs: HashSet::new(),
389            context_menu: None,
390            maximized: None,
391            theme_backgrounds: HashMap::new(),
392            stamped_frame_themes: None,
393            _staged_sub: staged_sub,
394            _layout_staged_sub: layout_staged_sub,
395        }
396    }
397
398    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
399        match msg {
400            MainPanelMsg::PointerEvent(event) => self.on_pointer_event(ctx, event),
401            MainPanelMsg::StagedChanged => true,
402            MainPanelMsg::LayoutStaged => true,
403            MainPanelMsg::LayoutUpdated => self.on_layout_updated(ctx),
404            MainPanelMsg::TabSelected(name) => self.on_tab_selected(ctx, name),
405            MainPanelMsg::ContextMenu(id, x, y) => self.on_context_menu(ctx, id, x, y),
406            MainPanelMsg::CloseContextMenu => self.on_close_context_menu(),
407            MainPanelMsg::Command(cmd) => self.on_command(ctx, cmd),
408            MainPanelMsg::BeforeResize(event) => self.on_before_resize(ctx, event),
409        }
410    }
411
412    fn changed(&mut self, ctx: &Context<Self>, old: &Self::Properties) -> bool {
413        self.close_unstaged_panels(ctx, old);
414        ctx.props() != old
415    }
416
417    fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
418        // The `contextmenu` listener attaches to the viewer HOST element —
419        // one stable attach point covering every surface (panels, status
420        // bar, sidebars), decoupled from the layout reconcile. Panel
421        // right-clicks reach it through their frames on the composed path;
422        // everything else resolves to the active panel (or the stage menu).
423        if first_render {
424            let _ = ctx
425                .props()
426                .presentation
427                .viewer_elem()
428                .add_event_listener_with_callback(
429                    "contextmenu",
430                    self._contextmenu_listener.as_ref().unchecked_ref(),
431                );
432        }
433
434        self.size_staging_wrappers(ctx);
435        self.reconcile(ctx);
436        self.stamp_frame_themes(ctx);
437    }
438
439    fn view(&self, ctx: &Context<Self>) -> Html {
440        self.render(ctx)
441    }
442
443    fn destroy(&mut self, ctx: &Context<Self>) {
444        // The host element outlives this component (`delete()` tears down
445        // the Yew app, not the custom element), so the listener must not
446        // dangle into a dropped `Closure`.
447        let _ = ctx
448            .props()
449            .presentation
450            .viewer_elem()
451            .remove_event_listener_with_callback(
452                "contextmenu",
453                self._contextmenu_listener.as_ref().unchecked_ref(),
454            );
455    }
456}
457
458impl MainPanel {
459    /// Report closed every panel gone from the model that the layout never
460    /// held, since it has no commit to wait for.
461    fn close_unstaged_panels(&self, ctx: &Context<Self>, old: &MainPanelProps) {
462        for id in &old.panel_ids {
463            if !ctx.props().panel_ids.contains(id)
464                && !self.inserted.iter().any(|n| n == id.as_str())
465            {
466                ctx.props().on_panel_closed.emit(id.as_str().to_owned());
467            }
468        }
469    }
470}