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
//! Action Center / Quick Settings widget
//!
//! Windows 10/11-style action center with quick toggles and notifications.

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

/// Quick action toggle
#[derive(Debug, Clone)]
pub struct QuickAction {
    pub id: String,
    pub name: String,
    pub icon: String,
    pub is_active: bool,
    pub is_available: bool,
}

impl QuickAction {
    pub fn new(id: impl Into<String>, name: impl Into<String>, icon: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            icon: icon.into(),
            is_active: false,
            is_available: true,
        }
    }

    pub fn active(mut self, active: bool) -> Self {
        self.is_active = active;
        self
    }

    pub fn available(mut self, available: bool) -> Self {
        self.is_available = available;
        self
    }
}

/// Notification priority
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NotificationPriority {
    Low,
    #[default]
    Normal,
    High,
    Critical,
}

/// A notification in the action center
#[derive(Debug, Clone)]
pub struct ActionNotification {
    pub id: String,
    pub app_name: String,
    pub app_icon: Option<String>,
    pub title: String,
    pub body: Option<String>,
    pub timestamp: u64,
    pub priority: NotificationPriority,
    pub is_read: bool,
    pub actions: Vec<(String, String)>,
}

impl ActionNotification {
    pub fn new(id: impl Into<String>, app: impl Into<String>, title: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            app_name: app.into(),
            app_icon: None,
            title: title.into(),
            body: None,
            timestamp: 0,
            priority: NotificationPriority::Normal,
            is_read: false,
            actions: Vec::new(),
        }
    }

    pub fn body(mut self, text: impl Into<String>) -> Self {
        self.body = Some(text.into());
        self
    }

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

    pub fn priority(mut self, priority: NotificationPriority) -> Self {
        self.priority = priority;
        self
    }

    pub fn action(mut self, id: impl Into<String>, label: impl Into<String>) -> Self {
        self.actions.push((id.into(), label.into()));
        self
    }
}

/// Action center widget
pub struct ActionCenter {
    base: WidgetBase,
    quick_actions: Vec<QuickAction>,
    notifications: Vec<ActionNotification>,
    is_open: bool,
    current_section: usize,
    width: f32,
    height: f32,
    brightness: f32,
    show_brightness: bool,
    do_not_disturb: bool,
    #[allow(clippy::type_complexity)]
    on_action_toggle: Option<Box<dyn Fn(&str, bool) + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    on_notification_action: Option<Box<dyn Fn(&str, &str) + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    on_notification_dismiss: Option<Box<dyn Fn(&str) + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    on_clear_all: Option<Box<dyn Fn() + Send + Sync>>,
    #[allow(clippy::type_complexity)]
    on_brightness_change: Option<Box<dyn Fn(f32) + Send + Sync>>,
}

impl ActionCenter {
    pub fn new() -> Self {
        Self {
            base: WidgetBase::new().with_class("action-center"),
            quick_actions: vec![
                QuickAction::new("wifi", "Wi-Fi", "📶"),
                QuickAction::new("bluetooth", "Bluetooth", "🔵"),
                QuickAction::new("airplane", "Airplane Mode", "✈️"),
                QuickAction::new("battery_saver", "Battery Saver", "🔋"),
                QuickAction::new("night_light", "Night Light", "🌙"),
                QuickAction::new("focus", "Focus Assist", "🎯"),
            ],
            notifications: Vec::new(),
            is_open: false,
            current_section: 0,
            width: 380.0,
            height: 600.0,
            brightness: 0.75,
            show_brightness: true,
            do_not_disturb: false,
            on_action_toggle: None,
            on_notification_action: None,
            on_notification_dismiss: None,
            on_clear_all: None,
            on_brightness_change: None,
        }
    }

    pub fn quick_actions(mut self, actions: Vec<QuickAction>) -> Self {
        self.quick_actions = actions;
        self
    }

    pub fn action(mut self, action: QuickAction) -> Self {
        self.quick_actions.push(action);
        self
    }

    pub fn notifications(mut self, notifications: Vec<ActionNotification>) -> Self {
        self.notifications = notifications;
        self
    }

    pub fn notification(mut self, notification: ActionNotification) -> Self {
        self.notifications.push(notification);
        self
    }

    pub fn size(mut self, width: f32, height: f32) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    pub fn show_brightness(mut self, show: bool) -> Self {
        self.show_brightness = show;
        self
    }

    pub fn brightness(mut self, level: f32) -> Self {
        self.brightness = level.clamp(0.0, 1.0);
        self
    }

    pub fn do_not_disturb(mut self, enabled: bool) -> Self {
        self.do_not_disturb = enabled;
        self
    }

    pub fn on_action_toggle<F>(mut self, f: F) -> Self
    where
        F: Fn(&str, bool) + Send + Sync + 'static,
    {
        self.on_action_toggle = Some(Box::new(f));
        self
    }

    pub fn on_notification_action<F>(mut self, f: F) -> Self
    where
        F: Fn(&str, &str) + Send + Sync + 'static,
    {
        self.on_notification_action = Some(Box::new(f));
        self
    }

