1use blit::{
2 Axis, LayoutCx, Scalar, Scope, Size, Ui,
3 state::{NoLayout, Open},
4};
5
6use crate::{Context, interact};
7
8blit::builder! {
9 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
10 pub struct ResizeState<T> {
11 new(),
12 #[into]
13 size: Option<Size<T>> = None,
14 }
15}
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18pub enum ResizeEdge {
19 Right,
20 Bottom,
21 Corner,
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub struct ResizeGrip {
27 pub edge: ResizeEdge,
28 pub hovered: bool,
29 pub active: bool,
31}
32
33blit::builder! {
34 pub struct Resize<'a, T: Scalar> {
35 new(state: &'a mut ResizeState<T>),
36 grip_size: Size<T> = Size::uniform(T::from_f32(1.0)),
37 }
38}
39
40impl<T: Scalar> Resize<'_, T> {
41 pub fn begin<'a, C, S>(self, mut ui: Ui<'a, C, S>) -> ResizeContent<'a, C>
42 where
43 C: Context<Scalar = T>,
44 S: NoLayout,
45 {
46 let state = self.state;
47 let previous = ui.area();
48 let scope = ui.scope();
49 let mut grip_input = |key: u8, edge| {
50 let id = ui.resolve(scope, key);
51 let mut part = ui.node(id);
52 let (press, drag) = (part.interact(interact::press()), part.interact(interact::drag()));
53 let grip = ResizeGrip {
54 edge,
55 hovered: press.hovered,
56 active: press.active || drag.active,
57 };
58 (grip, drag.delta)
59 };
60 let (right, right_delta) = grip_input(1, ResizeEdge::Right);
61 let (bottom, bottom_delta) = grip_input(2, ResizeEdge::Bottom);
62 let (corner, corner_delta) = grip_input(3, ResizeEdge::Corner);
63 let delta = Size::new(right_delta.x + corner_delta.x, bottom_delta.y + corner_delta.y);
64 if delta != Size::ZERO {
65 let base = previous.map(|area| area.size()).or(state.size).unwrap_or(Size::ZERO);
66 let next = base + delta;
67 state.size = Some(Size::new(next.width.max(T::ZERO), next.height.max(T::ZERO)));
68 }
69 let shell = ui.layout(Layout {
70 size: state.size,
71 grip_size: self.grip_size,
72 });
73 ResizeContent(ResizeGrips {
74 shell,
75 scope,
76 grips: [right, bottom, corner],
77 })
78 }
79}
80
81#[must_use]
82pub struct ResizeContent<'a, C: Context>(ResizeGrips<'a, C>);
83
84impl<'a, C: Context> ResizeContent<'a, C> {
85 pub fn content(mut self, content: impl FnOnce(Ui<'_, C>)) -> ResizeGrips<'a, C> {
86 self.0.shell.child().item(Item::Content).build(content);
87 self.0
88 }
89}
90
91#[must_use]
92pub struct ResizeGrips<'a, C: Context> {
93 shell: Ui<'a, C, Open<Layout<C::Scalar>>>,
94 scope: Scope,
95 grips: [ResizeGrip; 3],
96}
97
98impl<C: Context> ResizeGrips<'_, C> {
99 pub fn grip(mut self, mut grip: impl FnMut(Ui<'_, C>, ResizeGrip)) {
100 for (item, grip_id, handle) in [
101 (Item::Right, 1, self.grips[0]),
102 (Item::Bottom, 2, self.grips[1]),
103 (Item::Corner, 3, self.grips[2]),
104 ] {
105 self.shell
106 .child_in(self.scope, grip_id)
107 .item(item)
108 .build(|ui: Ui<'_, C>| grip(ui, handle));
109 }
110 }
111}
112
113#[derive(Clone, Copy, Default)]
114enum Item {
115 #[default]
116 Content,
117 Right,
118 Bottom,
119 Corner,
120}
121
122#[derive(Clone, Copy)]
123struct Layout<T> {
124 size: Option<Size<T>>,
125 grip_size: Size<T>,
126}
127
128impl<C: Context<Scalar = T>, T: Scalar> blit::Layout<C> for Layout<T> {
129 type Item = Item;
130
131 fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> blit::Fit<T> {
132 if let Some(size) = self.size {
133 return blit::Fit::exact(axis.extent(size).max(T::ZERO));
134 }
135 cx.children()
136 .find(|&child| matches!(*cx.item(child), Item::Content))
137 .map_or(blit::Fit::ZERO, |child| cx.fit(child, axis))
138 }
139
140 fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, extent: T) {
141 let grip = axis.extent(self.grip_size).max(T::ZERO).min(extent);
142 for child in cx.children() {
143 let (assigned, offset, z_index) = match (*cx.item(child), axis) {
144 (Item::Content, _) => (extent, T::ZERO, 0),
145 (Item::Right, Axis::Horizontal) | (Item::Bottom, Axis::Vertical) => (grip, extent - grip, 1),
146 (Item::Right, Axis::Vertical) | (Item::Bottom, Axis::Horizontal) => (extent, T::ZERO, 1),
147 (Item::Corner, _) => (grip, extent - grip, 2),
148 };
149 cx.set_extent(child, assigned);
150 cx.set_offset(child, offset);
151 cx.set_child_z_index(child, z_index);
152 }
153 }
154}