ori_core/views/
button.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
use ori_macro::{example, Build};

use crate::{
    canvas::{BorderRadius, BorderWidth, Color},
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Padding, Size, Space, Vector},
    rebuild::Rebuild,
    style::{Stylable, Style, StyleBuilder, Theme},
    transition::Transition,
    view::{Pod, PodState, View},
};

/// Create a new [`Button`].
pub fn button<V>(view: V) -> Button<V> {
    Button::new(view)
}

/// The style of a button.
#[derive(Clone, Default, Rebuild)]
pub struct ButtonStyle {
    /// The padding.
    #[rebuild(layout)]
    pub padding: Padding,

    /// The distance of the fancy effect.
    #[rebuild(draw)]
    pub fancy: f32,

    /// The transition of the button.
    #[rebuild(draw)]
    pub transition: Transition,

    /// The color of the button.
    #[rebuild(draw)]
    pub color: Color,

    /// The border radius.
    #[rebuild(draw)]
    pub border_radius: BorderRadius,

    /// The border width.
    #[rebuild(draw)]
    pub border_width: BorderWidth,

    /// The border color.
    #[rebuild(draw)]
    pub border_color: Color,
}

impl Style for ButtonStyle {
    fn default_style() -> StyleBuilder<Self> {
        StyleBuilder::new(|theme: &Theme| ButtonStyle {
            padding: Padding::all(8.0),
            fancy: 0.0,
            transition: Transition::ease(0.1),
            color: theme.surface(2),
            border_radius: BorderRadius::all(4.0),
            border_width: BorderWidth::all(0.0),
            border_color: theme.outline,
        })
    }
}

/// A button.
///
/// Can be styled using the [`ButtonStyle`].
#[example(name = "button", width = 400, height = 300)]
#[derive(Build)]
pub struct Button<V> {
    /// The content.
    #[build(ignore)]
    pub content: Pod<V>,

    /// The padding.
    pub padding: Option<Padding>,

    /// The distance of the fancy effect.
    pub fancy: Option<f32>,

    /// The transition of the button.
    pub transition: Option<Transition>,

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

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

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

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

impl<V> Button<V> {
    /// Create a new [`Button`].
    pub fn new(content: V) -> Self {
        Self {
            content: Pod::new(content),
            padding: None,
            fancy: None,
            transition: None,
            color: None,
            border_radius: None,
            border_width: None,
            border_color: None,
        }
    }
}

impl<V> Stylable for Button<V> {
    type Style = ButtonStyle;

    fn style(&self, style: &Self::Style) -> Self::Style {
        ButtonStyle {
            padding: self.padding.unwrap_or(style.padding),
            fancy: self.fancy.unwrap_or(style.fancy),
            transition: self.transition.unwrap_or(style.transition),
            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),
        }
    }
}

#[doc(hidden)]
pub struct ButtonState {
    pub hovered: f32,
    pub active: f32,
    pub style: ButtonStyle,
}

impl<T, V: View<T>> View<T> for Button<V> {
    type State = (ButtonState, PodState<T, V>);

    fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
        cx.set_focusable(true);

        let state = ButtonState {
            hovered: 0.0,
            active: 0.0,
            style: self.style(cx.style()),
        };

        (state, self.content.build(cx, data))
    }

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

    fn event(
        &mut self,
        (state, content): &mut Self::State,
        cx: &mut EventCx,
        data: &mut T,
        event: &Event,
    ) -> bool {
        if cx.focused_changed() {
            cx.draw();
        }

        let handled = self.content.event(content, cx, data, event);

        if cx.hovered_changed() || cx.active_changed() {
            cx.animate();
        }

        if let Event::Animate(dt) = event {
            let hover = (state.style.transition).step(&mut state.hovered, cx.is_hovered(), *dt);
            let active = (state.style.transition).step(&mut state.active, cx.is_active(), *dt);

            if hover || active {
                cx.animate();
                cx.draw();
            }
        }

        handled
    }

    fn layout(
        &mut self,
        (state, content): &mut Self::State,
        cx: &mut LayoutCx,
        data: &mut T,
        space: Space,
    ) -> Size {
        let content_space = space.shrink(state.style.padding.size());
        let content_size = self.content.layout(content, cx, data, content_space);

        content.translate(state.style.padding.offset());

        space.fit(content_size + state.style.padding.size())
    }

    fn draw(&mut self, (state, content): &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        cx.hoverable(|cx| {
            let dark = state.style.color.darken(0.05);
            let dim = state.style.color.darken(0.025);
            let bright = state.style.color.lighten(0.05);

            let hovered = state.style.transition.get(state.hovered);
            let active = state.style.transition.get(state.active);

            let face = state.style.color.mix(bright, hovered).mix(dim, active);

            if cx.is_focused() {
                let info = cx.theme().info;

                cx.quad(
                    cx.rect().expand(2.0),
                    Color::TRANSPARENT,
                    state.style.border_radius.expand(2.0),
                    BorderWidth::all(2.0),
                    info,
                );
            }

            if state.style.fancy == 0.0 {
                cx.quad(
                    cx.rect(),
                    face,
                    state.style.border_radius,
                    state.style.border_width,
                    state.style.border_color,
                );

                self.content.draw(content, cx, data);
                return;
            }

            let base = dim.mix(dark, 1.0 - active);

            cx.quad(
                cx.rect(),
                base,
                state.style.border_radius,
                BorderWidth::ZERO,
                Color::TRANSPARENT,
            );

            let float = Vector::NEG_Y * (1.0 - active) * state.style.fancy;

            cx.translated(float, |cx| {
                cx.quad(
                    cx.rect(),
                    face,
                    state.style.border_radius,
                    state.style.border_width,
                    state.style.border_color,
                );

                self.content.draw(content, cx, data);
            });
        });
    }
}