perspective-viewer 5.2.0

A data visualization and analytics component, especially well-suited for large and/or streaming datasets.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

//! Settings-sidebar handlers: the open/close toggle (with its presize
//! choreography), the deferred divider's latest-wins presize pump
//! (`PRESIZE_EVERYWHERE_PLAN.md` P1/P2), and the column-settings drawer. The
//! presize sweeps themselves live in [`crate::tasks`] (`presize_panels`).

use futures::channel::oneshot::{Sender, channel};
use perspective_js::utils::*;
use wasm_bindgen::prelude::*;
use yew::prelude::*;

use super::PerspectiveViewer;
use super::msg::PerspectiveViewerMsg::*;
use crate::components::font_loader::FontLoaderStatus;
use crate::components::settings_panel::SelectedTab;
use crate::config::*;
use crate::presentation::{ColumnLocator, ColumnSettingsTab};
use crate::tasks::*;

/// The settings sidebar's geometry state, folded into one field on
/// [`PerspectiveViewer`]: the pane/drawer width overrides, the divider
/// presize pump, and the open-state deltas cache.
#[derive(Default)]
pub(super) struct SettingsGeometry {
    /// User-dragged settings-pane width (the deferred `SplitPanel`'s
    /// controlled `size`); `None` until first dragged / after a reset.
    pub pane_width_override: Option<i32>,

    /// The selected settings-panel tab.
    pub selected_tab: SelectedTab,

    /// High-water-mark auto width reported by the settings panel.
    pub auto_width: f64,

    /// Latest-wins presize pump for the (deferred) settings divider: the
    /// newest proposed pane width not yet presized, and whether a pump
    /// iteration is in flight. See `SettingsDividerMove`.
    pub divider_target: Option<i32>,
    pub divider_pumping: bool,

    /// Open-state geometry deltas `(layout_area.w − mpc.w, main_column.h −
    /// mpc.h)`, cached at settings *close* time — used to presize panels to
    /// their shrunk boxes BEFORE the pane mounts on the next *open* (P2).
    /// `None` until the first close (the first-ever open is reactive).
    pub open_deltas: Option<(f64, f64)>,

    /// User-dragged column-settings drawer width override.
    pub column_settings_width_override: Option<i32>,

    /// High-water-mark auto width reported by the column-settings drawer -
    /// the trap-door shared across its Style/Attributes/Window tabs, lifted
    /// here (like `auto_width`) so it survives drawer re-opens and clears
    /// on divider reset.
    pub column_settings_auto_width: f64,

    /// Whether the column-settings drawer is PINNED - laid out as a static
    /// flex sibling between the main panel and the settings panel - rather
    /// than FLOATING over the main panel (the default absolute overlay).
    /// Session UI state, not part of the saved config.
    pub column_settings_pinned: bool,
}

impl PerspectiveViewer {
    pub(super) fn on_toggle_settings_init(
        &mut self,
        ctx: &Context<Self>,
        update: Option<SettingsUpdate>,
        resolve: Option<Sender<ApiResult<JsValue>>>,
    ) -> bool {
        match (update, resolve) {
            (Some(SettingsUpdate::Missing), None) => false,
            (Some(SettingsUpdate::Missing), Some(resolve)) => {
                resolve.send(Ok(JsValue::UNDEFINED)).unwrap();
                false
            },
            (Some(SettingsUpdate::SetDefault), resolve) => {
                self.init_toggle_settings_task(ctx, Some(false), resolve);
                false
            },
            (Some(SettingsUpdate::Update(force)), resolve) => {
                self.init_toggle_settings_task(ctx, Some(force), resolve);
                false
            },
            (None, resolve) => {
                self.init_toggle_settings_task(ctx, None, resolve);
                false
            },
        }
    }

    pub(super) fn on_toggle_settings_complete(
        &mut self,
        ctx: &Context<Self>,
        update: SettingsUpdate,
        resolve: Sender<()>,
    ) -> bool {
        match update {
            SettingsUpdate::SetDefault if self.settings_open => {
                ctx.props().presentation.set_open_column_settings(None);
                self.settings_open = false;
                self.on_rendered.push(resolve);
                true
            },
            SettingsUpdate::Update(force) if force != self.settings_open => {
                ctx.props().presentation.set_open_column_settings(None);
                self.settings_open = force;
                self.on_rendered.push(resolve);
                true
            },
            _ if matches!(self.fonts.get_status(), FontLoaderStatus::Finished) => {
                if let Err(e) = resolve.send(()) {
                    tracing::error!("toggle settings failed {:?}", e);
                }

                false
            },
            _ => {
                ctx.props().presentation.set_open_column_settings(None);
                self.on_rendered.push(resolve);
                true
            },
        }
    }

