hyperchad_renderer_fltk 0.3.0

HyperChad FLTK renderer package
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
# HyperChad FLTK Renderer

Cross-platform native GUI renderer for HyperChad using the FLTK (Fast Light Toolkit) framework.

## Overview

The HyperChad FLTK Renderer provides:

- **Lightweight Native GUI**: Fast, lightweight native desktop applications
- **Cross-platform**: Works on Windows, macOS, and Linux with native look and feel
- **Low Resource Usage**: Minimal memory and CPU footprint
- **Retained Mode GUI**: Traditional widget-based GUI architecture
- **Layout Engine**: Complete flexbox and positioning layout system
- **Image Support**: Async image loading with caching and format support
- **Event System**: Navigation events with async waiting support
- **Viewport Management**: Scrolling and viewport-aware rendering

## Supported Elements

### Fully Supported

- **Containers**: `div`, `aside`, `header`, `footer`, `main`, `section`, `form`, `span`, `details`, `summary`
- **Lists**: `ul` (unordered list), `ol` (ordered list), `li` (list item)
- **Tables**: `table`, `thead`, `th`, `tbody`, `tr`, `td`
- **Text**: `h1`, `h2`, `h3`, `h4`, `h5`, `h6` (headings), raw text
- **Images**: `img` (with async loading, HTTP support, and local file support)
- **Links**: `a` (anchor with navigation support)
- **Buttons**: `button` (rendered as containers)
- **Dropdowns**: `select` with `option` children (rendered as FLTK Choice widget)

### Not Rendered

- **Form Inputs**: `input` (text, password, checkbox, radio, etc.)
- **Canvas**: `canvas` (element exists but no rendering implementation)

## Features

### Native GUI Capabilities

- **FLTK Widgets**: Rendering of HyperChad elements using FLTK widgets
- **Native Styling**: Platform-native appearance and behavior
- **Window Management**: Single-window support with resize handling
- **Planned**: Menu systems, dialog boxes, and multi-window support

### Layout and Styling

- **Flexbox Layout**: Complete CSS flexbox implementation
- **Positioning**: Support for layout positioning
- **Spacing**: Margins, padding, and gap support
- **Sizing**: Width, height, min/max constraints
- **Typography**: Font families, sizes, and text styling
- **Colors**: Background colors, text colors, and theming

### Interactive Elements

- **Containers**: Divs, sections, headers, footers, and other semantic elements
- **Clickable Elements**: Anchors with navigation
- **Scrollable Areas**: Horizontal and vertical scrolling with overflow support
- **Event Handling**: Navigation events
- **Dropdowns**: Select elements with option children
- **Planned**: Form inputs (text, checkbox, radio)

### Image and Media

- **Image Loading**: Async HTTP image loading with caching
- **Image Formats**: Support for PNG, JPEG, GIF, and other formats
- **Image Scaling**: Automatic scaling and aspect ratio preservation
- **Asset Management**: Local and remote asset loading

## Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
hyperchad_renderer_fltk = { path = "../hyperchad/renderer/fltk" }

# With debug features
hyperchad_renderer_fltk = {
    path = "../hyperchad/renderer/fltk",
    features = ["debug"]
}
```

### System Dependencies

**Ubuntu/Debian:**

```bash
sudo apt-get install libfltk1.3-dev libxinerama-dev libxft-dev libxcursor-dev
```

**macOS:**

```bash
# FLTK is included in the build
# No additional dependencies needed
```

**Windows:**

```bash
# FLTK is statically linked
# No additional dependencies needed
```

## Usage

### Basic Desktop Application

```rust
use hyperchad_renderer_fltk::FltkRenderer;
use hyperchad_template::container;
use hyperchad_renderer::{View, Renderer, ToRenderRunner};
use hyperchad_actions::logic::Value;
use flume::unbounded;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create communication channels
    let (action_tx, action_rx) = unbounded();

    // Create FLTK renderer
    let mut renderer = FltkRenderer::new(action_tx);

    // Initialize window
    renderer.init(
        800.0,    // width
        600.0,    // height
        Some(100), // x position
        Some(100), // y position
        Some(hyperchad_color::Color::from_hex("#f0f0f0")), // background
        Some("FLTK App"), // title
        Some("My HyperChad FLTK App"), // description
        None,     // viewport
    ).await?;

    // Create HyperChad view
    let view = container! {
        div
            width=780
            height=580
            direction="column"
            padding=10
            gap=10
        {
            h1
                font-size=24
                color="blue"
                text-align="center"
            {
                "Welcome to FLTK!"
            }

            div
                direction="row"
                gap=10
                justify-content="center"
            {
                button
                    width=100
                    height=30
                    background="green"
                    color="white"
                {
                    "Show"
                }

                button
                    width=100
                    height=30
                    background="red"
                    color="white"
                {
                    "Hide"
                }
            }

            div
                str_id="message"
                width=400
                height=100
                background="yellow"
                padding=10
                align-self="center"
            {
                "Hello from FLTK! This is a native desktop application."
            }
        }
    };

    // Render the view
    renderer.render(View::from(view)).await?;

    // Convert to runner and start event loop
    let mut runner = renderer.to_runner(hyperchad_renderer::Handle::current())?;
    runner.run()?;

    Ok(())
}
```

### Button Layout Application

```rust
use hyperchad_template::container;

