grio 0.1.1

A fast, pure Rust alternative to Gradio with zero frontend dependencies
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
//! Conteneurs et composants de mise en page (Row, Column, Grid, Panel, Tabs, Accordion, Drawer).

use serde_json::{json, Value};

use super::{children_refs, Component, IntoBox, Role};
use crate::app::RowBuilder;

/// Conteneur horizontal : les enfants s'affichent côte à côte (flex).
pub struct Row {
    pub(crate) id: String,
    pub(crate) gap: f64,
    pub(crate) wrap: bool,
    pub(crate) align: Option<String>,
    pub(crate) justify: Option<String>,
    pub(crate) children: Vec<Box<dyn Component>>,
}

impl Row {
    /// Crée une rangée, avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            gap: 16.0,
            wrap: true,
            align: None,
            justify: None,
            children: Vec::new(),
        }
    }
    /// Espace entre les enfants, en pixels.
    pub fn gap(mut self, g: f64) -> Self {
        self.gap = g;
        self
    }
    /// Autorise ou interdit le retour à la ligne (`flex-wrap`).
    pub fn wrap(mut self, w: bool) -> Self {
        self.wrap = w;
        self
    }
    /// Alignement transversal (CSS `align-items` : `"start"`, `"center"`, `"end"`, `"stretch"`).
    pub fn align(mut self, a: impl Into<String>) -> Self {
        self.align = Some(a.into());
        self
    }
    /// Justification principale (CSS `justify-content` : `"start"`, `"center"`, `"end"`, `"space-between"`, `"space-around"`).
    pub fn justify(mut self, j: impl Into<String>) -> Self {
        self.justify = Some(j.into());
        self
    }
    /// Ajoute un composant dans la rangée.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }
}

impl Component for Row {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "row"
    }
    fn props(&self) -> Value {
        let mut obj = serde_json::Map::new();
        obj.insert("gap".into(), json!(self.gap));
        obj.insert("wrap".into(), json!(self.wrap));
        if let Some(ref a) = self.align {
            obj.insert("align".into(), json!(a));
        }
        if let Some(ref j) = self.justify {
            obj.insert("justify".into(), json!(j));
        }
        Value::Object(obj)
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Conteneur vertical : les enfants s'empilent (flex column).
pub struct Column {
    pub(crate) id: String,
    pub(crate) gap: f64,
    pub(crate) align: Option<String>,
    pub(crate) justify: Option<String>,
    pub(crate) children: Vec<Box<dyn Component>>,
}

impl Column {
    /// Crée une colonne, avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            gap: 16.0,
            align: None,
            justify: None,
            children: Vec::new(),
        }
    }
    /// Espace entre les enfants, en pixels.
    pub fn gap(mut self, g: f64) -> Self {
        self.gap = g;
        self
    }
    /// Alignement transversal (CSS `align-items` : `"start"`, `"center"`, `"end"`, `"stretch"`).
    pub fn align(mut self, a: impl Into<String>) -> Self {
        self.align = Some(a.into());
        self
    }
    /// Justification principale (CSS `justify-content` : `"start"`, `"center"`, `"end"`, `"space-between"`).
    pub fn justify(mut self, j: impl Into<String>) -> Self {
        self.justify = Some(j.into());
        self
    }
    /// Ajoute un composant dans la colonne.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }
    pub(crate) fn push(&mut self, c: Box<dyn Component>) {
        self.children.push(c);
    }
}

