azul_widgets/
button.rs

1use azul_core::{
2    dom::{Dom, DomString, TabIndex},
3    app_resources::ImageId,
4};
5
6#[derive(Debug, Clone, Hash, PartialEq, Eq)]
7pub struct Button {
8    pub content: ButtonContent,
9}
10
11#[derive(Debug, Clone, Hash, PartialEq, Eq)]
12pub enum ButtonContent {
13    Image(ImageId),
14    // Buttons should only contain short amounts of text
15    Text(DomString),
16}
17
18impl Button {
19    pub fn with_label<S: Into<DomString>>(text: S) -> Self {
20        Self {
21            content: ButtonContent::Text(text.into()),
22        }
23    }
24
25    pub fn with_image(image: ImageId) -> Self {
26        Self {
27            content: ButtonContent::Image(image),
28        }
29    }
30
31    pub fn dom(self) -> Dom {
32        use self::ButtonContent::*;
33
34        let mut button_root = Dom::div()
35            .with_class("__azul-native-button")
36            .with_tab_index(TabIndex::Auto);
37
38        button_root.add_child(match self.content {
39            Text(s) => Dom::label(s),
40            Image(i) => Dom::image(i),
41        });
42
43        button_root
44    }
45}
46
47#[test]
48fn test_button_ui_1() {
49    struct Mock;
50
51    let expected_html = "<div class=\"__azul-native-button\" tabindex=\"0\">\r\n    <p>\r\n        Hello\r\n    </p>\r\n</div>";
52
53    let button: Dom<Mock> = Button::with_label("Hello").dom();
54    let button_html = button.get_html_string();
55
56    if expected_html != button_html {
57        panic!("expected:\r\n{}\r\ngot:\r\n{}", expected_html, button_html);
58    }
59}