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
//! Button element
//!
//! Buttons are core element in any app, Savory ships a powerfull button that
//! cover 4 kinds of button:
//! - Default Button
//! - Dashed Button
//! - Text Button
//! - Link Button
//!
//! Button also can have `Suggestion` or `Destructive` (aka `Primary` and
//! `Danger` in web UI World), button can also go ghost.
//!
//! See [`Button`] docs to find out more about its methods.
//!
//! # Usage
//! TODO
//!
//! [`Button`]: crate::prelude::Button

use crate::{id::Id, prelude::*};
use derive_rich::Rich;
use savory::prelude::*;
use savory_style::prelude::*;
use std::borrow::Cow;

pub enum Msg {
    DesignSystem(DesignSystem),
    Focus(bool),
    MouseOver(bool),
    Disable(bool),
}

/// Button element
#[derive(Rich, Element)]
pub struct Button {
    // general element properties
    #[rich(read)]
    #[element(config)]
    id: Option<Id>,
    design_system: DesignSystem,

    // button element properties
    #[rich(read)]
    #[element(config)]
    text: Option<Cow<'static, str>>,
    #[rich(read)]
    #[element(config)]
    icon: Option<Svg<Msg>>,
    #[rich(read(copy, rename = is_disabled))]
    #[element(config(default), data_lens(copy))]
    disabled: bool,
    #[rich(read(copy, rename = is_focused))]
    #[element(data_lens(copy))]
    focused: bool,
    #[rich(read(copy, rename = is_mouse_over))]
    #[element(data_lens(copy))]
    mouse_over: bool,

    #[rich(read(copy))]
    #[element(config, data_lens(copy))]
    color: Option<palette::Hsl>,
    #[rich(read(copy))]
    #[element(config, data_lens(copy))]
    text_color: Option<palette::Hsl>,
    #[rich(read(copy))]
    #[element(config(default = "ActionType::Default"), data_lens(copy))]
    action_type: ActionType,
    #[rich(read(copy))]
    #[element(config(default = "Kind::Default"), data_lens(copy))]
    kind: Kind,
    #[rich(read(copy, rename = is_ghost))]
    #[element(config(default, no_pub), data_lens(copy))]
    ghost: bool,
}

#[derive(Debug, Copy, Eq, PartialEq, Clone)]
pub enum ActionType {
    Default,
    Suggested,
    Destructive,
}

#[derive(Debug, Copy, Eq, PartialEq, Clone)]
pub enum Kind {
    Default,
    Dashed,
    TextButton,
    LinkButton,
}

impl Element for Button {
    type Message = Msg;
    type Config = Config;

    fn init(config: Self::Config, orders: &mut impl Orders<Msg>) -> Self {
        orders.subscribe(|ds: DesignSystemChanged| Msg::DesignSystem(ds.0));

        Button {
            id: config.id,
            design_system: DesignSystem::default(),
            text: config.text,
            icon: config.icon,
            disabled: config.disabled,
            focused: false,
            mouse_over: false,
            color: config.color,
            text_color: config.text_color,
            action_type: config.action_type,
            kind: config.kind,
            ghost: config.ghost,
        }
    }

    fn update(&mut self, msg: Msg, _: &mut impl Orders<Msg>) {
        match msg {
            Msg::DesignSystem(val) => self.design_system = val,
            Msg::MouseOver(val) => self.mouse_over = val,
            Msg::Focus(val) => self.focused = val,
            Msg::Disable(val) => self.disabled = val,
        }
    }
}

impl View<Node<Msg>> for Button {
    fn view(&self) -> Node<Msg> {
        let style = self.design_system.button(self.data_lens());
        html::button()
            .class("button")
            .try_id(self.id.clone())
            .disabled(self.disabled)
            .style(style)
            .on_focus(|_| Msg::Focus(true))
            .on_blur(|_| Msg::Focus(false))
            .on_mouse_over(|_| Msg::MouseOver(true))
            .on_mouse_leave(|_| Msg::MouseOver(false))
            .try_push(self.icon.as_ref().map(|el| el.view()))
            .try_push(self.text.clone())
    }
}

impl Config {
    pub fn suggestion(mut self) -> Self {
        self.action_type = ActionType::Suggested;
        self
    }

    pub fn destructive(mut self) -> Self {
        self.action_type = ActionType::Destructive;
        self
    }

    pub fn dashed(mut self) -> Self {
        self.kind = Kind::Dashed;
        self
    }

    pub fn text_button(mut self) -> Self {
        self.kind = Kind::TextButton;
        self
    }

    pub fn link_button(mut self) -> Self {
        self.kind = Kind::LinkButton;
        self
    }

    pub fn ghost(mut self) -> Self {
        self.ghost = true;
        self
    }
}