Skip to main content

nomad_common/
types.rs

1use serde::{Deserialize, Serialize};
2
3// ── Viewport ──
4
5#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
6pub struct Viewport {
7    pub x: f32,
8    pub y: f32,
9    pub width: f32,
10    pub height: f32,
11}
12
13// ── Style bitfield ──
14
15#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
16pub struct Style(pub u64);
17
18impl Style {
19    pub const fn text_color_idx(self) -> u8 {
20        (self.0 & 0xFF) as u8
21    }
22    pub const fn bg_color_idx(self) -> u8 {
23        ((self.0 >> 8) & 0xFF) as u8
24    }
25    pub const fn font_size(self) -> u8 {
26        ((self.0 >> 16) & 0xFF) as u8
27    }
28    pub const fn text_align(self) -> u8 {
29        ((self.0 >> 24) & 0x3F) as u8
30    }
31    pub const fn display_type(self) -> u8 {
32        ((self.0 >> 30) & 0x3F) as u8
33    }
34    pub const fn position_type(self) -> u8 {
35        ((self.0 >> 36) & 0x3F) as u8
36    }
37    /// Role as u8 index (0 = no role). Bits 42-49.
38    pub const fn role_idx(self) -> u8 {
39        ((self.0 >> 42) & 0xFF) as u8
40    }
41    /// Build Style with a role index.
42    pub const fn with_role(self, role: u8) -> Self {
43        Style((self.0 & !(0xFF << 42)) | ((role as u64) << 42))
44    }
45    pub const fn flags(self) -> u8 {
46        ((self.0 >> 56) & 0xFF) as u8
47    }
48
49    pub const fn is_link(self) -> bool {
50        (self.flags() & 1) != 0
51    }
52    pub const fn is_image(self) -> bool {
53        (self.flags() & 2) != 0
54    }
55    pub const fn is_input(self) -> bool {
56        (self.flags() & 4) != 0
57    }
58    pub const fn is_clickable(self) -> bool {
59        (self.flags() & 8) != 0
60    }
61}
62
63// ── Display / Position constants ──
64
65pub mod display {
66    pub const BLOCK: u8 = 0;
67    pub const INLINE: u8 = 1;
68    pub const INLINE_BLOCK: u8 = 2;
69    pub const NONE: u8 = 3;
70}
71
72pub mod position {
73    pub const STATIC: u8 = 0;
74    pub const RELATIVE: u8 = 1;
75    pub const ABSOLUTE: u8 = 2;
76    pub const FIXED: u8 = 3;
77}
78
79// ── State tags for delta compression ──
80
81#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
82pub struct StateTag(pub u8);
83
84impl StateTag {
85    pub const STATIC: StateTag  = StateTag(0);
86    pub const MOVED: StateTag   = StateTag(1);
87    pub const CHANGED: StateTag = StateTag(2);
88    pub const REMOVED: StateTag = StateTag(3);
89}
90
91// ── Default palette indices ──
92
93pub const PALETTE_TEXT_DEFAULT: u8 = 0;
94pub const PALETTE_BG_DEFAULT: u8 = 1;
95pub const PALETTE_LINK: u8 = 2;
96pub const PALETTE_CHROME_BG: u8 = 3;
97pub const PALETTE_URL_BAR_BG: u8 = 4;
98pub const PALETTE_ACTIVE_TAB: u8 = 5;
99pub const PALETTE_INACTIVE_TAB: u8 = 6;
100pub const PALETTE_SEPARATOR: u8 = 7;
101
102pub const PALETTE: &[u32; 16] = &[
103    0x000000, // TEXT_DEFAULT
104    0xFFFFFF, // BG_DEFAULT
105    0x0000FF, // LINK
106    0xE0E0E0, // CHROME_BG
107    0xFFFFFF, // URL_BAR_BG
108    0xFFFFFF, // ACTIVE_TAB
109    0xD0D0D0, // INACTIVE_TAB
110    0xA0A0A0, // SEPARATOR
111    0x000000, // 8+
112    0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
113];
114
115pub fn palette_color(idx: u8) -> u32 {
116    PALETTE[(idx as usize).min(15)]
117}
118
119// ── Defaults ──
120
121pub const DEFAULT_FONT_SIZE: u8 = 16;