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
//! Container widgets (Column, Row).

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

/// A vertical stack container (Column).
pub struct Column {
    base: WidgetBase,
    children: Vec<Box<dyn Widget>>,
    gap: f32,
    align: Alignment,
    justify: Alignment,
    padding: EdgeInsets,
    child_positions: Vec<Point>,
}

impl Column {
    pub fn new() -> Self {
        Self {
            base: WidgetBase::new().with_class("column"),
            children: Vec::new(),
            gap: 0.0,
            align: Alignment::Stretch,
            justify: Alignment::Start,
            padding: EdgeInsets::ZERO,
            child_positions: Vec::new(),
        }
    }

    /// Add a child widget.
    pub fn child(mut self, widget: impl Widget + 'static) -> Self {
        self.children.push(Box::new(widget));
        self
    }

    /// Set the gap between children.
    pub fn gap(mut self, gap: f32) -> Self {
        self.gap = gap;
        self
    }

    /// Set cross-axis alignment.
    pub fn align(mut self, align: Alignment) -> Self {
        self.align = align;
        self
    }

    /// Set main-axis justification.
    pub fn justify(mut self, justify: Alignment) -> Self {
        self.justify = justify;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<EdgeInsets>) -> Self {
        self.padding = padding.into();
        self
    }

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

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

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

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

    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 {
        let mut width: f32 = 0.0;
        let mut height: f32 = 0.0;

        for child in &self.children {
            let child_size = child.intrinsic_size(ctx);
            width = width.max(child_size.width);
            height += child_size.height;
        }

        // Add gaps
        if self.children.len() > 1 {
            height += self.gap * (self.children.len() - 1) as f32;
        }

        // Add padding
        Size::new(
            width + self.padding.horizontal(),
            height + self.padding.vertical(),
        )
    }

    fn layout(&mut self, constraints: Constraints, ctx: &LayoutContext) -> LayoutResult {
        // First pass: get intrinsic sizes
        let child_sizes: Vec<Size> = self.children
            .iter()
            .map(|c| c.intrinsic_size(ctx))
            .collect();

        // Calculate total content size
        let total_height: f32 = child_sizes.iter().map(|s| s.height).sum::<f32>()
            + self.gap * (self.children.len().saturating_sub(1)) as f32;
        let max_width = child_sizes.iter().map(|s| s.width).fold(0.0_f32, |a, b| a.max(b));

        // Container size
        let container_size = constraints.constrain(Size::new(
            max_width + self.padding.horizontal(),
            total_height + self.padding.vertical(),
        ));

        // Calculate positions using flex layout
        let flex = FlexLayout {
            direction: FlexDirection::Column,
            justify: self.justify,
            align: self.align,
            gap: self.gap,
        };

        self.child_positions = flex.calculate_positions(container_size, &child_sizes, self.padding);

        // Second pass: layout children with their positions
        for (i, child) in self.children.iter_mut().enumerate() {
            let child_size = child_sizes[i];
            let available_width = match self.align {
                Alignment::Stretch => container_size.width - self.padding.horizontal(),
                _ => child_size.width,
            };

            let child_constraints = Constraints::new(
                0.0,
                available_width,
                0.0,
                child_size.height,
            );
            child.layout(child_constraints, ctx);

            if let Some(pos) = self.child_positions.get(i) {
                child.set_bounds(Rect::from_origin_size(*pos, child.bounds().size));
            }
        }

        self.base.bounds.size = container_size;
        LayoutResult::new(container_size)
    }

    fn paint(&self, painter: &mut Painter, rect: Rect, ctx: &PaintContext) {
        // Paint children
        for (i, child) in self.children.iter().enumerate() {
            if let Some(pos) = self.child_positions.get(i) {
                let child_rect = Rect::from_origin_size(
                    Point::new(rect.x() + pos.x, rect.y() + pos.y),
                    child.bounds().size,
                );
                child.paint(painter, child_rect, ctx);
            }
        }
    }

    fn handle_event(&mut self, event: &Event, ctx: &mut EventContext) -> EventResult {
        // Propagate to children in reverse order (front to back)
        for child in self.children.iter_mut().rev() {
            if child.handle_event(event, ctx) == EventResult::Handled {
                return EventResult::Handled;
            }
        }
        EventResult::Ignored
    }

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

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

    fn children(&self) -> &[Box<dyn Widget>] {
        &self.children
    }

    fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
        &mut self.children
    }
}

/// A horizontal stack container (Row).
pub struct Row {
    base: WidgetBase,
    children: Vec<Box<dyn Widget>>,
    gap: f32,
    align: Alignment,
    justify: Alignment,
    padding: EdgeInsets,
    child_positions: Vec<Point>,
}

impl Row {
    pub fn new() -> Self {
        Self {
            base: WidgetBase::new().with_class("row"),
            children: Vec::new(),
            gap: 0.0,
            align: Alignment::Center,
            justify: Alignment::Start,
            padding: EdgeInsets::ZERO,
            child_positions: Vec::new(),
        }
    }

    /// Add a child widget.
    pub fn child(mut self, widget: impl Widget + 'static) -> Self {
        self.children.push(Box::new(widget));
        self
    }

    /// Set the gap between children.
    pub fn gap(mut self, gap: f32) -> Self {
        self.gap = gap;
        self
    }

    /// Set cross-axis alignment.
    pub fn align(mut self, align: Alignment) -> Self {
        self.align = align;
        self
    }

    /// Set main-axis justification.
    pub fn justify(mut self, justify: Alignment) -> Self {
        self.justify = justify;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<EdgeInsets>) -> Self {
        self.padding = padding.into();
        self
    }

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

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

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

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

    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 {
        let mut width: f32 = 0.0;
        let mut height: f32 = 0.0;

        for child in &self.children {
            let child_size = child.intrinsic_size(ctx);
            width += child_size.width;
            height = height.max(child_size.height);
        }

        // Add gaps
        if self.children.len() > 1 {
            width += self.gap * (self.children.len() - 1) as f32;
        }

        // Add padding
        Size::new(
            width + self.padding.horizontal(),
            height + self.padding.vertical(),
        )
    }

    fn layout(&mut self, constraints: Constraints, ctx: &LayoutContext) -> LayoutResult {
        // First pass: get intrinsic sizes
        let child_sizes: Vec<Size> = self.children
            .iter()
            .map(|c| c.intrinsic_size(ctx))
            .collect();

        // Calculate total content size
        let total_width: f32 = child_sizes.iter().map(|s| s.width).sum::<f32>()
            + self.gap * (self.children.len().saturating_sub(1)) as f32;
        let max_height = child_sizes.iter().map(|s| s.height).fold(0.0_f32, |a, b| a.max(b));

        // Container size
        let container_size = constraints.constrain(Size::new(
            total_width + self.padding.horizontal(),
            max_height + self.padding.vertical(),
        ));

        // Calculate positions using flex layout
        let flex = FlexLayout {
            direction: FlexDirection::Row,
            justify: self.justify,
            align: self.align,
            gap: self.gap,
        };

        self.child_positions = flex.calculate_positions(container_size, &child_sizes, self.padding);

        // Second pass: layout children with their positions
        for (i, child) in self.children.iter_mut().enumerate() {
            let child_size = child_sizes[i];
            let available_height = match self.align {
                Alignment::Stretch => container_size.height - self.padding.vertical(),
                _ => child_size.height,
            };

            let child_constraints = Constraints::new(
                0.0,
                child_size.width,
                0.0,
                available_height,
            );
            child.layout(child_constraints, ctx);

            if let Some(pos) = self.child_positions.get(i) {
                child.set_bounds(Rect::from_origin_size(*pos, child.bounds().size));
            }
        }

        self.base.bounds.size = container_size;
        LayoutResult::new(container_size)
    }

    fn paint(&self, painter: &mut Painter, rect: Rect, ctx: &PaintContext) {
        // Paint children
        for (i, child) in self.children.iter().enumerate() {
            if let Some(pos) = self.child_positions.get(i) {
                let child_rect = Rect::from_origin_size(
                    Point::new(rect.x() + pos.x, rect.y() + pos.y),
                    child.bounds().size,
                );
                child.paint(painter, child_rect, ctx);
            }
        }
    }

    fn handle_event(&mut self, event: &Event, ctx: &mut EventContext) -> EventResult {
        // Propagate to children in reverse order (front to back)
        for child in self.children.iter_mut().rev() {
            if child.handle_event(event, ctx) == EventResult::Handled {
                return EventResult::Handled;
            }
        }
        EventResult::Ignored
    }

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

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

    fn children(&self) -> &[Box<dyn Widget>] {
        &self.children
    }

    fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
        &mut self.children
    }
}