impl Component for Column {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "column"
    }
    fn props(&self) -> Value {
        let mut obj = serde_json::Map::new();
        obj.insert("gap".into(), json!(self.gap));
        if let Some(ref a) = self.align {
            obj.insert("align".into(), json!(a));
        }
        if let Some(ref j) = self.justify {
            obj.insert("justify".into(), json!(j));
        }
        Value::Object(obj)
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Conteneur en grille CSS : organise ses enfants en lignes et colonnes.
pub struct Grid {
    pub(crate) id: String,
    pub(crate) columns: usize,
    pub(crate) gap: f64,
    pub(crate) gap_x: Option<f64>,
    pub(crate) gap_y: Option<f64>,
    pub(crate) children: Vec<Box<dyn Component>>,
}

impl Grid {
    /// Crée une grille avec son identifiant et son nombre de colonnes par défaut (2).
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            columns: 2,
            gap: 16.0,
            gap_x: None,
            gap_y: None,
            children: Vec::new(),
        }
    }
    /// Nombre de colonnes de la grille.
    pub fn columns(mut self, cols: usize) -> Self {
        self.columns = cols.max(1);
        self
    }
    /// Espacement global (horizontal et vertical) en pixels.
    pub fn gap(mut self, g: f64) -> Self {
        self.gap = g;
        self
    }
    /// Espacement horizontal en pixels.
    pub fn gap_x(mut self, gx: f64) -> Self {
        self.gap_x = Some(gx);
        self
    }
    /// Espacement vertical en pixels.
    pub fn gap_y(mut self, gy: f64) -> Self {
        self.gap_y = Some(gy);
        self
    }
    /// Ajoute un composant dans la grille.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }
}

impl Component for Grid {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "grid"
    }
    fn props(&self) -> Value {
        let mut obj = serde_json::Map::new();
        obj.insert("columns".into(), json!(self.columns));
        obj.insert("gap".into(), json!(self.gap));
        if let Some(gx) = self.gap_x {
            obj.insert("gap_x".into(), json!(gx));
        }
        if let Some(gy) = self.gap_y {
            obj.insert("gap_y".into(), json!(gy));
        }
        Value::Object(obj)
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Carte (panel) avec un titre et un corps empilé en colonne.
pub struct Panel {
    pub(crate) id: String,
    pub(crate) label: String,
    pub(crate) gap: f64,
    pub(crate) children: Vec<Box<dyn Component>>,
}

impl Panel {
    /// Crée une carte, avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            label: String::new(),
            gap: 16.0,
            children: Vec::new(),
        }
    }
    /// Titre affiché dans l'entête de la carte.
    pub fn label(mut self, l: impl Into<String>) -> Self {
        self.label = l.into();
        self
    }
    /// Espace entre les enfants, en pixels.
    pub fn gap(mut self, g: f64) -> Self {
        self.gap = g;
        self
    }
    /// Ajoute un composant dans la carte.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }
}

