mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-27 07:34:27 +02:00
Fix issue with click location for tables
This commit is contained in:
parent
ac23654c1c
commit
3411d320c9
@ -53,10 +53,10 @@ pub struct TextTable<Message> {
|
|||||||
key: Key,
|
key: Key,
|
||||||
column_widths: Vec<u16>,
|
column_widths: Vec<u16>,
|
||||||
columns: Vec<TextColumn>,
|
columns: Vec<TextColumn>,
|
||||||
show_gap: bool,
|
|
||||||
show_selected_entry: bool,
|
show_selected_entry: bool,
|
||||||
rows: Vec<DataRow>,
|
rows: Vec<DataRow>,
|
||||||
style_sheet: StyleSheet,
|
style_sheet: StyleSheet,
|
||||||
|
show_gap: bool,
|
||||||
table_gap: u16,
|
table_gap: u16,
|
||||||
on_select: Option<Box<dyn Fn(usize) -> Message>>,
|
on_select: Option<Box<dyn Fn(usize) -> Message>>,
|
||||||
on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
|
on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
|
||||||
@ -111,7 +111,7 @@ impl<Message> TextTable<Message> {
|
|||||||
self.column_widths = column_widths;
|
self.column_widths = column_widths;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_sort_column(&mut self, state: &mut TextTableState, x: u16) -> Status {
|
fn update_sort_column(&self, state: &mut TextTableState, x: u16) -> Status {
|
||||||
match state.sort {
|
match state.sort {
|
||||||
SortType::Unsortable => Status::Ignored,
|
SortType::Unsortable => Status::Ignored,
|
||||||
SortType::Ascending(column) | SortType::Descending(column) => {
|
SortType::Ascending(column) | SortType::Descending(column) => {
|
||||||
@ -148,19 +148,17 @@ impl<Message> TextTable<Message> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_page_up(&mut self, state: &mut TextTableState, rect: Rect) -> Status {
|
pub fn on_page_up(&self, state: &mut TextTableState, rect: Rect) -> Status {
|
||||||
let height = rect.height.saturating_sub(self.table_gap + 1);
|
let height = rect.height.saturating_sub(self.table_gap + 1);
|
||||||
state.scroll.move_up(height.into())
|
state.scroll.move_up(height.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn on_page_down(&mut self, state: &mut TextTableState, rect: Rect) -> Status {
|
pub fn on_page_down(&self, state: &mut TextTableState, rect: Rect) -> Status {
|
||||||
let height = rect.height.saturating_sub(self.table_gap + 1);
|
let height = rect.height.saturating_sub(self.table_gap + 1);
|
||||||
state.scroll.move_down(height.into())
|
state.scroll.move_down(height.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_down(
|
pub fn scroll_down(&self, state: &mut TextTableState, messages: &mut Vec<Message>) -> Status {
|
||||||
&mut self, state: &mut TextTableState, messages: &mut Vec<Message>,
|
|
||||||
) -> Status {
|
|
||||||
let status = state.scroll.move_down(1);
|
let status = state.scroll.move_down(1);
|
||||||
if let Some(on_select) = &self.on_select {
|
if let Some(on_select) = &self.on_select {
|
||||||
messages.push(on_select(state.scroll.current_index()));
|
messages.push(on_select(state.scroll.current_index()));
|
||||||
@ -168,13 +166,39 @@ impl<Message> TextTable<Message> {
|
|||||||
status
|
status
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scroll_up(&mut self, state: &mut TextTableState, messages: &mut Vec<Message>) -> Status {
|
pub fn scroll_up(&self, state: &mut TextTableState, messages: &mut Vec<Message>) -> Status {
|
||||||
let status = state.scroll.move_up(1);
|
let status = state.scroll.move_up(1);
|
||||||
if let Some(on_select) = &self.on_select {
|
if let Some(on_select) = &self.on_select {
|
||||||
messages.push(on_select(state.scroll.current_index()));
|
messages.push(on_select(state.scroll.current_index()));
|
||||||
}
|
}
|
||||||
status
|
status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn on_left_mouse_down(
|
||||||
|
&self, state: &mut TextTableState, rect: Rect, messages: &mut Vec<Message>, column: u16,
|
||||||
|
row: u16,
|
||||||
|
) -> Status {
|
||||||
|
let y = row.saturating_sub(rect.top());
|
||||||
|
if y == 0 {
|
||||||
|
let x = column - rect.left();
|
||||||
|
self.update_sort_column(state, x)
|
||||||
|
} else if y > self.table_gap {
|
||||||
|
let visual_index = usize::from(y.saturating_sub(self.table_gap + 1));
|
||||||
|
match state.scroll.set_visual_index(visual_index) {
|
||||||
|
Status::Captured => Status::Captured,
|
||||||
|
Status::Ignored => {
|
||||||
|
if let Some(on_selected_click) = &self.on_selected_click {
|
||||||
|
messages.push(on_selected_click(state.scroll.current_index()));
|
||||||
|
Status::Captured
|
||||||
|
} else {
|
||||||
|
Status::Ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Status::Ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> StatefulComponent<Message> for TextTable<Message> {
|
impl<Message> StatefulComponent<Message> for TextTable<Message> {
|
||||||
@ -200,11 +224,11 @@ impl<Message> StatefulComponent<Message> for TextTable<Message> {
|
|||||||
key,
|
key,
|
||||||
column_widths: props.column_widths,
|
column_widths: props.column_widths,
|
||||||
columns: props.columns,
|
columns: props.columns,
|
||||||
show_gap: props.show_gap,
|
|
||||||
show_selected_entry: props.show_selected_entry,
|
show_selected_entry: props.show_selected_entry,
|
||||||
rows: props.rows,
|
rows: props.rows,
|
||||||
style_sheet: props.style_sheet,
|
style_sheet: props.style_sheet,
|
||||||
table_gap: props.table_gap,
|
show_gap: props.show_gap,
|
||||||
|
table_gap: if props.show_gap { 1 } else { 0 },
|
||||||
on_select: props.on_select,
|
on_select: props.on_select,
|
||||||
on_selected_click: props.on_selected_click,
|
on_selected_click: props.on_selected_click,
|
||||||
}
|
}
|
||||||
@ -321,30 +345,13 @@ impl<Message> TmpComponent<Message> for TextTable<Message> {
|
|||||||
Event::Mouse(mouse_event) => {
|
Event::Mouse(mouse_event) => {
|
||||||
if mouse_event.does_mouse_intersect_bounds(rect) {
|
if mouse_event.does_mouse_intersect_bounds(rect) {
|
||||||
match mouse_event.kind {
|
match mouse_event.kind {
|
||||||
MouseEventKind::Down(MouseButton::Left) => {
|
MouseEventKind::Down(MouseButton::Left) => self.on_left_mouse_down(
|
||||||
let y = mouse_event.row - rect.top();
|
state,
|
||||||
if y == 0 {
|
rect,
|
||||||
let x = mouse_event.column - rect.left();
|
messages,
|
||||||
self.update_sort_column(state, x)
|
mouse_event.column,
|
||||||
} else if y > self.table_gap {
|
mouse_event.row,
|
||||||
let visual_index = usize::from(y - self.table_gap);
|
),
|
||||||
match state.scroll.set_visual_index(visual_index) {
|
|
||||||
Status::Captured => Status::Captured,
|
|
||||||
Status::Ignored => {
|
|
||||||
if let Some(on_selected_click) = &self.on_selected_click {
|
|
||||||
messages.push(on_selected_click(
|
|
||||||
state.scroll.current_index(),
|
|
||||||
));
|
|
||||||
Status::Captured
|
|
||||||
} else {
|
|
||||||
Status::Ignored
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Status::Ignored
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MouseEventKind::ScrollDown => self.scroll_down(state, messages),
|
MouseEventKind::ScrollDown => self.scroll_down(state, messages),
|
||||||
MouseEventKind::ScrollUp => self.scroll_up(state, messages),
|
MouseEventKind::ScrollUp => self.scroll_up(state, messages),
|
||||||
_ => Status::Ignored,
|
_ => Status::Ignored,
|
||||||
|
@ -12,7 +12,6 @@ pub struct TextTableProps<Message> {
|
|||||||
pub(crate) rows: Vec<DataRow>,
|
pub(crate) rows: Vec<DataRow>,
|
||||||
pub(crate) style_sheet: StyleSheet,
|
pub(crate) style_sheet: StyleSheet,
|
||||||
pub(crate) sort: SortType,
|
pub(crate) sort: SortType,
|
||||||
pub(crate) table_gap: u16,
|
|
||||||
pub(crate) on_select: Option<Box<dyn Fn(usize) -> Message>>,
|
pub(crate) on_select: Option<Box<dyn Fn(usize) -> Message>>,
|
||||||
pub(crate) on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
|
pub(crate) on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
|
||||||
}
|
}
|
||||||
@ -30,7 +29,6 @@ impl<Message> TextTableProps<Message> {
|
|||||||
rows: Vec::default(),
|
rows: Vec::default(),
|
||||||
style_sheet: StyleSheet::default(),
|
style_sheet: StyleSheet::default(),
|
||||||
sort: SortType::Unsortable,
|
sort: SortType::Unsortable,
|
||||||
table_gap: 0,
|
|
||||||
on_select: None,
|
on_select: None,
|
||||||
on_selected_click: None,
|
on_selected_click: None,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user