openkit 0.1.3

A cross-platform CSS-styled UI framework for Rust
Documentation
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Dropdown/Select widget for choosing from a list of options.

use super::{Widget, WidgetBase, WidgetId, LayoutContext, PaintContext, EventContext};
use crate::css::{ClassList, WidgetState};
use crate::event::{Event, EventResult, MouseEventKind, Key, KeyEventKind};
use crate::geometry::{BorderRadius, Point, Rect, Size};
use crate::layout::{Constraints, LayoutResult};
use crate::render::Painter;

/// An option in a dropdown.
#[derive(Debug, Clone)]
pub struct DropdownOption {
    /// Unique value/key
    pub value: String,
    /// Display label
    pub label: String,
    /// Optional icon name
    pub icon: Option<String>,
    /// Whether this option is disabled
    pub disabled: bool,
}

impl DropdownOption {
    /// Create a new option.
    pub fn new(value: impl Into<String>, label: impl Into<String>) -> Self {
        Self {
            value: value.into(),
            label: label.into(),
            icon: None,
            disabled: false,
        }
    }

    /// Set an icon.
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

/// A dropdown select widget.
///
/// # Example
///
/// ```rust,ignore
/// use openkit::prelude::*;
///
/// let dropdown = Dropdown::new()
///     .placeholder("Select a session...")
///     .options(vec![
///         DropdownOption::new("gnome", "GNOME"),
///         DropdownOption::new("plasma", "KDE Plasma"),
///         DropdownOption::new("xfce", "Xfce"),
///     ])
///     .on_change(|value| {
///         println!("Selected: {}", value);
///     });
/// ```
#[allow(clippy::type_complexity)]
pub struct Dropdown {
    base: WidgetBase,
    options: Vec<DropdownOption>,
    selected_value: Option<String>,
    placeholder: String,
    is_open: bool,
    hovered_index: Option<usize>,
    on_change: Option<Box<dyn Fn(&str) + Send + Sync>>,
}

impl Dropdown {
    /// Create a new dropdown.
    pub fn new() -> Self {
        Self {
            base: WidgetBase::new().with_class("dropdown"),
            options: Vec::new(),
            selected_value: None,
            placeholder: "Select...".to_string(),
            is_open: false,
            hovered_index: None,
            on_change: None,
        }
    }

    /// Set the options.
    pub fn options(mut self, options: Vec<DropdownOption>) -> Self {
        self.options = options;
        self
    }

    /// Add a single option.
    pub fn option(mut self, option: DropdownOption) -> Self {
        self.options.push(option);
        self
    }

    /// Set the placeholder text.
    pub fn placeholder(mut self, text: impl Into<String>) -> Self {
        self.placeholder = text.into();
        self
    }

    /// Set the initially selected value.
    pub fn selected(mut self, value: impl Into<String>) -> Self {
        self.selected_value = Some(value.into());
        self
    }

    /// Set the change handler.
    pub fn on_change<F>(mut self, handler: F) -> Self
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        self.on_change = Some(Box::new(handler));
        self
    }

    /// Add a CSS class.
    pub fn class(mut self, class: &str) -> Self {
        self.base.classes.add(class);
        self
    }

    /// Get the currently selected value.
    pub fn value(&self) -> Option<&str> {
        self.selected_value.as_deref()
    }

    /// Get the selected option.
    pub fn selected_option(&self) -> Option<&DropdownOption> {
        self.selected_value.as_ref().and_then(|v| {
            self.options.iter().find(|o| &o.value == v)
        })
    }

    /// Open the dropdown.
    pub fn open(&mut self) {
        self.is_open = true;
    }

    /// Close the dropdown.
    pub fn close(&mut self) {
        self.is_open = false;
        self.hovered_index = None;
    }

    /// Toggle the dropdown.
    pub fn toggle(&mut self) {
        if self.is_open {
            self.close();
        } else {
            self.open();
        }
    }

    /// Select an option by value.
    pub fn select(&mut self, value: &str) {
        let found = self.options.iter()
            .find(|o| o.value == value && !o.disabled)
            .map(|o| o.value.clone());

        if let Some(selected_value) = found {
            self.selected_value = Some(selected_value.clone());
            self.close();
            if let Some(handler) = &self.on_change {
                handler(&selected_value);
            }
        }
    }

    fn option_height() -> f32 {
        36.0
    }

    fn dropdown_max_height(&self) -> f32 {
        // Show at most 6 items
        (self.options.len().min(6) as f32) * Self::option_height()
    }
}

