feat: Update CSV preview with resizable column

HalavicH created

Change summary

crates/csv_preview/src/csv_preview.rs           | 31 +++++++-----------
crates/csv_preview/src/renderer/render_table.rs | 10 +++---
crates/ui/src/components/data_table.rs          |  4 ++
3 files changed, 21 insertions(+), 24 deletions(-)

Detailed changes

crates/csv_preview/src/csv_preview.rs 🔗

@@ -10,8 +10,8 @@ use std::{
 
 use crate::table_data_engine::TableDataEngine;
 use ui::{
-    AbsoluteLength, DefiniteLength, RedistributableColumnsState, SharedString,
-    TableInteractionState, TableResizeBehavior, prelude::*,
+    AbsoluteLength, ResizableColumnsState, SharedString, TableInteractionState,
+    TableResizeBehavior, prelude::*,
 };
 use workspace::{Item, SplitDirection, Workspace};
 
@@ -56,27 +56,20 @@ pub fn init(cx: &mut App) {
 
 impl CsvPreviewView {
     pub(crate) fn sync_column_widths(&self, cx: &mut Context<Self>) {
-        // plus 1 for the rows column
+        // plus 1 for the row identifier column
         let cols = self.engine.contents.headers.cols() + 1;
-        let remaining_col_number = cols.saturating_sub(1);
-        let fraction = if remaining_col_number > 0 {
-            1. / remaining_col_number as f32
-        } else {
-            1.
-        };
-        let mut widths = vec![DefiniteLength::Fraction(fraction); cols];
         let line_number_width = self.calculate_row_identifier_column_width();
-        widths[0] = DefiniteLength::Absolute(AbsoluteLength::Pixels(line_number_width.into()));
+
+        let mut widths: Vec<AbsoluteLength> =
+            vec![AbsoluteLength::Pixels(px(150.)); cols];
+        widths[0] = AbsoluteLength::Pixels(px(line_number_width));
 
         let mut resize_behaviors = vec![TableResizeBehavior::Resizable; cols];
         resize_behaviors[0] = TableResizeBehavior::None;
 
         self.column_widths.widths.update(cx, |state, _cx| {
-            if state.cols() != cols
-                || state.initial_widths().as_slice() != widths.as_slice()
-                || state.resize_behavior().as_slice() != resize_behaviors.as_slice()
-            {
-                *state = RedistributableColumnsState::new(cols, widths, resize_behaviors);
+            if state.cols() != cols {
+                *state = ResizableColumnsState::new(cols, widths, resize_behaviors);
             }
         });
     }
@@ -313,16 +306,16 @@ impl PerformanceMetrics {
 
 /// Holds state of column widths for a table component in CSV preview.
 pub(crate) struct ColumnWidths {
-    pub widths: Entity<RedistributableColumnsState>,
+    pub widths: Entity<ResizableColumnsState>,
 }
 
 impl ColumnWidths {
     pub(crate) fn new(cx: &mut Context<CsvPreviewView>, cols: usize) -> Self {
         Self {
             widths: cx.new(|_cx| {
-                RedistributableColumnsState::new(
+                ResizableColumnsState::new(
                     cols,
-                    vec![ui::DefiniteLength::Fraction(1.0 / cols as f32); cols],
+                    vec![AbsoluteLength::Pixels(px(150.)); cols],
                     vec![ui::TableResizeBehavior::Resizable; cols],
                 )
             }),

crates/csv_preview/src/renderer/render_table.rs 🔗

@@ -2,7 +2,7 @@ use crate::types::TableCell;
 use gpui::{AnyElement, Entity};
 use std::ops::Range;
 use ui::{
-    ColumnWidthConfig, RedistributableColumnsState, Table, UncheckedTableRow, div, prelude::*,
+    ColumnWidthConfig, ResizableColumnsState, Table, UncheckedTableRow, div, prelude::*,
 };
 
 use crate::{
@@ -13,10 +13,10 @@ use crate::{
 
 impl CsvPreviewView {
     /// Creates a new table.
-    /// Column number is derived from the `RedistributableColumnsState` entity.
+    /// Column number is derived from the `ResizableColumnsState` entity.
     pub(crate) fn create_table(
         &self,
-        current_widths: &Entity<RedistributableColumnsState>,
+        current_widths: &Entity<ResizableColumnsState>,
         cx: &mut Context<Self>,
     ) -> AnyElement {
         self.create_table_inner(self.engine.contents.rows.len(), current_widths, cx)
@@ -25,7 +25,7 @@ impl CsvPreviewView {
     fn create_table_inner(
         &self,
         row_count: usize,
-        current_widths: &Entity<RedistributableColumnsState>,
+        current_widths: &Entity<ResizableColumnsState>,
         cx: &mut Context<Self>,
     ) -> AnyElement {
         let cols = current_widths.read(cx).cols();
@@ -54,7 +54,7 @@ impl CsvPreviewView {
         Table::new(cols)
             .interactable(&self.table_interaction_state)
             .striped()
-            .width_config(ColumnWidthConfig::redistributable(current_widths.clone()))
+            .width_config(ColumnWidthConfig::Resizable(current_widths.clone()))
             .header(headers)
             .disable_base_style()
             .map(|table| {

crates/ui/src/components/data_table.rs 🔗

@@ -61,6 +61,10 @@ impl ResizableColumnsState {
         }
     }
 
+    pub fn cols(&self) -> usize {
+        self.widths.cols()
+    }
+
     pub fn resize_behavior(&self) -> &TableRow<TableResizeBehavior> {
         &self.resize_behavior
     }