    /// Toggle the settings, or force the settings panel either open (true) or
    /// closed (false) explicitly.  In order to reduce apparent
    /// screen-shear, `toggle_settings()` uses a somewhat complex render
    /// order:  it first resize the plugin's `<div>` without moving it,
    /// using `overflow: hidden` to hide the extra draw area;  then,
    /// after the _async_ drawing of the plugin is complete, it will send a
    /// message to complete the toggle action and re-render the element with
    /// the settings removed.
    ///
    /// # Arguments
    /// * `force` - Whether to explicitly set the settings panel state to
    ///   Open/Close (`Some(true)`/`Some(false)`), or to just toggle the current
    ///   state (`None`).
    fn init_toggle_settings_task(
        &mut self,
        ctx: &Context<Self>,
        force: Option<bool>,
        sender: Option<Sender<ApiResult<JsValue>>>,
    ) {
        let is_open = ctx.props().presentation.is_settings_open();
        ctx.props().presentation.set_settings_before_open(!is_open);
        match force {
            Some(force) if is_open == force => {
                if let Some(sender) = sender {
                    sender.send(Ok(JsValue::UNDEFINED)).unwrap();
                }
            },
            Some(_) | None => {
                let force = !is_open;
                let callback = ctx.link().callback(move |resolve| {
                    let update = SettingsUpdate::Update(force);
                    ToggleSettingsComplete(update, resolve)
                });

                // P2: at CLOSE time the open-state geometry is measurable —
                // cache the deltas the next OPEN needs to presize with.
                if is_open {
                    self.settings_geometry.open_deltas =
                        measure_settings_open_deltas(&ctx.props().elem);
                }

                let open_deltas = self.settings_geometry.open_deltas;
                let workspace = ctx.props().workspace.clone();
                let presentation = ctx.props().presentation.clone();
                let elem = ctx.props().elem.clone();

                ApiFuture::spawn(async move {
                    // Resize every visible plugin (not just the active one) to its
                    // new cell as the settings pane toggles. The pane is the outer
                    // SplitPanel, which emits no `before-resize`, so this is driven
                    // explicitly here. CLOSING grows the cells, so pre-size each
                    // plugin to its grown box BEFORE collapsing the pane; OPENING
                    // shrinks them, so pre-size to the shrunk box (from the deltas
                    // cached at the last close; the first-ever open has none and
                    // stays reactive) BEFORE the pane mounts — either way, one
                    // clean resize with the reactive pass after as the exactness
                    // finalizer.
                    let result: ApiResult<JsValue> = async {
                        if is_open {
                            let presents = presize_visible_panels_grown(&workspace, &elem).await;
                            let (notify, rendered) = channel::<()>();
                            callback.emit(notify);
                            presentation.set_settings_open(false);
                            rendered.await?;
                            // `notify` fires in `rendered()` (same task as the
                            // Yew DOM patch) and this future resumes as one of
                            // its microtasks — so the staged reveal below and
                            // the pane's geometry change reach the screen in a
                            // single paint.
                            presents.reveal();
                            // I6: the exactness-finalizer resize is part of
                            // what this toggle caused — await it here rather
                            // than leaving it to the ResizeObserver (whose
                            // continuous pass remains as the reactive
                            // backstop).
                            resize_visible_panels(&workspace).await;
                        } else {
                            let presents = if let Some((delta_w, delta_h)) = open_deltas {
                                presize_visible_panels_open(&workspace, &elem, delta_w, delta_h)
                                    .await
                            } else {
                                StagedPresents::default()
                            };

                            let (notify, rendered) = channel::<()>();
                            callback.emit(notify);
                            presentation.set_settings_open(true);
                            rendered.await?;
                            presents.reveal();
                            resize_visible_panels(&workspace).await;
                        }
                        Ok(JsValue::UNDEFINED)
                    }
                    .await;

                    if let Some(sender) = sender {
                        let msg = result.ignore_view_delete();
                        sender
                            .send(msg.map(|x| x.unwrap_or(JsValue::UNDEFINED)))
                            .into_apierror()?;
                    };

                    Ok(JsValue::undefined())
                });
            },
        };
    }

    pub(super) fn on_settings_panel_size_update(&mut self, x: Option<i32>) -> bool {
        match x {
            Some(x) => {
                self.settings_geometry.pane_width_override = Some(x);
                false
            },
            None => {
                self.settings_geometry.pane_width_override = None;
                self.settings_geometry.auto_width = 0.0;
                self.on_settings_panel_dimensions_reset.emit(());
                true
            },
        }
    }

    pub(super) fn on_settings_divider_move(
        &mut self,
        ctx: &Context<Self>,
        pane_width: i32,
    ) -> bool {
        // Latest-wins: overwrite the pending target; start the pump if
        // idle. Intermediate targets that arrive while a presize is in
        // flight are dropped (mirrors `PresizeQueue`'s single queued
        // slot) — the pane tracks the pointer at content-render rate.
        self.settings_geometry.divider_target = Some(pane_width);
        if !self.settings_geometry.divider_pumping {
            self.settings_geometry.divider_pumping = true;
            ctx.link().send_message(SettingsDividerPump);
        }

        false
    }