    pub fn on_notification_dismiss<F>(mut self, f: F) -> Self
    where
        F: Fn(&str) + Send + Sync + 'static,
    {
        self.on_notification_dismiss = Some(Box::new(f));
        self
    }

    pub fn on_clear_all<F>(mut self, f: F) -> Self
    where
        F: Fn() + Send + Sync + 'static,
    {
        self.on_clear_all = Some(Box::new(f));
        self
    }

    pub fn on_brightness_change<F>(mut self, f: F) -> Self
    where
        F: Fn(f32) + Send + Sync + 'static,
    {
        self.on_brightness_change = Some(Box::new(f));
        self
    }

    pub fn open(&mut self) {
        self.is_open = true;
    }

    pub fn close(&mut self) {
        self.is_open = false;
    }

    pub fn toggle(&mut self) {
        self.is_open = !self.is_open;
    }

    pub fn is_open(&self) -> bool {
        self.is_open
    }

    pub fn add_notification(&mut self, notification: ActionNotification) {
        self.notifications.insert(0, notification);
    }

    pub fn remove_notification(&mut self, id: &str) {
        self.notifications.retain(|n| n.id != id);
    }

    pub fn clear_notifications(&mut self) {
        self.notifications.clear();
    }

    pub fn unread_count(&self) -> usize {
        self.notifications.iter().filter(|n| !n.is_read).count()
    }

    pub fn toggle_action(&mut self, id: &str) {
        if let Some(action) = self.quick_actions.iter_mut().find(|a| a.id == id) {
            action.is_active = !action.is_active;
            let active = action.is_active;
            if let Some(ref cb) = self.on_action_toggle {
                cb(id, active);
            }
        }
    }

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

    pub fn id(mut self, id: &str) -> Self {
        self.base.element_id = Some(id.to_string());
        self
    }
}

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

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

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

    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 {
        if !self.is_open {
            Size::ZERO
        } else {
            Size::new(self.width, self.height)
        }
    }

    fn layout(&mut self, constraints: Constraints, ctx: &LayoutContext) -> LayoutResult {
        let size = constraints.constrain(self.intrinsic_size(ctx));
        self.base.bounds.size = size;
        LayoutResult::new(size)
    }

    fn paint(&self, painter: &mut Painter, rect: Rect, _ctx: &PaintContext) {
        if !self.is_open {
            return;
        }

        // Background
        painter.fill_rounded_rect(rect, Color::rgba(0.1, 0.1, 0.1, 0.95), BorderRadius::all(8.0));

        // Header
        let header_rect = Rect::new(rect.x(), rect.y(), rect.width(), 48.0);
        painter.fill_rect(header_rect, Color::rgba(1.0, 1.0, 1.0, 0.05));

        // Header title
        painter.draw_text(
            "Quick actions",
            Point::new(rect.x() + 16.0, rect.y() + 30.0),
            Color::WHITE,
            14.0,
        );

        // Quick actions grid
        let grid_top = rect.y() + 56.0;
        let tile_size = 80.0;
        let tiles_per_row = 4;
        let padding = 8.0;

        for (i, action) in self.quick_actions.iter().enumerate() {
            let row = i / tiles_per_row;
            let col = i % tiles_per_row;

            let tile_rect = Rect::new(
                rect.x() + padding + (col as f32 * (tile_size + padding)),
                grid_top + (row as f32 * (tile_size + padding)),
                tile_size,
                tile_size,
            );

            let bg_color = if action.is_active {
                Color::rgba(0.0, 0.47, 0.84, 0.8)
            } else {
                Color::rgba(1.0, 1.0, 1.0, 0.1)
            };
            painter.fill_rounded_rect(tile_rect, bg_color, BorderRadius::all(4.0));

            // Icon
            painter.draw_text(
                &action.icon,
                Point::new(tile_rect.x() + tile_size / 2.0 - 10.0, tile_rect.y() + 36.0),
                Color::WHITE,
                20.0,
            );

            // Label
            painter.draw_text(
                &action.name,
                Point::new(tile_rect.x() + 4.0, tile_rect.y() + tile_size - 8.0),
                Color::WHITE,
                10.0,
            );
        }

        // Brightness slider
        if self.show_brightness {
            let rows = (self.quick_actions.len() as f32 / tiles_per_row as f32).ceil();
            let slider_top = grid_top + (rows * (tile_size + padding)) + 16.0;

            painter.draw_text("☀️", Point::new(rect.x() + 16.0, slider_top + 4.0), Color::WHITE, 16.0);

            let track_rect = Rect::new(rect.x() + 48.0, slider_top, rect.width() - 64.0, 4.0);
            painter.fill_rounded_rect(track_rect, Color::rgba(1.0, 1.0, 1.0, 0.2), BorderRadius::all(2.0));

            let fill_width = (rect.width() - 64.0) * self.brightness;
            let fill_rect = Rect::new(rect.x() + 48.0, slider_top, fill_width, 4.0);
            painter.fill_rounded_rect(fill_rect, Color::rgb(1.0, 0.8, 0.2), BorderRadius::all(2.0));
        }
    }

    fn handle_event(&mut self, _event: &Event, _ctx: &mut EventContext) -> EventResult {
        EventResult::Ignored
    }

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

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