blinc_app 0.4.0

Blinc application framework with clean layout and rendering API
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
//! CSS Debug Example
//!
//! Tests three known CSS issues:
//! 1. var() not picking up values from :root
//! 2. width/height percentage not working
//! 3. Text not inheriting color from parent div
//!
//! Run with: cargo run -p blinc_app --example css_debug --features windowed

use blinc_app::prelude::*;
use blinc_app::windowed::{WindowedApp, WindowedContext};
use blinc_core::State;

fn main() -> Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();

    let config = WindowConfig {
        title: "CSS Debug — var(), %, color inheritance".to_string(),
        width: 900,
        height: 700,
        resizable: true,
        ..Default::default()
    };

    let mut css_loaded = false;
    WindowedApp::run(config, move |ctx| {
        if !css_loaded {
            ctx.add_css(STYLESHEET);
            css_loaded = true;
        }
        build_ui(ctx)
    })
}

const STYLESHEET: &str = r#"
        :root {
            --brand-color: #3b82f6;
            --accent-color: #f59e0b;
            --text-color: #ffffff;
            --card-radius: 16px;
            --card-padding: 24px;
        }

        /* === TEST 1: var() from :root === */
        #var-test {
            background: var(--brand-color);
            border-radius: var(--card-radius);
            padding: var(--card-padding);
        }

        #var-test-accent {
            background: var(--accent-color);
            border-radius: 12px;
            padding: 16px;
        }

        #var-test-fallback {
            background: var(--nonexistent, #ef4444);
            border-radius: 12px;
            padding: 16px;
        }

        /* === TEST 2: Percentage width/height === */
        #percent-container {
            background: #1e293b;
            border-radius: 12px;
            padding: 16px;
        }

        #percent-half {
            width: 50%;
            background: #3b82f6;
            border-radius: 8px;
            padding: 12px;
        }

        #percent-full {
            width: 100%;
            background: #8b5cf6;
            border-radius: 8px;
            padding: 12px;
        }

        #percent-third {
            width: 33.3%;
            background: #10b981;
            border-radius: 8px;
            padding: 12px;
        }

        /* === TEST 3: Text color inheritance === */
        #inherit-parent {
            color: #3b82f6;
            background: #1e293b;
            border-radius: 12px;
            padding: 16px;
        }

        #inherit-nested {
            color: #f59e0b;
        }

        .white-text {
            color: #ffffff;
        }

        /* Section styles */
        #root {
            background: #0f172a;
        }

        .section {
            background: #1e293b;
            border-radius: 16px;
            padding: 24px;
        }

        /* === TEST 4: CSS inside stateful containers === */
        #sf-var-test {
            background: var(--brand-color);
            border-radius: 12px;
            padding: 16px;
        }

        #sf-percent-container {
            background: #1e293b;
            border-radius: 8px;
            padding: 12px;
        }

        #sf-percent-half {
            width: 50%;
            background: #3b82f6;
            border-radius: 8px;
            padding: 12px;
        }

        #sf-percent-third {
            width: 33.3%;
            background: #10b981;
            border-radius: 8px;
            padding: 12px;
        }

        #sf-color-parent {
            color: #10b981;
            background: #1e293b;
            border-radius: 8px;
            padding: 12px;
        }

        .sf-card {
            background: #7c3aed;
            border-radius: 12px;
            padding: 16px;
        }
"#;

fn build_ui(ctx: &WindowedContext) -> impl ElementBuilder {
    let count = ctx.use_state_keyed("rebuild-trigger", || 0i32);

    div()
        .w(ctx.width)
        .h(ctx.height)
        .id("root")
        .flex_col()
        .gap(5.0)
        .p(5.0)
        .overflow_y_scroll()
        .child(
            div()
                .flex_row()
                .items_center()
                .gap(4.0)
                .child(
                    text("CSS Debug Tests (inside stateful containers)")
                        .size(28.0)
                        .weight(FontWeight::Bold)
                        .color(Color::WHITE),
                )
                // Button to trigger rebuild
                .child(rebuild_button(count.clone())),
        )
        // All tests wrapped in stateful containers
        .child(test_var_from_root_stateful(count.clone()))
        .child(test_percentage_stateful(count.clone()))
        .child(test_color_inheritance_stateful(count.clone()))
        .child(test_layout_stateful())
        .child(test_inner_click_persistence(ctx, count.clone()))
}

