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_color(cx.theme().colors().border_variant)
57 .map(|div| match vertical_alignment {
58 VerticalAlignment::Top => div.items_start(),
59 VerticalAlignment::Center => div.items_center(),
60 })
61 .map(|div| match vertical_alignment {
62 VerticalAlignment::Top => div.content_start(),
63 VerticalAlignment::Center => div.content_center(),
64 })
65 .map(|div| match font_type {
66 FontType::Ui => div.font_ui(cx),
67 FontType::Monospace => div.font_buffer(cx),
68 })
69 .tooltip(Tooltip::text(cell_content.clone()))
70 .child(div().child(cell_content))
71}