cushy/widgets/
disclose.rs

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
//! A widget that hides/shows associated content.

use std::time::Duration;

use figures::units::{Lp, Px, UPx};
use figures::{Angle, IntoSigned, Point, Rect, Round, ScreenScale, Size, Zero};
use kludgine::app::winit::window::CursorIcon;
use kludgine::shapes::{PathBuilder, StrokeOptions};
use kludgine::{Color, DrawableExt};

use super::button::{ButtonActiveBackground, ButtonBackground, ButtonHoverBackground};
use crate::animation::{AnimationHandle, AnimationTarget, Spawn};
use crate::context::{EventContext, LayoutContext};
use crate::styles::components::{HighlightColor, IntrinsicPadding, LineHeight, OutlineColor};
use crate::styles::Dimension;
use crate::value::{Destination, Dynamic, IntoDynamic, IntoValue, Source, Value};
use crate::widget::{
    EventHandling, MakeWidget, MakeWidgetWithTag, Widget, WidgetInstance, WidgetRef, WidgetTag,
    HANDLED, IGNORED,
};
use crate::window::DeviceId;
use crate::ConstraintLimit;

/// A widget that hides and shows another widget.
pub struct Disclose {
    contents: WidgetInstance,
    label: Option<WidgetInstance>,
    collapsed: Value<bool>,
}

impl Disclose {
    /// Returns a new widget that allows hiding and showing `contents`.
    #[must_use]
    pub fn new(contents: impl MakeWidget) -> Self {
        Self {
            contents: contents.make_widget(),
            label: None,
            collapsed: Value::Constant(true),
        }
    }

    /// Sets `label` as a clickable label for this widget.
    #[must_use]
    pub fn labelled_by(mut self, label: impl MakeWidget) -> Self {
        self.label = Some(label.make_widget());
        self
    }

    /// Sets this widget's collapsed value.
    ///
    /// If a `Value::Constant` is provided, it is used as the initial collapse
    /// state. If a `Value::Dynamic` is provided, it will be updated when the
    /// contents are shown and hidden.
    #[must_use]
    pub fn collapsed(mut self, collapsed: impl IntoValue<bool>) -> Self {
        self.collapsed = collapsed.into_value();
        self
    }
}

impl MakeWidgetWithTag for Disclose {
    fn make_with_tag(self, tag: WidgetTag) -> WidgetInstance {
        let collapsed = self.collapsed.into_dynamic();

        DiscloseIndicator::new(collapsed.clone(), self.label, self.contents).make_with_tag(tag)
    }
}

#[derive(Debug)]
struct DiscloseIndicator {
    label: Option<WidgetRef>,
    contents: WidgetRef,
    collapsed: Dynamic<bool>,
    hovering_indicator: bool,
    target_colors: Option<(Color, Color)>,
    color_animation: AnimationHandle,
    color: Dynamic<Color>,
    stroke_color: Dynamic<Color>,
    angle: Dynamic<Angle>,
    mouse_buttons_pressed: usize,
}

fn collapse_angle(collapsed: bool) -> Angle {
    if collapsed {
        Angle::degrees(0)
    } else {
        Angle::degrees(90)
    }
}

impl DiscloseIndicator {
    fn new(
        collapsed: Dynamic<bool>,
        label: Option<WidgetInstance>,
        contents: WidgetInstance,
    ) -> Self {
        let angle = Dynamic::new(collapse_angle(collapsed.get()));

        let mut _angle_animation = AnimationHandle::default();
        angle.set_source({
            let angle = angle.clone();
            collapsed.for_each(move |collapsed| {
                _angle_animation = angle
                    .transition_to(collapse_angle(*collapsed))
                    .over(Duration::from_millis(125))
                    .spawn();
            })
        });

        Self {
            contents: WidgetRef::new(contents.collapse_vertically(collapsed.clone())),
            collapsed,
            hovering_indicator: false,
            label: label.map(WidgetRef::new),
            target_colors: None,
            color: Dynamic::new(Color::CLEAR_WHITE),
            stroke_color: Dynamic::new(Color::CLEAR_WHITE),
            color_animation: AnimationHandle::default(),
            angle,
            mouse_buttons_pressed: 0,
        }
    }

    fn effective_colors(
        &mut self,
        context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>,
    ) -> (Color, Color) {
        let current_color = if context.active() {
            context.get(&ButtonActiveBackground)
        } else if self.hovering_indicator {
            context.get(&ButtonHoverBackground)
        } else {
            context.get(&ButtonBackground)
        };
        let stroke_color = if self.hovering_indicator {
            context.get(&OutlineColor)
        } else if context.focused(true) {
            context.get(&HighlightColor)
        } else {
            context.get(&OutlineColor).with_alpha(0)
        };
        let target_colors = (current_color, stroke_color);
        if self.target_colors.is_none() {
            self.target_colors = Some(target_colors);
            self.color.set(current_color);
            self.stroke_color.set(stroke_color);
        } else if self.target_colors != Some(target_colors) {
            self.target_colors = Some(target_colors);
            self.color_animation = (
                self.color.transition_to(current_color),
                self.stroke_color.transition_to(stroke_color),
            )
                .over(Duration::from_millis(125))
                .spawn();
        }

        (
            self.color.get_tracking_redraw(context),
            self.stroke_color.get_tracking_redraw(context),
        )
    }
}

impl Widget for DiscloseIndicator {
    fn unmounted(&mut self, context: &mut EventContext<'_>) {
        if let Some(label) = &mut self.label {
            label.unmount_in(context);
        }
        self.contents.unmount_in(context);
    }

