From 1ee034751ab87a81fefd14931df31e2acbe44d31 Mon Sep 17 00:00:00 2001 From: Ben Kunkle Date: Fri, 13 Jun 2025 09:25:51 +0200 Subject: [PATCH] click to focus row --- crates/settings_ui/src/keybindings.rs | 3 ++ crates/settings_ui/src/ui_components/table.rs | 49 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/crates/settings_ui/src/keybindings.rs b/crates/settings_ui/src/keybindings.rs index fac792a30a1b28e642f115583beef37319988819..0948aca8695cf4cc679d0035bb63f5412d2e883f 100644 --- a/crates/settings_ui/src/keybindings.rs +++ b/crates/settings_ui/src/keybindings.rs @@ -319,6 +319,9 @@ impl Render for KeymapEditor { .column_widths([rems(24.), rems(16.), rems(32.), rems(8.)]) .header(["Command", "Keystrokes", "Context", "Source"]) .selected_item_index(self.selected_index.clone()) + .on_click_row(cx.processor(|this, row_index, _window, _cx| { + this.selected_index = Some(row_index); + })) .uniform_list( "keymap-editor-table", row_count, diff --git a/crates/settings_ui/src/ui_components/table.rs b/crates/settings_ui/src/ui_components/table.rs index f36b7931df4b59edb04304d904ab40047fe3662f..bc5296438322b5e671988d0be12aa19b9b6563c4 100644 --- a/crates/settings_ui/src/ui_components/table.rs +++ b/crates/settings_ui/src/ui_components/table.rs @@ -1,4 +1,4 @@ -use std::{ops::Range, time::Duration}; +use std::{ops::Range, rc::Rc, time::Duration}; use editor::{EditorSettings, ShowScrollbar, scroll::ScrollbarAutoHide}; use gpui::{ @@ -355,6 +355,7 @@ pub struct Table { interaction_state: Option>, selected_item_index: Option, column_widths: Option<[Length; COLS]>, + on_click_row: Option>, } impl Table { @@ -368,6 +369,7 @@ impl Table { interaction_state: None, selected_item_index: None, column_widths: None, + on_click_row: None, } } @@ -428,6 +430,14 @@ impl Table { self.column_widths = Some(widths.map(Into::into)); self } + + pub fn on_click_row( + mut self, + callback: impl Fn(usize, &mut Window, &mut App) + 'static, + ) -> Self { + self.on_click_row = Some(Rc::new(callback)); + self + } } fn base_cell_style(width: Option, cx: &App) -> Div { @@ -460,7 +470,7 @@ pub fn render_row( .map_or([None; COLS], |widths| widths.map(|width| Some(width))); let is_selected = table_context.selected_item_index == Some(row_index); - div() + let row = div() .w_full() .border_2() .border_color(transparent_black()) @@ -489,8 +499,15 @@ pub fn render_row( .zip(column_widths) .map(|(cell, width)| base_cell_style(width, cx).child(cell)), ), - ) - .into_any_element() + ); + + if let Some(on_click) = table_context.on_click_row { + row.id(ElementId::named_usize("table-row", row_index)) + .on_click(move |_, window, cx| on_click(row_index, window, cx)) + .into_any_element() + } else { + row.into_any_element() + } } pub fn render_header( @@ -517,12 +534,13 @@ pub fn render_header( })) } -#[derive(Clone, Copy)] +#[derive(Clone)] pub struct TableRenderContext { pub striped: bool, pub total_row_count: usize, pub selected_item_index: Option, pub column_widths: Option<[Length; COLS]>, + pub on_click_row: Option>, } impl TableRenderContext { @@ -532,6 +550,7 @@ impl TableRenderContext { total_row_count: table.rows.len(), column_widths: table.column_widths, selected_item_index: table.selected_item_index.clone(), + on_click_row: table.on_click_row.clone(), } } } @@ -581,7 +600,7 @@ impl RenderOnce for Table { }) }) .when_some(self.headers.take(), |this, headers| { - this.child(render_header(headers, table_context, cx)) + this.child(render_header(headers, table_context.clone(), cx)) }) .child( div() @@ -590,12 +609,11 @@ impl RenderOnce for Table { .relative() .overflow_hidden() .map(|parent| match self.rows { - TableContents::Vec(items) => parent.children( - items - .into_iter() - .enumerate() - .map(|(index, row)| render_row(index, row, table_context, cx)), - ), + TableContents::Vec(items) => { + parent.children(items.into_iter().enumerate().map(|(index, row)| { + render_row(index, row, table_context.clone(), cx) + })) + } TableContents::UniformList(uniform_list_data) => parent.child( uniform_list( uniform_list_data.element_id, @@ -608,7 +626,12 @@ impl RenderOnce for Table { .into_iter() .zip(range) .map(|(row, row_index)| { - render_row(row_index, row, table_context, cx) + render_row( + row_index, + row, + table_context.clone(), + cx, + ) }) .collect() }