ori_core/views/
slider.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
use std::ops::RangeInclusive;

use ori_macro::Build;

use crate::{
    canvas::{BorderRadius, BorderWidth, Color},
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Axis, Rect, Size, Space},
    rebuild::Rebuild,
    style::{Stylable, Style, StyleBuilder, Theme},
    view::View,
};

/// Create a new [`Slider`].
pub fn slider<T>(value: f32) -> Slider<T> {
    Slider::new(value)
}

/// The style of a [`Slider`].
#[derive(Clone, Rebuild)]
pub struct SliderStyle {
    /// The length of the slider.
    pub length: f32,

    /// The width of the slider.
    pub width: f32,

    /// The background color of the slider.
    pub background: Color,

    /// The foreground color of the slider.
    pub color: Color,

    /// The border radius of the slider.
    pub border_radius: BorderRadius,

    /// The border width of the slider.
    pub border_width: BorderWidth,

    /// The border color of the slider.
    pub border_color: Color,
}

impl Style for SliderStyle {
    fn default_style() -> StyleBuilder<Self> {
        StyleBuilder::new(|theme: &Theme| Self {
            length: 100.0,
            width: 10.0,
            background: theme.surface(1),
            color: theme.primary,
            border_radius: BorderRadius::all(5.0),
            border_width: BorderWidth::all(0.0),
            border_color: theme.outline,
        })
    }
}

/// A slider.
///
/// Can be styled with a [`SliderStyle`].
#[derive(Build, Rebuild)]
pub struct Slider<T> {
    /// The value of the slider.
    #[rebuild(draw)]
    pub value: f32,

    /// The range of the slider.
    #[rebuild(draw)]
    pub range: RangeInclusive<f32>,

    /// The callback for when the value changes.
    #[build(ignore)]
    #[allow(clippy::type_complexity)]
    pub on_input: Option<Box<dyn FnMut(&mut EventCx, &mut T, f32) + 'static>>,

    /// The axis of the slider.
    #[rebuild(layout)]
    pub axis: Axis,

    /// The width of the slider.
    pub width: Option<f32>,

    /// The length of the slider.
    pub length: Option<f32>,

    /// The foreground color of the slider.
    pub color: Option<Color>,

    /// The background color of the slider.
    pub background: Option<Color>,

    /// The border radius of the slider.
    pub border_radius: Option<BorderRadius>,

    /// The border width of the slider.
    pub border_width: Option<BorderWidth>,

    /// The border color of the slider.
    pub border_color: Option<Color>,
}

impl<T> Slider<T> {
    /// Create a new [`Slider`].
    pub fn new(value: f32) -> Self {
        Self {
            value,
            range: 0.0..=1.0,
            on_input: None,
            axis: Axis::Horizontal,
            width: None,
            length: None,
            color: None,
            background: None,
            border_radius: None,
            border_width: None,
            border_color: None,
        }
    }

    /// Set the callback for when the value changes.
    pub fn on_input(mut self, on_input: impl FnMut(&mut EventCx, &mut T, f32) + 'static) -> Self {
        self.on_input = Some(Box::new(on_input));
        self
    }
}

fn normalize(value: f32, range: &RangeInclusive<f32>) -> f32 {
    let value = value.clamp(*range.start(), *range.end());
    (value - range.start()) / (range.end() - range.start())
}

fn denormalize(value: f32, range: &RangeInclusive<f32>) -> f32 {
    let value = value.clamp(0.0, 1.0);
    value * (range.end() - range.start()) + range.start()
}

impl<T> Stylable for Slider<T> {
    type Style = SliderStyle;

    fn style(&self, style: &Self::Style) -> Self::Style {
        SliderStyle {
            length: self.length.unwrap_or(style.length),
            width: self.width.unwrap_or(style.width),
            background: self.background.unwrap_or(style.background),
            color: self.color.unwrap_or(style.color),
            border_radius: self.border_radius.unwrap_or(style.border_radius),
            border_width: self.border_width.unwrap_or(style.border_width),
            border_color: self.border_color.unwrap_or(style.border_color),
        }
    }
}

impl<T> View<T> for Slider<T> {
    type State = SliderStyle;

    fn build(&mut self, cx: &mut BuildCx, _data: &mut T) -> Self::State {
        self.style(cx.style())
    }

    fn rebuild(&mut self, style: &mut Self::State, cx: &mut RebuildCx, _data: &mut T, old: &Self) {
        Rebuild::rebuild(self, cx, old);
        self.rebuild_style(cx, style);
    }

    fn event(
        &mut self,
        style: &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        match event {
            Event::PointerPressed(e) if cx.is_hovered() => {
                let local = cx.local(e.position);

                let value = self.axis.unpack(local).0 / style.length;
                let value = denormalize(value, &self.range);

                if let Some(on_input) = &mut self.on_input {
                    on_input(cx, data, value);
                }

                cx.set_active(true);

                true
            }
            Event::PointerMoved(e) => {
                let local = cx.local(e.position);

                if cx.is_active() {
                    let value = self.axis.unpack(local).0 / style.length;
                    let value = denormalize(value, &self.range);

                    if let Some(on_input) = &mut self.on_input {
                        on_input(cx, data, value);
                    }
                }

                false
            }
            Event::PointerReleased(_) if cx.is_active() => {
                cx.set_active(false);

                true
            }
            _ => false,
        }
    }

    fn layout(
        &mut self,
        style: &mut Self::State,
        _cx: &mut LayoutCx,
        _data: &mut T,
        space: Space,
    ) -> Size {
        let size = self.axis.pack(style.length, style.width);
        space.fit(size)
    }

    fn draw(&mut self, style: &mut Self::State, cx: &mut DrawCx, _data: &mut T) {
        cx.hoverable(|cx| {
            cx.quad(
                cx.rect(),
                style.background,
                style.border_radius,
                style.border_width,
                style.border_color,
            );

            let (length, width) = self.axis.unpack(cx.size());
            let value = normalize(self.value, &self.range);

            let min_length = style.border_radius.max_element() * 2.0;
            let length = f32::max(length * value, min_length);
            let size = self.axis.pack(length, width);

            cx.quad(
                Rect::min_size(cx.rect().min, size),
                style.color,
                style.border_radius,
                style.border_width,
                style.border_color,
            );
        });
    }
}