/// Rebuild trigger button — click to force stateful containers to re-run on_state
fn rebuild_button(count: State<i32>) -> impl ElementBuilder {
    let count_click = count.clone();
    stateful::<NoState>()
        .deps([count.signal_id()])
        .on_state(move |_ctx| {
            let n = count.get();
            div()
                .bg(Color::rgba(0.23, 0.51, 0.96, 1.0))
                .rounded(8.0)
                .p(2.0)
                .px(4.0)
                .child(
                    text(format!("Rebuild ({})", n))
                        .size(16.0)
                        .weight(FontWeight::Bold)
                        .color(Color::WHITE),
                )
        })
        .on_click(move |_| {
            count_click.set(count_click.get() + 1);
        })
}

/// TEST 1: var() from :root inside stateful
fn test_var_from_root_stateful(count: State<i32>) -> impl ElementBuilder {
    stateful::<NoState>()
        .deps([count.signal_id()])
        .on_state(move |_ctx| {
            div()
                .class("section")
                .flex_col()
                .gap(3.0)
                .child(
                    div().child(
                        text("Test 1: var() from :root (stateful)")
                            .size(20.0)
                            .weight(FontWeight::Bold)
                            .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                    ),
                )
                .child(
                    div()
                        .flex_row()
                        .gap(3.0)
                        .child(
                            div().id("var-test").child(
                                text("var(--brand-color)\nExpect: Blue bg")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        )
                        .child(
                            div().id("var-test-accent").child(
                                text("var(--accent-color)\nExpect: Amber bg")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        )
                        .child(
                            div().id("var-test-fallback").child(
                                text("var(--nonexistent, #ef4444)\nExpect: Red bg (fallback)")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        ),
                )
        })
}

/// TEST 2: Percentage width inside stateful
fn test_percentage_stateful(count: State<i32>) -> impl ElementBuilder {
    stateful::<NoState>()
        .deps([count.signal_id()])
        .on_state(move |_ctx| {
            div()
                .class("section")
                .flex_col()
                .gap(3.0)
                .child(
                    div().child(
                        text("Test 2: Percentage Width (stateful)")
                            .size(20.0)
                            .weight(FontWeight::Bold)
                            .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                    ),
                )
                .child(
                    div()
                        .id("percent-container")
                        .w_full()
                        .flex_col()
                        .gap(2.0)
                        .child(
                            div().id("percent-half").child(
                                text("width: 50% — should be half")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        )
                        .child(
                            div().id("percent-full").child(
                                text("width: 100% — should be full")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        )
                        .child(
                            div().id("percent-third").child(
                                text("width: 33.3% — should be one-third")
                                    .size(14.0)
                                    .color(Color::WHITE),
                            ),
                        ),
                )
        })
}

/// TEST 3: Text color inheritance inside stateful
fn test_color_inheritance_stateful(count: State<i32>) -> impl ElementBuilder {
    stateful::<NoState>()
        .deps([count.signal_id()])
        .on_state(move |_ctx| {
            div()
                .class("section")
                .flex_col()
                .gap(3.0)
                .child(
                    div().child(
                        text("Test 3: Color Inheritance (stateful)")
                            .size(20.0)
                            .weight(FontWeight::Bold)
                            .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                    ),
                )
                .child(
                    div()
                        .flex_col()
                        .gap(2.0)
                        .child(
                            div()
                                .id("inherit-parent")
                                .flex_col()
                                .gap(2.0)
                                .child(
                                    text("Should be BLUE (inherited from #inherit-parent { color: #3b82f6 })")
                                        .size(14.0),
                                )
                                .child(
                                    div()
                                        .id("inherit-nested")
                                        .child(
                                            text("Should be AMBER (inherited from #inherit-nested { color: #f59e0b })")
                                                .size(14.0),
                                        ),
                                ),
                        )
                        .child(
                            div()
                                .class("white-text")
                                .child(
                                    text("Should be WHITE (inherited from .white-text { color: #fff })")
                                        .size(14.0),
                                ),
                        )
                        .child(
                            text("Control: direct .color(Color::GREEN) — should be green")
                                .size(14.0)
                                .color(Color::GREEN),
                        ),
                )
        })
}

/// TEST 4: CSS layout properties inside stateful (no hover — pure layout test)
fn test_layout_stateful() -> impl ElementBuilder {
    stateful::<NoState>().on_state(|_ctx| {
        div()
            .class("section")
            .flex_col()
            .gap(3.0)
            .child(
                div().child(
                    text("Test 4: CSS Layout in stateful (class, var, %, color)")
                        .size(20.0)
                        .weight(FontWeight::Bold)
                        .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                ),
            )
            // var() test
            .child(
                div().id("sf-var-test").child(
                    text("var(--brand-color) bg inside stateful")
                        .size(14.0)
                        .color(Color::WHITE),
                ),
            )
            // Percentage width test
            .child(
                div()
                    .id("sf-percent-container")
                    .flex_col()
                    .gap(2.0)
                    .child(
                        div().id("sf-percent-half").child(
                            text("width: 50% inside stateful")
                                .size(14.0)
                                .color(Color::WHITE),
                        ),
                    )
                    .child(
                        div().id("sf-percent-third").child(
                            text("width: 33.3% inside stateful")
                                .size(14.0)
                                .color(Color::WHITE),
                        ),
                    ),
            )
            // Color inheritance test
            .child(
                div().id("sf-color-parent").child(
                    text("Should be GREEN (inherited from #sf-color-parent { color: #10b981 })")
                        .size(14.0),
                ),
            )
            // Class-based styling test
            .child(
                div().class("sf-card").child(
                    text(".sf-card class styling inside stateful")
                        .size(14.0)
                        .color(Color::WHITE),
                ),
            )
    })
}

/// TEST 5: Inner child click handlers persist across stateful rebuilds
///
/// This tests that on_click handlers attached to children INSIDE on_state
/// callbacks survive when the parent stateful container rebuilds due to
/// signal changes. The inner buttons should remain clickable after pressing
/// the main "Rebuild" button.
fn test_inner_click_persistence(ctx: &WindowedContext, count: State<i32>) -> impl ElementBuilder {
    let inner_count = ctx.use_state_keyed("inner-click-count", || 0i32);
    let inner_count_for_state = inner_count.clone();
    let inner_count_click = inner_count.clone();

    stateful::<NoState>()
        .deps([count.signal_id(), inner_count.signal_id()])
        .on_state(move |_ctx| {
            let n = count.get();
            let clicks = inner_count_for_state.get();
            div()
                .class("section")
                .flex_col()
                .gap(3.0)
                .child(
                    div().child(
                        text("Test 5: Inner Child Click Persistence")
                            .size(20.0)
                            .weight(FontWeight::Bold)
                            .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                    ),
                )
                .child(
                    div().child(
                        text(format!(
                            "Parent rebuild count: {} | Inner click count: {}",
                            n, clicks
                        ))
                        .size(14.0)
                        .color(Color::WHITE),
                    ),
                )
                // Inner clickable button — this handler should persist across rebuilds
                .child(
                    div()
                        .bg(Color::rgba(0.08, 0.65, 0.51, 1.0))
                        .rounded(8.0)
                        .p(3.0)
                        .px(4.0)
                        .child(
                            text("Click me (inner child handler)")
                                .size(16.0)
                                .weight(FontWeight::Bold)
                                .color(Color::WHITE),
                        )
                        .on_click({
                            let ic = inner_count_click.clone();
                            move |_| {
                                ic.set(ic.get() + 1);
                            }
                        }),
                )
                .child(
                    div().child(
                        text("After pressing 'Rebuild', this inner button should still work")
                            .size(12.0)
                            .color(Color::rgba(0.58, 0.64, 0.72, 1.0)),
                    ),
                )
        })
}