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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! A widget that indicates a value.

use std::fmt::Debug;
use std::time::Duration;

use figures::units::{Px, UPx};
use figures::{IntoSigned, IntoUnsigned, Point, Rect, Round, ScreenScale, Size, Zero};
use kludgine::app::winit::window::CursorIcon;
use kludgine::Color;

use crate::animation::{AnimationHandle, AnimationTarget, LinearInterpolate, Spawn, ZeroToOne};
use crate::context::{EventContext, GraphicsContext, LayoutContext, WidgetContext};
use crate::styles::components::{
    AutoFocusableControls, Easing, IntrinsicPadding, WidgetAccentColor,
};
use crate::styles::ColorExt;
use crate::value::{Destination, Dynamic, Source};
use crate::widget::{EventHandling, MakeWidget, Widget, WidgetRef, HANDLED, IGNORED};
use crate::window::WindowLocal;
use crate::ConstraintLimit;

/// A type that defines how an [`Indicator`] behaves and is drawn.
pub trait IndicatorBehavior: Send + Debug + 'static {
    /// The type that contains all the colors needed to draw this indicator.
    ///
    /// These colors are transitioned using animations depending on how the user
    /// is interacting with the indicator.
    type Colors: LinearInterpolate + PartialEq + Debug + Send + Sync + Copy + 'static;

    /// Returns the colors desired for the current state of the indicator.
    fn desired_colors(
        &mut self,
        context: &mut WidgetContext<'_>,
        state: IndicatorState,
    ) -> Self::Colors;
    /// Updates the indicator's state from the indicator being activated.
    fn activate(&mut self);
    /// Returns true if the indicator will display empty if the indicator is
    /// activated.
    fn will_be_empty_if_activated(&self) -> bool;
    /// Returns true if the indicator is not currently filled in.
    fn empty(&self) -> bool;
    /// Render the indicator in `region` given the current state and colors.
    ///
    /// - `is_active` is true if the widget is currently being activated by the
    ///   user.
    /// - `colors` is the currently interpolated colors to draw.
    /// - `selected_color` is the color that a selected indicator should be
    ///   drawn using.
    /// - `region` is the region the indicator should be drawn inside
    /// - `context` is the context to draw to.
    fn render(
        &mut self,
        is_active: bool,
        colors: &Self::Colors,
        selected_color: Color,
        region: Rect<Px>,
        context: &mut GraphicsContext<'_, '_, '_, '_>,
    );
    /// Returns the size of this indicator.
    fn size(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> Size<UPx>;
}

/// The current state of an [`Indicator`] widget.
#[derive(Debug, Clone, Copy)]
#[allow(clippy::struct_excessive_bools)]
pub struct IndicatorState {
    /// If true, the mouse is currently above the widget.
    pub hovered: bool,
    /// If true, the user is currently activating the widget.
    pub active: bool,
    /// If true, the indicator has keyboard focus.
    pub focused: bool,
    /// If true, the indicator is enabled.
    pub enabled: bool,
}

#[derive(Debug)]
struct WindowLocalState<Colors> {
    active_colors: Option<Dynamic<Colors>>,
    target_colors: Option<Colors>,
    color_animation: AnimationHandle,
    checkbox_region: Rect<Px>,
    label_region: Rect<Px>,
    focused: bool,
    hovered: bool,
    mouse_buttons_pressed: usize,
    size: Size<Px>,
}

impl<Colors> Default for WindowLocalState<Colors> {
    fn default() -> Self {
        Self {
            active_colors: None,
            target_colors: None,
            color_animation: AnimationHandle::new(),
            checkbox_region: Rect::ZERO,
            label_region: Rect::ZERO,
            focused: false,
            hovered: false,
            mouse_buttons_pressed: 0,
            size: Size::ZERO,
        }
    }
}

