theframework 0.9.3

A cross platform application UI framework.
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::prelude::*;

pub struct TheTreeIcons {
    id: TheId,
    limiter: TheSizeLimiter,

    state: TheWidgetState,

    dim: TheDim,
    is_dirty: bool,

    layout_id: TheId,
    scroll_offset: i32,

    icons: Vec<Option<TheRGBABuffer>>,
    texts: Vec<Option<String>>,
    status_texts: Vec<Option<String>>,
    selected_index: Option<usize>,
    hovered_index: Option<usize>,

    icon_size: i32,
    icons_per_row: i32,
    spacing: i32,
    dynamic_layout: bool,
    last_debug_icons_per_row: Option<(i32, i32, i32)>,

    rectangles: Vec<TheDim>,

    context_menu: Option<TheContextMenu>,
}

impl TheWidget for TheTreeIcons {
    fn new(id: TheId) -> Self
    where
        Self: Sized,
    {
        let mut limiter = TheSizeLimiter::new();
        limiter.set_max_height(100);

        Self {
            id,
            limiter,

            state: TheWidgetState::None,

            dim: TheDim::zero(),
            is_dirty: true,

            layout_id: TheId::empty(),
            scroll_offset: 0,

            icons: vec![],
            texts: vec![],
            status_texts: vec![],
            selected_index: None,
            hovered_index: None,

            icon_size: 18,
            icons_per_row: 10,
            spacing: 1,
            dynamic_layout: false,
            last_debug_icons_per_row: None,

            rectangles: vec![],

            context_menu: None,
        }
    }

    fn id(&self) -> &TheId {
        &self.id
    }

    fn set_context_menu(&mut self, menu: Option<TheContextMenu>) {
        self.context_menu = menu;
    }

    fn on_event(&mut self, event: &TheEvent, ctx: &mut TheContext) -> bool {
        let mut redraw = false;

        match event {
            TheEvent::Drop(coord, drop) => {
                if drop.id.name == "Tile" {
                    // Adjust coordinates for scroll offset from layout
                    let adjusted_coord = Vec2::new(coord.x, coord.y + self.scroll_offset);
                    // Find which icon was dropped on
                    for (i, rect) in self.rectangles.iter().enumerate() {
                        if rect.contains(adjusted_coord) && i < self.icons.len() {
                            let tile_id = drop.id.uuid;
                            ctx.ui
                                .send(TheEvent::TileDropped(self.id().clone(), tile_id, i));
                            self.is_dirty = true;
                            return true;
                        }
                    }
                }
            }
            TheEvent::Context(coord) => {
                if let Some(context_menu) = &self.context_menu {
                    ctx.ui.send(TheEvent::ShowContextMenu(
                        self.id().clone(),
                        *coord,
                        context_menu.clone(),
                    ));
                    ctx.ui.set_focus(self.id());
                    redraw = true;
                    self.is_dirty = true;
                }
            }
            TheEvent::MouseDown(coord) => {
                // Adjust coordinates for scroll offset from layout
                let adjusted_coord = Vec2::new(coord.x, coord.y + self.scroll_offset);

                // Check if clicking on an icon
                let mut clicked_icon = false;
                for (i, rect) in self.rectangles.iter().enumerate() {
                    if rect.contains(adjusted_coord) && i < self.icons.len() {
                        self.selected_index = Some(i);
                        ctx.ui.send(TheEvent::IndexChanged(self.id().clone(), i));
                        clicked_icon = true;
                        self.is_dirty = true;
                        redraw = true;
                        break;
                    }
                }

                // Only set widget state as selected if we clicked on an icon
                if clicked_icon {
                    if self.state != TheWidgetState::Selected || !self.id().equals(&ctx.ui.focus) {
                        self.is_dirty = true;
                        self.state = TheWidgetState::Selected;
                        ctx.ui.send_widget_state_changed(self.id(), self.state);
                        ctx.ui.send(TheEvent::NewListItemSelected(
                            self.id().clone(),
                            self.layout_id.clone(),
                        ));
                        redraw = true;
                    }
                    ctx.ui.set_focus(self.id());
                }
            }
            TheEvent::Hover(coord) => {
                // Adjust coordinates for scroll offset from layout
                let adjusted_coord = Vec2::new(coord.x, coord.y + self.scroll_offset);

                let mut new_hover = None;
                for (i, rect) in self.rectangles.iter().enumerate() {
                    if rect.contains(adjusted_coord) && i < self.icons.len() {
                        new_hover = Some(i);
                        break;
                    }
                }

                if new_hover != self.hovered_index {
                    self.hovered_index = new_hover;

                    // Send status text update event
                    if let Some(index) = new_hover {
                        if index < self.status_texts.len() {
                            if let Some(status_text) = &self.status_texts[index] {
                                ctx.ui.send(TheEvent::SetStatusText(
                                    self.id().clone(),
                                    status_text.clone(),
                                ));
                            } else {
                                ctx.ui.send(TheEvent::SetStatusText(
                                    self.id().clone(),
                                    "".to_string(),
                                ));
                            }
                        }
                    } else {
                        ctx.ui
                            .send(TheEvent::SetStatusText(self.id().clone(), "".to_string()));
                    }

                    self.is_dirty = true;
                    redraw = true;
                }

                // Set hover state on the widget itself
                if !self.id().equals(&ctx.ui.hover) {
                    self.is_dirty = true;
                    ctx.ui.set_hover(self.id());
                    redraw = true;
                }
            }
            TheEvent::MouseWheel(delta) => {
                ctx.ui
                    .send(TheEvent::ScrollLayout(self.layout_id.clone(), *delta));
            }
            _ => {}
        }
        redraw
    }

