Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

csv_preview: Allow copying cell and column header content (#61769)

Oleksandr Kholiavkod8664715ad9b
diff --git a/crates/csv_preview/src/renderer/table_cell.rs b/crates/csv_preview/src/renderer/table_cell.rs
index e792bcac68d..2678497711f 100644
--- a/crates/csv_preview/src/renderer/table_cell.rs
+++ b/crates/csv_preview/src/renderer/table_cell.rs
@@ -1,10 +1,35 @@
//! Table Cell Rendering
-use gpui::{AnyElement, ElementId};
-use ui::{SharedString, Tooltip, div, prelude::*};
+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(
@@ -26,12 +51,11 @@ fn create_table_cell(
vertical_alignment: VerticalAlignment,
cx: &Context<'_, CsvPreviewView>,
) -> gpui::Stateful<Div> {
- div()
+ let cell = div()
.id(ElementId::NamedInteger(
format!("csv-display-cell-{}", *display_cell_id.row).into(),
*display_cell_id.col as u64,
))
- .cursor_pointer()
.flex()
.h_full()
.px_1()
@@ -40,7 +64,7 @@ fn create_table_cell(
VerticalAlignment::Top => div.items_start(),
VerticalAlignment::Center => div.items_center(),
})
- .font_buffer(cx)
- .tooltip(Tooltip::text(cell_content.clone()))
+ .font_buffer(cx);
+ with_copy_on_right_click(cell, cell_content.clone(), "Right click to copy content")
.child(div().child(cell_content))
}
diff --git a/crates/csv_preview/src/renderer/table_header.rs b/crates/csv_preview/src/renderer/table_header.rs
index 44f93bae75c..9c0f41ebdd3 100644
--- a/crates/csv_preview/src/renderer/table_header.rs
+++ b/crates/csv_preview/src/renderer/table_header.rs
@@ -18,6 +18,7 @@ use ui::{
use crate::{
CsvPreviewView,
+ renderer::table_cell::with_copy_on_right_click,
settings::FilterSortOrder,
table_data_engine::{
filtering_by_column::{FilterEntry, FilterEntryState},
@@ -549,14 +550,23 @@ impl CsvPreviewView {
.items_center()
.font_buffer(cx)
.text_buffer(cx)
- .child(
- div()
+ .child({
+ let header_text_cell = div()
+ .id(ElementId::NamedInteger(
+ "csv-col-header-text".into(),
+ col_idx.get() as u64,
+ ))
.flex_1()
.min_w_0()
.overflow_hidden()
- .whitespace_nowrap()
- .child(header_text),
- )
+ .whitespace_nowrap();
+ with_copy_on_right_click(
+ header_text_cell,
+ header_text.clone(),
+ "Right click to copy column name",
+ )
+ .child(header_text)
+ })
.child(
GradientFade::new(base_bg, base_bg, base_bg)
.width(grad_width)