contacts: add delete_contact shortcut

New exciting feature: contact deletion.

Default shortcut is `d`.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/285/head
Manos Pitsidianakis 2023-08-23 17:27:48 +03:00
parent 095d24f914
commit ab57e9420d
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
3 changed files with 60 additions and 33 deletions

View File

@ -866,6 +866,9 @@ Create new contact.
.It Ic edit_contact
Edit contact under cursor.
.Pq Em e \" default value
.It Ic delete_contact
Delete contact under cursor.
.Pq Em d \" default value
.It Ic mail_contact
Mail contact under cursor.
.Pq Em m \" default value

View File

@ -187,6 +187,7 @@ shortcut_key_values! { "contact-list",
scroll_down |> "Scroll down list." |> Key::Char('j'),
create_contact |> "Create new contact." |> Key::Char('c'),
edit_contact |> "Edit contact under cursor." |> Key::Char('e'),
delete_contact |> "Delete contact under cursor." |> Key::Char('d'),
mail_contact |> "Mail contact under cursor." |> Key::Char('m'),
next_account |> "Go to next account." |> Key::Char('h'),
prev_account |> "Go to previous account." |> Key::Char('l'),

View File

@ -620,12 +620,31 @@ impl Component for ContactList {
}
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
if let UIEvent::ConfigReload { old_settings: _ } = event {
self.theme_default = crate::conf::value(context, "theme_default");
self.initialized = false;
self.sidebar_divider = context.settings.listing.sidebar_divider;
self.sidebar_divider_theme = conf::value(context, "mail.sidebar_divider");
self.set_dirty(true);
match event {
UIEvent::ConfigReload { old_settings: _ } => {
self.theme_default = crate::conf::value(context, "theme_default");
self.initialized = false;
self.sidebar_divider = context.settings.listing.sidebar_divider;
self.sidebar_divider_theme = conf::value(context, "mail.sidebar_divider");
self.set_dirty(true);
}
UIEvent::AccountStatusChange(_, _) => {
self.initialized = false;
self.set_dirty(true);
}
UIEvent::ComponentUnrealize(ref kill_id) if self.mode == ViewMode::View(*kill_id) => {
self.mode = ViewMode::List;
self.view.take();
self.set_dirty(true);
return true;
}
UIEvent::ChangeMode(UIMode::Normal) => {
self.set_dirty(true);
}
UIEvent::Resize => {
self.set_dirty(true);
}
_ => {}
}
if let Some(ref mut v) = self.view {
@ -633,6 +652,7 @@ impl Component for ContactList {
return true;
}
}
let shortcuts = self.shortcuts(context);
if self.view.is_none() {
match *event {
@ -655,9 +675,11 @@ impl Component for ContactList {
}
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["edit_contact"])
&& self.length > 0 =>
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["edit_contact"]) =>
{
if self.length == 0 {
return true;
}
let account = &mut context.accounts[self.account_pos];
let book = &mut account.address_book;
let card = book[&self.id_positions[self.cursor_pos]].clone();
@ -677,9 +699,11 @@ impl Component for ContactList {
return true;
}
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["mail_contact"])
&& self.length > 0 =>
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["mail_contact"]) =>
{
if self.length == 0 {
return true;
}
let account = &context.accounts[self.account_pos];
let account_hash = account.hash();
let book = &account.address_book;
@ -695,6 +719,24 @@ impl Component for ContactList {
return true;
}
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["delete_contact"]) =>
{
if self.length == 0 {
return true;
}
// [ref:TODO]: add a confirmation dialog?
context.accounts[self.account_pos]
.address_book
.remove_card(self.id_positions[self.cursor_pos]);
self.initialized = false;
self.set_dirty(true);
context
.replies
.push_back(UIEvent::StatusEvent(StatusEvent::BufClear));
return true;
}
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["next_account"]) =>
{
@ -713,9 +755,6 @@ impl Component for ContactList {
.push_back(UIEvent::StatusEvent(StatusEvent::BufClear));
return true;
};
if self.accounts.is_empty() {
return true;
}
if self.account_pos + amount < self.accounts.len() {
self.account_pos += amount;
self.set_dirty(true);
@ -817,9 +856,11 @@ impl Component for ContactList {
return true;
}
UIEvent::Input(ref key)
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["scroll_down"])
&& self.cursor_pos < self.length.saturating_sub(1) =>
if shortcut!(key == shortcuts[Shortcuts::CONTACT_LIST]["scroll_down"]) =>
{
if self.cursor_pos >= self.length.saturating_sub(1) {
return true;
}
let amount = if self.cmd_buf.is_empty() {
1
} else if let Ok(amount) = self.cmd_buf.parse::<usize>() {
@ -899,24 +940,6 @@ impl Component for ContactList {
}
_ => {}
}
} else {
match event {
UIEvent::ComponentUnrealize(ref kill_id)
if self.mode == ViewMode::View(*kill_id) =>
{
self.mode = ViewMode::List;
self.view.take();
self.set_dirty(true);
return true;
}
UIEvent::ChangeMode(UIMode::Normal) => {
self.set_dirty(true);
}
UIEvent::Resize => {
self.set_dirty(true);
}
_ => {}
}
}
false
}