pub struct Padding<T, W> { /* private fields */ }
Expand description
A widget that just adds padding around its child.
Implementations§
source§impl<T, W: Widget<T>> Padding<T, W>
impl<T, W: Widget<T>> Padding<T, W>
sourcepub fn new(insets: impl Into<KeyOrValue<Insets>>, child: W) -> Padding<T, W>
pub fn new(insets: impl Into<KeyOrValue<Insets>>, child: W) -> Padding<T, W>
Create a new Padding
with the specified padding and child.
The insets
argument can either be an instance of Insets
,
a Key
referring to Insets
in the Env
,
an f64
for uniform padding, an (f64, f64)
for axis-uniform padding,
or (f64, f64, f64, f64)
(left, top, right, bottom) values.
Examples
Uniform padding:
use druid::widget::{Label, Padding};
use druid::kurbo::Insets;
let _: Padding<(), _> = Padding::new(10.0, Label::new("uniform!"));
let _: Padding<(), _> = Padding::new(Insets::uniform(10.0), Label::new("uniform!"));
Uniform padding across each axis:
use druid::widget::{Label, Padding};
use druid::kurbo::Insets;
let child: Label<()> = Label::new("I need my space!");
let _: Padding<(), _> = Padding::new((10.0, 20.0), Label::new("more y than x!"));
// equivalent:
let _: Padding<(), _> = Padding::new(Insets::uniform_xy(10.0, 20.0), Label::new("ditto :)"));
Examples found in repository?
More examples
examples/multiwin.rs (line 67)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
fn ui_builder() -> impl Widget<State> {
let text = LocalizedString::new("hello-counter")
.with_arg("count", |data: &State, _env| data.menu_count.into());
let label = Label::new(text);
let inc_button =
Button::<State>::new("Add menu item").on_click(|_ctx, data, _env| data.menu_count += 1);
let dec_button = Button::<State>::new("Remove menu item")
.on_click(|_ctx, data, _env| data.menu_count = data.menu_count.saturating_sub(1));
let new_button = Button::<State>::new("New window").on_click(|ctx, _data, _env| {
ctx.submit_command(sys_cmds::NEW_FILE.to(Global));
});
let quit_button = Button::<State>::new("Quit app").on_click(|_ctx, _data, _env| {
Application::global().quit();
});
let mut col = Flex::column();
col.add_flex_child(Align::centered(Padding::new(5.0, label)), 1.0);
let mut row = Flex::row();
row.add_child(Padding::new(5.0, inc_button));
row.add_child(Padding::new(5.0, dec_button));
col.add_flex_child(Align::centered(row), 1.0);
let mut row = Flex::row();
row.add_child(Padding::new(5.0, new_button));
row.add_child(Padding::new(5.0, quit_button));
col.add_flex_child(Align::centered(row), 1.0);
let content = ControllerHost::new(col, ContextMenuController);
Glow::new(content)
}
examples/switches.rs (line 41)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
fn build_widget() -> impl Widget<DemoState> {
let mut col = Flex::column();
let mut row = Flex::row();
let switch = LensWrap::new(Switch::new(), DemoState::value);
let check_box = LensWrap::new(Checkbox::new(""), DemoState::value);
let switch_label = Label::new("Setting label");
row.add_child(Padding::new(5.0, switch_label));
row.add_child(Padding::new(5.0, switch));
row.add_child(Padding::new(5.0, check_box));
let stepper = LensWrap::new(
Stepper::new()
.with_range(0.0, 10.0)
.with_step(0.5)
.with_wraparound(false),
DemoState::stepper_value,
);
let mut textbox_row = Flex::row();
// TODO: Replace Parse usage with TextBox::with_formatter
#[allow(deprecated)]
let textbox = LensWrap::new(
Parse::new(TextBox::new()),
DemoState::stepper_value.map(|x| Some(*x), |x, y| *x = y.unwrap_or(0.0)),
);
textbox_row.add_child(Padding::new(5.0, textbox));
textbox_row.add_child(Padding::new(5.0, stepper.center()));
let mut label_row = Flex::row();
let label = Label::new(|data: &DemoState, _env: &_| {
format!("Stepper value: {0:.2}", data.stepper_value)
});
label_row.add_child(Padding::new(5.0, label));
col.set_main_axis_alignment(MainAxisAlignment::Center);
col.add_child(Padding::new(5.0, row));
col.add_child(Padding::new(5.0, textbox_row));
col.add_child(Padding::new(5.0, label_row));
col.center()
}
examples/split_demo.rs (lines 25-35)
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
fn build_app() -> impl Widget<u32> {
let fixed_cols = Padding::new(
10.0,
Container::new(
Split::columns(
Align::centered(Label::new("Left Split")),
Align::centered(Label::new("Right Split")),
)
.split_point(0.5),
)
.border(Color::WHITE, 1.0),
);
let fixed_rows = Padding::new(
10.0,
Container::new(
Split::rows(
Align::centered(Label::new("Top Split")),
Align::centered(Label::new("Bottom Split")),
)
.split_point(0.4)
.bar_size(3.0),
)
.border(Color::WHITE, 1.0),
);
let draggable_cols = Padding::new(
10.0,
Container::new(
Split::columns(
Align::centered(Label::new("Split A")),
Align::centered(Label::new("Split B")),
)
.split_point(0.5)
.draggable(true)
.solid_bar(true)
.min_size(60.0, 60.0),
)
.border(Color::WHITE, 1.0),
);
Padding::new(
10.0,
Container::new(
Split::rows(
Split::rows(fixed_cols, fixed_rows)
.split_point(0.33)
.bar_size(3.0)
.min_bar_area(3.0)
.draggable(true),
draggable_cols,
)
.split_point(0.75)
.bar_size(5.0)
.min_bar_area(11.0)
.draggable(true),
)
.border(Color::WHITE, 1.0),
)
}
Trait Implementations§
source§impl<T: Data, W: Widget<T>> Widget<T> for Padding<T, W>
impl<T: Data, W: Widget<T>> Widget<T> for Padding<T, W>
source§fn event(
&mut self,
ctx: &mut EventCtx<'_, '_>,
event: &Event,
data: &mut T,
env: &Env
)
fn event( &mut self, ctx: &mut EventCtx<'_, '_>, event: &Event, data: &mut T, env: &Env )
Handle an event. Read more
source§fn lifecycle(
&mut self,
ctx: &mut LifeCycleCtx<'_, '_>,
event: &LifeCycle,
data: &T,
env: &Env
)
fn lifecycle( &mut self, ctx: &mut LifeCycleCtx<'_, '_>, event: &LifeCycle, data: &T, env: &Env )
Handle a life cycle notification. Read more
source§fn layout(
&mut self,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &T,
env: &Env
) -> Size
fn layout( &mut self, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &T, env: &Env ) -> Size
Compute layout. Read more
source§fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)
fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &T, env: &Env)
Paint the widget appearance. Read more
source§fn compute_max_intrinsic(
&mut self,
axis: Axis,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &T,
env: &Env
) -> f64
fn compute_max_intrinsic( &mut self, axis: Axis, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &T, env: &Env ) -> f64
Computes max intrinsic/preferred dimension of a widget on the provided axis. Read more
source§impl<T, W> WidgetWrapper for Padding<T, W>
impl<T, W> WidgetWrapper for Padding<T, W>
§type Wrapped = W
type Wrapped = W
The type of the wrapped widget.
Maybe we would like to constrain this to
Widget<impl Data>
(if existential bounds were supported).
Any other scheme leads to T
being unconstrained in unification at some pointsource§fn wrapped_mut(&mut self) -> &mut Self::Wrapped
fn wrapped_mut(&mut self) -> &mut Self::Wrapped
Get mutable access to the wrapped child
Auto Trait Implementations§
impl<T, W> !RefUnwindSafe for Padding<T, W>
impl<T, W> !Send for Padding<T, W>
impl<T, W> !Sync for Padding<T, W>
impl<T, W> Unpin for Padding<T, W>where T: Unpin, W: Unpin,
impl<T, W> !UnwindSafe for Padding<T, W>
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> RoundFrom<T> for T
impl<T> RoundFrom<T> for T
§fn round_from(x: T) -> T
fn round_from(x: T) -> T
Performs the conversion.
§impl<T, U> RoundInto<U> for Twhere
U: RoundFrom<T>,
impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,
§fn round_into(self) -> U
fn round_into(self) -> U
Performs the conversion.