impl Component for Panel {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "panel"
    }
    fn props(&self) -> Value {
        json!({ "label": self.label, "gap": self.gap })
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Collecteur d'éléments pour une section d'onglet / accordéon.
pub struct SectionBuilder {
    pub(crate) children: Vec<Box<dyn Component>>,
    pub(crate) gap: f64,
}

impl Default for SectionBuilder {
    fn default() -> Self {
        Self {
            children: Vec::new(),
            gap: 14.0,
        }
    }
}

impl SectionBuilder {
    /// Ajoute un composant à la section en construction.
    pub fn item(&mut self, c: impl IntoBox) {
        self.children.push(c.into_box());
    }
    /// Ajoute une ligne fluide à la section.
    pub fn row(&mut self, task: impl FnOnce(&mut RowBuilder)) {
        let mut b = RowBuilder::default();
        task(&mut b);
        let mut r = Row::new(format!("row-{}", self.children.len())).gap(b.gap);
        for c in b.children {
            r.children.push(c);
        }
        self.children.push(Box::new(r));
    }
    /// Ajoute une colonne à la section.
    pub fn column(&mut self, task: impl FnOnce(&mut RowBuilder)) {
        let mut b = RowBuilder::default();
        task(&mut b);
        let mut col = Column::new(format!("col-{}", self.children.len())).gap(b.gap);
        for c in b.children {
            col.children.push(c);
        }
        self.children.push(Box::new(col));
    }
    /// Ajoute une grille à la section.
    pub fn grid(&mut self, columns: usize, task: impl FnOnce(&mut RowBuilder)) {
        let mut b = RowBuilder::default();
        task(&mut b);
        let mut g = Grid::new(format!("grid-{}", self.children.len()))
            .columns(columns)
            .gap(b.gap);
        for c in b.children {
            g.children.push(c);
        }
        self.children.push(Box::new(g));
    }
    /// Ajoute un panneau repliable à la section.
    pub fn panel(&mut self, label: impl Into<String>, task: impl FnOnce(&mut RowBuilder)) {
        let label = label.into();
        let mut b = RowBuilder::default();
        task(&mut b);
        let mut p = Panel::new(format!("panel-{}", label))
            .label(label)
            .gap(b.gap);
        for c in b.children {
            p.children.push(c);
        }
        self.children.push(Box::new(p));
    }
    /// Espace entre les composants de la section, en pixels.
    pub fn gap(&mut self, g: f64) {
        self.gap = g;
    }
}

/// Conteneur à onglets : chaque `.tab(label, …)` devient un panneau.
pub struct Tabs {
    id: String,
    labels: Vec<String>,
    children: Vec<Box<dyn Component>>,
    selected: usize,
}

impl Tabs {
    /// Crée un bloc d'onglets, avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            labels: Vec::new(),
            children: Vec::new(),
            selected: 0,
        }
    }
    /// Définit l'onglet sélectionné initialement (0-indexed).
    pub fn selected(mut self, idx: usize) -> Self {
        self.selected = idx;
        self
    }
    /// Ajoute un onglet (étiquette + contenu empilé en colonne).
    pub fn tab(mut self, label: impl Into<String>, task: impl FnOnce(&mut SectionBuilder)) -> Self {
        let mut b = SectionBuilder::default();
        task(&mut b);
        let mut col = Column::new(format!("{}-{}", self.id, self.labels.len())).gap(b.gap);
        for c in b.children {
            col.push(c);
        }
        self.labels.push(label.into());
        self.children.push(Box::new(col));
        self
    }
}

impl Component for Tabs {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "tabs"
    }
    fn props(&self) -> Value {
        json!({ "labels": self.labels, "selected": self.selected })
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Accordéon : liste de sections repliables (balises `<details>` natives).
pub struct Accordion {
    id: String,
    labels: Vec<String>,
    children: Vec<Box<dyn Component>>,
    open: bool,
}

impl Accordion {
    /// Crée un accordéon, avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            labels: Vec::new(),
            children: Vec::new(),
            open: false,
        }
    }
    /// La première section est dépliée au chargement.
    pub fn open(mut self, open: bool) -> Self {
        self.open = open;
        self
    }
    /// Ajoute une section repliable (titre + contenu empilé en colonne).
    pub fn section(
        mut self,
        label: impl Into<String>,
        task: impl FnOnce(&mut SectionBuilder),
    ) -> Self {
        let mut b = SectionBuilder::default();
        task(&mut b);
        let mut col = Column::new(format!("{}-{}", self.id, self.labels.len())).gap(b.gap);
        for c in b.children {
            col.push(c);
        }
        self.labels.push(label.into());
        self.children.push(Box::new(col));
        self
    }
}

