Files
zed/crates/csv_preview/src/renderer/table_cell.rs
T
Oleksandr KholiavkoandAnthony Eid ae472aac1d Rework column/table width API in data table (#51060)
data_table: Replace column width builder API with `ColumnWidthConfig`
enum

This PR consolidates the data table width configuration API from three
separate builder methods (`.column_widths()`, `.resizable_columns()`,
`.width()`) into a single `.width_config(ColumnWidthConfig)` call. This
makes invalid state combinations unrepresentable and clarifies the two
distinct width management modes.

**What changed:**

- Introduces `ColumnWidthConfig` enum with two variants:
  - `Static`: Fixed column widths, no resize handles
- `Redistributable`: Drag-to-resize columns that redistribute space
within a fixed table width
- Introduces `TableResizeBehavior` enum (`None`, `Resizable`,
`MinSize(f32)`) for per-column resize policy
- Renames `TableColumnWidths` → `RedistributableColumnsState` to better
reflect its purpose
- Extracts all width management logic into a new `width_management.rs`
module
- Updates all callers: `csv_preview`, `git_graph`, `keymap_editor`,
`edit_prediction_context_view`

```rust
pub enum ColumnWidthConfig {
    /// Static column widths (no resize handles).
    Static {
        widths: StaticColumnWidths,
        /// Controls widths of the whole table.
        table_width: Option<DefiniteLength>,
    },
    /// Redistributable columns — dragging redistributes the fixed available space
    /// among columns without changing the overall table width.
    Redistributable {
        entity: Entity<RedistributableColumnsState>,
        table_width: Option<DefiniteLength>,
    },
}
```

**Why:**

The old API allowed callers to combine methods incorrectly. The new
enum-based design enforces correct usage at compile time and provides a
clearer path for adding independently resizable columns in PR #3.

**Context:**

This is part 2 of a 3-PR series improving data table column width
handling:
1. [#51059](https://github.com/zed-industries/zed/pull/51059) - Extract
modules into separate files (mechanical change)
2. **This PR**: Introduce width config enum for redistributable column
widths (API rework)
3. Implement independently resizable column widths (new feature)

The series builds on previously merged infrastructure:
- [#46341](https://github.com/zed-industries/zed/pull/46341) - Data
table dynamic column support
- [#46190](https://github.com/zed-industries/zed/pull/46190) - Variable
row height mode for data tables

Primary beneficiary: CSV preview feature
([#48207](https://github.com/zed-industries/zed/pull/48207))


### Anthony's note

This PR also fixes the table dividers being a couple pixels off, and the
csv preview from having double line rendering for a single column in
some cases.

Before you mark this PR as ready for review, make sure that you have:
- [x] Added a solid test coverage and/or screenshots from doing manual
testing
- [x] Done a self-review taking into account security and performance
aspects
- [ ] Aligned any UI changes with the [UI
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)

Release Notes:

- N/A

---------

Co-authored-by: Anthony Eid <anthony@zed.dev>
2026-04-01 05:13:24 +00:00

72 lines
2.1 KiB
Rust

//! Table Cell Rendering
use gpui::{AnyElement, ElementId};
use ui::{SharedString, Tooltip, div, prelude::*};
use crate::{
CsvPreviewView,
settings::{FontType, VerticalAlignment},
types::DisplayCellId,
};
impl CsvPreviewView {
/// Create selectable table cell with mouse event handlers.
pub fn create_selectable_cell(
display_cell_id: DisplayCellId,
cell_content: SharedString,
vertical_alignment: VerticalAlignment,
font_type: FontType,
cx: &Context<CsvPreviewView>,
) -> AnyElement {
create_table_cell(
display_cell_id,
cell_content,
vertical_alignment,
font_type,
cx,
)
// Mouse events handlers will be here
.into_any_element()
}
}
/// Create styled table cell div element.
fn create_table_cell(
display_cell_id: DisplayCellId,
cell_content: SharedString,
vertical_alignment: VerticalAlignment,
font_type: FontType,
cx: &Context<'_, CsvPreviewView>,
) -> gpui::Stateful<Div> {
div()
.id(ElementId::NamedInteger(
format!(
"csv-display-cell-{}-{}",
*display_cell_id.row, *display_cell_id.col
)
.into(),
0,
))
.cursor_pointer()
.flex()
.h_full()
.px_1()
.bg(cx.theme().colors().editor_background)
.border_b_1()
.border_color(cx.theme().colors().border_variant)
.map(|div| match vertical_alignment {
VerticalAlignment::Top => div.items_start(),
VerticalAlignment::Center => div.items_center(),
})
.map(|div| match vertical_alignment {
VerticalAlignment::Top => div.content_start(),
VerticalAlignment::Center => div.content_center(),
})
.map(|div| match font_type {
FontType::Ui => div.font_ui(cx),
FontType::Monospace => div.font_buffer(cx),
})
.tooltip(Tooltip::text(cell_content.clone()))
.child(div().child(cell_content))
}