pane_resizer/
context.rs

1use std::collections::BTreeMap;
2
3use leptos::{html::Div, prelude::*};
4
5use crate::Direction;
6
7#[derive(Copy, Clone)]
8pub struct PaneContext {
9    pub index: usize,
10    pub size: RwSignal<f64>,
11    pub pane_ref: NodeRef<Div>,
12}
13
14impl Default for PaneContext {
15    fn default() -> Self {
16        Self {
17            index: 0,
18            size: RwSignal::new(50.0),
19            pane_ref: NodeRef::new(),
20        }
21    }
22}
23
24#[derive(Copy, Clone)]
25pub struct PaneResizerContext {
26    pub direction: Direction,
27    pub group_ref: NodeRef<Div>,
28    pub resizing: RwSignal<bool>,
29    pub resizer_ref: NodeRef<Div>,
30    pub panes: RwSignal<BTreeMap<usize, PaneContext>>,
31}
32
33impl Default for PaneResizerContext {
34    fn default() -> Self {
35        Self {
36            direction: Direction::Horizontal,
37            group_ref: NodeRef::new(),
38            resizing: RwSignal::new(false),
39            resizer_ref: NodeRef::new(),
40            panes: RwSignal::new(BTreeMap::new()),
41        }
42    }
43}
44
45impl PaneResizerContext {
46    pub fn next_index(&self) -> usize {
47        self.panes.get_untracked().len()
48    }
49
50    pub fn upsert_pane(&self, index: usize, pane: PaneContext) {
51        self.panes.update(|panes| {
52            *panes.entry(index).or_insert(pane) = pane;
53        });
54    }
55
56    pub fn remove_pane(&self, index: usize) {
57        self.panes.update(|panes| {
58            panes.remove(&index);
59        });
60    }
61}