ui: don't quit if editing a draft

Ask user to save draft or discard it.
This commit is contained in:
Manos Pitsidianakis 2019-09-27 13:14:16 +03:00
parent 713c4f73b9
commit d44a68ec69
No known key found for this signature in database
GPG Key ID: 73627C2F690DF710
5 changed files with 35 additions and 2 deletions

View File

@ -254,8 +254,12 @@ fn main() -> std::result::Result<(), std::io::Error> {
UIMode::Normal => {
match k {
Key::Char('q') | Key::Char('Q') => {
drop(state);
break 'main;
if state.can_quit_cleanly() {
drop(state);
break 'main;
} else {
state.redraw();
}
},
Key::Char(' ') => {
state.mode = UIMode::Execute;

View File

@ -90,6 +90,9 @@ pub trait Component: Display + Debug + Send {
fn is_visible(&self) -> bool {
true
}
fn can_quit_cleanly(&mut self) -> bool {
true
}
fn set_dirty(&mut self);
fn kill(&mut self, _id: ComponentId, _context: &mut Context) {}
fn set_id(&mut self, _id: ComponentId) {}

View File

@ -756,6 +756,13 @@ impl Component for Composer {
fn set_id(&mut self, id: ComponentId) {
self.id = id;
}
fn can_quit_cleanly(&mut self) -> bool {
/* Play it safe and ask user for confirmation */
self.mode = ViewMode::Discard(self.id);
self.set_dirty();
false
}
}
pub fn send_draft(context: &mut Context, account_cursor: usize, draft: Draft) -> bool {

View File

@ -1066,6 +1066,10 @@ impl Component for StatusBar {
fn set_id(&mut self, id: ComponentId) {
self.id = id;
}
fn can_quit_cleanly(&mut self) -> bool {
self.container.can_quit_cleanly()
}
}
#[derive(Debug)]
@ -1530,6 +1534,17 @@ impl Component for Tabbed {
fn set_id(&mut self, id: ComponentId) {
self.id = id;
}
fn can_quit_cleanly(&mut self) -> bool {
for (i, c) in self.children.iter_mut().enumerate() {
if !c.can_quit_cleanly() {
self.cursor_pos = i;
self.set_dirty();
return false;
}
}
true
}
}
type EntryIdentifier = Vec<u8>;

View File

@ -514,6 +514,10 @@ impl State {
}
}
pub fn can_quit_cleanly(&mut self) -> bool {
self.components.iter_mut().all(|c| c.can_quit_cleanly())
}
pub fn register_component(&mut self, component: Box<dyn Component>) {
self.components.push(component);
}