impl<Colors> WindowLocalState<Colors>
where
    Colors: LinearInterpolate + PartialEq + Copy + Send + Sync + 'static,
{
    fn update_colors<B>(
        &mut self,
        context: &mut WidgetContext<'_>,
        immediate: bool,
        behavior: &mut B,
    ) where
        B: IndicatorBehavior<Colors = Colors>,
    {
        let desired_colors = behavior.desired_colors(
            context,
            IndicatorState {
                hovered: self.hovered,
                active: self.hovered && self.mouse_buttons_pressed > 0,
                focused: self.focused,
                enabled: context.enabled(),
            },
        );

        if let Some(active_colors) = &self.active_colors {
            if self.target_colors.as_ref() != Some(&desired_colors) {
                if immediate {
                    active_colors.set(desired_colors);
                    self.color_animation.clear();
                } else {
                    self.color_animation = active_colors
                        .transition_to(desired_colors)
                        .over(Duration::from_millis(150))
                        .with_easing(context.get(&Easing))
                        .spawn();
                }
            }
        } else {
            self.active_colors = Some(Dynamic::new(desired_colors));
        }
        self.target_colors = Some(desired_colors);
    }

    fn hit_test(&self, location: Point<Px>) -> bool {
        self.checkbox_region.contains(location)
            || self.label_region.contains(location)
            || (location.x > self.checkbox_region.size.width
                && location.x < self.label_region.origin.x
                && location.y >= self.checkbox_region.origin.y
                && location.y <= self.checkbox_region.origin.y + self.checkbox_region.size.height)
    }
}

/// A widget that indicates a value.
///
/// This base widget type is used to implement the
/// [`Checkbox`](crate::widgets::Checkbox) and [`Radio`](crate::widgets::Radio)
/// widgets.
#[derive(Debug)]
pub struct Indicator<T>
where
    T: IndicatorBehavior,
{
    behavior: T,
    label: Option<WidgetRef>,
    focusable: bool,
    per_window: WindowLocal<WindowLocalState<T::Colors>>,
}

impl<T> Indicator<T>
where
    T: IndicatorBehavior,
{
    /// Returns a new indicator widget driven by `behavior`.
    pub fn new(behavior: T) -> Self {
        Self {
            behavior,
            label: None,
            focusable: true,
            per_window: WindowLocal::default(),
        }
    }

    /// Displays `label` next to this indicator. When unhandled clicks are
    /// received in the label's area, the indicator will be toggled.
    #[must_use]
    pub fn labelled_by(mut self, label: impl MakeWidget) -> Self {
        self.label = Some(WidgetRef::new(label));
        self
    }

    /// Sets whether this widget should receive keyboard focus.
    #[must_use]
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    fn update_colors(&mut self, context: &mut WidgetContext<'_>, immediate: bool) {
        let window_local = self.per_window.entry(context).or_default();
        window_local.update_colors(context, immediate, &mut self.behavior);
    }

    fn clicked(&mut self, context: &WidgetContext<'_>) {
        if context.enabled() {
            self.behavior.activate();
        }
    }
}

