From 88a15a75f170f045c4d6a4fd72c762a7f35b6e53 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 18 Jun 2018 23:22:16 -0700 Subject: [PATCH] Dialog: add methods to deal with buttons --- src/views/dialog.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 8129780..4b6820c 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -151,13 +151,38 @@ impl Dialog { /// Adds a button to the dialog with the given label and callback. /// /// Consumes and returns self for easy chaining. - pub fn button>(mut self, label: S, cb: F) -> Self + pub fn button>(self, label: S, cb: F) -> Self + where + F: 'static + Fn(&mut Cursive), + { + self.with(|s| s.add_button(label, cb)) + } + + /// Adds a button to the dialog with the given label and callback. + pub fn add_button>(&mut self, label: S, cb: F) where F: 'static + Fn(&mut Cursive), { self.buttons.push(ChildButton::new(label, cb)); + } - self + /// Returns the number of buttons on this dialog. + pub fn buttons_len(&self) -> usize { + self.buttons.len() + } + + /// Removes any button from `self`. + pub fn clear_buttons(&mut self) { + self.buttons.clear(); + } + + /// Removes a button from this dialog. + /// + /// # Panics + /// + /// Panics if `i >= self.buttons_len()`. + pub fn remove_button(&mut self, i: usize) { + self.buttons.remove(i); } /// Sets the horizontal alignment for the buttons, if any. @@ -247,8 +272,8 @@ impl Dialog { /// Returns an iterator on this buttons for this dialog. pub fn buttons_mut<'a>( &'a mut self, - ) -> Box<'a + Iterator> { - Box::new(self.buttons.iter_mut().map(|b| &mut b.button.view)) + ) -> impl Iterator { + self.buttons.iter_mut().map(|b| &mut b.button.view) } /// Returns currently focused element