Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- `im` feature, with `Data` support for the [`im` crate](https://docs.rs/im/) collections. ([#924] by [@cmyr])
- `im::Vector` support for the `List` widget. ([#940] by [@xStrom])
- `LifeCycle::Size` event to inform widgets that their size changed. ([#953] by [@xStrom])
- `Button::dynamic` constructor. ([#963] by [@totsteps])

### Changed

Expand Down Expand Up @@ -204,6 +205,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#954]: https://github.com/xi-editor/druid/pull/954
[#959]: https://github.com/xi-editor/druid/pull/959
[#961]: https://github.com/xi-editor/druid/pull/961
[#963]: https://github.com/xi-editor/druid/pull/963

## [0.5.0] - 2020-04-01

Expand Down
25 changes: 25 additions & 0 deletions druid/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ impl<T: Data> Button<T> {
}
}

/// Construct a new dynamic button.
///
/// The contents of this button are generated from the data using a closure.
///
/// This is provided as a convenience; a closure can also be passed to [`new`],
/// but due to limitations of the implementation of that method, the types in
/// the closure need to be annotated, which is not true for this method.
///
/// # Examples
///
/// The following are equivalent.
///
/// ```
/// use druid::Env;
/// use druid::widget::Button;
/// let button1: Button<u32> = Button::new(|data: &u32, _: &Env| format!("total is {}", data));
/// let button2: Button<u32> = Button::dynamic(|data, _| format!("total is {}", data));
/// ```
///
/// [`new`]: #method.new
pub fn dynamic(text: impl Fn(&T, &Env) -> String + 'static) -> Self {
let text: LabelText<T> = text.into();
Button::new(text)
}

/// Provide a closure to be called when this button is clicked.
pub fn on_click(
self,
Expand Down
4 changes: 2 additions & 2 deletions druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ impl<T: Data> Label<T> {
/// ```
/// use druid::Env;
/// use druid::widget::Label;
/// let button1: Label<u32> = Label::new(|data: &u32, _: &Env| format!("total is {}", data));
/// let button2: Label<u32> = Label::dynamic(|data, _| format!("total is {}", data));
/// let label1: Label<u32> = Label::new(|data: &u32, _: &Env| format!("total is {}", data));
/// let label2: Label<u32> = Label::dynamic(|data, _| format!("total is {}", data));
/// ```
///
/// [`new`]: #method.new
Expand Down