Files
zed/crates/csv_preview/src/renderer/table_cell.rs
T
Oleksandr Kholiavko d8664715ad csv_preview: Allow copying cell and column header content (#61769)
# Objective

- There was no way to copy a cell's or a column header's text out of the
CSV preview table (at all).

## Solution

- Right-clicking a table cell now copies its full content to the
clipboard; the cell tooltip shows the content plus a "Right click to
copy content" hint.
- Right-clicking a column header name copies the column name to the
clipboard, with the same tooltip pattern ("Right click to copy column
name").

## Testing

- Right-click a cell: content is copied, tooltip shows the hint.
- Right-click a column header: column name is copied, tooltip shows the
hint.

## Demo

<img width="994" height="241" alt="image"
src="https://github.com/user-attachments/assets/827a0356-e152-4bfd-84ea-5745e638f13c"
/>


## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Added copy-to-clipboard on right-click for CSV preview cells and
column headers
2026-08-13 22:03:46 +00:00

71 lines
2.5 KiB
Rust

//! Table Cell Rendering
use gpui::{AnyElement, ClipboardItem, ElementId, MouseButton, StatefulInteractiveElement};
use ui::{Color, Divider, Label, LabelSize, SharedString, Tooltip, div, prelude::*};
use crate::{CsvPreviewView, settings::VerticalAlignment, types::DisplayCellId};
/// Adds a right-click-to-copy handler and a tooltip showing `text` plus `hint`
/// (e.g. "Right click to copy content") to a `Stateful` element.
pub(crate) fn with_copy_on_right_click<E: StatefulInteractiveElement>(
element: E,
text: SharedString,
hint: &'static str,
) -> E {
element
.on_mouse_down(MouseButton::Right, {
let text_to_copy = text.clone();
move |_event, _window, cx| {
cx.stop_propagation();
cx.write_to_clipboard(ClipboardItem::new_string(text_to_copy.to_string()));
}
})
.tooltip(Tooltip::element(move |_window, cx| {
v_flex()
.gap_1()
.child(div().font_buffer(cx).child(text.clone()))
.child(Divider::horizontal())
.child(Label::new(hint).size(LabelSize::Small).color(Color::Muted))
.into_any_element()
}))
}
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,
cx: &Context<CsvPreviewView>,
) -> AnyElement {
create_table_cell(display_cell_id, cell_content, vertical_alignment, 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,
cx: &Context<'_, CsvPreviewView>,
) -> gpui::Stateful<Div> {
let cell = div()
.id(ElementId::NamedInteger(
format!("csv-display-cell-{}", *display_cell_id.row).into(),
*display_cell_id.col as u64,
))
.flex()
.h_full()
.px_1()
.border_color(cx.theme().colors().border_variant)
.map(|div| match vertical_alignment {
VerticalAlignment::Top => div.items_start(),
VerticalAlignment::Center => div.items_center(),
})
.font_buffer(cx);
with_copy_on_right_click(cell, cell_content.clone(), "Right click to copy content")
.child(div().child(cell_content))
}