windjammer_ui/components/generated/
switch.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3pub struct Switch {
4    checked: bool,
5    disabled: bool,
6    size: SwitchSize,
7    label: String,
8    class: String,
9}
10
11pub enum SwitchSize {
12    Small,
13    Medium,
14    Large,
15}
16
17impl Switch {
18    #[inline]
19    pub fn new() -> Switch {
20        Switch {
21            checked: false,
22            disabled: false,
23            size: SwitchSize::Medium,
24            label: String::new(),
25            class: String::new(),
26        }
27    }
28    #[inline]
29    pub fn checked(mut self, checked: bool) -> Switch {
30        self.checked = checked;
31        self
32    }
33    #[inline]
34    pub fn disabled(mut self, disabled: bool) -> Switch {
35        self.disabled = disabled;
36        self
37    }
38    #[inline]
39    pub fn size(mut self, size: SwitchSize) -> Switch {
40        self.size = size;
41        self
42    }
43    #[inline]
44    pub fn label(mut self, label: String) -> Switch {
45        self.label = label;
46        self
47    }
48    #[inline]
49    pub fn class(mut self, class: String) -> Switch {
50        self.class = class;
51        self
52    }
53    pub fn render(&self) -> String {
54        let (width, height, thumb_size) = match self.size {
55            SwitchSize::Small => ("32px", "18px", "14px"),
56            SwitchSize::Medium => ("44px", "24px", "20px"),
57            SwitchSize::Large => ("56px", "32px", "28px"),
58        };
59        let bg_color = {
60            if self.checked {
61                "#3b82f6"
62            } else {
63                "#d1d5db"
64            }
65        };
66        let thumb_pos = {
67            if self.checked {
68                match self.size {
69                    SwitchSize::Small => "16px",
70                    SwitchSize::Medium => "22px",
71                    SwitchSize::Large => "26px",
72                }
73            } else {
74                "2px"
75            }
76        };
77        let disabled_style = {
78            if self.disabled {
79                " opacity: 0.5; cursor: not-allowed;"
80            } else {
81                " cursor: pointer;"
82            }
83        };
84        let disabled_attr = {
85            if self.disabled {
86                " disabled"
87            } else {
88                ""
89            }
90        };
91        let mut html = String::new();
92        html.push_str("<label class=\"wj-switch ");
93        html.push_str(self.class.as_str());
94        html.push_str("\" style=\"display: inline-flex; align-items: center; gap: 8px;");
95        html.push_str(disabled_style);
96        html.push_str("\">");
97        html.push_str("<input type=\"checkbox\"");
98        if self.checked {
99            html.push_str(" checked")
100        }
101        html.push_str(disabled_attr);
102        html.push_str(" style=\"position: absolute; opacity: 0; width: 0; height: 0;\">");
103        html.push_str("<span style=\"position: relative; display: inline-block; width: ");
104        html.push_str(width);
105        html.push_str("; height: ");
106        html.push_str(height);
107        html.push_str("; background-color: ");
108        html.push_str(bg_color);
109        html.push_str("; border-radius: 999px; transition: background-color 0.2s;\">");
110        html.push_str("<span style=\"position: absolute; top: 2px; left: ");
111        html.push_str(thumb_pos);
112        html.push_str("; width: ");
113        html.push_str(thumb_size);
114        html.push_str("; height: ");
115        html.push_str(thumb_size);
116        html.push_str("; background-color: white; border-radius: 50%; transition: left 0.2s; box-shadow: 0 2px 4px rgba(0,0,0,0.2);\"></span>");
117        html.push_str("</span>");
118        if !self.label.is_empty() {
119            html.push_str("<span style=\"font-size: 14px;\">");
120            html.push_str(self.label.as_str());
121            html.push_str("</span>")
122        }
123        html.push_str("</label>");
124        html
125    }
126}