Skip to content

Commit

Permalink
update code style
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jan 13, 2025
1 parent 91f9a4c commit af6a3b4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 40 deletions.
28 changes: 27 additions & 1 deletion crates/story/src/table_story.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::time::{self, Duration};
use std::{
ops::Range,
time::{self, Duration},
};

use fake::{Fake, Faker};
use gpui::{
Expand All @@ -10,6 +13,7 @@ use serde::Deserialize;
use ui::{
button::Button,
checkbox::Checkbox,
divider::Divider,

Check failure on line 16 in crates/story/src/table_story.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `divider::Divider`
h_flex,
indicator::Indicator,
input::{InputEvent, TextInput},
Expand Down Expand Up @@ -181,6 +185,8 @@ struct StockTableDelegate {
loading: bool,
fixed_cols: bool,
is_eof: bool,
visible_rows: Range<usize>,
visible_cols: Range<usize>,
}

impl StockTableDelegate {
Expand Down Expand Up @@ -250,6 +256,8 @@ impl StockTableDelegate {
fixed_cols: false,
loading: false,
is_eof: false,
visible_cols: Range::default(),
visible_rows: Range::default(),
}
}

Expand Down Expand Up @@ -490,6 +498,22 @@ impl TableDelegate for StockTableDelegate {
})
.detach();
}

fn visible_rows_changed(
&mut self,
visible_range: Range<usize>,
_: &mut ViewContext<Table<Self>>,
) {
self.visible_rows = visible_range;
}

fn visible_cols_changed(
&mut self,
visible_range: Range<usize>,
_: &mut ViewContext<Table<Self>>,
) {
self.visible_cols = visible_range;
}
}

pub struct TableStory {
Expand Down Expand Up @@ -842,6 +866,8 @@ impl Render for TableStory {
this.child(h_flex().gap_1().child(Indicator::new()).child("Loading..."))
})
.child(format!("Total Rows: {}", rows_count))
.child(format!("Visible Rows: {:?}", delegate.visible_rows))
.child(format!("Visible Cols: {:?}", delegate.visible_cols))
.when(delegate.is_eof, |this| this.child("All data loaded.")),
),
)
Expand Down
115 changes: 76 additions & 39 deletions crates/ui/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ struct FixedCols {
left: usize,
}

/// The visible range of the rows and columns.
#[derive(Debug, Default)]
pub struct VisibleRangeState {
/// The visible range of the rows.
rows: Range<usize>,
/// The visible range of the columns.
cols: Range<usize>,
}

impl VisibleRangeState {
/// Returns the visible range of the rows.
pub fn rows(&self) -> Range<usize> {
self.rows.clone()
}

/// Returns the visible range of the columns.
pub fn cols(&self) -> Range<usize> {
self.cols.clone()
}
}

pub struct Table<D: TableDelegate> {
focus_handle: FocusHandle,
delegate: D,
Expand Down Expand Up @@ -148,11 +169,8 @@ pub struct Table<D: TableDelegate> {
border: bool,
/// The cell size of the table.
size: Size,

/// The visible range of the rows.
row_visible_range: Range<usize>,
/// The visible range of the columns.
col_visible_range: Range<usize>,
/// The visible range of the rows and columns.
visible_range: VisibleRangeState,
}

#[allow(unused)]
Expand Down Expand Up @@ -286,22 +304,30 @@ pub trait TableDelegate: Sized + 'static {
h_flex().w_5().h_full().flex_shrink_0()
}

/// Return the visible range of the rows.
fn row_visible_range(
&self,
/// Called when the visible range of the rows changed.
///
/// NOTE: Make sure this method is fast, because it will be called frequently.
///
/// This can used to handle some data update, to only update the visible rows.
/// Please ensure that the data is updated in the background task.
fn visible_rows_changed(
&mut self,
visible_range: Range<usize>,
cx: &mut ViewContext<Table<Self>>,
) -> Range<usize> {
visible_range
) {
}

/// Return the visible range of the columns.
fn col_visible_range(
&self,
/// Called when the visible range of the columns changed.
///
/// NOTE: Make sure this method is fast, because it will be called frequently.
///
/// This can used to handle some data update, to only update the visible rows.
/// Please ensure that the data is updated in the background task.
fn visible_cols_changed(
&mut self,
visible_range: Range<usize>,
cx: &mut ViewContext<Table<Self>>,
) -> Range<usize> {
visible_range
) {
}
}

Expand Down Expand Up @@ -330,8 +356,7 @@ where
stripe: false,
border: true,
size: Size::default(),
row_visible_range: Range::default(),
col_visible_range: Range::default(),
visible_range: VisibleRangeState::default(),
};

this.prepare_col_groups(cx);
Expand Down Expand Up @@ -461,6 +486,11 @@ where
cx.notify();
}

/// Returns the visible range of the rows and columns.
pub fn visible_range(&self) -> &VisibleRangeState {
&self.visible_range
}

fn on_row_click(&mut self, ev: &MouseDownEvent, row_ix: usize, cx: &mut ViewContext<Self>) {
if ev.button == MouseButton::Right {
self.right_clicked_row = Some(row_ix);
Expand Down Expand Up @@ -655,33 +685,32 @@ where
}
}

fn row_visible_range_if_need(
fn update_visible_range_if_need(
&mut self,
visible_range: Range<usize>,
axis: Axis,
cx: &mut ViewContext<Self>,
) {
// if visible_range is 0..1, do nothing, because it's more and unnecessary from render
if visible_range.start == 0 && visible_range.end == 1 {
// Skip when visible range is only 1 item.
// The visual_list will use first item to measure.
if visible_range.len() <= 1 {
return;
}
if self.row_visible_range != visible_range {
self.delegate.row_visible_range(visible_range.clone(), cx);
self.row_visible_range = visible_range;
}
}

fn col_visible_range_if_need(
&mut self,
visible_range: Range<usize>,
cx: &mut ViewContext<Self>,
) {
// if visible_range is 1..2, do nothing, because it's more and unnecessary from render
if visible_range.start == 1 && visible_range.end == 2 {
return;
}
if self.col_visible_range != visible_range {
self.delegate.col_visible_range(visible_range.clone(), cx);
self.col_visible_range = visible_range;
if axis == Axis::Vertical {
if self.visible_range.rows == visible_range {
return;
}
self.delegate_mut()
.visible_rows_changed(visible_range.clone(), cx);
self.visible_range.rows = visible_range;
} else {
if self.visible_range.cols == visible_range {
return;
}
self.delegate_mut()
.visible_cols_changed(visible_range.clone(), cx);
self.visible_range.cols = visible_range;
}
}

Expand Down Expand Up @@ -1109,7 +1138,11 @@ where
.child(
virtual_list(view, row_ix, Axis::Horizontal, col_sizes, {
move |table, visible_range: Range<usize>, _, cx| {
table.col_visible_range_if_need(visible_range.clone(), cx);
table.update_visible_range_if_need(
visible_range.clone(),
Axis::Horizontal,
cx,
);

visible_range
.map(|col_ix| {
Expand Down Expand Up @@ -1284,7 +1317,11 @@ where
{
move |table, visible_range, cx| {
table.load_more_if_need(visible_range.clone(), cx);
table.row_visible_range_if_need(visible_range.clone(), cx);
table.update_visible_range_if_need(
visible_range.clone(),
Axis::Vertical,
cx,
);

if visible_range.end > rows_count {
table.scroll_to_row(
Expand Down

0 comments on commit af6a3b4

Please sign in to comment.