resizable.rs

  1use std::{cell::Cell, rc::Rc};
  2
  3use pathfinder_geometry::vector::{vec2f, Vector2F};
  4use serde_json::json;
  5
  6use crate::{
  7    geometry::rect::RectF,
  8    platform::{CursorStyle, MouseButton},
  9    scene::MouseDrag,
 10    Axis, Drawable, Element, ElementStateHandle, MouseRegion, SceneBuilder, View, ViewContext,
 11};
 12
 13use super::{ConstrainedBox, Hook};
 14
 15#[derive(Copy, Clone, Debug)]
 16pub enum Side {
 17    Top,
 18    Bottom,
 19    Left,
 20    Right,
 21}
 22
 23impl Side {
 24    fn axis(&self) -> Axis {
 25        match self {
 26            Side::Left | Side::Right => Axis::Horizontal,
 27            Side::Top | Side::Bottom => Axis::Vertical,
 28        }
 29    }
 30
 31    /// 'before' is in reference to the standard english document ordering of left-to-right
 32    /// then top-to-bottom
 33    fn before_content(self) -> bool {
 34        match self {
 35            Side::Left | Side::Top => true,
 36            Side::Right | Side::Bottom => false,
 37        }
 38    }
 39
 40    fn relevant_component(&self, vector: Vector2F) -> f32 {
 41        match self.axis() {
 42            Axis::Horizontal => vector.x(),
 43            Axis::Vertical => vector.y(),
 44        }
 45    }
 46
 47    fn compute_delta(&self, e: MouseDrag) -> f32 {
 48        if self.before_content() {
 49            self.relevant_component(e.prev_mouse_position) - self.relevant_component(e.position)
 50        } else {
 51            self.relevant_component(e.position) - self.relevant_component(e.prev_mouse_position)
 52        }
 53    }
 54
 55    fn of_rect(&self, bounds: RectF, handle_size: f32) -> RectF {
 56        match self {
 57            Side::Top => RectF::new(bounds.origin(), vec2f(bounds.width(), handle_size)),
 58            Side::Left => RectF::new(bounds.origin(), vec2f(handle_size, bounds.height())),
 59            Side::Bottom => {
 60                let mut origin = bounds.lower_left();
 61                origin.set_y(origin.y() - handle_size);
 62                RectF::new(origin, vec2f(bounds.width(), handle_size))
 63            }
 64            Side::Right => {
 65                let mut origin = bounds.upper_right();
 66                origin.set_x(origin.x() - handle_size);
 67                RectF::new(origin, vec2f(handle_size, bounds.height()))
 68            }
 69        }
 70    }
 71}
 72
 73struct ResizeHandleState {
 74    actual_dimension: Cell<f32>,
 75    custom_dimension: Cell<f32>,
 76}
 77
 78pub struct Resizable<V: View> {
 79    side: Side,
 80    handle_size: f32,
 81    child: Element<V>,
 82    state: Rc<ResizeHandleState>,
 83    _state_handle: ElementStateHandle<Rc<ResizeHandleState>>,
 84}
 85
 86impl<V: View> Resizable<V> {
 87    pub fn new<Tag: 'static, T: View>(
 88        child: Element<V>,
 89        element_id: usize,
 90        side: Side,
 91        handle_size: f32,
 92        initial_size: f32,
 93        cx: &mut ViewContext<V>,
 94    ) -> Self {
 95        let state_handle = cx.element_state::<Tag, Rc<ResizeHandleState>>(
 96            element_id,
 97            Rc::new(ResizeHandleState {
 98                actual_dimension: Cell::new(initial_size),
 99                custom_dimension: Cell::new(initial_size),
100            }),
101        );
102
103        let state = state_handle.read(cx).clone();
104
105        let child = Hook::new({
106            let constrained = ConstrainedBox::new(child);
107            match side.axis() {
108                Axis::Horizontal => constrained.with_max_width(state.custom_dimension.get()),
109                Axis::Vertical => constrained.with_max_height(state.custom_dimension.get()),
110            }
111            .boxed()
112        })
113        .on_after_layout({
114            let state = state.clone();
115            move |size, _| {
116                state.actual_dimension.set(side.relevant_component(size));
117            }
118        })
119        .boxed();
120
121        Self {
122            side,
123            child,
124            handle_size,
125            state,
126            _state_handle: state_handle,
127        }
128    }
129
130    pub fn current_size(&self) -> f32 {
131        self.state.actual_dimension.get()
132    }
133}
134
135impl<V: View> Drawable<V> for Resizable<V> {
136    type LayoutState = ();
137    type PaintState = ();
138
139    fn layout(
140        &mut self,
141        constraint: crate::SizeConstraint,
142        view: &mut V,
143        cx: &mut ViewContext<V>,
144    ) -> (Vector2F, Self::LayoutState) {
145        (self.child.layout(constraint, view, cx), ())
146    }
147
148    fn paint(
149        &mut self,
150        scene: &mut SceneBuilder,
151        bounds: pathfinder_geometry::rect::RectF,
152        visible_bounds: pathfinder_geometry::rect::RectF,
153        _child_size: &mut Self::LayoutState,
154        view: &mut V,
155        cx: &mut ViewContext<V>,
156    ) -> Self::PaintState {
157        scene.push_stacking_context(None, None);
158
159        let handle_region = self.side.of_rect(bounds, self.handle_size);
160
161        enum ResizeHandle {}
162        scene.push_mouse_region(
163            MouseRegion::new::<ResizeHandle>(cx.view_id(), self.side as usize, handle_region)
164                .on_down(MouseButton::Left, |_, _: &mut V, _| {}) // This prevents the mouse down event from being propagated elsewhere
165                .on_drag(MouseButton::Left, {
166                    let state = self.state.clone();
167                    let side = self.side;
168                    move |e, _: &mut V, cx| {
169                        let prev_width = state.actual_dimension.get();
170                        state
171                            .custom_dimension
172                            .set(0f32.max(prev_width + side.compute_delta(e)).round());
173                        cx.notify();
174                    }
175                }),
176        );
177
178        scene.push_cursor_region(crate::CursorRegion {
179            bounds: handle_region,
180            style: match self.side.axis() {
181                Axis::Horizontal => CursorStyle::ResizeLeftRight,
182                Axis::Vertical => CursorStyle::ResizeUpDown,
183            },
184        });
185
186        scene.pop_stacking_context();
187
188        self.child
189            .paint(scene, bounds.origin(), visible_bounds, view, cx);
190    }
191
192    fn rect_for_text_range(
193        &self,
194        range_utf16: std::ops::Range<usize>,
195        _bounds: pathfinder_geometry::rect::RectF,
196        _visible_bounds: pathfinder_geometry::rect::RectF,
197        _layout: &Self::LayoutState,
198        _paint: &Self::PaintState,
199        view: &V,
200        cx: &ViewContext<V>,
201    ) -> Option<pathfinder_geometry::rect::RectF> {
202        self.child.rect_for_text_range(range_utf16, view, cx)
203    }
204
205    fn debug(
206        &self,
207        _bounds: pathfinder_geometry::rect::RectF,
208        _layout: &Self::LayoutState,
209        _paint: &Self::PaintState,
210        view: &V,
211        cx: &ViewContext<V>,
212    ) -> serde_json::Value {
213        json!({
214            "child": self.child.debug(view, cx),
215        })
216    }
217}