    fn dim(&self) -> &TheDim {
        &self.dim
    }

    fn dim_mut(&mut self) -> &mut TheDim {
        &mut self.dim
    }

    fn set_dim(&mut self, dim: TheDim, _ctx: &mut TheContext) {
        if self.dim != dim {
            self.dim = dim;
            self.is_dirty = true;
            self.update_height();
        }
    }

    fn limiter(&self) -> &TheSizeLimiter {
        &self.limiter
    }

    fn limiter_mut(&mut self) -> &mut TheSizeLimiter {
        &mut self.limiter
    }

    fn needs_redraw(&mut self) -> bool {
        self.is_dirty
    }

    fn set_needs_redraw(&mut self, redraw: bool) {
        self.is_dirty = redraw;
    }

    fn state(&self) -> TheWidgetState {
        self.state
    }

    fn set_state(&mut self, state: TheWidgetState) {
        self.state = state;
        self.is_dirty = true;
    }

    fn supports_hover(&mut self) -> bool {
        true
    }

    fn status_text(&self) -> Option<String> {
        if let Some(index) = self.hovered_index {
            if index < self.status_texts.len() {
                return self.status_texts[index].clone();
            }
        }
        None
    }

    fn draw(
        &mut self,
        buffer: &mut TheRGBABuffer,
        style: &mut Box<dyn TheStyle>,
        ctx: &mut TheContext,
    ) {
        if !self.dim().is_valid() {
            return;
        }

        // Fundamental safety check: ensure we don't draw outside buffer bounds
        let buffer_width = buffer.dim().width as i32;
        let buffer_height = buffer.dim().height as i32;
        let item_buffer_x = self.dim().buffer_x;
        let item_buffer_y = self.dim().buffer_y;

        if item_buffer_x < 0
            || item_buffer_y < 0
            || item_buffer_x + self.dim().width > buffer_width
            || item_buffer_y + self.dim().height > buffer_height
        {
            return;
        }

        // Always use normal background color - selection is shown per-icon
        let color = *style.theme().color(ListItemNormal);

        let stride = buffer.stride();
        let shrinker = TheDimShrinker::zero();

        // Draw background outline
        let mut adjusted_utuple = self.dim.to_buffer_shrunk_utuple(&shrinker);
        adjusted_utuple.1 += 1; // Draw 1px lower
        adjusted_utuple.3 = adjusted_utuple.3.saturating_sub(2); // Reduce height by 2px
        let buffer_width = buffer.dim().width as usize;
        let buffer_height = buffer.dim().height as usize;

        if adjusted_utuple.0 < buffer_width
            && adjusted_utuple.1 < buffer_height
            && adjusted_utuple.0 + adjusted_utuple.2 <= buffer_width
            && adjusted_utuple.1 + adjusted_utuple.3 <= buffer_height
        {
            ctx.draw.rect_outline_border_open(
                buffer.pixels_mut(),
                &adjusted_utuple,
                stride,
                &color,
                1,
            );
        }

        let mut shrinker = TheDimShrinker::zero();
        shrinker.shrink(1);

        // Draw background fill
        let mut adjusted_utuple = self.dim.to_buffer_shrunk_utuple(&shrinker);
        adjusted_utuple.1 += 1; // Draw 1px lower
        adjusted_utuple.3 = adjusted_utuple.3.saturating_sub(2); // Reduce height by 2px
        let buffer_width = buffer.dim().width as usize;
        let buffer_height = buffer.dim().height as usize;

        if adjusted_utuple.0 < buffer_width
            && adjusted_utuple.1 < buffer_height
            && adjusted_utuple.0 + adjusted_utuple.2 <= buffer_width
            && adjusted_utuple.1 + adjusted_utuple.3 <= buffer_height
        {
            ctx.draw
                .rect(buffer.pixels_mut(), &adjusted_utuple, stride, &color);
        }

        // Draw icons in a grid
        self.rectangles.clear();

        let start_x = 9;
        let start_y = 5;
        let mut x_off = start_x;
        let mut y_off = start_y;
        let mut col = 0;
        let icons_per_row = self.effective_icons_per_row();

        for (index, icon_opt) in self.icons.iter().enumerate() {
            // Buffer coordinates for drawing
            let icon_rect = (
                adjusted_utuple.0 + x_off,
                adjusted_utuple.1 + y_off,
                self.icon_size as usize,
                self.icon_size as usize,
            );

            // Widget-relative coordinates for hit testing
            // Events use logical widget coordinates (dim.x, dim.y based)
            // NOT buffer coordinates, so just use the raw offsets
            let hit_rect = TheDim::new(x_off as i32, y_off as i32, self.icon_size, self.icon_size);
            self.rectangles.push(hit_rect);

            // Safety check: only draw if icon is within buffer bounds
            if icon_rect.0 < buffer_width
                && icon_rect.1 < buffer_height
                && icon_rect.0 + icon_rect.2 <= buffer_width
                && icon_rect.1 + icon_rect.3 <= buffer_height
            {
                // Draw selection/hover highlight
                if Some(index) == self.selected_index {
                    ctx.draw
                        .rect_outline(buffer.pixels_mut(), &icon_rect, stride, &WHITE);
                } else if Some(index) == self.hovered_index {
                    let mut hover_color = *style.theme().color(ListItemHover);
                    hover_color[3] = 128; // Semi-transparent
                    ctx.draw
                        .rect_outline(buffer.pixels_mut(), &icon_rect, stride, &hover_color);
                }

                // Draw icon border
                ctx.draw.rect_outline(
                    buffer.pixels_mut(),
                    &(
                        icon_rect.0 + 1,
                        icon_rect.1 + 1,
                        icon_rect.2.saturating_sub(2),
                        icon_rect.3.saturating_sub(2),
                    ),
                    stride,
                    &BLACK,
                );

                // Draw icon content
                if let Some(icon) = icon_opt {
                    // Only draw if the icon matches the expected size
                    if icon.dim().width == self.icon_size - 4
                        && icon.dim().height == self.icon_size - 4
                    {
                        let content_rect = (
                            icon_rect.0 + 2,
                            icon_rect.1 + 2,
                            (self.icon_size - 4) as usize,
                            (self.icon_size - 4) as usize,
                        );
                        ctx.draw.blend_slice(
                            buffer.pixels_mut(),
                            icon.pixels(),
                            &content_rect,
                            stride,
                        );
                    }
                }

                // Draw text overlay (always if present, even over icons)
                if index < self.texts.len() {
                    if let Some(text) = &self.texts[index] {
                        let text_color = WHITE;
                        let font_size = 9.0; // Small font size

                        // Calculate text rectangle to center it in the icon area
                        let text_rect = (
                            icon_rect.0,
                            icon_rect.1,
                            (self.icon_size) as usize,
                            (self.icon_size) as usize,
                        );

                        ctx.draw.text_rect_blend(
                            buffer.pixels_mut(),
                            &text_rect,
                            stride,
                            text,
                            TheFontSettings {
                                size: font_size,
                                ..Default::default()
                            },
                            &text_color,
                            TheHorizontalAlign::Center,
                            TheVerticalAlign::Center,
                        );
                    }
                }
            }

            col += 1;
            if col >= icons_per_row {
                col = 0;
                x_off = start_x;
                y_off += self.icon_size as usize + self.spacing as usize;
            } else {
                x_off += self.icon_size as usize + self.spacing as usize;
            }
        }

        self.is_dirty = false;
    }

