Refactor OfflineListing

Move offline status drawing to OfflineListing
pull/234/head
Manos Pitsidianakis 4 years ago
parent 2c549f5fcb
commit c8391983ee
No known key found for this signature in database
GPG Key ID: 73627C2F690DF710

@ -399,66 +399,31 @@ impl Component for Listing {
} }
if right_component_width == total_cols { if right_component_width == total_cols {
if let Err(err) = context.is_online(self.cursor_pos.0) { if context.is_online(self.cursor_pos.0).is_err() {
clear_area(grid, area, self.theme_default); match self.component {
let (x, _) = write_string_to_grid( ListingComponent::Offline(_) => {}
"offline: ", _ => {
grid, self.component = Offline(OfflineListing::new((self.cursor_pos.0, 0)));
Color::Byte(243), }
self.theme_default.bg, }
self.theme_default.attrs,
(set_x(upper_left, mid + 1), bottom_right),
None,
);
write_string_to_grid(
&err.to_string(),
grid,
Color::Red,
self.theme_default.bg,
self.theme_default.attrs,
(set_x(upper_left, x + 1), bottom_right),
None,
);
context.dirty_areas.push_back(area);
return;
} else {
self.component.draw(grid, area, context);
} }
self.component.draw(grid, area, context);
} else if right_component_width == 0 { } else if right_component_width == 0 {
self.draw_menu(grid, area, context); self.draw_menu(grid, area, context);
} else { } else {
self.draw_menu(grid, (upper_left, (mid, get_y(bottom_right))), context); self.draw_menu(grid, (upper_left, (mid, get_y(bottom_right))), context);
if let Err(err) = context.is_online(self.cursor_pos.0) { if context.is_online(self.cursor_pos.0).is_err() {
clear_area( match self.component {
grid, ListingComponent::Offline(_) => {}
(set_x(upper_left, mid + 1), bottom_right), _ => {
self.theme_default, self.component = Offline(OfflineListing::new((self.cursor_pos.0, 0)));
); }
let (x, _) = write_string_to_grid( }
"offline: ", }
grid,
Color::Byte(243),
self.theme_default.bg,
self.theme_default.attrs,
(set_x(upper_left, mid + 1), bottom_right),
None,
);
write_string_to_grid(
&err.to_string(),
grid,
Color::Red,
self.theme_default.bg,
self.theme_default.attrs,
(set_x(upper_left, x + 1), bottom_right),
None,
);
self.component.set_dirty(false);
context.dirty_areas.push_back(area);
} else {
self.component self.component
.draw(grid, (set_x(upper_left, mid + 1), bottom_right), context); .draw(grid, (set_x(upper_left, mid + 1), bottom_right), context);
} }
}
self.dirty = false; self.dirty = false;
} }
@ -560,28 +525,24 @@ impl Component for Listing {
}; };
match k { match k {
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_mailbox"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_mailbox"]) => {
if let Some((_, mailbox_hash)) = self.accounts[self.cursor_pos.0] if self.accounts[self.cursor_pos.0]
.entries .entries
.get(self.cursor_pos.1 + amount) .get(self.cursor_pos.1 + amount)
.is_some()
{ {
self.cursor_pos.1 += amount; self.cursor_pos.1 += amount;
self.component
.set_coordinates((self.cursor_pos.0, *mailbox_hash));
self.set_dirty(true);
} else { } else {
return true; return true;
} }
} }
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_mailbox"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_mailbox"]) => {
if self.cursor_pos.1 >= amount { if self.cursor_pos.1 >= amount {
if let Some((_, mailbox_hash)) = self.accounts[self.cursor_pos.0] if self.accounts[self.cursor_pos.0]
.entries .entries
.get(self.cursor_pos.1 - amount) .get(self.cursor_pos.1 - amount)
.is_some()
{ {
self.cursor_pos.1 -= amount; self.cursor_pos.1 -= amount;
self.component
.set_coordinates((self.cursor_pos.0, *mailbox_hash));
self.set_dirty(true);
} else { } else {
return true; return true;
} }
@ -591,22 +552,7 @@ impl Component for Listing {
} }
_ => {} _ => {}
} }
if let Some((_, mailbox_hash)) = self.accounts[self.cursor_pos.0] self.change_account(context);
.entries
.get(self.cursor_pos.1)
{
/* Account might have no mailboxes yet if it's offline */
/* Check if per-mailbox configuration overrides general configuration */
let index_style = mailbox_settings!(
context[self.cursor_pos.0][mailbox_hash].listing.index_style
);
self.component.set_style(*index_style);
}
context
.replies
.push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus(
self.get_status(context),
)));
return true; return true;
} }
UIEvent::Input(ref k) UIEvent::Input(ref k)
@ -930,9 +876,10 @@ impl Component for Listing {
}; };
match k { match k {
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_mailbox"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_mailbox"]) => {
if let Some((_, mailbox_hash)) = self.accounts[self.menu_cursor_pos.0] if self.accounts[self.menu_cursor_pos.0]
.entries .entries
.get(self.menu_cursor_pos.1 + amount) .get(self.menu_cursor_pos.1 + amount)
.is_some()
{ {
self.menu_cursor_pos.1 += amount; self.menu_cursor_pos.1 += amount;
self.set_dirty(true); self.set_dirty(true);
@ -942,10 +889,10 @@ impl Component for Listing {
} }
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_mailbox"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_mailbox"]) => {
if self.cursor_pos.1 >= amount { if self.cursor_pos.1 >= amount {
if let Some((_, mailbox_hash)) = self.accounts if self.accounts[self.menu_cursor_pos.0]
[self.menu_cursor_pos.0]
.entries .entries
.get(self.menu_cursor_pos.1 - amount) .get(self.menu_cursor_pos.1 - amount)
.is_some()
{ {
self.menu_cursor_pos.1 -= amount; self.menu_cursor_pos.1 -= amount;
self.set_dirty(true); self.set_dirty(true);
@ -962,7 +909,9 @@ impl Component for Listing {
} }
UIEvent::Input(ref k) UIEvent::Input(ref k)
if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_account"]) if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_account"])
|| shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_account"]) => || shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_account"])
|| shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_page"])
|| shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_page"]) =>
{ {
let amount = if self.cmd_buf.is_empty() { let amount = if self.cmd_buf.is_empty() {
1 1
@ -980,14 +929,18 @@ impl Component for Listing {
return true; return true;
}; };
match k { match k {
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_account"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_account"])
|| shortcut!(k == shortcuts[Listing::DESCRIPTION]["next_page"]) =>
{
if self.menu_cursor_pos.0 + amount < self.accounts.len() { if self.menu_cursor_pos.0 + amount < self.accounts.len() {
self.menu_cursor_pos = (self.menu_cursor_pos.0 + amount, 0); self.menu_cursor_pos = (self.menu_cursor_pos.0 + amount, 0);
} else { } else {
return true; return true;
} }
} }
k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_account"]) => { k if shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_account"])
|| shortcut!(k == shortcuts[Listing::DESCRIPTION]["prev_page"]) =>
{
if self.menu_cursor_pos.0 >= amount { if self.menu_cursor_pos.0 >= amount {
self.menu_cursor_pos = (self.menu_cursor_pos.0 - amount, 0); self.menu_cursor_pos = (self.menu_cursor_pos.0 - amount, 0);
} else { } else {
@ -1417,5 +1370,6 @@ impl Listing {
.push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus(debug!( .push_back(UIEvent::StatusEvent(StatusEvent::UpdateStatus(debug!(
self.get_status(context) self.get_status(context)
)))); ))));
self.menu_cursor_pos = self.cursor_pos;
} }
} }

@ -39,7 +39,6 @@ impl MailListingTrait for OfflineListing {
return SmallVec::new(); return SmallVec::new();
} }
/// chosen.
fn refresh_mailbox(&mut self, _context: &mut Context, _force: bool) {} fn refresh_mailbox(&mut self, _context: &mut Context, _force: bool) {}
} }
@ -85,7 +84,35 @@ impl OfflineListing {
} }
impl Component for OfflineListing { impl Component for OfflineListing {
fn draw(&mut self, _grid: &mut CellBuffer, _area: Area, _context: &mut Context) {} fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
let theme_default = conf::value(context, "theme_default");
clear_area(grid, area, theme_default);
if let Err(err) = context.is_online(self.cursor_pos.0) {
let (x, _) = write_string_to_grid(
"offline: ",
grid,
Color::Byte(243),
theme_default.bg,
theme_default.attrs,
area,
None,
);
write_string_to_grid(
&err.to_string(),
grid,
Color::Red,
theme_default.bg,
theme_default.attrs,
(set_x(upper_left!(area), x + 1), bottom_right!(area)),
None,
);
} else {
context
.replies
.push_back(UIEvent::AccountStatusChange(self.cursor_pos.0));
}
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 {
false false
} }

Loading…
Cancel
Save