kael_ui 0.2.0

Professional shadcn-inspired UI component library for Kael. 100+ accessible components for building beautiful, performant desktop applications.
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use kael::*;
use kael_ui::{
    components::{input::Input, input_state::InputState, scrollable::scrollable_vertical},
    layout::VStack,
    theme::{install_theme, Theme},
};

struct CustomInputApp {
    // Simple usage - just a basic input
    simple_input: Entity<InputState>,

    // Custom validation - company email only
    company_email_input: Entity<InputState>,

    // Custom filter - username with specific characters
    username_input: Entity<InputState>,

    // Custom formatter - currency formatting
    currency_input: Entity<InputState>,

    // Complex custom - product code with all three
    product_code_input: Entity<InputState>,
}

impl CustomInputApp {
    fn new(cx: &mut Context<Self>) -> Self {
        Self {
            // 1. SIMPLE USAGE - Just a basic input, no configuration needed
            simple_input: cx.new(|cx| InputState::new(cx)),

            // 2. Custom validation - company email
            company_email_input: cx.new(|cx| InputState::new(cx)),

            // 3. Custom filter - username
            username_input: cx.new(|cx| InputState::new(cx)),

            // 4. Custom formatter - currency
            currency_input: cx.new(|cx| InputState::new(cx)),

            // 5. Complex custom - product code
            product_code_input: cx.new(|cx| InputState::new(cx)),
        }
    }
}

impl Render for CustomInputApp {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let theme = kael_ui::theme::use_theme();

