Add key bindings to select and open links

This commit is contained in:
JRF 2022-08-29 19:00:42 -05:00
parent c09608d1f8
commit a1043eafd3
4 changed files with 66 additions and 32 deletions

View file

@ -1,12 +1,8 @@
use std::io;
use std::sync::Arc;
use crossterm::event::KeyCode;
use crossterm::style::{ContentStyle, Stylize};
use parking_lot::FairMutex;
use toss::terminal::Terminal;
use crate::euph::Room;
use crate::ui::input::{key, InputEvent, KeyBindingsList, KeyEvent};
use crate::ui::widgets::list::ListState;
use crate::ui::widgets::popup::Popup;
@ -38,7 +34,7 @@ impl LinksState {
}
pub fn widget(&self) -> BoxedWidget {
let mut list = self.list.widget();
let mut list = self.list.widget().focus(true);
for (id, link) in self.links.iter().enumerate() {
list.add_sel(
id,
@ -50,20 +46,42 @@ impl LinksState {
Popup::new(list).title("Links").build()
}
pub fn list_key_bindings(&self, bindings: &mut KeyBindingsList) {
bindings.binding("esc", "close links popup")
pub fn open_link(&self) -> EventResult {
if let Some(id) = self.list.cursor() {
if let Some(link) = self.links.get(id) {
if let Err(error) = open::that(link) {
return EventResult::ErrorOpeningLink {
link: link.to_string(),
error,
};
}
}
}
EventResult::Handled
}
pub fn handle_input_event(
&mut self,
terminal: &mut Terminal,
crossterm_lock: &Arc<FairMutex<()>>,
event: &InputEvent,
room: &Option<Room>,
) -> EventResult {
pub fn list_key_bindings(&self, bindings: &mut KeyBindingsList) {
bindings.binding("esc", "close links popup");
bindings.binding("j/k, ↓/↑", "move cursor up/down");
bindings.binding("g, home", "move cursor to top");
bindings.binding("G, end", "move cursor to bottom");
bindings.binding("ctrl+y/e", "scroll up/down");
bindings.empty();
bindings.binding("enter", "open selected link");
}
pub fn handle_input_event(&mut self, event: &InputEvent) -> EventResult {
match event {
key!(Esc) => EventResult::Close,
_ => EventResult::NotHandled,
key!(Esc) => return EventResult::Close,
key!('k') | key!(Up) => self.list.move_cursor_up(),
key!('j') | key!(Down) => self.list.move_cursor_down(),
key!('g') | key!(Home) => self.list.move_cursor_to_top(),
key!('G') | key!(End) => self.list.move_cursor_to_bottom(),
key!(Ctrl + 'y') => self.list.scroll_up(1),
key!(Ctrl + 'e') => self.list.scroll_down(1),
key!(Enter) => return self.open_link(),
_ => return EventResult::NotHandled,
}
EventResult::Handled
}
}

View file

@ -464,23 +464,21 @@ impl EuphRoom {
}
}
}
State::Links(links) => {
match links.handle_input_event(terminal, crossterm_lock, event, &self.room) {
links::EventResult::NotHandled => false,
links::EventResult::Handled => true,
links::EventResult::Close => {
self.state = State::Normal;
true
}
links::EventResult::ErrorOpeningLink { link, error } => {
self.popups.push_front(RoomPopup::Error {
description: format!("Failed to open link: {link}"),
reason: format!("{error}"),
});
true
}
State::Links(links) => match links.handle_input_event(event) {
links::EventResult::NotHandled => false,
links::EventResult::Handled => true,
links::EventResult::Close => {
self.state = State::Normal;
true
}
}
links::EventResult::ErrorOpeningLink { link, error } => {
self.popups.push_front(RoomPopup::Error {
description: format!("Failed to open link: {link}"),
reason: format!("{error}"),
});
true
}
},
}
}