    fn as_tree_icons(&mut self) -> Option<&mut dyn TheTreeIconsTrait> {
        Some(self)
    }

    fn as_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

impl TheTreeIcons {
    fn effective_icons_per_row(&mut self) -> i32 {
        if !self.dynamic_layout {
            return self.icons_per_row.max(1);
        }

        let start_x = 9;
        let available = (self.dim.width - start_x * 2).max(self.icon_size);
        let step = (self.icon_size + self.spacing).max(1);
        let icons_per_row = ((available + self.spacing) / step).max(1);
        let debug = (self.dim.width, available, icons_per_row);
        if self.last_debug_icons_per_row != Some(debug) {
            println!(
                "TreeIcons layout ({}): dim_width={} available={} icons_per_row={}",
                self.id.name, self.dim.width, available, icons_per_row
            );
            self.last_debug_icons_per_row = Some(debug);
        }
        icons_per_row
    }

    /// Calculate and update the height based on the number of icons and layout
    fn update_height(&mut self) {
        if self.icons.is_empty() {
            self.limiter.set_max_height(22);
            return;
        }

        let icons_per_row = self.effective_icons_per_row();
        let rows = (self.icons.len() as i32 + icons_per_row - 1) / icons_per_row;
        // Account for padding: 5px top + 5px bottom = 10px total vertical padding
        // Also account for the drawing adjustments in draw():
        // - shrinker.shrink(1) removes 2px total height (1px top, 1px bottom)
        // - adjusted_utuple.1 += 1 and adjusted_utuple.3 -= 2 removes another 2px
        // Total shrinkage: 4px, so we need to add that back
        let height = 10 + rows * self.icon_size + (rows - 1).max(0) * self.spacing + 4;
        self.limiter.set_max_height(height);
    }
}

pub trait TheTreeIconsTrait {
    fn set_associated_layout(&mut self, id: TheId);
    fn set_scroll_offset(&mut self, offset: i32);
    fn set_icon_size(&mut self, size: i32);
    fn set_icons_per_row(&mut self, count: i32);
    fn set_spacing(&mut self, spacing: i32);
    fn set_dynamic_layout(&mut self, dynamic_layout: bool);
    fn set_icon_count(&mut self, count: usize);
    fn set_icon(&mut self, index: usize, icon: TheRGBABuffer);
    fn set_text(&mut self, index: usize, text: String);
    fn set_status_text_for(&mut self, index: usize, text: String);
    fn set_palette(&mut self, palette: &ThePalette);
    fn clear_icons(&mut self);
    fn selected_index(&self) -> Option<usize>;
    fn set_selected_index(&mut self, index: Option<usize>);
}

impl TheTreeIconsTrait for TheTreeIcons {
    fn set_associated_layout(&mut self, layout_id: TheId) {
        self.layout_id = layout_id;
    }

