ui/shortcuts: Replace arrow key use with configurable shortcuts

async
Manos Pitsidianakis 2019-12-14 14:16:12 +02:00
parent 41a4de394a
commit 18a8d22b85
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
6 changed files with 77 additions and 26 deletions

View File

@ -728,7 +728,9 @@ impl Component for ContactList {
)));
return true;
}
UIEvent::Input(Key::Up) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Self::DESCRIPTION]["scroll_up"]) =>
{
let amount = if self.cmd_buf.is_empty() {
1
} else if let Ok(amount) = self.cmd_buf.parse::<usize>() {
@ -748,7 +750,10 @@ impl Component for ContactList {
self.set_dirty();
return true;
}
UIEvent::Input(Key::Down) if self.cursor_pos < self.length.saturating_sub(1) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Self::DESCRIPTION]["scroll_down"])
&& self.cursor_pos < self.length.saturating_sub(1) =>
{
let amount = if self.cmd_buf.is_empty() {
1
} else if let Ok(amount) = self.cmd_buf.parse::<usize>() {

View File

@ -686,12 +686,16 @@ impl Component for Composer {
}
return true;
}*/
UIEvent::Input(Key::Up) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Self::DESCRIPTION]["scroll_up"]) =>
{
self.cursor = Cursor::Headers;
self.form.process_event(event, context);
self.dirty = true;
}
UIEvent::Input(Key::Down) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Self::DESCRIPTION]["scroll_down"]) =>
{
self.cursor = Cursor::Body;
self.dirty = true;
}

View File