    fn redraw(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) {
        let angle = self.angle.get_tracking_redraw(context);
        let (color, stroke_color) = self.effective_colors(context);
        let size = context
            .get(&IndicatorSize)
            .into_px(context.gfx.scale())
            .round();
        let stroke_options =
            StrokeOptions::px_wide(Lp::points(1).into_px(context.gfx.scale()).round())
                .colored(stroke_color);

        let radius = ((size - stroke_options.line_width) / 2).round();
        let pt1 = Point::new(radius, Px::ZERO).rotate_by(Angle::degrees(0));
        let pt2 = Point::new(radius, Px::ZERO).rotate_by(Angle::degrees(120));
        let pt3 = Point::new(radius, Px::ZERO).rotate_by(Angle::degrees(240));

        let path = PathBuilder::new(pt1).line_to(pt2).line_to(pt3).close();

        let indicator_layout_height = if let Some(label) = &mut self.label {
            let label = label.mounted(context);
            context.for_other(&label).redraw();
            label
                .last_layout()
                .unwrap_or_default()
                .size
                .height
                .max(size)
        } else {
            size
        };

        let center = (Point::new(size, indicator_layout_height) / 2).round();
        context
            .gfx
            .draw_shape(path.fill(color).translate_by(center).rotate_by(angle));

        context.gfx.draw_shape(
            path.stroke(stroke_options)
                .translate_by(center)
                .rotate_by(angle),
        );

        let contents = self.contents.mounted(context);
        context.for_other(&contents).redraw();
    }

    fn layout(
        &mut self,
        mut available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        let indicator_size = context
            .get(&IndicatorSize)
            .into_upx(context.gfx.scale())
            .round();
        let padding = context
            .get(&IntrinsicPadding)
            .into_upx(context.gfx.scale())
            .round();

        let content_inset = indicator_size + padding;
        available_space.width -= content_inset;

        let label_size = if let Some(label) = &mut self.label {
            let label = label.mounted(context);
            let label_size = context.for_other(&label).layout(Size::new(
                available_space.width,
                ConstraintLimit::SizeToFit(available_space.height.max()),
            ));
            let label_vertical_offset = if label_size.height < indicator_size {
                (indicator_size - label_size.height).round()
            } else {
                UPx::ZERO
            };
            context.set_child_layout(
                &label,
                Rect::new(Point::new(content_inset, label_vertical_offset), label_size)
                    .into_signed(),
            );
            Size::new(label_size.width, label_size.height.max(indicator_size))
        } else {
            Size::ZERO
        };

        let content_vertical_offset = if label_size.height > 0 {
            label_size.height + padding
        } else {
            label_size.height
        };

        available_space.height -= content_vertical_offset;

        let contents = self.contents.mounted(context);
        let content_size = context.for_other(&contents).layout(available_space);
        let content_rect = Rect::new(
            Point::new(content_inset, content_vertical_offset),
            content_size,
        );
        context.set_child_layout(&contents, content_rect.into_signed());
        Size::new(
            content_inset + content_rect.size.width.max(label_size.width),
            indicator_size.max(content_rect.origin.y + content_rect.size.height),
        )
    }

    fn accept_focus(&mut self, _context: &mut EventContext<'_>) -> bool {
        true
    }

    fn focus(&mut self, context: &mut EventContext<'_>) {
        context.set_needs_redraw();
    }

    fn blur(&mut self, context: &mut EventContext<'_>) {
        context.set_needs_redraw();
    }

    fn hit_test(&mut self, location: Point<Px>, context: &mut EventContext<'_>) -> bool {
        let size = context
            .get(&IndicatorSize)
            .into_px(context.kludgine.scale())
            .round();
        if let Some(label) = &mut self.label {
            let layout = label.mounted(context).last_layout().unwrap_or_default();

            location.y < size.max(layout.size.height)
        } else {
            location.x < size && location.y < size
        }
    }

    fn hover(&mut self, location: Point<Px>, context: &mut EventContext<'_>) -> Option<CursorIcon> {
        let hovering = self.hit_test(location, context);
        if self.hovering_indicator != hovering {
            context.set_needs_redraw();
            self.hovering_indicator = hovering;
        }

        hovering.then_some(CursorIcon::Pointer)
    }

    fn unhover(&mut self, context: &mut EventContext<'_>) {
        if self.hovering_indicator {
            self.hovering_indicator = false;
            context.set_needs_redraw();
        }
    }

    fn mouse_down(
        &mut self,
        location: Point<Px>,
        _device_id: DeviceId,
        _button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) -> EventHandling {
        if self.hit_test(location, context) {
            self.mouse_buttons_pressed += 1;
            self.activate(context);
            context.focus();
            HANDLED
        } else {
            IGNORED
        }
    }

    fn mouse_up(
        &mut self,
        location: Option<Point<Px>>,
        _device_id: DeviceId,
        _button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) {
        self.mouse_buttons_pressed -= 1;
        if self.mouse_buttons_pressed == 0 {
            self.deactivate(context);
            self.collapsed.toggle();
        }
        let hovering = location.map_or(false, |location| self.hit_test(location, context));
        if hovering != self.hovering_indicator {
            self.hovering_indicator = hovering;
            context.set_needs_redraw();
        }
    }

    fn activate(&mut self, _context: &mut EventContext<'_>) {
        if self.mouse_buttons_pressed == 0 {
            self.collapsed.toggle();
        }
    }
}

define_components! {
    Disclose {
        /// The size to render a [`Disclose`] indicator.
        IndicatorSize(Dimension, "size", @LineHeight)
    }
}