impl Component for Accordion {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "accordion"
    }
    fn props(&self) -> Value {
        json!({ "labels": self.labels, "open": self.open })
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// Conteneur tiroir coulissant (horizontal : gauche/droite, ou vertical : bas/haut).
pub struct Drawer {
    pub(crate) id: String,
    pub(crate) title: String,
    pub(crate) placement: String,
    pub(crate) size: u32,
    pub(crate) open: bool,
    pub(crate) backdrop: bool,
    pub(crate) gap: f64,
    pub(crate) children: Vec<Box<dyn Component>>,
}

impl Drawer {
    /// Crée un tiroir coulissant avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            title: String::new(),
            placement: "right".into(),
            size: 380,
            open: false,
            backdrop: true,
            gap: 16.0,
            children: Vec::new(),
        }
    }

    /// Titre affiché dans l'entête du tiroir.
    pub fn title(mut self, t: impl Into<String>) -> Self {
        self.title = t.into();
        self
    }

    /// Position et direction du tiroir (`"left"`, `"right"`, `"bottom"`, `"top"`).
    pub fn placement(mut self, p: impl Into<String>) -> Self {
        self.placement = p.into();
        self
    }

    /// Taille du tiroir en pixels (largeur si left/right, hauteur si bottom/top).
    pub fn size(mut self, px: u32) -> Self {
        self.size = px;
        self
    }

    /// État initial ouvert/fermé.
    pub fn open(mut self, on: bool) -> Self {
        self.open = on;
        self
    }

    /// Affiche ou non le fond sombre translucide (backdrop).
    pub fn backdrop(mut self, on: bool) -> Self {
        self.backdrop = on;
        self
    }

    /// Espace entre les éléments à l'intérieur du tiroir en pixels.
    pub fn gap(mut self, g: f64) -> Self {
        self.gap = g;
        self
    }

    /// Ajoute un élément directement dans le tiroir.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }

    /// Configure le contenu du tiroir via un builder de section.
    pub fn content(mut self, task: impl FnOnce(&mut SectionBuilder)) -> Self {
        let mut b = SectionBuilder::default();
        task(&mut b);
        for c in b.children {
            self.children.push(c);
        }
        self
    }
}

impl Component for Drawer {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "drawer"
    }
    fn role(&self) -> Role {
        Role::Output
    }
    fn props(&self) -> Value {
        json!({
            "title": self.title,
            "placement": self.placement,
            "size": self.size,
            "open": self.open,
            "backdrop": self.backdrop,
            "gap": self.gap,
        })
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}

/// **DynamicContainer** : Conteneur dynamique (slot runtime) dont les enfants
/// peuvent être insérés, remplacés ou vidés à chaud depuis le serveur (`Context`).
///
/// ```rust
/// # use grio::*;
/// DynamicContainer::new("slot_ticket_details")
///     .item(Text::new("placeholder").value("Sélectionnez un ticket pour afficher les détails."));
/// ```
pub struct DynamicContainer {
    id: String,
    children: Vec<Box<dyn Component>>,
    direction: String,
    gap: f32,
}

impl DynamicContainer {
    /// Crée un nouveau conteneur dynamique avec son identifiant.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            children: Vec::new(),
            direction: "column".into(),
            gap: 12.0,
        }
    }

    /// Espacement entre les composants enfants injectés (en pixels).
    pub fn gap(mut self, g: f32) -> Self {
        self.gap = g;
        self
    }

    /// Direction d'empilement (`column` ou `row`).
    pub fn direction(mut self, d: impl Into<String>) -> Self {
        self.direction = d.into();
        self
    }

    /// Ajoute un composant initial par défaut.
    pub fn item(mut self, c: impl IntoBox) -> Self {
        self.children.push(c.into_box());
        self
    }

    /// Remplace ou configure les composants initiaux via une closure.
    pub fn content(mut self, task: impl FnOnce(&mut SectionBuilder)) -> Self {
        let mut b = SectionBuilder::default();
        task(&mut b);
        for c in b.children {
            self.children.push(c);
        }
        self
    }
}

impl Component for DynamicContainer {
    fn id(&self) -> &str {
        &self.id
    }
    fn kind(&self) -> &'static str {
        "dynamic_container"
    }
    fn role(&self) -> Role {
        Role::Output
    }
    fn props(&self) -> Value {
        json!({
            "direction": self.direction,
            "gap": self.gap,
        })
    }
    fn children(&self) -> Vec<&dyn Component> {
        children_refs(&self.children)
    }
}