Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit lens. #1232

Merged
merged 1 commit into from
Sep 14, 2020
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 @@ -36,6 +36,7 @@ You can find its changes [documented below](#060---2020-06-01).
- `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])
- Lens: Added Unit lens for type erased / display only widgets that do not need data. ([#1232] by [@rjwittams])

### Changed

Expand Down Expand Up @@ -449,6 +450,7 @@ Last release without a changelog :(
[#1210]: https://github.com/linebender/druid/pull/1210
[#1214]: https://github.com/linebender/druid/pull/1214
[#1226]: https://github.com/linebender/druid/pull/1226
[#1232]: https://github.com/linebender/druid/pull/1232

[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
Expand Down
26 changes: 26 additions & 0 deletions druid/src/lens/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,29 @@ where
v
}
}

/// A `Lens` that always yields ().
///
/// This is useful when you wish to have a display only widget, require a type-erased widget, or
/// obtain app data out of band and ignore your input. (E.g sub-windows)
#[derive(Debug, Copy, Clone)]
pub struct Unit<T> {
phantom_t: PhantomData<T>,
}

impl<T> Default for Unit<T> {
fn default() -> Self {
Unit {
phantom_t: Default::default(),
}
}
}

impl<T> Lens<T, ()> for Unit<T> {
fn with<V, F: FnOnce(&()) -> V>(&self, _data: &T, f: F) -> V {
f(&())
}
fn with_mut<V, F: FnOnce(&mut ()) -> V>(&self, _data: &mut T, f: F) -> V {
f(&mut ())
}
}
2 changes: 1 addition & 1 deletion druid/src/lens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@

#[allow(clippy::module_inception)]
mod lens;
pub use lens::{Deref, Field, Id, InArc, Index, Map, Ref, Then};
pub use lens::{Deref, Field, Id, InArc, Index, Map, Ref, Then, Unit};
#[doc(hidden)]
pub use lens::{Lens, LensExt, LensWrap};