use key config in tab component

pull/33/head
Takayuki Maeda 3 years ago
parent 6ededd0e23
commit 8bda0d9c62

@ -14,8 +14,6 @@ pub enum MoveSelection {
Right,
Top,
End,
PageDown,
PageUp,
}
#[derive(Debug, Clone, Copy)]
@ -109,7 +107,6 @@ impl DatabaseTree {
MoveSelection::Right => self.selection_right(selection),
MoveSelection::Top => Self::selection_start(selection),
MoveSelection::End => self.selection_end(selection),
MoveSelection::PageDown | MoveSelection::PageUp => None,
};
let changed_index = new_index.map(|i| i != selection).unwrap_or_default();

@ -110,9 +110,10 @@ impl App {
fn commands(&self) -> Vec<CommandInfo> {
let mut res = vec![
CommandInfo::new(command::scroll(&self.config.key_config)),
CommandInfo::new(command::scroll_to_top_bottom(&self.config.key_config)),
CommandInfo::new(command::move_focus(&self.config.key_config)),
CommandInfo::new(command::filter(&self.config.key_config)),
CommandInfo::new(command::help(&self.config.key_config)),
CommandInfo::new(command::move_focus(&self.config.key_config)),
CommandInfo::new(command::toggle_tabs(&self.config.key_config)),
];

@ -44,6 +44,17 @@ pub fn scroll(key: &KeyConfig) -> CommandText {
)
}
pub fn scroll_to_top_bottom(key: &KeyConfig) -> CommandText {
CommandText::new(
format!(
"Scroll to top/bottom [{},{}]",
key.scroll_to_top.to_string(),
key.scroll_to_bottom.to_string(),
),
CMD_GROUP_GENERAL,
)
}
pub fn expand_collapse(key: &KeyConfig) -> CommandText {
CommandText::new(
format!(

@ -67,16 +67,13 @@ impl Component for TabComponent {
fn commands(&self, _out: &mut Vec<CommandInfo>) {}
fn event(&mut self, key: Key) -> Result<EventState> {
match key {
Key::Char('1') => {
self.selected_tab = Tab::Records;
Ok(EventState::Consumed)
}
Key::Char('2') => {
self.selected_tab = Tab::Structure;
Ok(EventState::Consumed)
}
_ => Ok(EventState::NotConsumed),
if key == self.key_config.tab_records {
self.selected_tab = Tab::Records;
return Ok(EventState::Consumed);
} else if key == self.key_config.tab_structure {
self.selected_tab = Tab::Structure;
return Ok(EventState::Consumed);
}
Ok(EventState::NotConsumed)
}
}

@ -499,10 +499,10 @@ impl Component for TableComponent {
} else if key == self.key_config.scroll_up_multiple_lines {
self.previous_row(10);
return Ok(EventState::Consumed);
} else if key == self.key_config.jump_to_first_row {
} else if key == self.key_config.scroll_to_top {
self.scroll_top();
return Ok(EventState::Consumed);
} else if key == self.key_config.jump_to_last_row {
} else if key == self.key_config.scroll_to_bottom {
self.scroll_bottom();
return Ok(EventState::Consumed);
} else if key == self.key_config.scroll_right {

@ -51,8 +51,8 @@ pub struct KeyConfig {
pub filter: Key,
pub scroll_down_multiple_lines: Key,
pub scroll_up_multiple_lines: Key,
pub jump_to_first_row: Key,
pub jump_to_last_row: Key,
pub scroll_to_top: Key,
pub scroll_to_bottom: Key,
pub extend_selection_by_one_cell_left: Key,
pub extend_selection_by_one_cell_right: Key,
pub extend_selection_by_one_cell_up: Key,
@ -79,8 +79,8 @@ impl Default for KeyConfig {
filter: Key::Char('/'),
scroll_down_multiple_lines: Key::Ctrl('d'),
scroll_up_multiple_lines: Key::Ctrl('u'),
jump_to_first_row: Key::Char('g'),
jump_to_last_row: Key::Char('G'),
scroll_to_top: Key::Char('g'),
scroll_to_bottom: Key::Char('G'),
extend_selection_by_one_cell_left: Key::Char('H'),
extend_selection_by_one_cell_right: Key::Char('L'),
extend_selection_by_one_cell_down: Key::Char('J'),

@ -3,11 +3,8 @@ use crossterm::event;
use std::{sync::mpsc, thread, time::Duration};
#[derive(Debug, Clone, Copy)]
/// Configuration for event handling.
pub struct EventConfig {
/// The key that is used to exit the application.
pub exit_key: Key,
/// The tick rate at which the application will sent an tick event.
pub tick_rate: Duration,
}
@ -20,25 +17,18 @@ impl Default for EventConfig {
}
}
/// An occurred event.
#[derive(Copy, Clone)]
pub enum Event<I> {
/// An input event occurred.
Input(I),
/// An tick event occurred.
Tick,
}
/// A small event handler that wrap crossterm input and tick event. Each event
/// type is handled in its own thread and returned to a common `Receiver`
pub struct Events {
rx: mpsc::Receiver<Event<Key>>,
// Need to be kept around to prevent disposing the sender side.
_tx: mpsc::Sender<Event<Key>>,
}
impl Events {
/// Constructs an new instance of `Events` with the default config.
pub fn new(tick_rate: u64) -> Events {
Events::with_config(EventConfig {
tick_rate: Duration::from_millis(tick_rate),
@ -46,31 +36,25 @@ impl Events {
})
}
/// Constructs an new instance of `Events` from given config.
pub fn with_config(config: EventConfig) -> Events {
let (tx, rx) = mpsc::channel();
let event_tx = tx.clone();
thread::spawn(move || {
loop {
// poll for tick rate duration, if no event, sent tick event.
if event::poll(config.tick_rate).unwrap() {
if let event::Event::Key(key) = event::read().unwrap() {
let key = Key::from(key);
thread::spawn(move || loop {
if event::poll(config.tick_rate).unwrap() {
if let event::Event::Key(key) = event::read().unwrap() {
let key = Key::from(key);
event_tx.send(Event::Input(key)).unwrap();
}
event_tx.send(Event::Input(key)).unwrap();
}
event_tx.send(Event::Tick).unwrap();
}
event_tx.send(Event::Tick).unwrap();
});
Events { rx, _tx: tx }
}
/// Attempts to read an event.
/// This function will block the current thread.
pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv()
}

@ -10,14 +10,14 @@ pub fn common_nav(key: Key, key_config: &KeyConfig) -> Option<MoveSelection> {
Some(MoveSelection::Down)
} else if key == key_config.scroll_up {
Some(MoveSelection::Up)
} else if key == Key::PageUp {
Some(MoveSelection::PageUp)
} else if key == Key::PageDown {
Some(MoveSelection::PageDown)
} else if key == key_config.scroll_right {
Some(MoveSelection::Right)
} else if key == key_config.scroll_left {
Some(MoveSelection::Left)
} else if key == key_config.scroll_to_top {
Some(MoveSelection::Top)
} else if key == key_config.scroll_to_bottom {
Some(MoveSelection::End)
} else {
None
}

Loading…
Cancel
Save