let button_view = container! {
    div
        width=400
        height=200
        direction="column"
        padding=20
        gap=15
        background="white"
    {
        h2
            text-align="center"
            margin-bottom=20
        {
            "Button Layout"
        }

        div
            direction="row"
            justify-content="center"
            gap=10
        {
            button
                width=120
                height=40
                background="blue"
                color="white"
                padding=10
            {
                "Action 1"
            }

            button
                width=120
                height=40
                background="green"
                color="white"
                padding=10
            {
                "Action 2"
            }

            button
                width=120
                height=40
                background="red"
                color="white"
                padding=10
            {
                "Action 3"
            }
        }

        div
            str_id="status"
            padding=10
            text-align="center"
        {
            "Button layout example"
        }
    }
};

renderer.render(View::from(button_view)).await?;
```

**Note**: Form inputs (text, password, checkbox) are not yet implemented. The `input` element exists but is not currently rendered by the FLTK renderer. `button` elements are rendered, but click action dispatch is not implemented by this renderer.

### Image Gallery

```rust
use hyperchad_template::container;

let gallery_view = container! {
    div
        width=600
        height=400
        direction="column"
        padding=20
        gap=15
    {
        h2
            text-align="center"
        {
            "Image Gallery"
        }

        div
            direction="row"
            gap=10
            justify-content="center"
            flex-wrap="wrap"
        {
            img
                src="https://picsum.photos/150/150?random=1"
                width=150
                height=150
                fit="cover"
            {}

            img
                src="https://picsum.photos/150/150?random=2"
                width=150
                height=150
                fit="cover"
            {}

            img
                src="https://picsum.photos/150/150?random=3"
                width=150
                height=150
                fit="cover"
            {}
        }

        div
            align-self="center"
            border="1px solid #ccc"
        {
            img
                str_id="main-image"
                src="https://picsum.photos/400/300?random=1"
                width=400
                height=300
                fit="contain"
            {}
        }
    }
};

renderer.render(View::from(gallery_view)).await?;
```

### Scrollable Content

```rust
use hyperchad_template::container;

let scrollable_view = container! {
    div
        width=400
        height=300
        direction="column"
    {
        h2
            padding=10
            background="lightgray"
        {
            "Scrollable List"
        }

        div
            flex=1
            overflow-y="scroll"
            padding=10
            gap=5
            direction="column"
        {
            // Generate many items
            @for i in 0..50 {
                div
                    padding=10
                    background=if i % 2 == 0 { "lightblue" } else { "white" }
                    border="1px solid #ccc"
                {
                    format!("Item {}", i + 1)
                }
            }
        }
    }
};

renderer.render(View::from(scrollable_view)).await?;
```

### Event Handling

```rust
// Handle navigation events from anchor interactions
tokio::spawn({
    let renderer = renderer.clone();
    async move {
        while let Some(href) = renderer.wait_for_navigation().await {
            println!("Navigating to: {}", href);
        }
    }
});
```

### Navigation Between Views

```rust
use hyperchad_template::container;