impl Default for Dropdown {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for Dropdown {
    fn id(&self) -> WidgetId {
        self.base.id
    }

    fn type_name(&self) -> &'static str {
        "dropdown"
    }

    fn element_id(&self) -> Option<&str> {
        self.base.element_id.as_deref()
    }

    fn classes(&self) -> &ClassList {
        &self.base.classes
    }

    fn state(&self) -> WidgetState {
        self.base.state
    }

    fn intrinsic_size(&self, _ctx: &LayoutContext) -> Size {
        Size::new(200.0, 40.0)
    }

    fn layout(&mut self, constraints: Constraints, ctx: &LayoutContext) -> LayoutResult {
        let intrinsic = self.intrinsic_size(ctx);
        let size = Size::new(
            constraints.max_width.min(intrinsic.width.max(constraints.min_width)),
            intrinsic.height,
        );
        self.base.bounds.size = size;
        LayoutResult::new(size)
    }

    fn paint(&self, painter: &mut Painter, rect: Rect, ctx: &PaintContext) {
        let theme = ctx.style_ctx.theme;
        let radius = BorderRadius::all(theme.radii.md * theme.typography.base_size);

        // Main button background
        let bg_color = if self.base.state.disabled {
            theme.colors.muted
        } else if self.is_open || self.base.state.pressed {
            theme.colors.accent
        } else if self.base.state.hovered {
            theme.colors.secondary
        } else {
            theme.colors.background
        };
        painter.fill_rounded_rect(rect, bg_color, radius);

        // Border
        let border_color = if self.base.state.focused || self.is_open {
            theme.colors.ring
        } else {
            theme.colors.border
        };
        painter.stroke_rect(rect, border_color, 1.0);

        // Selected text or placeholder
        let font_size = 14.0;
        let padding = 12.0;
        let text_x = rect.x() + padding;
        let text_y = rect.y() + (rect.height() + font_size * 0.8) / 2.0;

        let (text, text_color) = if let Some(option) = self.selected_option() {
            (&option.label, theme.colors.foreground)
        } else {
            (&self.placeholder, theme.colors.muted_foreground)
        };

        painter.draw_text(text, Point::new(text_x, text_y), text_color, font_size);

        // Chevron icon
        let chevron_x = rect.x() + rect.width() - 24.0;
        let chevron_y = rect.y() + rect.height() / 2.0;

        // Draw simple chevron (â–¼ or â–²)
        let chevron = if self.is_open { "â–²" } else { "â–¼" };
        painter.draw_text(
            chevron,
            Point::new(chevron_x, chevron_y + 4.0),
            theme.colors.muted_foreground,
            12.0,
        );

        // Draw dropdown menu if open
        if self.is_open && !self.options.is_empty() {
            let menu_y = rect.y() + rect.height() + 4.0;
            let menu_height = self.dropdown_max_height();
            let menu_rect = Rect::new(rect.x(), menu_y, rect.width(), menu_height);

            // Menu background
            painter.fill_rounded_rect(menu_rect, theme.colors.popover, radius);
            painter.stroke_rect(menu_rect, theme.colors.border, 1.0);

            // Draw options
            let option_height = Self::option_height();
            for (i, option) in self.options.iter().enumerate() {
                let option_y = menu_y + (i as f32) * option_height;
                let option_rect = Rect::new(rect.x(), option_y, rect.width(), option_height);

                // Highlight hovered option
                let is_selected = self.selected_value.as_ref() == Some(&option.value);
                let is_hovered = self.hovered_index == Some(i);

                if is_selected {
                    painter.fill_rect(option_rect, theme.colors.accent);
                } else if is_hovered && !option.disabled {
                    painter.fill_rect(option_rect, theme.colors.accent.with_alpha(0.5));
                }

                // Option text
                let text_color = if option.disabled {
                    theme.colors.muted_foreground
                } else if is_selected {
                    theme.colors.accent_foreground
                } else {
                    theme.colors.popover_foreground
                };

                painter.draw_text(
                    &option.label,
                    Point::new(rect.x() + padding, option_y + (option_height + font_size * 0.8) / 2.0),
                    text_color,
                    font_size,
                );

                // Check mark for selected
                if is_selected {
                    painter.draw_text(
                        "✓",
                        Point::new(rect.x() + rect.width() - 28.0, option_y + (option_height + font_size * 0.8) / 2.0),
                        theme.colors.accent_foreground,
                        font_size,
                    );
                }
            }
        }

        // Focus ring
        if self.base.state.focused && ctx.focus_visible && !self.is_open {
            let ring_rect = Rect::new(
                rect.x() - 2.0,
                rect.y() - 2.0,
                rect.width() + 4.0,
                rect.height() + 4.0,
            );
            painter.stroke_rect(ring_rect, theme.colors.ring, 2.0);
        }
    }

