rgpui 1.3.0

GUI 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use crate::{
    AnyElement, App, InteractiveElement as _, IntoElement, ParentElement, Pixels, RenderOnce,
    StyleRefinement, Styled, TextAlign, Window, div, prelude::FluentBuilder as _, px, relative,
};

use crate::{
    ActiveTheme as _, AnyChildElement, ChildElement, ElementSize, Sizable, StyledExt as _,
};

const MIN_CELL_WIDTH: Pixels = px(100.);

/// 用于直接渲染表格数据的基础表格组件。
///
/// 与 [`DataTable`](crate::DataTable) 不同,这是一个简单的、无状态的、
/// 可组合的表格,不包含虚拟滚动或列管理。
///
/// 通过 [`Sizable`] 设置的尺寸会自动传递给所有子元素。
///
/// # 示例
///
/// ```rust,ignore
/// Table::new()
///     .small()
///     .child(TableHeader::new().child(
///         TableRow::new()
///             .child(TableHead::new().child("Name"))
///             .child(TableHead::new().child("Email"))
///     ))
///     .child(TableBody::new()
///         .child(TableRow::new()
///             .child(TableCell::new().child("John"))
///             .child(TableCell::new().child("john@example.com")))
///     )
///     .child(TableCaption::new().child("A list of recent invoices."))
/// ```
#[derive(IntoElement)]
pub struct Table {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyChildElement>,
    size: ElementSize,
}

impl Table {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }

    /// 添加一个子元素到组件中。
    pub fn child(mut self, child: impl ChildElement + 'static) -> Self {
        self.children.push(AnyChildElement::new(child));
        self
    }

    /// 批量添加多个子元素到组件中。
    pub fn children<E: ChildElement + 'static>(
        mut self,
        children: impl IntoIterator<Item = E>,
    ) -> Self {
        self.children
            .extend(children.into_iter().map(AnyChildElement::new));
        self
    }
}

impl Styled for Table {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Sizable for Table {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for Table {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl RenderOnce for Table {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        div()
            .id(("table", self.ix))
            .w_full()
            .text_sm()
            .overflow_hidden()
            .bg(cx.theme().tokens.table)
            .refine_style(&self.style)
            .children(
                self.children
                    .into_iter()
                    .enumerate()
                    .map(|(ix, c)| c.into_any(ix, self.size)),
            )
    }
}

/// [`Table`] 的头部区域,包裹头部行。
#[derive(IntoElement)]
pub struct TableHeader {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyChildElement>,
    size: ElementSize,
}

impl TableHeader {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }

    /// 添加一个子元素到组件中。
    pub fn child(mut self, child: impl ChildElement + 'static) -> Self {
        self.children.push(AnyChildElement::new(child));
        self
    }

    /// 批量添加多个子元素到组件中。
    pub fn children<E: ChildElement + 'static>(
        mut self,
        children: impl IntoIterator<Item = E>,
    ) -> Self {
        self.children
            .extend(children.into_iter().map(AnyChildElement::new));
        self
    }
}

impl Styled for TableHeader {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl ChildElement for TableHeader {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl Sizable for TableHeader {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl RenderOnce for TableHeader {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        div()
            .id(("table-header", self.ix))
            .w_full()
            .bg(cx.theme().tokens.table_head)
            .text_color(cx.theme().table_head_foreground)
            .refine_style(&self.style)
            .border_b_1()
            .border_color(cx.theme().table_row_border)
            .children(
                self.children
                    .into_iter()
                    .enumerate()
                    .map(|(ix, c)| c.into_any(ix, self.size)),
            )
    }
}

/// [`Table`] 的主体区域,包裹数据行。
#[derive(IntoElement)]
pub struct TableBody {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyChildElement>,
    size: ElementSize,
}

impl TableBody {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }

    /// 添加一个子元素到组件中。
    pub fn child(mut self, child: impl ChildElement + 'static) -> Self {
        self.children.push(AnyChildElement::new(child));
        self
    }

    /// 批量添加多个子元素到组件中。
    pub fn children<E: ChildElement + 'static>(
        mut self,
        children: impl IntoIterator<Item = E>,
    ) -> Self {
        self.children
            .extend(children.into_iter().map(AnyChildElement::new));
        self
    }
}

impl Styled for TableBody {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Sizable for TableBody {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableBody {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl RenderOnce for TableBody {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        div()
            .id(("table-body", self.ix))
            .w_full()
            .refine_style(&self.style)
            .children(
                self.children
                    .into_iter()
                    .enumerate()
                    .map(|(ix, c)| c.into_any(ix, self.size)),
            )
    }
}

/// [`Table`] 的底部区域,包裹底部行。
#[derive(IntoElement)]
pub struct TableFooter {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyChildElement>,
    size: ElementSize,
}

impl TableFooter {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }

    /// 添加一个子元素到组件中。
    pub fn child(mut self, child: impl ChildElement + 'static) -> Self {
        self.children.push(AnyChildElement::new(child));
        self
    }

    /// 批量添加多个子元素到组件中。
    pub fn children<E: ChildElement + 'static>(
        mut self,
        children: impl IntoIterator<Item = E>,
    ) -> Self {
        self.children
            .extend(children.into_iter().map(AnyChildElement::new));
        self
    }
}

