Introduce on_press_maybe helper for Button
This commit is contained in:
parent
5324928044
commit
5b5000e3ae
2 changed files with 14 additions and 4 deletions
|
|
@ -79,12 +79,13 @@ impl Application for Example {
|
||||||
}
|
}
|
||||||
Message::Png => {
|
Message::Png => {
|
||||||
if let Some(screenshot) = &self.screenshot {
|
if let Some(screenshot) = &self.screenshot {
|
||||||
|
self.png_saving = true;
|
||||||
|
|
||||||
return Command::perform(
|
return Command::perform(
|
||||||
save_to_png(screenshot.clone()),
|
save_to_png(screenshot.clone()),
|
||||||
Message::PngSaved,
|
Message::PngSaved,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.png_saving = true;
|
|
||||||
}
|
}
|
||||||
Message::PngSaved(res) => {
|
Message::PngSaved(res) => {
|
||||||
self.png_saving = false;
|
self.png_saving = false;
|
||||||
|
|
@ -185,7 +186,7 @@ impl Application for Example {
|
||||||
button("Save to png")
|
button("Save to png")
|
||||||
.style(Button::Secondary)
|
.style(Button::Secondary)
|
||||||
.padding([10, 20, 10, 20])
|
.padding([10, 20, 10, 20])
|
||||||
.on_press(Message::Png)
|
.on_press_maybe(self.screenshot.is_some().then(|| Message::Png))
|
||||||
} else {
|
} else {
|
||||||
button("Saving...")
|
button("Saving...")
|
||||||
.style(Button::Secondary)
|
.style(Button::Secondary)
|
||||||
|
|
|
||||||
|
|
@ -102,8 +102,17 @@ where
|
||||||
/// Sets the message that will be produced when the [`Button`] is pressed.
|
/// Sets the message that will be produced when the [`Button`] is pressed.
|
||||||
///
|
///
|
||||||
/// Unless `on_press` is called, the [`Button`] will be disabled.
|
/// Unless `on_press` is called, the [`Button`] will be disabled.
|
||||||
pub fn on_press(mut self, msg: Message) -> Self {
|
pub fn on_press(mut self, on_press: Message) -> Self {
|
||||||
self.on_press = Some(msg);
|
self.on_press = Some(on_press);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the message that will be produced when the [`Button`] is pressed,
|
||||||
|
/// if `Some`.
|
||||||
|
///
|
||||||
|
/// If `None`, the [`Button`] will be disabled.
|
||||||
|
pub fn on_press_maybe(mut self, on_press: Option<Message>) -> Self {
|
||||||
|
self.on_press = on_press;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue