Add Button::set_label

This commit is contained in:
Alexandre Bury 2018-01-12 09:33:47 +01:00
parent 0666a8b585
commit 56f81eee7c

View File

@ -25,9 +25,10 @@ pub struct Button {
impl Button { impl Button {
/// Creates a new button with the given content and callback. /// Creates a new button with the given content and callback.
pub fn new<F, R, S: Into<String>>(label: S, cb: F) -> Self pub fn new<F, R, S>(label: S, cb: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive) -> R,
S: Into<String>,
{ {
let label = label.into(); let label = label.into();
Self::new_raw(format!("<{}>", label), cb) Self::new_raw(format!("<{}>", label), cb)
@ -99,6 +100,26 @@ impl Button {
&self.label &self.label
} }
/// Sets the label to the given value.
///
/// This will include brackets.
pub fn set_label<S>(&mut self, label: S)
where
S: Into<String>,
{
self.set_label_raw(format!("<{}>", label.into()));
}
/// Sets the label exactly to the given value.
///
/// This will not include brackets.
pub fn set_label_raw<S>(&mut self, label: S)
where
S: Into<String>,
{
self.label = label.into();
}
fn req_size(&self) -> Vec2 { fn req_size(&self) -> Vec2 {
Vec2::new(self.label.width(), 1) Vec2::new(self.label.width(), 1)
} }