reflow table api so that it matches structure of actual table better

Ben Kunkle created

Change summary

crates/settings_ui/src/keybindings.rs         | 55 ++++++++++----------
crates/settings_ui/src/ui_components/table.rs | 36 ++++++------
2 files changed, 46 insertions(+), 45 deletions(-)

Detailed changes

crates/settings_ui/src/keybindings.rs 🔗

@@ -146,33 +146,34 @@ impl Render for KeymapEditor {
             .id("keymap-editor")
             .track_focus(&self.focus_handle)
             .child(
-                Table::uniform_list(
-                    "keymap-editor-table",
-                    row_count,
-                    cx.processor(|this, range: Range<usize>, window, cx| {
-                        range
-                            .map(|index| {
-                                let binding = &this.processed_bindings[index];
-                                let row = [
-                                    binding.action.clone(),
-                                    binding.keystroke_text.clone(),
-                                    binding.context.clone(),
-                                    // TODO: Add a source field
-                                    // binding.source.clone(),
-                                ];
-
-                                // fixme: pass through callback as a row_cx param
-                                let striped = false;
-
-                                crate::ui_components::table::render_row(
-                                    index, row, row_count, striped, cx,
-                                )
-                            })
-                            .collect()
-                    }),
-                )
-                .header(["Command", "Keystrokes", "Context"])
-                .interactable(&self.table_interaction_state),
+                Table::new()
+                    .interactable(&self.table_interaction_state)
+                    .header(["Command", "Keystrokes", "Context"])
+                    .uniform_list(
+                        "keymap-editor-table",
+                        row_count,
+                        cx.processor(move |this, range: Range<usize>, _window, cx| {
+                            range
+                                .map(|index| {
+                                    let binding = &this.processed_bindings[index];
+                                    let row = [
+                                        binding.action.clone(),
+                                        binding.keystroke_text.clone(),
+                                        binding.context.clone(),
+                                        // TODO: Add a source field
+                                        // binding.source.clone(),
+                                    ];
+
+                                    // fixme: pass through callback as a row_cx param
+                                    let striped = false;
+
+                                    crate::ui_components::table::render_row(
+                                        index, row, row_count, striped, cx,
+                                    )
+                                })
+                                .collect()
+                        }),
+                    ),
             )
     }
 }

crates/settings_ui/src/ui_components/table.rs 🔗

@@ -275,24 +275,6 @@ pub struct Table<const COLS: usize = 3> {
 }
 
 impl<const COLS: usize> Table<COLS> {
-    pub fn uniform_list(
-        id: impl Into<ElementId>,
-        row_count: usize,
-        render_item_fn: impl Fn(Range<usize>, &mut Window, &mut App) -> Vec<AnyElement> + 'static,
-    ) -> Self {
-        Table {
-            striped: false,
-            width: Length::Auto,
-            headers: None,
-            rows: TableContents::UniformList(UniformListData {
-                element_id: id.into(),
-                row_count: row_count,
-                render_item_fn: Box::new(render_item_fn),
-            }),
-            interaction_state: None,
-        }
-    }
-
     /// number of headers provided.
     pub fn new() -> Self {
         Table {
@@ -304,6 +286,24 @@ impl<const COLS: usize> Table<COLS> {
         }
     }
 
+    /// Enables uniform list rendering.
+    /// The provided function will be passed directly to the `uniform_list` element.
+    /// Therefore, if this method is called, any calls to [`Table::row`] before or after
+    /// this method is called will be ignored.
+    pub fn uniform_list(
+        mut self,
+        id: impl Into<ElementId>,
+        row_count: usize,
+        render_item_fn: impl Fn(Range<usize>, &mut Window, &mut App) -> Vec<AnyElement> + 'static,
+    ) -> Self {
+        self.rows = TableContents::UniformList(UniformListData {
+            element_id: id.into(),
+            row_count: row_count,
+            render_item_fn: Box::new(render_item_fn),
+        });
+        self
+    }
+
     /// Enables row striping.
     pub fn striped(mut self) -> Self {
         self.striped = true;