Skip to content

Commit

Permalink
Revert "feat(ui): Keep the filter input field visible."
Browse files Browse the repository at this point in the history
  • Loading branch information
sarub0b0 authored Aug 22, 2023
1 parent 0338a1d commit 8900eee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
60 changes: 21 additions & 39 deletions src/ui/widget/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ratatui::{
backend::Backend,
layout::{Constraint, Rect},
style::{Modifier, Style},
text::{Line, Span},
widgets::{Table as TuiTable, TableState},
Frame,
};
Expand All @@ -26,7 +27,8 @@ use crate::{
};

use super::{
config::WidgetConfig, styled_graphemes, Item, RenderTrait, SelectedItem, TableItem, WidgetTrait,
config::{Title, WidgetConfig},
styled_graphemes, Item, RenderTrait, SelectedItem, TableItem, WidgetTrait,
};

const COLUMN_SPACING: u16 = 3;
Expand Down Expand Up @@ -140,12 +142,8 @@ impl TableBuilder {

#[derive(Debug)]
enum Mode {
/// 通常(検索フォーム非表示)
Normal,
/// フィルターワード入力中(検索フォーム表示)
FilterInput,
/// フィルターワード確定後(検索フォーム表示)
FilterConfirm,
}

impl Default for Mode {
Expand All @@ -163,10 +161,6 @@ impl Mode {
*self = Self::FilterInput;
}

fn filter_confirm(&mut self) {
*self = Self::FilterConfirm;
}

#[allow(dead_code)]
fn is_normal(&self) -> bool {
matches!(self, Self::Normal)
Expand All @@ -175,10 +169,6 @@ impl Mode {
fn is_filter_input(&self) -> bool {
matches!(self, Self::FilterInput)
}

fn is_filter_confirm(&self) -> bool {
matches!(self, Self::FilterConfirm)
}
}

#[derive(Derivative)]
Expand Down Expand Up @@ -286,7 +276,7 @@ impl<'a> Table<'a> {

match self.mode {
Mode::Normal => self.chunk,
Mode::FilterInput | Mode::FilterConfirm => {
Mode::FilterInput => {
let filter_hight = 3;
Rect::new(
x,
Expand Down Expand Up @@ -334,14 +324,6 @@ impl<'a> Table<'a> {
}
}
}

fn filter_cancel(&mut self) {
self.mode.normal();

self.filter_widget.clear();

self.filter_items();
}
}

impl WidgetTrait for Table<'_> {
Expand Down Expand Up @@ -488,7 +470,7 @@ impl WidgetTrait for Table<'_> {

fn on_key_event(&mut self, ev: KeyEvent) -> EventResult {
match self.mode {
Mode::Normal | Mode::FilterConfirm => match key_event_to_code(ev) {
Mode::Normal => match key_event_to_code(ev) {
KeyCode::Char('j') | KeyCode::Down | KeyCode::PageDown => {
self.select_next(1);
}
Expand All @@ -509,10 +491,6 @@ impl WidgetTrait for Table<'_> {
self.mode.filter_input();
}

KeyCode::Char('q') | KeyCode::Esc if self.mode.is_filter_confirm() => {
self.filter_cancel();
}

KeyCode::Enter => {
return EventResult::Callback(self.on_select_callback());
}
Expand All @@ -528,11 +506,11 @@ impl WidgetTrait for Table<'_> {

Mode::FilterInput => match key_event_to_code(ev) {
KeyCode::Enter => {
self.mode.filter_confirm();
self.mode.normal();
}

KeyCode::Esc => {
self.filter_cancel();
self.mode.normal();
}

_ => {
Expand Down Expand Up @@ -626,12 +604,22 @@ impl RenderTrait for Table<'_> {
where
B: Backend,
{
let widget_config = if let Some(block_injection) = &self.block_injection {
let mut widget_config = if let Some(block_injection) = &self.block_injection {
(block_injection)(&*self)
} else {
self.widget_config.clone()
};

if let Some(appended_title) = widget_config.append_title_mut().as_mut() {
if !self.filter_widget.word().is_empty() {
let mut spans = appended_title.spans().spans;

spans.push(Span::from(format!(" ({})", self.filter_widget.word())));

*appended_title = Title::from(Line::from(spans));
}
}

let block = widget_config.render_block(self.can_activate() && is_active, is_mouse_over);

let constraints = constraints(self.items.digits());
Expand All @@ -649,16 +637,10 @@ impl RenderTrait for Table<'_> {
widget = widget.header(self.items.header().rendered());
}

match self.mode {
Mode::Normal => {
f.render_stateful_widget(widget, self.chunk(), &mut self.state);
}
f.render_stateful_widget(widget, self.chunk(), &mut self.state);

Mode::FilterInput | Mode::FilterConfirm => {
self.filter_widget.render(f, self.mode.is_filter_input());

f.render_stateful_widget(widget, self.chunk(), &mut self.state);
}
if self.mode.is_filter_input() {
self.filter_widget.render(f, self.mode.is_filter_input())
}

logger!(debug, "{:?}", self.items);
Expand Down
4 changes: 0 additions & 4 deletions src/ui/widget/table/filter_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ impl FilterForm {
self.input_widget.on_key_event(ev)
}

pub fn clear(&mut self) {
self.input_widget.clear();
}

pub fn render<B>(&mut self, f: &mut Frame<'_, B>, is_active: bool)
where
B: Backend,
Expand Down

0 comments on commit 8900eee

Please sign in to comment.