Skip to content

Commit 536c23a

Browse files
committed
Padding can be constructed with Key<Insets>
1 parent fa156d7 commit 536c23a

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ You can find its changes [documented below](#070---2021-01-01).
2626
- New `TextBox` widget with IME integration ([#1636] by [@cmyr])
2727
- `Notification`s can be submitted while handling other `Notification`s ([#1640] by [@cmyr])
2828
- Added ListIter implementations for OrdMap ([#1641] by [@Lejero])
29+
- `Padding` can now use `Key<Insets>` ([#1662] by [@cmyr])
2930

3031
### Changed
3132

@@ -644,6 +645,7 @@ Last release without a changelog :(
644645
[#1640]: https://github.com/linebender/druid/pull/1640
645646
[#1641]: https://github.com/linebender/druid/pull/1641
646647
[#1647]: https://github.com/linebender/druid/pull/1647
648+
[#1662]: https://github.com/linebender/druid/pull/1662
647649

648650
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
649651
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

druid/src/env.rs

+18
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ impl<T: ValueType> From<Key<T>> for KeyOrValue<T> {
623623
}
624624
}
625625

626+
impl Into<KeyOrValue<Insets>> for f64 {
627+
fn into(self) -> KeyOrValue<Insets> {
628+
KeyOrValue::Concrete(self.into())
629+
}
630+
}
631+
632+
impl Into<KeyOrValue<Insets>> for (f64, f64) {
633+
fn into(self) -> KeyOrValue<Insets> {
634+
KeyOrValue::Concrete(self.into())
635+
}
636+
}
637+
638+
impl Into<KeyOrValue<Insets>> for (f64, f64, f64, f64) {
639+
fn into(self) -> KeyOrValue<Insets> {
640+
KeyOrValue::Concrete(self.into())
641+
}
642+
}
643+
626644
#[cfg(test)]
627645
mod tests {
628646
use super::*;

druid/src/widget/padding.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,21 @@
1515
//! A widget that just adds padding during layout.
1616
1717
use crate::widget::{prelude::*, WidgetWrapper};
18-
use crate::{Data, Insets, Point, WidgetPod};
18+
use crate::{Data, Insets, KeyOrValue, Point, WidgetPod};
1919

2020
use tracing::{instrument, trace};
2121

2222
/// A widget that just adds padding around its child.
2323
pub struct Padding<T, W> {
24-
left: f64,
25-
right: f64,
26-
top: f64,
27-
bottom: f64,
28-
24+
insets: KeyOrValue<Insets>,
2925
child: WidgetPod<T, W>,
3026
}
3127

3228
impl<T, W: Widget<T>> Padding<T, W> {
3329
/// Create a new widget with the specified padding. This can either be an instance
34-
/// of [`kurbo::Insets`], a f64 for uniform padding, a 2-tuple for axis-uniform padding
35-
/// or 4-tuple with (left, top, right, bottom) values.
30+
/// of [`Insets`], a [`Key`] referring to [`Insets`] in the [`Env`],
31+
/// an `f64` for uniform padding, a `(f64, f64)` for axis-uniform padding,
32+
/// or `(f64, f64, f64, f64)` (left, top, right, bottom) values.
3633
///
3734
/// # Examples
3835
///
@@ -58,14 +55,10 @@ impl<T, W: Widget<T>> Padding<T, W> {
5855
/// let _: Padding<(), _> = Padding::new(Insets::uniform_xy(10.0, 20.0), Label::new("ditto :)"));
5956
/// ```
6057
///
61-
/// [`kurbo::Insets`]: https://docs.rs/kurbo/0.5.3/kurbo/struct.Insets.html
62-
pub fn new(insets: impl Into<Insets>, child: W) -> Padding<T, W> {
63-
let insets = insets.into();
58+
/// [`Key`]: crate::Key
59+
pub fn new(insets: impl Into<KeyOrValue<Insets>>, child: W) -> Padding<T, W> {
6460
Padding {
65-
left: insets.x0,
66-
right: insets.x1,
67-
top: insets.y0,
68-
bottom: insets.y1,
61+
insets: insets.into(),
6962
child: WidgetPod::new(child),
7063
}
7164
}
@@ -86,25 +79,25 @@ impl<T: Data, W: Widget<T>> Widget<T> for Padding<T, W> {
8679
self.child.lifecycle(ctx, event, data, env)
8780
}
8881

89-
#[instrument(
90-
name = "Padding",
91-
level = "trace",
92-
skip(self, ctx, _old_data, data, env)
93-
)]
94-
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &T, data: &T, env: &Env) {
82+
#[instrument(name = "Padding", level = "trace", skip(self, ctx, _old, data, env))]
83+
fn update(&mut self, ctx: &mut UpdateCtx, _old: &T, data: &T, env: &Env) {
84+
if ctx.env_key_changed(&self.insets) {
85+
ctx.request_layout();
86+
}
9587
self.child.update(ctx, data, env);
9688
}
9789

9890
#[instrument(name = "Padding", level = "trace", skip(self, ctx, bc, data, env))]
9991
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
10092
bc.debug_check("Padding");
93+
let insets = self.insets.resolve(env);
10194

102-
let hpad = self.left + self.right;
103-
let vpad = self.top + self.bottom;
95+
let hpad = insets.x0 + insets.x1;
96+
let vpad = insets.y0 + insets.y1;
10497

10598
let child_bc = bc.shrink((hpad, vpad));
10699
let size = self.child.layout(ctx, &child_bc, data, env);
107-
let origin = Point::new(self.left, self.top);
100+
let origin = Point::new(insets.x0, insets.y0);
108101
self.child.set_origin(ctx, data, env, origin);
109102

110103
let my_size = Size::new(size.width + hpad, size.height + vpad);

druid/src/widget/widget_ext.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ use crate::{
2828
pub trait WidgetExt<T: Data>: Widget<T> + Sized + 'static {
2929
/// Wrap this widget in a [`Padding`] widget with the given [`Insets`].
3030
///
31-
/// [`Padding`]: widget/struct.Padding.html
32-
/// [`Insets`]: kurbo/struct.Insets.html
33-
fn padding(self, insets: impl Into<Insets>) -> Padding<T, Self> {
31+
/// Like [`Padding::new`], this can accept a variety of arguments, including
32+
/// a [`Key`] referring to [`Insets`] in the [`Env`].
33+
///
34+
/// [`Key`]: crate::Key
35+
/// [`Insets`]: crate::Insets
36+
fn padding(self, insets: impl Into<KeyOrValue<Insets>>) -> Padding<T, Self> {
3437
Padding::new(insets, self)
3538
}
3639

0 commit comments

Comments
 (0)