deweygui 1.0.0

An agentic-first GUI framework with pluggable rendering backends and complete ontology for AI agent discoverability
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Color and style system for GUI rendering.
//!
//! Provides [`Color`] (RGBA f32), [`Style`] (composable visual overrides),
//! [`Shadow`], [`TextStyle`], and alignment types.

use serde::{Deserialize, Serialize};

/// RGBA color with f32 components in \[0.0, 1.0\].
///
/// Use named constants (`Color::RED`, `Color::BLUE`), hex strings
/// ([`Color::hex`]), or 0-255 integers ([`Color::from_rgb8`]).
///
/// # Examples
///
/// ```
/// # use dewey::core::Color;
/// let red   = Color::RED;
/// let brand = Color::hex("#1A73E8");
/// let soft  = Color::from_rgb8(200, 180, 160);
/// let half  = red.with_alpha(0.5);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Default for Color {
    fn default() -> Self {
        Self::WHITE
    }
}

impl Color {
    pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
    pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
    pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
    pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
    pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
    pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
    pub const YELLOW: Self = Self::rgb(1.0, 1.0, 0.0);
    pub const CYAN: Self = Self::rgb(0.0, 1.0, 1.0);
    pub const MAGENTA: Self = Self::rgb(1.0, 0.0, 1.0);
    pub const GRAY: Self = Self::rgb(0.5, 0.5, 0.5);
    pub const DARK_GRAY: Self = Self::rgb(0.25, 0.25, 0.25);
    pub const LIGHT_GRAY: Self = Self::rgb(0.75, 0.75, 0.75);
    pub const ORANGE: Self = Self::rgb(1.0, 0.647, 0.0);
    pub const PURPLE: Self = Self::rgb(0.502, 0.0, 0.502);
    pub const PINK: Self = Self::rgb(1.0, 0.412, 0.706);
    pub const BROWN: Self = Self::rgb(0.647, 0.165, 0.165);
    pub const INDIGO: Self = Self::rgb(0.294, 0.0, 0.51);

    /// Parse a hex color string: `"#RRGGBB"` or `"#RRGGBBAA"`.
    ///
    /// Panics if the hex string is invalid. Use [`from_hex`](Self::from_hex)
    /// for fallible parsing.
    ///
    /// ```
    /// # use dewey::core::Color;
    /// let c = Color::hex("#1A2B3C");
    /// ```
    #[must_use]
    pub fn hex(hex: &str) -> Self {
        Self::from_hex(hex).expect("invalid hex color")
    }

    #[must_use]
    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
        Self { r, g, b, a: 1.0 }
    }

    #[must_use]
    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    /// Create from 0-255 integer components.
    #[must_use]
    pub fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
        Self::rgb(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0)
    }

    /// Create from 0-255 integer components with alpha.
    #[must_use]
    pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self::rgba(
            r as f32 / 255.0,
            g as f32 / 255.0,
            b as f32 / 255.0,
            a as f32 / 255.0,
        )
    }

    /// Parse hex color string: "#RRGGBB" or "#RRGGBBAA".
    #[must_use]
    pub fn from_hex(hex: &str) -> Option<Self> {
        let hex = hex.strip_prefix('#').unwrap_or(hex);
        match hex.len() {
            6 => {
                let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
                let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
                let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
                Some(Self::from_rgb8(r, g, b))
            }
            8 => {
                let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
                let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
                let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
                let a = u8::from_str_radix(&hex[6..8], 16).ok()?;
                Some(Self::from_rgba8(r, g, b, a))
            }
            _ => None,
        }
    }

    /// Linear interpolation between two colors.
    #[must_use]
    pub fn lerp(&self, other: &Color, t: f32) -> Color {
        let t = t.clamp(0.0, 1.0);
        Color {
            r: self.r + (other.r - self.r) * t,
            g: self.g + (other.g - self.g) * t,
            b: self.b + (other.b - self.b) * t,
            a: self.a + (other.a - self.a) * t,
        }
    }

    #[must_use]
    pub fn with_alpha(self, a: f32) -> Self {
        Self { a, ..self }
    }
}

#[cfg(feature = "egui-backend")]
impl From<Color> for egui::Color32 {
    fn from(c: Color) -> Self {
        egui::Color32::from_rgba_unmultiplied(
            (c.r * 255.0) as u8,
            (c.g * 255.0) as u8,
            (c.b * 255.0) as u8,
            (c.a * 255.0) as u8,
        )
    }
}