    fn set_scroll_offset(&mut self, offset: i32) {
        self.scroll_offset = offset;
    }

    fn set_icon_size(&mut self, size: i32) {
        self.icon_size = size;
        self.update_height();
        self.is_dirty = true;
    }

    fn set_icons_per_row(&mut self, count: i32) {
        self.icons_per_row = count;
        self.update_height();
        self.is_dirty = true;
    }

    fn set_spacing(&mut self, spacing: i32) {
        self.spacing = spacing;
        self.update_height();
        self.is_dirty = true;
    }

    fn set_dynamic_layout(&mut self, dynamic_layout: bool) {
        self.dynamic_layout = dynamic_layout;
        self.update_height();
        self.is_dirty = true;
    }

    fn set_icon_count(&mut self, count: usize) {
        self.icons.resize(count, None);
        self.texts.resize(count, None);
        self.status_texts.resize(count, None);
        self.update_height();
        self.is_dirty = true;
    }

    fn set_icon(&mut self, index: usize, icon: TheRGBABuffer) {
        if index < self.icons.len() {
            let expected_size = self.icon_size - 4;

            // Automatically resize icon if it doesn't match the expected size
            let final_icon =
                if icon.dim().width != expected_size || icon.dim().height != expected_size {
                    icon.scaled(expected_size, expected_size)
                } else {
                    icon
                };

            self.icons[index] = Some(final_icon);
            self.is_dirty = true;
        }
    }