        VStack::new()
            .size_full()
            .bg(theme.tokens.background)
            // Header
            .child(
                VStack::new()
                    .w_full()
                    .p(px(24.0))
                    .bg(theme.tokens.card)
                    .border_b_1()
                    .border_color(theme.tokens.border)
                    .child(
                        div()
                            .text_size(px(28.0))
                            .font_weight(FontWeight::BOLD)
                            .text_color(theme.tokens.foreground)
                            .child("Flexible Input Component Demo")
                    )
                    .child(
                        div()
                            .text_size(px(16.0))
                            .text_color(theme.tokens.muted_foreground)
                            .child("Simple to use, powerful when you need it")
                    )
            )
            // Scrollable content
            .child(
                div()
                    .flex_1()
                    .w_full()
                    .overflow_hidden()
                    .child(
                        scrollable_vertical(
                            div()
                                .flex()
                                .flex_col()
                                .w_full()
                                .p(px(32.0))
                                .gap(px(32.0))
                                // 1. SIMPLE USAGE
                                .child(
                                    VStack::new()
                                        .gap(px(16.0))
                                        .p(px(20.0))
                                        .bg(theme.tokens.card)
                                        .border_1()
                                        .border_color(theme.tokens.border)
                                        .rounded(theme.tokens.radius_lg)
                                        .child(
                                            div()
                                                .text_size(px(18.0))
                                                .font_weight(FontWeight::SEMIBOLD)
                                                .text_color(theme.tokens.foreground)
                                                .child("1. Simple Usage - Just Works™")
                                        )
                                        .child(
                                            div()
                                                .text_size(px(14.0))
                                                .text_color(theme.tokens.muted_foreground)
                                                .child("The simplest possible usage - just create an Input with a state:")
                                        )
                                        .child(
                                            // Simplest possible usage
                                            Input::new(&self.simple_input)
                                                .placeholder("Just type anything...")
                                        )
                                        .child(
                                            div()
                                                .p(px(12.0))
                                                .bg(theme.tokens.muted.opacity(0.1))
                                                .rounded(theme.tokens.radius_md)
                                                .font_family("monospace")
                                                .text_size(px(12.0))
                                                .child("Input::new(&state).placeholder(\"Just type anything...\")")
                                        )
                                )
                                // 2. CUSTOM VALIDATION
                                .child(
                VStack::new()
                    .gap(px(16.0))
                    .p(px(20.0))
                    .bg(theme.tokens.card)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_lg)
                    .child(
                        div()
                            .text_size(px(18.0))
                            .font_weight(FontWeight::SEMIBOLD)
                            .text_color(theme.tokens.foreground)
                            .child("2. Custom Validation - Your Rules")
                    )
                    .child(
                        div()
                            .text_size(px(14.0))
                            .text_color(theme.tokens.muted_foreground)
                            .child("Only accepts @company.com email addresses:")
                    )
                    .child(
                        Input::new(&self.company_email_input)
                            .placeholder("john@company.com")
                            .helper_text("Must be a company email address")
                            .custom_validator(|value| {
                                if value.is_empty() {
                                    return Ok(());
                                }
                                if value.contains("@company.com") {
                                    Ok(())
                                } else {
                                    Err("Email must end with @company.com".to_string())
                                }
                            })
                    )
                    .child(
                        div()
                            .p(px(12.0))
                            .bg(theme.tokens.muted.opacity(0.1))
                            .rounded(theme.tokens.radius_md)
                            .font_family("monospace")
                            .text_size(px(12.0))
                            .line_height(relative(1.5))
                            .child(".custom_validator(|value| {\n    if value.contains(\"@company.com\") {\n        Ok(())\n    } else {\n        Err(\"Must be company email\")\n    }\n})")
                    )
                                )
                                // 3. CUSTOM FILTER
                                .child(
                VStack::new()
                    .gap(px(16.0))
                    .p(px(20.0))
                    .bg(theme.tokens.card)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_lg)
                    .child(
                        div()
                            .text_size(px(18.0))
                            .font_weight(FontWeight::SEMIBOLD)
                            .text_color(theme.tokens.foreground)
                            .child("3. Custom Filter - Control Input Characters")
                    )
                    .child(
                        div()
                            .text_size(px(14.0))
                            .text_color(theme.tokens.muted_foreground)
                            .child("Username that only allows letters, numbers, and underscores:")
                    )
                    .child(
                        Input::new(&self.username_input)
                            .placeholder("john_doe_123")
                            .helper_text("Letters, numbers, and underscores only")
                            .custom_filter(|input| {
                                input.chars()
                                    .filter(|c| c.is_alphanumeric() || *c == '_')
                                    .collect()
                            })
                    )
                    .child(
                        div()
                            .p(px(12.0))
                            .bg(theme.tokens.muted.opacity(0.1))
                            .rounded(theme.tokens.radius_md)
                            .font_family("monospace")
                            .text_size(px(12.0))
                            .line_height(relative(1.5))
                            .child(".custom_filter(|input| {\n    input.chars()\n        .filter(|c| c.is_alphanumeric() || *c == '_')\n        .collect()\n})")
                    )
                                )
                                // 4. CUSTOM FORMATTER
                                .child(
                VStack::new()
                    .gap(px(16.0))
                    .p(px(20.0))
                    .bg(theme.tokens.card)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_lg)
                    .child(
                        div()
                            .text_size(px(18.0))
                            .font_weight(FontWeight::SEMIBOLD)
                            .text_color(theme.tokens.foreground)
                            .child("4. Custom Formatter - Display Formatting")
                    )
                    .child(
                        div()
                            .text_size(px(14.0))
                            .text_color(theme.tokens.muted_foreground)
                            .child("Currency input with automatic formatting (type numbers):")
                    )
                    .child(
                        Input::new(&self.currency_input)
                            .placeholder("0.00")
                            .prefix(
                                div()
                                    .text_color(theme.tokens.muted_foreground)
                                    .child("$")
                            )
                            .helper_text("Enter amount in dollars")
                            .custom_filter(|input| {
                                // Only allow numbers and decimal point
                                input.chars()
                                    .filter(|c| c.is_ascii_digit() || *c == '.')
                                    .collect()
                            })
                            .custom_validator(|value| {
                                if value.is_empty() {
                                    return Ok(());
                                }
                                match value.parse::<f64>() {
                                    Ok(_) => Ok(()),
                                    Err(_) => Err("Invalid currency format".to_string()),
                                }
                            })
                    )
                    .child(
                        div()
                            .p(px(12.0))
                            .bg(theme.tokens.muted.opacity(0.1))
                            .rounded(theme.tokens.radius_md)
                            .font_family("monospace")
                            .text_size(px(12.0))
                            .child("Filter: numbers and '.' only | Validate: must be valid number")
                    )
                                )
                                // 5. COMPLEX EXAMPLE
                                .child(
                VStack::new()
                    .gap(px(16.0))
                    .p(px(20.0))
                    .bg(theme.tokens.card)
                    .border_1()
                    .border_color(theme.tokens.border)
                    .rounded(theme.tokens.radius_lg)
                    .child(
                        div()
                            .text_size(px(18.0))
                            .font_weight(FontWeight::SEMIBOLD)
                            .text_color(theme.tokens.foreground)
                            .child("5. Complex Example - Product Code")
                    )
                    .child(
                        div()
                            .text_size(px(14.0))
                            .text_color(theme.tokens.muted_foreground)
                            .child("Product code with format ABC-1234 (3 letters, dash, 4 numbers):")
                    )
                    .child(
                        Input::new(&self.product_code_input)
                            .placeholder("ABC-1234")
                            .helper_text("Format: XXX-#### (3 letters, dash, 4 digits)")
                            .max_length(8)
                            .custom_filter(|input| {
                                let mut result = String::new();
                                let mut letter_count = 0;
                                let mut digit_count = 0;
                                let mut has_dash = false;

                                for c in input.chars() {
                                    if letter_count < 3 && c.is_ascii_alphabetic() && !has_dash {
                                        result.push(c.to_ascii_uppercase());
                                        letter_count += 1;
                                    } else if letter_count == 3 && c == '-' && !has_dash {
                                        result.push(c);
                                        has_dash = true;
                                    } else if has_dash && digit_count < 4 && c.is_ascii_digit() {
                                        result.push(c);
                                        digit_count += 1;
                                    }

                                    // Auto-insert dash after 3 letters
                                    if letter_count == 3 && !has_dash && !result.contains('-') {
                                        result.push('-');
                                        has_dash = true;
                                    }
                                }

                                result
                            })
                            .custom_validator(|value| {
                                if value.is_empty() {
                                    return Ok(());
                                }

                                let parts: Vec<&str> = value.split('-').collect();
                                if parts.len() != 2 {
                                    return Err("Must be in format XXX-####".to_string());
                                }

                                let letters = parts[0];
                                let numbers = parts[1];

                                if letters.len() != 3 || !letters.chars().all(|c| c.is_ascii_alphabetic()) {
                                    return Err("First part must be exactly 3 letters".to_string());
                                }

                                if numbers.len() != 4 || !numbers.chars().all(|c| c.is_ascii_digit()) {
                                    return Err("Second part must be exactly 4 digits".to_string());
                                }

                                Ok(())
                            })
                    )
                    .child(
                        div()
                            .p(px(12.0))
                            .bg(theme.tokens.muted.opacity(0.1))
                            .rounded(theme.tokens.radius_md)
                            .font_family("monospace")
                            .text_size(px(12.0))
                            .child("Auto-formats as you type, validates format, enforces max length")
                    )
                                )
                                // Summary
                                .child(
                VStack::new()
                    .gap(px(12.0))
                    .p(px(20.0))
                    .bg(theme.tokens.primary.opacity(0.1))
                    .border_1()
                    .border_color(theme.tokens.primary)
                    .rounded(theme.tokens.radius_lg)
                    .child(
                        div()
                            .text_size(px(16.0))
                            .font_weight(FontWeight::SEMIBOLD)
                            .text_color(theme.tokens.foreground)
                            .child("✨ Key Features")
                    )
                    .child(
                        div()
                            .text_size(px(14.0))
                            .text_color(theme.tokens.foreground)
                            .line_height(relative(1.6))
                            .child("• Start simple - Just Input::new(&state)\n• Add validation when needed - custom_validator()\n• Control input characters - custom_filter()\n• Format display - custom_formatter()\n• Combine with built-in types (Email, Number, Tel, etc.)\n• Full accessibility and keyboard support included\n• Industry-standard UX patterns by default")
                    )
                                )
                        ),
                    ),
            )
    }
}

fn main() {
    Application::new().run(move |cx| {
        // Install dark theme
        install_theme(cx, Theme::dark());

        // Initialize input system
        kael_ui::init(cx);

        cx.open_window(
            WindowOptions {
                window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
                    None,
                    size(px(900.0), px(900.0)),
                    cx,
                ))),
                titlebar: Some(TitlebarOptions {
                    title: Some("Custom Input Extensions Demo".into()),
                    ..Default::default()
                }),
                ..Default::default()
            },
            |_window, cx| cx.new(|cx| CustomInputApp::new(cx)),
        )
        .unwrap();
    });
}