ori_core/views/
button.rs

1use glam::Vec2;
2
3use crate::{
4    BoxConstraints, Div, DrawContext, Event, EventContext, Events, LayoutContext, Parent,
5    PointerEvent, Scope, Sendable, Style, View,
6};
7
8pub struct Button {
9    pub content: Div,
10}
11
12impl Default for Button {
13    fn default() -> Self {
14        Self {
15            content: Div::new(),
16        }
17    }
18}
19
20impl Button {
21    pub fn new(view: impl View) -> Self {
22        Self::default().child(view)
23    }
24
25    pub fn child(mut self, view: impl View) -> Self {
26        self.content = self.content.child(view);
27        self
28    }
29
30    pub fn on_press<'a>(
31        mut self,
32        cx: Scope<'a>,
33        callback: impl FnMut(&PointerEvent) + Sendable + 'a,
34    ) -> Self {
35        self.content = self.content.on_press(cx, callback);
36        self
37    }
38}
39
40impl Parent for Button {
41    fn add_child(&mut self, view: impl View) {
42        self.content.add_child(view);
43    }
44}
45
46impl Events for Button {
47    type Setter<'a> = <Div as Events>::Setter<'a>;
48
49    fn setter(&mut self) -> Self::Setter<'_> {
50        Events::setter(&mut self.content)
51    }
52}
53
54impl View for Button {
55    type State = <Div as View>::State;
56
57    fn build(&self) -> Self::State {}
58
59    fn style(&self) -> Style {
60        Style::new("button")
61    }
62
63    fn event(&self, state: &mut Self::State, cx: &mut EventContext, event: &Event) {
64        self.content.event(state, cx, event);
65    }
66
67    fn layout(&self, state: &mut Self::State, cx: &mut LayoutContext, bc: BoxConstraints) -> Vec2 {
68        self.content.layout(state, cx, bc)
69    }
70
71    fn draw(&self, state: &mut Self::State, cx: &mut DrawContext) {
72        self.content.draw(state, cx);
73    }
74}