windjammer_ui/components/generated/
switch.rs

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