@ -528,7 +528,9 @@ impl Component for Listing {
UIEvent::Resize => {
self.set_dirty();
}
UIEvent::Input(Key::Up) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Listing::DESCRIPTION]["scroll_up"]) =>
{
let amount = if self.cmd_buf.is_empty() {
1
} else if let Ok(amount) = self.cmd_buf.parse::<usize>() {
@ -547,7 +549,9 @@ impl Component for Listing {
self.component.set_movement(PageMovement::Up(amount));
return true;
}
UIEvent::Input(Key::Down) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Listing::DESCRIPTION]["scroll_down"]) =>
{
let amount = if self.cmd_buf.is_empty() {
1
} else if let Ok(amount) = self.cmd_buf.parse::<usize>() {

View File

@ -968,14 +968,18 @@ impl Component for ThreadView {
let shortcuts = self.get_shortcuts(context);
match *event {
UIEvent::Input(Key::Up) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[ThreadView::DESCRIPTION]["scroll_up"]) =>
{
if self.cursor_pos > 0 {
self.new_cursor_pos = self.new_cursor_pos.saturating_sub(1);
self.dirty = true;
}
return true;
}
UIEvent::Input(Key::Down) => {
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[ThreadView::DESCRIPTION]["scroll_down"]) =>
{
let height = self.visible_entries.iter().flat_map(|v| v.iter()).count();
if height > 0 && self.new_cursor_pos + 1 < height {
self.new_cursor_pos += 1;

View File

@ -1688,18 +1688,18 @@ impl Component for Tabbed {
UIEvent::Resize => {
self.dirty = true;
}
UIEvent::Input(ref k) if self.show_shortcuts => {
match k {
Key::Up => {
UIEvent::Input(ref key) if self.show_shortcuts => {
match key {
_ if shortcut!(key == shortcuts["general"]["scroll_up"]) => {
self.help_screen_cursor.1 = self.help_screen_cursor.1.saturating_sub(1);
}
Key::Down => {
_ if shortcut!(key == shortcuts["general"]["scroll_down"]) => {
self.help_screen_cursor.1 = self.help_screen_cursor.1 + 1;
}
Key::Left => {
_ if shortcut!(key == shortcuts["general"]["scroll_left"]) => {
self.help_screen_cursor.0 = self.help_screen_cursor.0.saturating_sub(1);
}
Key::Right => {
_ if shortcut!(key == shortcuts["general"]["scroll_right"]) => {
self.help_screen_cursor.0 = self.help_screen_cursor.0 + 1;
}
_ => {
@ -1790,8 +1790,9 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
copy_area_with_break(grid, &self.content, area, ((0, 0), (width, height)));
context.dirty_areas.push_back(area);
}
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
let (width, height) = self.content.size();
let shortcuts = self.get_shortcuts(context);
match (event, self.cursor) {
(UIEvent::Input(Key::Char('\n')), _) if self.single_only => {
/* User can only select one entry, so Enter key finalises the selection */
@ -1880,8 +1881,10 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Up), SelectorCursor::Ok)
| (UIEvent::Input(Key::Up), SelectorCursor::Cancel) => {
(UIEvent::Input(ref key), SelectorCursor::Ok)
| (UIEvent::Input(ref key), SelectorCursor::Cancel)
if shortcut!(key == shortcuts["general"]["scroll_up"]) =>
{
change_colors(
&mut self.content,
(
@ -1902,8 +1905,9 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Down), SelectorCursor::Entry(c))
if c < self.entries.len().saturating_sub(1) =>
(UIEvent::Input(ref key), SelectorCursor::Entry(c))
if c < self.entries.len().saturating_sub(1)
&& shortcut!(key == shortcuts["general"]["scroll_down"]) =>
{
if self.single_only {
// Redraw selection
@ -1940,7 +1944,9 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Down), SelectorCursor::Entry(c)) if !self.single_only => {
(UIEvent::Input(ref key), SelectorCursor::Entry(c))
if !self.single_only && shortcut!(key == shortcuts["general"]["scroll_down"]) =>
{
self.cursor = SelectorCursor::Ok;
change_colors(
&mut self.content,
@ -1960,8 +1966,9 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Down), _) | (UIEvent::Input(Key::Up), _) => return true,
(UIEvent::Input(Key::Right), SelectorCursor::Ok) => {
(UIEvent::Input(ref key), SelectorCursor::Ok)
if shortcut!(key == shortcuts["general"]["scroll_right"]) =>
{
self.cursor = SelectorCursor::Cancel;
change_colors(
&mut self.content,
@ -1984,7 +1991,9 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Left), SelectorCursor::Cancel) => {
(UIEvent::Input(ref key), SelectorCursor::Cancel)
if shortcut!(key == shortcuts["general"]["scroll_left"]) =>
{
self.cursor = SelectorCursor::Ok;
change_colors(
&mut self.content,
@ -2007,12 +2016,25 @@ impl<T: PartialEq + Debug + Clone + Sync + Send> Component for Selector<T> {
self.dirty = true;
return true;
}
(UIEvent::Input(Key::Left), _) | (UIEvent::Input(Key::Right), _) => return true,
(UIEvent::Input(ref key), _)
if shortcut!(key == shortcuts["general"]["scroll_left"])
|| shortcut!(key == shortcuts["general"]["scroll_right"])
|| shortcut!(key == shortcuts["general"]["scroll_up"])
|| shortcut!(key == shortcuts["general"]["scroll_down"]) =>
{
return true
}
_ => {}
}
false
}
fn get_shortcuts(&self, context: &Context) -> ShortcutMaps {
let mut map = ShortcutMaps::default();
map.insert("general", context.settings.shortcuts.general.key_values());
map
}
fn is_dirty(&self) -> bool {
self.dirty
}

View File

@ -105,6 +105,8 @@ shortcut_key_values! { "compact-listing",
shortcut_key_values! { "listing",
/// Shortcut listing for a mail listing.
pub struct ListingShortcuts {
scroll_up |> "Scroll up list." |> Key::Up,
scroll_down |> "Scroll down list." |> Key::Down,
new_mail |> "Start new mail draft in new tab." |> Key::Char('m'),
next_account |> "Go to next account." |> Key::Char('h'),
next_folder |> "Go to next folder." |> Key::Char('J'),
@ -121,6 +123,8 @@ shortcut_key_values! { "listing",
shortcut_key_values! { "contact-list",
/// Shortcut listing for the contact list view
pub struct ContactListShortcuts {
scroll_up |> "Scroll up list." |> Key::Up,
scroll_down |> "Scroll down list." |> Key::Down,
create_contact |> "Create new contact." |> Key::Char('c'),
edit_contact |> "Edit contact under cursor." |> Key::Char('e'),
mail_contact |> "Mail contact under cursor." |> Key::Char('m'),
@ -143,14 +147,20 @@ shortcut_key_values! { "pager",
shortcut_key_values! { "general",
pub struct GeneralShortcuts {
go_to_tab |> "Go to the nth tab" |> Key::Alt('n'),
next_tab |> "Next tab." |> Key::Char('T')
next_tab |> "Next tab." |> Key::Char('T'),
scroll_right |> "Generic scroll right (catch-all setting)" |> Key::Right,
scroll_left |> "Generic scroll left (catch-all setting)" |> Key::Left,
scroll_up |> "Generic scroll up (catch-all setting)" |> Key::Up,
scroll_down |> "Generic scroll down (catch-all setting)" |> Key::Down
}
}
shortcut_key_values! { "composing",
pub struct ComposingShortcuts {
edit_mail |> "Edit mail." |> Key::Char('e'),
send_mail |> "Deliver draft to mailer" |> Key::Char('s')
send_mail |> "Deliver draft to mailer" |> Key::Char('s'),
scroll_up |> "Change field focus." |> Key::Up,
scroll_down |> "Change field focus." |> Key::Down
}
}
@ -171,6 +181,8 @@ shortcut_key_values! { "envelope-view",
shortcut_key_values! { "thread-view",
pub struct ThreadViewShortcuts {
scroll_up |> "Scroll up list." |> Key::Up,
scroll_down |> "Scroll down list." |> Key::Down,
collapse_subtree |> "collapse thread branches" |> Key::Char('h'),
next_page |> "Go to next page." |> Key::PageDown,
prev_page |> "Go to previous page." |> Key::PageUp,