#[cfg(feature = "egui-backend")]
impl From<egui::Color32> for Color {
    fn from(c: egui::Color32) -> Self {
        let [r, g, b, a] = c.to_array();
        Self::from_rgba8(r, g, b, a)
    }
}

/// Font weight categories for text rendering.
///
/// Defaults to [`FontWeight::Regular`]. Use [`TextStyle::bold`] as a
/// shorthand for [`FontWeight::Bold`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum FontWeight {
    Thin,
    Light,
    #[default]
    Regular,
    Medium,
    SemiBold,
    Bold,
    ExtraBold,
}

/// Text style properties for font size, color, weight, and decorations.
///
/// Build with a chainable API:
///
/// ```
/// # use dewey::core::{TextStyle, FontWeight, Color};
/// let heading = TextStyle::new().size(24.0).bold().color(Color::WHITE);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TextStyle {
    pub font_size: f32,
    pub color: Color,
    pub weight: FontWeight,
    pub italic: bool,
    pub underline: bool,
    pub strikethrough: bool,
    pub line_height: Option<f32>,
    pub letter_spacing: f32,
}

impl Default for TextStyle {
    fn default() -> Self {
        Self {
            font_size: 14.0,
            color: Color::WHITE,
            weight: FontWeight::Regular,
            italic: false,
            underline: false,
            strikethrough: false,
            line_height: None,
            letter_spacing: 0.0,
        }
    }
}

impl TextStyle {
    /// Create a new text style with default values.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set font size.
    #[must_use]
    pub fn size(mut self, size: f32) -> Self {
        self.font_size = size;
        self
    }

    /// Set text color.
    #[must_use]
    pub fn color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    /// Set font weight.
    #[must_use]
    pub fn weight(mut self, weight: FontWeight) -> Self {
        self.weight = weight;
        self
    }

    /// Set bold weight.
    #[must_use]
    pub fn bold(mut self) -> Self {
        self.weight = FontWeight::Bold;
        self
    }

    /// Set italic.
    #[must_use]
    pub fn italic(mut self) -> Self {
        self.italic = true;
        self
    }
}

/// Visual style for a widget, composable with optional overrides.
///
/// Every field is `Option` — only set values take effect. Styles compose
/// via [`Style::merge`] (non-None fields in the overlay win).
///
/// # Examples
///
/// ```
/// # use dewey::core::{Style, Color};
/// let card = Style::new()
///     .bg(Color::DARK_GRAY)
///     .fg(Color::WHITE)
///     .rounded(12.0)
///     .text_size(16.0);
/// ```
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Style {
    pub foreground: Option<Color>,
    pub background: Option<Color>,
    pub border_color: Option<Color>,
    pub border_width: Option<f32>,
    pub border_radius: Option<f32>,
    pub padding: Option<super::rect::Margin>,
    pub text: Option<TextStyle>,
    pub opacity: Option<f32>,
    pub shadow: Option<Shadow>,
}

impl Style {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn fg(mut self, color: Color) -> Self {
        self.foreground = Some(color);
        self
    }

    #[must_use]
    pub fn bg(mut self, color: Color) -> Self {
        self.background = Some(color);
        self
    }

    #[must_use]
    pub fn border(mut self, color: Color, width: f32) -> Self {
        self.border_color = Some(color);
        self.border_width = Some(width);
        self
    }

    #[must_use]
    pub fn rounded(mut self, radius: f32) -> Self {
        self.border_radius = Some(radius);
        self
    }

    #[must_use]
    pub fn padding(mut self, margin: super::rect::Margin) -> Self {
        self.padding = Some(margin);
        self
    }

    #[must_use]
    pub fn opacity(mut self, opacity: f32) -> Self {
        self.opacity = Some(opacity.clamp(0.0, 1.0));
        self
    }

    #[must_use]
    pub fn shadow(mut self, shadow: Shadow) -> Self {
        self.shadow = Some(shadow);
        self
    }

    /// Set text font size (creates/updates embedded [`TextStyle`]).
    ///
    /// ```
    /// # use dewey::core::Style;
    /// let heading = Style::new().text_size(24.0);
    /// ```
    #[must_use]
    pub fn text_size(mut self, size: f32) -> Self {
        self.text.get_or_insert_with(TextStyle::default).font_size = size;
        self
    }

    /// Set text color (creates/updates embedded TextStyle).
    #[must_use]
    pub fn text_color(mut self, color: Color) -> Self {
        self.text.get_or_insert_with(TextStyle::default).color = color;
        self
    }