    pub(super) fn on_settings_divider_pump(&mut self, ctx: &Context<Self>) -> bool {
        if let Some(pane_width) = self.settings_geometry.divider_target.take() {
            let workspace = ctx.props().workspace.clone();
            let elem = ctx.props().elem.clone();
            let link = ctx.link().clone();
            ApiFuture::spawn(async move {
                let presents =
                    presize_visible_panels_pane_width(&workspace, &elem, pane_width as f64).await;
                link.send_message(SettingsDividerCommit(pane_width));
                // Same task as the commit's re-render (whether Yew drained it
                // synchronously inside `send_message` or deferred it to a
                // microtask): the staged reveal and the pane-width geometry
                // land in one paint.
                presents.reveal();
                Ok(())
            });
        } else {
            self.settings_geometry.divider_pumping = false;
        }

        false
    }

    pub(super) fn on_settings_divider_commit(
        &mut self,
        ctx: &Context<Self>,
        pane_width: i32,
    ) -> bool {
        // Every visible panel has rendered at (approximately) its
        // target box — NOW move the geometry: the re-render below
        // applies this width to the deferred `SplitPanel`'s controlled
        // `size`, in the same task as the presizes' inline clears.
        self.settings_geometry.pane_width_override = Some(pane_width);
        ctx.link().send_message(SettingsDividerPump);
        true
    }

    pub(super) fn on_settings_divider_finish(&mut self, ctx: &Context<Self>) -> bool {
        let workspace = ctx.props().workspace.clone();
        ApiFuture::spawn(async move {
            resize_visible_panels(&workspace).await;
            Ok(())
        });

        false
    }

    pub(super) fn on_settings_panel_tab_changed(&mut self, tab: SelectedTab) -> bool {
        let changed = tab != self.settings_geometry.selected_tab;
        self.settings_geometry.selected_tab = tab;
        changed
    }

    pub(super) fn on_settings_panel_auto_width(&mut self, w: f64) -> bool {
        if w > self.settings_geometry.auto_width {
            self.settings_geometry.auto_width = w;
            true
        } else {
            false
        }
    }

    pub(super) fn on_open_column_settings(
        &mut self,
        ctx: &Context<Self>,
        locator: Option<ColumnLocator>,
        sender: Option<Sender<()>>,
        toggle: bool,
    ) -> bool {
        let mut open_column_settings = ctx.props().presentation.get_open_column_settings();
        if locator == open_column_settings.locator {
            if toggle {
                ctx.props().presentation.set_open_column_settings(None);
            }
        } else {
            open_column_settings.locator.clone_from(&locator);
            open_column_settings.tab = if matches!(locator, Some(ColumnLocator::NewExpression)) {
                Some(ColumnSettingsTab::Attributes)
            } else {
                locator.as_ref().and_then(|x| {
                    x.name().map(|x| {
                        if self.session_props.is_column_active(x) {
                            ColumnSettingsTab::Style
                        } else {
                            ColumnSettingsTab::Attributes
                        }
                    })
                })
            };

            ctx.props()
                .presentation
                .set_open_column_settings(Some(open_column_settings));

            if locator.is_some() {
                self.settings_geometry.selected_tab = SelectedTab::Query;
            }
        }

        if let Some(sender) = sender {
            // I6: resolve on the render commit that applies this change (the
            // shared `on_rendered` queue, drained in `rendered()` once fonts
            // settle), not at message-handling time.
            self.on_rendered.push(sender);
        }

        true
    }

    pub(super) fn on_column_settings_panel_size_update(&mut self, x: Option<i32>) -> bool {
        self.settings_geometry.column_settings_width_override = x;
        if x.is_none() {
            self.settings_geometry.column_settings_auto_width = 0.0;
            true
        } else {
            false
        }
    }

    pub(super) fn on_column_settings_panel_auto_width(&mut self, w: f64) -> bool {
        if w > self.settings_geometry.column_settings_auto_width {
            self.settings_geometry.column_settings_auto_width = w;
            true
        } else {
            false
        }
    }

    pub(super) fn on_toggle_column_settings_pin(&mut self) -> bool {
        self.settings_geometry.column_settings_pinned =
            !self.settings_geometry.column_settings_pinned;
        true
    }

    pub(super) fn on_column_settings_tab_changed(
        &mut self,
        ctx: &Context<Self>,
        tab: ColumnSettingsTab,
    ) -> bool {
        let mut open_column_settings = ctx.props().presentation.get_open_column_settings();
        open_column_settings.tab.clone_from(&Some(tab));
        ctx.props()
            .presentation
            .set_open_column_settings(Some(open_column_settings));
        true
    }

    /// Toggling the debug tab re-renders the settings panel only — the
    /// `DebugPanel` populates itself (`get_viewer_config` on mount +
    /// change subscriptions), so no plugin dispatch is owed. The old
    /// `just_render` here relied on the pre-amendment unconditional
    /// `Unchanged → update` arm and repainted the plugin as a side effect
    /// (`PLUGIN_DRAW_INVARIANT_PLAN.md` amendment, migration 2).
    pub(super) fn on_toggle_debug(&mut self, _ctx: &Context<Self>) -> bool {
        self.debug_open = !self.debug_open;
        true
    }
}