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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use kael::*;
use kael_ui::{
    components::{
        input::{Input, InputType, InputVariant},
        input_state::{InputEvent, InputState},
        scrollable::scrollable_vertical,
    },
    layout::{HStack, VStack},
    theme::{install_theme, Theme},
};

struct FocusTestApp {
    // Form inputs to test tab navigation
    first_name: Entity<InputState>,
    last_name: Entity<InputState>,
    email: Entity<InputState>,
    phone: Entity<InputState>,
    password: Entity<InputState>,

    // Track which input has focus
    #[allow(dead_code)]
    current_focus_index: usize,
}

impl FocusTestApp {
    fn new(cx: &mut Context<Self>) -> Self {
        let mut app = Self {
            first_name: cx.new(|cx| InputState::new(cx)),
            last_name: cx.new(|cx| InputState::new(cx)),
            email: cx.new(|cx| InputState::new(cx).input_type(InputType::Email)),
            phone: cx.new(|cx| InputState::new(cx).input_type(InputType::Tel)),
            password: cx.new(|cx| InputState::new(cx).input_type(InputType::Password)),
            current_focus_index: 0,
        };

        // Set up tab navigation handlers
        app.setup_tab_navigation(cx);
        app
    }

    fn setup_tab_navigation(&mut self, cx: &mut Context<Self>) {
        // Create a list of all inputs in order
        let inputs = vec![
            self.first_name.clone(),
            self.last_name.clone(),
            self.email.clone(),
            self.phone.clone(),
            self.password.clone(),
        ];

        // Set up tab navigation for each input
        for (i, input) in inputs.iter().enumerate() {
            let inputs_clone = inputs.clone();
            let current_index = i;

            cx.subscribe(
                input,
                move |_this, _emitter: Entity<InputState>, event: &InputEvent, cx| {
                    match event {
                        InputEvent::Tab => {
                            // Queue focus change for next input
                            let next_index = (current_index + 1) % inputs_clone.len();
                            let next_input = inputs_clone[next_index].clone();
                            cx.defer(move |cx| {
                                cx.update_window(cx.active_window().unwrap(), |_, window, cx| {
                                    window.focus(&next_input.read(cx).focus_handle(cx));
                                })
                                .ok();
                            });
                        }
                        InputEvent::ShiftTab => {
                            // Queue focus change for previous input
                            let prev_index = if current_index == 0 {
                                inputs_clone.len() - 1
                            } else {
                                current_index - 1
                            };
                            let prev_input = inputs_clone[prev_index].clone();
                            cx.defer(move |cx| {
                                cx.update_window(cx.active_window().unwrap(), |_, window, cx| {
                                    window.focus(&prev_input.read(cx).focus_handle(cx));
                                })
                                .ok();
                            });
                        }
                        _ => {}
                    }
                },
            )
            .detach();
        }
    }
}

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

        div()
            .size_full()
            .bg(theme.tokens.background)
            .child(
                VStack::new()
                    .size_full()
                    // 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(24.0))
                                    .font_weight(FontWeight::BOLD)
                                    .text_color(theme.tokens.foreground)
                                    .child("Focus Management Demo")
                            )
                            .child(
                                div()
                                    .text_size(px(14.0))
                                    .text_color(theme.tokens.muted_foreground)
                                    .child("Test tab navigation and click-outside-to-blur behavior")
                            )
                    )
                    // Content
                    .child(
                        div()
                            .flex_1()
                            .w_full()
                            .overflow_hidden()
                            .child(
                                scrollable_vertical(
                                    div()
                                        .flex()
                                        .flex_col()
                                        .w_full()
                                        .p(px(32.0))
                                        .gap(px(24.0))
                                        // Instructions
                                        .child(
                                            VStack::new()
                                                .w_full()
                                                .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("📋 Instructions")
                                                )
                                                .child(
                                                    div()
                                                        .text_size(px(14.0))
                                                        .text_color(theme.tokens.foreground)
                                                        .line_height(relative(1.6))
                                                        .child("• Click on an input to focus it\n• Double-click to select all text\n• Press Tab to move to next input, Shift+Tab for previous\n• Press Escape to blur the focused input\n• Use all standard keyboard shortcuts (arrows, Home/End, Cmd/Ctrl+A/C/V/X)")
                                                )
                                        )
                                        // Form container
                                        .child(
                                            VStack::new()
                                                .w_full()
                                                .max_w(px(500.0))
                                                .gap(px(20.0))
                                                .p(px(24.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("Sample Registration Form")
                                                )
                                                // First Name
                                                .child(
                                                    VStack::new()
                                                        .w_full()
                                                        .gap(px(4.0))
                                                        .child(
                                                            div()
                                                                .text_size(px(14.0))
                                                                .font_weight(FontWeight::MEDIUM)
                                                                .text_color(theme.tokens.foreground)
                                                                .child("First Name")
                                                        )
                                                        .child(
                                                            Input::new(&self.first_name)
                                                                .placeholder("John")
                                                                .variant(InputVariant::Outline)
                                                        )
                                                )
                                                // Last Name
                                                .child(
                                                    VStack::new()
                                                        .w_full()
                                                        .gap(px(4.0))
                                                        .child(
                                                            div()
                                                                .text_size(px(14.0))
                                                                .font_weight(FontWeight::MEDIUM)
                                                                .text_color(theme.tokens.foreground)
                                                                .child("Last Name")
                                                        )
                                                        .child(
                                                            Input::new(&self.last_name)
                                                                .placeholder("Doe")
                                                                .variant(InputVariant::Outline)
                                                        )
                                                )
                                                // Email
                                                .child(
                                                    VStack::new()
                                                        .w_full()
                                                        .gap(px(4.0))
                                                        .child(
                                                            div()
                                                                .text_size(px(14.0))
                                                                .font_weight(FontWeight::MEDIUM)
                                                                .text_color(theme.tokens.foreground)
                                                                .child("Email Address")
                                                        )
                                                        .child(
                                                            Input::new(&self.email)
                                                                .input_type(InputType::Email)
                                                                .placeholder("john.doe@example.com")
                                                                .variant(InputVariant::Outline)
                                                        )
                                                )
                                                // Phone
                                                .child(
                                                    VStack::new()
                                                        .w_full()
                                                        .gap(px(4.0))
                                                        .child(
                                                            div()
                                                                .text_size(px(14.0))
                                                                .font_weight(FontWeight::MEDIUM)
                                                                .text_color(theme.tokens.foreground)
                                                                .child("Phone Number")
                                                        )
                                                        .child(
                                                            Input::new(&self.phone)
                                                                .input_type(InputType::Tel)
                                                                .placeholder("(555) 123-4567")
                                                                .variant(InputVariant::Outline)
                                                        )
                                                )
                                                // Password
                                                .child(
                                                    VStack::new()
                                                        .w_full()
                                                        .gap(px(4.0))
                                                        .child(
                                                            div()
                                                                .text_size(px(14.0))
                                                                .font_weight(FontWeight::MEDIUM)
                                                                .text_color(theme.tokens.foreground)
                                                                .child("Password")
                                                        )
                                                        .child(
                                                            Input::new(&self.password)
                                                                .input_type(InputType::Password)
                                                                .placeholder("Enter password")
                                                                .variant(InputVariant::Outline)
                                                        )
                                                )
                                        )
                                        // Status indicators
                                        .child(
                                            VStack::new()
                                                .w_full()
                                                .max_w(px(500.0))
                                                .gap(px(12.0))
                                                .p(px(16.0))
                                                .bg(theme.tokens.muted.opacity(0.3))
                                                .border_1()
                                                .border_color(theme.tokens.border)
                                                .rounded(theme.tokens.radius_lg)
                                                .child(
                                                    div()
                                                        .text_size(px(14.0))
                                                        .font_weight(FontWeight::SEMIBOLD)
                                                        .text_color(theme.tokens.foreground)
                                                        .child("Focus Status")
                                                )
                                                .child(
                                                    VStack::new()
                                                        .gap(px(6.0))
                                                        .child(
                                                            HStack::new()
                                                                .gap(px(8.0))
                                                                .child(
                                                                    div()
                                                                        .w(px(8.0))
                                                                        .h(px(8.0))
                                                                        .rounded(px(4.0))
                                                                        .bg(if self.first_name.read(cx).focus_handle(cx).is_focused(window) {
                                                                            theme.tokens.primary
                                                                        } else {
                                                                            theme.tokens.muted
                                                                        })
                                                                )
                                                                .child(
                                                                    div()
                                                                        .text_size(px(13.0))
                                                                        .text_color(theme.tokens.muted_foreground)
                                                                        .child("First Name")
                                                                )
                                                        )
                                                        .child(
                                                            HStack::new()
                                                                .gap(px(8.0))
                                                                .child(
                                                                    div()
                                                                        .w(px(8.0))
                                                                        .h(px(8.0))
                                                                        .rounded(px(4.0))
                                                                        .bg(if self.last_name.read(cx).focus_handle(cx).is_focused(window) {
                                                                            theme.tokens.primary
                                                                        } else {
                                                                            theme.tokens.muted
                                                                        })
                                                                )
                                                                .child(
                                                                    div()
                                                                        .text_size(px(13.0))
                                                                        .text_color(theme.tokens.muted_foreground)
                                                                        .child("Last Name")
                                                                )
                                                        )
                                                        .child(
                                                            HStack::new()
                                                                .gap(px(8.0))
                                                                .child(
                                                                    div()
                                                                        .w(px(8.0))
                                                                        .h(px(8.0))
                                                                        .rounded(px(4.0))
                                                                        .bg(if self.email.read(cx).focus_handle(cx).is_focused(window) {
                                                                            theme.tokens.primary
                                                                        } else {
                                                                            theme.tokens.muted
                                                                        })
                                                                )
                                                                .child(
                                                                    div()
                                                                        .text_size(px(13.0))
                                                                        .text_color(theme.tokens.muted_foreground)
                                                                        .child("Email")
                                                                )
                                                        )
                                                        .child(
                                                            HStack::new()
                                                                .gap(px(8.0))
                                                                .child(
                                                                    div()
                                                                        .w(px(8.0))
                                                                        .h(px(8.0))
                                                                        .rounded(px(4.0))
                                                                        .bg(if self.phone.read(cx).focus_handle(cx).is_focused(window) {
                                                                            theme.tokens.primary
                                                                        } else {
                                                                            theme.tokens.muted
                                                                        })
                                                                )
                                                                .child(
                                                                    div()
                                                                        .text_size(px(13.0))
                                                                        .text_color(theme.tokens.muted_foreground)
                                                                        .child("Phone")
                                                                )
                                                        )
                                                        .child(
                                                            HStack::new()
                                                                .gap(px(8.0))
                                                                .child(
                                                                    div()
                                                                        .w(px(8.0))
                                                                        .h(px(8.0))
                                                                        .rounded(px(4.0))
                                                                        .bg(if self.password.read(cx).focus_handle(cx).is_focused(window) {
                                                                            theme.tokens.primary
                                                                        } else {
                                                                            theme.tokens.muted
                                                                        })
                                                                )
                                                                .child(
                                                                    div()
                                                                        .text_size(px(13.0))
                                                                        .text_color(theme.tokens.muted_foreground)
                                                                        .child("Password")
                                                                )
                                                        )
                                                )
                                        )
                                ),
                            ),
                    ),
            )
    }
}

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(800.0), px(700.0)),
                    cx,
                ))),
                titlebar: Some(TitlebarOptions {
                    title: Some("Input Focus Management Demo".into()),
                    ..Default::default()
                }),
                ..Default::default()
            },
            |_window, cx| cx.new(|cx| FocusTestApp::new(cx)),
        )
        .unwrap();
    });
}