From 09ca94f8d686bc1d73cacf69f52a4017b29e5ef2 Mon Sep 17 00:00:00 2001 From: ForLoveOfCats Date: Sat, 12 Sep 2020 02:00:11 -0400 Subject: [PATCH] Add `Button::from_label` --- CHANGELOG.md | 2 ++ druid/src/widget/button.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eeddda2919..64cad5d73a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ You can find its changes [documented below](#060---2020-06-01). - Implementation of `Data` trait for `i128` and `u128` primitive data types. ([#1214] by [@koutoftimer]) - `LineBreaking` enum allows configuration of label line-breaking ([#1195] by [@cmyr]) - `TextAlignment` support in `TextLayout` and `Label` ([#1210] by [@cmyr])` +- `Button::from_label` to construct a `Button` with a provided `Label`. ([#1226] by [@ForLoveOfCats]) ### Changed @@ -447,6 +448,7 @@ Last release without a changelog :( [#1205]: https://github.com/linebender/druid/pull/1205 [#1210]: https://github.com/linebender/druid/pull/1210 [#1214]: https://github.com/linebender/druid/pull/1214 +[#1226]: https://github.com/linebender/druid/pull/1226 [Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master [0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0 diff --git a/druid/src/widget/button.rs b/druid/src/widget/button.rs index 2bf928c127..2615133f9c 100644 --- a/druid/src/widget/button.rs +++ b/druid/src/widget/button.rs @@ -52,6 +52,27 @@ impl Button { } } + /// Create a new button with the provided label. + /// + /// Use the `.on_click` method to provide a closure to be called when the + /// button is clicked. + /// + /// # Examples + /// + /// ``` + /// use druid::widget::Button; + /// + /// let button = Button::from_label(Label::new("Increment")).on_click(|_ctx, data: &mut u32, _env| { + /// *data += 1; + /// }); + /// ``` + pub fn from_label(label: Label) -> Button { + Button { + label, + label_size: Size::ZERO, + } + } + /// Construct a new dynamic button. /// /// The contents of this button are generated from the data using a closure.