impl<T> Widget for Indicator<T>
where
    T: IndicatorBehavior,
{
    fn redraw(&mut self, context: &mut GraphicsContext<'_, '_, '_, '_>) {
        let window_local = self.per_window.entry(context).or_default();
        let is_active = window_local.mouse_buttons_pressed > 0 && window_local.hovered;
        window_local.update_colors(context, false, &mut self.behavior);
        let colors = window_local
            .active_colors
            .as_ref()
            .expect("always present after update_colors")
            .get_tracking_redraw(context);
        let mut selected_color = context.get(&WidgetAccentColor);
        if window_local.mouse_buttons_pressed > 0 {
            selected_color = selected_color.darken_by(ZeroToOne::new(0.8));
        }

        self.behavior.render(
            is_active,
            &colors,
            selected_color,
            window_local.checkbox_region,
            context,
        );

        if let Some(label) = &mut self.label {
            let label = label.mounted(context);
            context.for_other(&label).redraw();
        }
    }

    fn layout(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        let window_local = self.per_window.entry(context).or_default();
        window_local.size = self.behavior.size(context).into_signed().ceil();
        window_local.checkbox_region.size = window_local.size;

        let full_size = if let Some(label) = &mut self.label {
            let padding = context
                .get(&IntrinsicPadding)
                .into_px(context.gfx.scale())
                .ceil();
            let x_offset = window_local.size.width + padding;
            let remaining_space = Size::new(
                available_space.width - x_offset.into_unsigned(),
                available_space.height,
            );
            let mounted = label.mounted(context);
            let label_size = context
                .for_other(&mounted)
                .layout(remaining_space)
                .into_signed();
            let height = available_space
                .height
                .fit_measured(label_size.height.into_unsigned())
                .into_signed()
                .max(window_local.size.height);

            window_local.label_region = Rect::new(
                Point::new(x_offset, (height - label_size.height) / 2),
                label_size,
            );
            context.set_child_layout(&mounted, window_local.label_region);

            Size::new(label_size.width + x_offset, height).into_unsigned()
        } else {
            window_local.size.into_unsigned()
        };

        window_local.checkbox_region.origin.y =
            (full_size.height.into_signed() - window_local.size.height) / 2;

        full_size
    }

    fn hit_test(&mut self, location: Point<Px>, context: &mut EventContext<'_>) -> bool {
        let window_local = self.per_window.entry(context).or_default();
        window_local.hit_test(location)
    }

    fn mouse_down(
        &mut self,
        _location: Point<Px>,
        _device_id: crate::window::DeviceId,
        _button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) -> EventHandling {
        if context.enabled() {
            let window_local = self.per_window.entry(context).or_default();
            window_local.mouse_buttons_pressed += 1;

            context.set_needs_redraw();

            HANDLED
        } else {
            IGNORED
        }
    }

    fn mouse_drag(
        &mut self,
        location: Point<Px>,
        _device_id: crate::window::DeviceId,
        _button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) {
        let window_local = self.per_window.entry(context).or_default();
        let hovered = window_local.hit_test(location);
        if hovered != window_local.hovered {
            window_local.hovered = hovered;
            context.set_needs_redraw();
        }
    }

    fn mouse_up(
        &mut self,
        _location: Option<Point<Px>>,
        _device_id: crate::window::DeviceId,
        _button: kludgine::app::winit::event::MouseButton,
        context: &mut EventContext<'_>,
    ) {
        let window_local = self.per_window.entry(context).or_default();
        window_local.mouse_buttons_pressed -= 1;
        let hovered = window_local.hovered;
        if window_local.mouse_buttons_pressed == 0 {
            self.clicked(context);
        }
        if self.focusable && hovered {
            context.focus();
        }
        context.set_needs_redraw();
    }

    fn accept_focus(&mut self, context: &mut EventContext<'_>) -> bool {
        self.focusable && context.enabled() && context.get(&AutoFocusableControls).is_all()
    }

    fn focus(&mut self, context: &mut EventContext<'_>) {
        let window_local = self.per_window.entry(context).or_default();
        window_local.focused = true;
        context.set_needs_redraw();
    }

    fn blur(&mut self, context: &mut EventContext<'_>) {
        let window_local = self.per_window.entry(context).or_default();
        window_local.focused = false;
        context.set_needs_redraw();
    }

    fn hover(
        &mut self,
        _location: Point<Px>,
        context: &mut EventContext<'_>,
    ) -> Option<CursorIcon> {
        if context.enabled() {
            let window_local = self.per_window.entry(context).or_default();
            window_local.hovered = true;
            context.set_needs_redraw();
            Some(CursorIcon::Pointer)
        } else {
            Some(CursorIcon::NotAllowed)
        }
    }

    fn unhover(&mut self, context: &mut EventContext<'_>) {
        let window_local = self.per_window.entry(context).or_default();
        window_local.hovered = false;
        context.set_needs_redraw();
    }

    fn activate(&mut self, context: &mut EventContext<'_>) {
        let window_local = self.per_window.entry(context).or_default();
        // If we have no buttons pressed, the event should fire on activate not
        // on deactivate.
        if window_local.mouse_buttons_pressed == 0 {
            self.clicked(context);
        }
        self.update_colors(context, true);
    }
}