floem/views/
button.rs

1use crate::{style_class, views::Decorators, IntoView, View, ViewId};
2use core::ops::FnMut;
3
4style_class!(pub ButtonClass);
5
6pub fn button<V: IntoView + 'static>(child: V) -> Button {
7    Button::new(child)
8}
9
10pub struct Button {
11    id: ViewId,
12}
13impl View for Button {
14    fn id(&self) -> ViewId {
15        self.id
16    }
17}
18impl Button {
19    pub fn new(child: impl IntoView) -> Self {
20        let id = ViewId::new();
21        id.add_child(Box::new(child.into_view()));
22        Button { id }.keyboard_navigable().class(ButtonClass)
23    }
24
25    pub fn action(self, mut on_press: impl FnMut() + 'static) -> Self {
26        self.on_click_stop(move |_| {
27            on_press();
28        })
29    }
30}
31
32pub trait ButtonExt {
33    fn button(self) -> Button;
34}
35impl<T: IntoView + 'static> ButtonExt for T {
36    fn button(self) -> Button {
37        button(self)
38    }
39}