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, Element, ElementBox, ElementStateHandle, MouseRegion, RenderContext, View,
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 {
79 side: Side,
80 handle_size: f32,
81 child: ElementBox,
82 state: Rc<ResizeHandleState>,
83 _state_handle: ElementStateHandle<Rc<ResizeHandleState>>,
84}
85
86impl Resizable {
87 pub fn new<Tag: 'static, T: View>(
88 child: ElementBox,
89 element_id: usize,
90 side: Side,
91 handle_size: f32,
92 initial_size: f32,
93 cx: &mut RenderContext<T>,
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 Element for Resizable {
136 type LayoutState = ();
137 type PaintState = ();
138
139 fn layout(
140 &mut self,
141 constraint: crate::SizeConstraint,
142 cx: &mut crate::LayoutContext,
143 ) -> (Vector2F, Self::LayoutState) {
144 (self.child.layout(constraint, cx), ())
145 }
146
147 fn paint(
148 &mut self,
149 bounds: pathfinder_geometry::rect::RectF,
150 visible_bounds: pathfinder_geometry::rect::RectF,
151 _child_size: &mut Self::LayoutState,
152 cx: &mut crate::PaintContext,
153 ) -> Self::PaintState {
154 cx.scene.push_stacking_context(None, None);
155
156 let handle_region = self.side.of_rect(bounds, self.handle_size);
157
158 enum ResizeHandle {}
159 cx.scene.push_mouse_region(
160 MouseRegion::new::<ResizeHandle>(
161 cx.current_view_id(),
162 self.side as usize,
163 handle_region,
164 )
165 .on_down(MouseButton::Left, |_, _| {}) // This prevents the mouse down event from being propagated elsewhere
166 .on_drag(MouseButton::Left, {
167 let state = self.state.clone();
168 let side = self.side;
169 move |e, cx| {
170 let prev_width = state.actual_dimension.get();
171 state
172 .custom_dimension
173 .set(0f32.max(prev_width + side.compute_delta(e)).round());
174 cx.notify();
175 }
176 }),
177 );
178
179 cx.scene.push_cursor_region(crate::CursorRegion {
180 bounds: handle_region,
181 style: match self.side.axis() {
182 Axis::Horizontal => CursorStyle::ResizeLeftRight,
183 Axis::Vertical => CursorStyle::ResizeUpDown,
184 },
185 });
186
187 cx.scene.pop_stacking_context();
188
189 self.child.paint(bounds.origin(), visible_bounds, 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 cx: &crate::MeasurementContext,
200 ) -> Option<pathfinder_geometry::rect::RectF> {
201 self.child.rect_for_text_range(range_utf16, cx)
202 }
203
204 fn debug(
205 &self,
206 _bounds: pathfinder_geometry::rect::RectF,
207 _layout: &Self::LayoutState,
208 _paint: &Self::PaintState,
209 cx: &crate::DebugContext,
210 ) -> serde_json::Value {
211 json!({
212 "child": self.child.debug(cx),
213 })
214 }
215}