BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / card_table.rs

card_table.rs

card_table.rs 3.6 KiB Download Raw
1use gpui_kit::component::{h_flex, v_flex};
2use gpui_kit::prelude::FluentBuilder as _;
3use gpui_kit::*;
4use teams_core::{CardTable, ContainerStyle, HorizontalAlignment, VerticalAlignment};
5
6use super::adaptive_card::{CardContext, container_tint, items_view, selectable, sized_column};
7use crate::theme;
8
9const CELL_PADDING: f32 = 6.;
10const GRID_RADIUS: f32 = 4.;
11
12pub(super) fn table_view(table: &CardTable, id: &str, context: &CardContext) -> AnyElement {
13 let grid_color = grid_color(table.grid_style);
14 v_flex()
15 .w_full()
16 .overflow_hidden()
17 .rounded(px(GRID_RADIUS))
18 .when(table.show_grid_lines, |grid| {
19 grid.border_1().border_color(grid_color)
20 })
21 .children(table.rows.iter().enumerate().map(|(row_index, row)| {
22 let header = table.first_row_as_header && row_index == 0;
23 h_flex()
24 .w_full()
25 .items_stretch()
26 .when(header, |line| {
27 line.bg(theme::table_header())
28 .font_weight(FontWeight::SEMIBOLD)
29 })
30 .when_some(container_tint(row.style), |line, tint| line.bg(tint))
31 .when(row_index > 0 && table.show_grid_lines, |line| {
32 line.border_t_1().border_color(grid_color)
33 })
34 .children(row.cells.iter().enumerate().map(|(column_index, cell)| {
35 let cell_id = format!("{id}-{row_index}-{column_index}");
36 let body = selectable(
37 items_view(&cell.items, &cell_id, context, false).into_any_element(),
38 cell.select_action.as_ref(),
39 &cell_id,
40 false,
41 context,
42 );
43 let aligned = v_flex()
44 .min_w(px(0.))
45 .p(px(CELL_PADDING))
46 .overflow_hidden()
47 .when(column_index > 0 && table.show_grid_lines, |cell| {
48 cell.border_l_1().border_color(grid_color)
49 })
50 .when_some(container_tint(cell.style), |cell, tint| cell.bg(tint))
51 .map(|cell_box| {
52 match table.cell_vertical_alignment(row, cell, column_index) {
53 VerticalAlignment::Top => cell_box.justify_start(),
54 VerticalAlignment::Center => cell_box.justify_center(),
55 VerticalAlignment::Bottom => cell_box.justify_end(),
56 }
57 })
58 .map(
59 |cell_box| match table.cell_horizontal_alignment(row, column_index) {
60 HorizontalAlignment::Left => cell_box.text_left(),
61 HorizontalAlignment::Center => cell_box.text_center(),
62 HorizontalAlignment::Right => cell_box.text_right(),
63 },
64 );
65 sized_column(aligned, table.columns[column_index].width).child(body)
66 }))
67 }))
68 .into_any_element()
69}
70
71fn grid_color(style: ContainerStyle) -> Hsla {
72 match style {
73 ContainerStyle::Default => theme::border_strong(),
74 ContainerStyle::Emphasis => theme::text_faint(),
75 ContainerStyle::Accent => theme::accent(),
76 ContainerStyle::Good => theme::green(),
77 ContainerStyle::Warning => theme::amber(),
78 ContainerStyle::Attention => theme::red(),
79 }
80}