1//! Table Cell Rendering
2
3use gpui::{AnyElement, ElementId};
4use ui::{SharedString, Tooltip, div, prelude::*};
5
6use crate::{
7 CsvPreviewView,
8 settings::{FontType, VerticalAlignment},
9 types::DisplayCellId,
10};
11
12impl CsvPreviewView {
13 /// Create selectable table cell with mouse event handlers.
14 pub fn create_selectable_cell(
15 display_cell_id: DisplayCellId,
16 cell_content: SharedString,
17 vertical_alignment: VerticalAlignment,
18 font_type: FontType,
19 cx: &Context<CsvPreviewView>,
20 ) -> AnyElement {
21 create_table_cell(
22 display_cell_id,
23 cell_content,
24 vertical_alignment,
25 font_type,
26 cx,
27 )
28 // Mouse events handlers will be here
29 .into_any_element()
30 }
31}
32
33/// Create styled table cell div element.
34fn create_table_cell(
35 display_cell_id: DisplayCellId,
36 cell_content: SharedString,
37 vertical_alignment: VerticalAlignment,
38 font_type: FontType,
39 cx: &Context<'_, CsvPreviewView>,
40) -> gpui::Stateful<Div> {
41 div()
42 .id(ElementId::NamedInteger(
43 format!(
44 "csv-display-cell-{}-{}",
45 *display_cell_id.row, *display_cell_id.col
46 )
47 .into(),
48 0,
49 ))
50 .cursor_pointer()
51 .flex()
52 .h_full()
53 .px_1()
54 .bg(cx.theme().colors().editor_background)
55 .border_b_1()
56 .border_r_1()
57 .border_color(cx.theme().colors().border_variant)
58 .map(|div| match vertical_alignment {
59 VerticalAlignment::Top => div.items_start(),
60 VerticalAlignment::Center => div.items_center(),
61 })
62 .map(|div| match vertical_alignment {
63 VerticalAlignment::Top => div.content_start(),
64 VerticalAlignment::Center => div.content_center(),
65 })
66 .map(|div| match font_type {
67 FontType::Ui => div.font_ui(cx),
68 FontType::Monospace => div.font_buffer(cx),
69 })
70 .tooltip(Tooltip::text(cell_content.clone()))
71 .child(div().child(cell_content))
72}