Skip to content

Commit

Permalink
Merge pull request #387 from sarub0b0/fix-select-item-on-table
Browse files Browse the repository at this point in the history
fix(ui/table): Fix issue with incorrect item selection on mouse click when the filter is active.
  • Loading branch information
sarub0b0 committed Aug 23, 2023
2 parents 5231e5d + f923544 commit 8725be6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
35 changes: 20 additions & 15 deletions src/ui/widget/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::{
},
};

use self::filter_form::FILTER_HEIGHT;

use super::{
config::WidgetConfig, styled_graphemes, Item, RenderTrait, SelectedItem, TableItem, WidgetTrait,
};
Expand Down Expand Up @@ -222,7 +224,7 @@ impl<'a> Table<'a> {
}

fn max_width(&self) -> usize {
self.inner_chunk.width.saturating_sub(2) as usize
self.inner_chunk().width.saturating_sub(2) as usize
}

pub fn update_header_and_rows(&mut self, header: &[String], rows: &[TableItem]) {
Expand Down Expand Up @@ -257,7 +259,7 @@ impl<'a> Table<'a> {
}

fn showable_height(&self) -> usize {
self.inner_chunk.height.saturating_sub(2) as usize
self.inner_chunk().height.saturating_sub(2) as usize
}

fn max_offset(&self) -> usize {
Expand Down Expand Up @@ -286,18 +288,20 @@ impl<'a> Table<'a> {

match self.mode {
Mode::Normal => self.chunk,
Mode::FilterInput | Mode::FilterConfirm => {
let filter_hight = 3;
Rect::new(
x,
y + filter_hight,
width,
height.saturating_sub(filter_hight),
)
}

Mode::FilterInput | Mode::FilterConfirm => Rect::new(
x,
y + FILTER_HEIGHT,
width,
height.saturating_sub(FILTER_HEIGHT),
),
}
}

fn inner_chunk(&self) -> Rect {
self.widget_config.block().inner(self.chunk())
}

fn filter_items(&mut self) {
let old_len = self.items.len();

Expand Down Expand Up @@ -433,14 +437,16 @@ impl WidgetTrait for Table<'_> {
return EventResult::Nop;
}

let inner_chunk = self.inner_chunk();

let (_, row) = (
ev.column.saturating_sub(self.inner_chunk.left()) as usize,
ev.row.saturating_sub(self.inner_chunk.top()) as usize,
ev.column.saturating_sub(inner_chunk.left()) as usize,
ev.row.saturating_sub(inner_chunk.top()) as usize,
);

match ev.kind {
MouseEventKind::Down(MouseButton::Left) => {
if !self.inner_chunk.contains_point(ev.position()) {
if !inner_chunk.contains_point(ev.position()) {
return EventResult::Nop;
}

Expand Down Expand Up @@ -550,7 +556,6 @@ impl WidgetTrait for Table<'_> {

fn update_chunk(&mut self, chunk: Rect) {
self.chunk = chunk;
self.inner_chunk = self.widget_config.block().inner(chunk);

self.items.update_max_width(self.max_width());

Expand Down
8 changes: 7 additions & 1 deletion src/ui/widget/table/filter_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ impl Default for FilterForm {
}
}

pub const FILTER_HEIGHT: u16 = 3;

impl FilterForm {
pub fn update_chunk(&mut self, chunk: Rect) {
self.chunk = Rect::new(chunk.x, chunk.y, chunk.width, 3);
self.chunk = Rect::new(chunk.x, chunk.y, chunk.width, FILTER_HEIGHT);
}

pub fn chunk(&self) -> Rect {
self.chunk
}

pub fn word(&self) -> String {
Expand Down

0 comments on commit 8725be6

Please sign in to comment.