    fn set_text(&mut self, index: usize, text: String) {
        // Ensure texts vec is large enough
        if index >= self.texts.len() {
            self.texts.resize(index + 1, None);
        }
        self.texts[index] = Some(text);
        self.is_dirty = true;
    }

    fn set_status_text_for(&mut self, index: usize, text: String) {
        // Ensure status_texts vec is large enough
        if index >= self.status_texts.len() {
            self.status_texts.resize(index + 1, None);
        }
        self.status_texts[index] = Some(text);
    }

    fn set_palette(&mut self, palette: &ThePalette) {
        self.icons.clear();
        self.texts.clear();
        self.status_texts.clear();

        for (index, color_opt) in palette.colors.iter().enumerate() {
            if let Some(color) = color_opt {
                // Create a small buffer with the palette color
                let mut icon =
                    TheRGBABuffer::new(TheDim::sized(self.icon_size - 4, self.icon_size - 4));
                let color_array = color.to_u8_array();
                for pixel in icon.pixels_mut().chunks_exact_mut(4) {
                    pixel.copy_from_slice(&color_array);
                }
                self.icons.push(Some(icon));

                // Create status text with index and hex color
                let hex_color = format!(
                    "#{:02X}{:02X}{:02X}",
                    color_array[0], color_array[1], color_array[2]
                );
                self.status_texts.push(Some(format!(
                    "Palette Index {}. Color {}",
                    index, hex_color
                )));
            } else {
                self.icons.push(None);
                self.status_texts.push(None);
            }
        }

        self.update_height();
        self.is_dirty = true;
    }

    fn clear_icons(&mut self) {
        self.icons.clear();
        self.texts.clear();
        self.status_texts.clear();
        self.selected_index = None;
        self.hovered_index = None;
        self.update_height();
        self.is_dirty = true;
    }

    fn selected_index(&self) -> Option<usize> {
        self.selected_index
    }

    fn set_selected_index(&mut self, index: Option<usize>) {
        self.selected_index = index;
        self.is_dirty = true;
    }
}