    fn handle_event(&mut self, event: &Event, ctx: &mut EventContext) -> EventResult {
        match event {
            Event::Mouse(mouse) => {
                let in_button = self.bounds().contains(mouse.position);

                // Check if in dropdown menu
                let in_menu = if self.is_open {
                    let menu_y = self.bounds().y() + self.bounds().height() + 4.0;
                    let menu_rect = Rect::new(
                        self.bounds().x(),
                        menu_y,
                        self.bounds().width(),
                        self.dropdown_max_height(),
                    );
                    menu_rect.contains(mouse.position)
                } else {
                    false
                };

                match mouse.kind {
                    MouseEventKind::Move | MouseEventKind::Enter => {
                        if in_button && !self.base.state.hovered {
                            self.base.state.hovered = true;
                            ctx.request_redraw();
                        } else if !in_button && !in_menu && self.base.state.hovered {
                            self.base.state.hovered = false;
                            ctx.request_redraw();
                        }

                        // Track hovered option in menu
                        if in_menu {
                            let menu_y = self.bounds().y() + self.bounds().height() + 4.0;
                            let relative_y = mouse.position.y - menu_y;
                            let index = (relative_y / Self::option_height()) as usize;
                            if index < self.options.len() && self.hovered_index != Some(index) {
                                self.hovered_index = Some(index);
                                ctx.request_redraw();
                            }
                        } else if self.hovered_index.is_some() {
                            self.hovered_index = None;
                            ctx.request_redraw();
                        }
                    }
                    MouseEventKind::Leave => {
                        self.base.state.hovered = false;
                        self.hovered_index = None;
                        ctx.request_redraw();
                    }
                    MouseEventKind::Down if in_button => {
                        self.base.state.pressed = true;
                        ctx.request_focus(self.base.id);
                        ctx.request_redraw();
                        return EventResult::Handled;
                    }
                    MouseEventKind::Up => {
                        if self.base.state.pressed && in_button {
                            self.base.state.pressed = false;
                            self.toggle();
                            ctx.request_redraw();
                            return EventResult::Handled;
                        } else if in_menu {
                            // Select clicked option
                            let menu_y = self.bounds().y() + self.bounds().height() + 4.0;
                            let relative_y = mouse.position.y - menu_y;
                            let index = (relative_y / Self::option_height()) as usize;
                            if index < self.options.len() {
                                let option = &self.options[index];
                                if !option.disabled {
                                    self.select(&option.value.clone());
                                    ctx.request_redraw();
                                    return EventResult::Handled;
                                }
                            }
                        } else if self.is_open {
                            // Click outside closes dropdown
                            self.close();
                            ctx.request_redraw();
                        }
                    }
                    _ => {}
                }
            }
            Event::Key(key) if self.base.state.focused => {
                if key.kind == KeyEventKind::Down {
                    match key.key {
                        Key::Enter | Key::Space => {
                            if self.is_open {
                                if let Some(idx) = self.hovered_index {
                                    if idx < self.options.len() {
                                        let value = self.options[idx].value.clone();
                                        self.select(&value);
                                    }
                                }
                            } else {
                                self.open();
                            }
                            ctx.request_redraw();
                            return EventResult::Handled;
                        }
                        Key::Escape => {
                            self.close();
                            ctx.request_redraw();
                            return EventResult::Handled;
                        }
                        Key::Up => {
                            if self.is_open {
                                let current = self.hovered_index.unwrap_or(0);
                                if current > 0 {
                                    self.hovered_index = Some(current - 1);
                                    ctx.request_redraw();
                                }
                            }
                            return EventResult::Handled;
                        }
                        Key::Down => {
                            if self.is_open {
                                let current = self.hovered_index.unwrap_or(0);
                                if current + 1 < self.options.len() {
                                    self.hovered_index = Some(current + 1);
                                    ctx.request_redraw();
                                }
                            } else {
                                self.open();
                                self.hovered_index = Some(0);
                                ctx.request_redraw();
                            }
                            return EventResult::Handled;
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
        EventResult::Ignored
    }

    fn bounds(&self) -> Rect {
        self.base.bounds
    }

    fn set_bounds(&mut self, bounds: Rect) {
        self.base.bounds = bounds;
    }
}