impl Styled for TableFooter {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Sizable for TableFooter {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableFooter {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl RenderOnce for TableFooter {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        div()
            .id(("table-footer", self.ix))
            .w_full()
            .bg(cx.theme().tokens.table_foot)
            .text_color(cx.theme().table_foot_foreground)
            .border_t_1()
            .border_color(cx.theme().table_row_border)
            .refine_style(&self.style)
            .children(
                self.children
                    .into_iter()
                    .enumerate()
                    .map(|(ix, c)| c.into_any(ix, self.size)),
            )
    }
}

/// [`Table`] 中的一行。
#[derive(IntoElement)]
pub struct TableRow {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyChildElement>,
    size: ElementSize,
}

impl TableRow {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }

    /// 添加一个子元素到组件中。
    pub fn child(mut self, child: impl ChildElement + 'static) -> Self {
        self.children.push(AnyChildElement::new(child));
        self
    }

    /// 批量添加多个子元素到组件中。
    pub fn children<E: ChildElement + 'static>(
        mut self,
        children: impl IntoIterator<Item = E>,
    ) -> Self {
        self.children
            .extend(children.into_iter().map(AnyChildElement::new));
        self
    }
}

impl Styled for TableRow {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Sizable for TableRow {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableRow {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl RenderOnce for TableRow {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        div()
            .id(("table-row", self.ix))
            .w_full()
            .flex()
            .flex_row()
            .refine_style(&self.style)
            .border_color(cx.theme().table_row_border)
            .when(self.ix > 0, |this| this.border_t_1())
            .children(
                self.children
                    .into_iter()
                    .enumerate()
                    .map(|(ix, c)| c.into_any(ix, self.size)),
            )
    }
}

/// [`TableRow`] 中的表头单元格。
#[derive(IntoElement)]
pub struct TableHead {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyElement>,
    col_span: usize,
    align: TextAlign,
    size: ElementSize,
}

impl TableHead {
    /// 创建新的单元格组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            col_span: 1,
            align: TextAlign::Left,
            size: ElementSize::default(),
        }
    }

    /// 设置此表头单元格的列跨度。
    pub fn col_span(mut self, span: usize) -> Self {
        self.col_span = span.max(1);
        self
    }

    /// 设置文本居中对齐。
    pub fn text_center(mut self) -> Self {
        self.align = TextAlign::Center;
        self
    }

    /// 设置文本右对齐。
    pub fn text_right(mut self) -> Self {
        self.align = TextAlign::Right;
        self
    }
}

impl ParentElement for TableHead {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Sizable for TableHead {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableHead {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl Styled for TableHead {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for TableHead {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let paddings = self.size.table_cell_padding();

        div()
            .id(("table-head", self.ix))
            .flex()
            .items_center()
            .when(self.style.size.width.is_none(), |this| {
                this.flex_shrink_1()
                    .flex_basis(relative(self.col_span as f32))
            })
            .min_w(MIN_CELL_WIDTH * self.col_span)
            .px(paddings.left)
            .py(paddings.top)
            .when(self.align == TextAlign::Center, |this| {
                this.justify_center()
            })
            .when(self.align == TextAlign::Right, |this| this.justify_end())
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// [`TableRow`] 中的数据单元格。
#[derive(IntoElement)]
pub struct TableCell {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyElement>,
    col_span: usize,
    align: TextAlign,
    size: ElementSize,
}

impl TableCell {
    /// 创建新的单元格组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            col_span: 1,
            align: TextAlign::Left,
            size: ElementSize::default(),
        }
    }

    /// 设置此单元格的列跨度。
    pub fn col_span(mut self, span: usize) -> Self {
        self.col_span = span.max(1);
        self
    }

    /// 设置文本居中对齐。
    pub fn text_center(mut self) -> Self {
        self.align = TextAlign::Center;
        self
    }

    /// 设置文本右对齐。
    pub fn text_right(mut self) -> Self {
        self.align = TextAlign::Right;
        self
    }
}

impl ParentElement for TableCell {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Sizable for TableCell {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableCell {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl Styled for TableCell {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for TableCell {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let paddings = self.size.table_cell_padding();

        div()
            .id(("table-cell", self.ix))
            .flex()
            .items_center()
            .when(self.style.size.width.is_none(), |this| {
                this.flex_shrink_1()
                    .flex_basis(relative(self.col_span as f32))
            })
            .min_w(MIN_CELL_WIDTH * self.col_span)
            .px(paddings.left)
            .py(paddings.top)
            .when(self.align == TextAlign::Center, |this| {
                this.justify_center()
            })
            .when(self.align == TextAlign::Right, |this| this.justify_end())
            .refine_style(&self.style)
            .children(self.children)
    }
}

/// 显示在 [`Table`] 下方的说明。
#[derive(IntoElement)]
pub struct TableCaption {
    ix: usize,
    style: StyleRefinement,
    children: Vec<AnyElement>,
    size: ElementSize,
}

impl TableCaption {
    /// 创建新的组件实例。
    pub fn new() -> Self {
        Self {
            ix: 0,
            style: StyleRefinement::default(),
            children: Vec::new(),
            size: ElementSize::default(),
        }
    }
}

impl ParentElement for TableCaption {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl Sizable for TableCaption {
    fn with_size(mut self, size: impl Into<ElementSize>) -> Self {
        self.size = size.into();
        self
    }
}

impl ChildElement for TableCaption {
    fn with_ix(mut self, ix: usize) -> Self {
        self.ix = ix;
        self
    }
}

impl Styled for TableCaption {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl RenderOnce for TableCaption {
    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
        let paddings = self.size.table_cell_padding();

        div()
            .id(("table-caption", self.ix))
            .w_full()
            .px(paddings.left)
            .py(paddings.top)
            .text_sm()
            .text_color(cx.theme().muted_foreground)
            .text_center()
            .refine_style(&self.style)
            .children(self.children)
    }
}