// Main menu view
let menu_view = container! {
    div
        width=600
        height=400
        direction="column"
        padding=20
        gap=15
    {
        h1
            text-align="center"
        {
            "Main Menu"
        }

        div
            direction="column"
            gap=10
            align-items="center"
        {
            a
                href="/gallery"
                width=200
                height=50
                background="blue"
                color="white"
                padding=10
                text-align="center"
            {
                div { "View Gallery" }
            }

            a
                href="/about"
                width=200
                height=50
                background="green"
                color="white"
                padding=10
                text-align="center"
            {
                div { "About" }
            }
        }
    }
};

// Render and handle navigation
renderer.render(View::from(menu_view)).await?;

// Wait for navigation event
if let Some(href) = renderer.wait_for_navigation().await {
    println!("Navigating to: {}", href);
    // Render the new view based on href
}
```

## Layout System

### Flexbox Support

- **Direction**: row, column, row-reverse, column-reverse
- **Justify Content**: start, center, end, space-between, space-around
- **Align Items**: start, center, end, stretch
- **Flex Properties**: flex-grow, flex-shrink, flex-basis
- **Gap**: Space between flex items

### Positioning

- **Static**: Normal document flow
- **Relative**: Positioned relative to normal position
- **Absolute**: Positioned relative to parent container
- **Fixed**: Positioned relative to window

### Sizing

- **Fixed Sizes**: Pixel values for width and height
- **Percentage**: Relative to parent container
- **Constraints**: min-width, max-width, min-height, max-height
- **Flex**: Flexible sizing based on available space

## Image Loading

### Supported Formats

- **PNG**: Portable Network Graphics
- **JPEG**: Joint Photographic Experts Group
- **GIF**: Graphics Interchange Format
- **BMP**: Windows Bitmap
- **TIFF**: Tagged Image File Format

### Loading Features

- **Async Loading**: Non-blocking image loading
- **HTTP Support**: Load images from URLs
- **Caching**: Automatic image caching
- **Scaling**: Automatic scaling to fit containers
- **Error Handling**: Graceful handling of load failures

## Feature Flags

- **`debug`**: Enable debug rendering and logging (default: enabled)
- **`format`**: Enable formatter support for templates (default: enabled)
- **`unsafe`**: Enable unsafe optimizations (default: enabled)

## Performance Characteristics

### Advantages

- **Low Memory**: Minimal memory footprint
- **Fast Startup**: Quick application startup time
- **Native Performance**: Native widget performance
- **Small Binary**: Compact executable size

### Considerations

- **Retained Mode**: Widgets persist between updates
- **Layout Calculation**: Efficient layout algorithms
- **Image Caching**: Smart caching to reduce memory usage
- **Event Handling**: Efficient event propagation

## Dependencies

### Core Dependencies

- **fltk**: Fast Light Toolkit GUI library (with ninja build support)
- **hyperchad_renderer**: Core renderer traits and utilities (with canvas and viewport-retained features)
- **hyperchad_transformer**: Template transformation and layout engine (with html and layout features)
- **hyperchad_actions**: Action system and logic (with logic feature)
- **moosicbox_app_native_image**: Native image asset handling

### Runtime Dependencies

- **image**: Image processing and format support
- **switchy_async**: Async runtime and task utilities (with sync and tokio features)
- **switchy_http**: HTTP client for remote image loading (reqwest backend)
- **flume**: Multi-producer, multi-consumer channels
- **bytes**: Byte buffer utilities

## Integration

This renderer is designed for:

- **Desktop Applications**: Traditional desktop GUI applications
- **Utility Tools**: System utilities and development tools
- **Embedded Systems**: Applications for embedded devices
- **Legacy Systems**: Integration with existing FLTK applications
- **Cross-platform Tools**: Applications targeting multiple desktop platforms

## Limitations

### Not Yet Implemented

- **Form Inputs**: Text inputs, checkboxes, radio buttons are not rendered
- **Multi-Window Support**: Currently limited to single window applications
- **Native Menus**: Menu bars and context menus not implemented
- **Dialog Boxes**: File choosers and message boxes not integrated

### Design Constraints

- **Theming**: Basic theming capabilities compared to web renderers
- **Animations**: No animation support
- **Advanced CSS**: Some advanced CSS features not supported
- **Web Technologies**: No HTML/CSS/JavaScript integration
- **Buttons**: Rendered as containers rather than native FLTK buttons