dusk_ui/component/
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
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use twilight_model::channel::message;
use twilight_model::channel::message::component::ButtonStyle;
use twilight_model::channel::message::ReactionType;
use twilight_model::gateway::payload::incoming::InteractionCreate;

use rand::distributions::Alphanumeric;
use rand::Rng;

use crate::context::{BuildContextPrefix, Callback, Context};

pub struct Button<D> {
    pub id: String,
    pub disabled: bool,
    pub emoji: Option<ReactionType>,
    pub label: Option<String>,
    pub style: ButtonStyle,
    pub url: Option<String>,
    pub on_click: Option<Callback<D>>,
}

impl<D> Button<D> {
    pub fn empty() -> Self {
        Self {
            id: rand::thread_rng()
                .sample_iter(&Alphanumeric)
                .take(7)
                .map(char::from)
                .collect(),
            ..Default::default()
        }
    }

    pub fn new<S: Into<String>>(label: S) -> Self {
        Self {
            label: Some(label.into()),
            ..Self::empty()
        }
    }

    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
        self.id = id.into();
        self
    }

    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub fn emoji(mut self, emoji: ReactionType) -> Self {
        self.emoji = Some(emoji);
        self
    }

    pub fn label<S: Into<String>>(mut self, label: S) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn style(mut self, style: ButtonStyle) -> Self {
        self.style = style;
        self
    }

    pub fn url<S: Into<String>>(mut self, url: S) -> Self {
        self.url = Some(url.into());
        self
    }

    pub fn on_click<
        F: 'static
            + Fn(
                &Box<InteractionCreate>,
                &Arc<Context<D>>,
                D,
            ) -> Pin<Box<dyn Future<Output = D> + Send>>
            + Send
            + Sync,
    >(
        mut self,
        f: F,
    ) -> Self {
        self.on_click = Some(Box::new(f));
        self
    }
}

impl<D> Default for Button<D> {
    fn default() -> Self {
        Self {
            id: "".to_string(),
            disabled: false,
            emoji: None,
            label: None,
            style: ButtonStyle::Primary,
            url: None,
            on_click: None,
        }
    }
}

impl<D> Button<D> {
    pub(crate) fn build(mut self: Self, ctx: BuildContextPrefix<D>) -> message::Component {
        let id = format!("{}.{}", ctx.prefix, self.id);
        if let Some(on_click) = self.on_click.take() {
            ctx.parent.binding.insert(id.clone(), on_click);
        }
        message::Component::Button(message::component::Button {
            custom_id: Some(id),
            disabled: self.disabled,
            emoji: self.emoji.clone(),
            label: self.label.clone(),
            style: self.style,
            url: self.url.clone(),
        })
    }
}