    /// Set bold text weight (creates/updates embedded TextStyle).
    #[must_use]
    pub fn bold(mut self) -> Self {
        self.text.get_or_insert_with(TextStyle::default).weight = FontWeight::Bold;
        self
    }

    /// Resolve the text style, merging with defaults.
    ///
    /// Returns a complete [`TextStyle`] by:
    /// 1. Starting from `self.text` (or [`TextStyle::default`] if unset).
    /// 2. Inheriting `self.foreground` as the text color when no explicit
    ///    text color override has been applied.
    #[must_use]
    pub fn resolved_text(&self) -> TextStyle {
        let mut ts = self.text.clone().unwrap_or_default();
        // If foreground is set but no explicit text color override, use foreground.
        if self.foreground.is_some() && self.text.as_ref().is_none_or(|t| t.color == Color::WHITE) {
            ts.color = self.foreground.unwrap_or(Color::WHITE);
        }
        ts
    }

    /// Merge another style on top. Non-None fields in `other` override `self`.
    ///
    /// ```
    /// # use dewey::core::{Style, Color};
    /// let base = Style::new().bg(Color::BLACK).fg(Color::WHITE);
    /// let highlight = Style::new().bg(Color::BLUE);
    /// let merged = base.merge(&highlight);
    /// assert_eq!(merged.background, Some(Color::BLUE));   // overridden
    /// assert_eq!(merged.foreground, Some(Color::WHITE));   // inherited
    /// ```
    #[must_use]
    pub fn merge(&self, other: &Style) -> Style {
        Style {
            foreground: other.foreground.or(self.foreground),
            background: other.background.or(self.background),
            border_color: other.border_color.or(self.border_color),
            border_width: other.border_width.or(self.border_width),
            border_radius: other.border_radius.or(self.border_radius),
            padding: other.padding.or(self.padding),
            text: other.text.clone().or(self.text.clone()),
            opacity: other.opacity.or(self.opacity),
            shadow: other.shadow.or(self.shadow),
        }
    }

    /// Resolve foreground color with fallback.
    #[must_use]
    pub fn resolved_fg(&self) -> Color {
        self.foreground.unwrap_or(Color::WHITE)
    }

    /// Resolve background color with fallback.
    #[must_use]
    pub fn resolved_bg(&self) -> Color {
        self.background.unwrap_or(Color::TRANSPARENT)
    }
}

/// Drop shadow specification for widgets.
///
/// ```
/// # use dewey::core::style::{Shadow, Color};
/// let drop = Shadow::new(2.0, 4.0, 8.0, Color::BLACK.with_alpha(0.3));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Shadow {
    pub offset_x: f32,
    pub offset_y: f32,
    pub blur: f32,
    pub spread: f32,
    pub color: Color,
}

impl Default for Shadow {
    fn default() -> Self {
        Self {
            offset_x: 0.0,
            offset_y: 0.0,
            blur: 0.0,
            spread: 0.0,
            color: Color::TRANSPARENT,
        }
    }
}

impl Shadow {
    #[must_use]
    pub fn new(offset_x: f32, offset_y: f32, blur: f32, color: Color) -> Self {
        Self {
            offset_x,
            offset_y,
            blur,
            spread: 0.0,
            color,
        }
    }
}

/// Text alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum Alignment {
    #[default]
    Start,
    Center,
    End,
}

/// Vertical alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum VerticalAlignment {
    #[default]
    Top,
    Center,
    Bottom,
}

/// Cursor style for the system cursor.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum CursorIcon {
    #[default]
    Default,
    Pointer,
    Text,
    Crosshair,
    Move,
    NotAllowed,
    ResizeNS,
    ResizeEW,
    ResizeNESW,
    ResizeNWSE,
    Grab,
    Grabbing,
    Wait,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn color_from_hex() {
        let c = Color::from_hex("#ff8800").unwrap();
        assert!((c.r - 1.0).abs() < 0.01);
        assert!((c.g - 0.533).abs() < 0.01);
        assert!((c.b - 0.0).abs() < 0.01);
    }

    #[test]
    fn color_lerp() {
        let a = Color::BLACK;
        let b = Color::WHITE;
        let mid = a.lerp(&b, 0.5);
        assert!((mid.r - 0.5).abs() < 0.01);
    }

    #[test]
    fn style_merge() {
        let base = Style::new().fg(Color::RED);
        let over = Style::new().bg(Color::BLUE);
        let merged = base.merge(&over);
        assert_eq!(merged.foreground, Some(Color::RED));
        assert_eq!(merged.background, Some(Color::BLUE));
    }
}