Skip to content

Commit f8337b8

Browse files
committed
Add helper functions for alignment to widget module
1 parent 75a6f32 commit f8337b8

File tree

12 files changed

+208
-64
lines changed

12 files changed

+208
-64
lines changed

examples/bezier_tool/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This example showcases an interactive `Canvas` for drawing Bézier curves.
2-
use iced::widget::{button, container, horizontal_space, hover};
3-
use iced::{Element, Fill, Theme};
2+
use iced::widget::{button, container, horizontal_space, hover, right};
3+
use iced::{Element, Theme};
44

55
pub fn main() -> iced::Result {
66
iced::application("Bezier Tool - Iced", Example::update, Example::view)
@@ -41,13 +41,12 @@ impl Example {
4141
if self.curves.is_empty() {
4242
container(horizontal_space())
4343
} else {
44-
container(
44+
right(
4545
button("Clear")
4646
.style(button::danger)
4747
.on_press(Message::Clear),
4848
)
4949
.padding(10)
50-
.align_right(Fill)
5150
},
5251
))
5352
.padding(20)

examples/editor/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use iced::highlighter;
22
use iced::keyboard;
33
use iced::widget::{
4-
self, button, column, container, horizontal_space, pick_list, row, text,
5-
text_editor, toggler, tooltip,
4+
self, button, center_x, column, container, horizontal_space, pick_list,
5+
row, text, text_editor, toggler, tooltip,
66
};
77
use iced::{Center, Element, Fill, Font, Task, Theme};
88

@@ -288,7 +288,7 @@ fn action<'a, Message: Clone + 'a>(
288288
label: &'a str,
289289
on_press: Option<Message>,
290290
) -> Element<'a, Message> {
291-
let action = button(container(content).center_x(30));
291+
let action = button(center_x(content).width(30));
292292

293293
if let Some(on_press) = on_press {
294294
tooltip(

examples/geometry/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ mod rainbow {
152152
}
153153
}
154154

155-
use iced::widget::{column, container, scrollable};
156-
use iced::{Element, Length};
155+
use iced::widget::{center_x, center_y, column, scrollable};
156+
use iced::Element;
157157
use rainbow::rainbow;
158158

159159
pub fn main() -> iced::Result {
@@ -176,7 +176,7 @@ fn view(_state: &()) -> Element<'_, ()> {
176176
.spacing(20)
177177
.max_width(500);
178178

179-
let scrollable = scrollable(container(content).center_x(Length::Fill));
179+
let scrollable = scrollable(center_x(content));
180180

181-
container(scrollable).center_y(Length::Fill).into()
181+
center_y(scrollable).into()
182182
}

examples/integration/src/controls.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use iced_wgpu::Renderer;
2-
use iced_widget::{column, container, row, slider, text, text_input};
3-
use iced_winit::core::{Color, Element, Length::*, Theme};
2+
use iced_widget::{bottom, column, row, slider, text, text_input};
3+
use iced_winit::core::{Color, Element, Theme};
44
use iced_winit::runtime::{Program, Task};
55

66
pub struct Controls {
@@ -74,18 +74,17 @@ impl Program for Controls {
7474
.width(500)
7575
.spacing(20);
7676

77-
container(
77+
bottom(
7878
column![
7979
text("Background color").color(Color::WHITE),
8080
text!("{background_color:?}").size(14).color(Color::WHITE),
81-
text_input("Placeholder", &self.input)
82-
.on_input(Message::InputChanged),
8381
sliders,
82+
text_input("Type something...", &self.input)
83+
.on_input(Message::InputChanged),
8484
]
8585
.spacing(10),
8686
)
8787
.padding(10)
88-
.align_bottom(Fill)
8988
.into()
9089
}
9190
}

examples/layout/src/main.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use iced::border;
22
use iced::keyboard;
33
use iced::mouse;
44
use iced::widget::{
5-
button, canvas, center, checkbox, column, container, horizontal_rule,
6-
horizontal_space, pick_list, pin, row, scrollable, stack, text,
7-
vertical_rule,
5+
button, canvas, center, center_y, checkbox, column, container,
6+
horizontal_rule, horizontal_space, pick_list, pin, row, scrollable, stack,
7+
text, vertical_rule,
88
};
99
use iced::{
1010
color, Center, Element, Fill, Font, Length, Point, Rectangle, Renderer,
@@ -253,15 +253,14 @@ fn application<'a>() -> Element<'a, Message> {
253253
.border(border::color(palette.background.strong.color).width(1))
254254
});
255255

256-
let sidebar = container(
256+
let sidebar = center_y(
257257
column!["Sidebar!", square(50), square(50)]
258258
.spacing(40)
259259
.padding(10)
260260
.width(200)
261261
.align_x(Center),
262262
)
263-
.style(container::rounded_box)
264-
.center_y(Fill);
263+
.style(container::rounded_box);
265264

266265
let content = container(
267266
scrollable(

examples/multi_window/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use iced::widget::{
2-
button, center, column, container, horizontal_space, scrollable, text,
2+
button, center, center_x, column, horizontal_space, scrollable, text,
33
text_input,
44
};
55
use iced::window;
@@ -193,6 +193,6 @@ impl Window {
193193
.align_x(Center),
194194
);
195195

196-
container(content).center_x(200).into()
196+
center_x(content).width(200).into()
197197
}
198198
}

examples/pane_grid/src/main.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use iced::keyboard;
22
use iced::widget::pane_grid::{self, PaneGrid};
33
use iced::widget::{
4-
button, column, container, responsive, row, scrollable, text,
4+
button, center_y, column, container, responsive, row, scrollable, text,
55
};
66
use iced::{Center, Color, Element, Fill, Size, Subscription};
77

@@ -196,11 +196,7 @@ impl Example {
196196
.on_drag(Message::Dragged)
197197
.on_resize(10, Message::Resized);
198198

199-
container(pane_grid)
200-
.width(Fill)
201-
.height(Fill)
202-
.padding(10)
203-
.into()
199+
container(pane_grid).padding(10).into()
204200
}
205201
}
206202

@@ -295,10 +291,7 @@ fn view_content<'a>(
295291
.spacing(10)
296292
.align_x(Center);
297293

298-
container(scrollable(content))
299-
.center_y(Fill)
300-
.padding(5)
301-
.into()
294+
center_y(scrollable(content)).padding(5).into()
302295
}
303296

304297
fn view_controls<'a>(

examples/screenshot/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use iced::keyboard;
2-
use iced::widget::{button, column, container, image, row, text, text_input};
2+
use iced::widget::{
3+
button, center_y, column, container, image, row, text, text_input,
4+
};
35
use iced::window;
46
use iced::window::screenshot::{self, Screenshot};
57
use iced::{
@@ -131,8 +133,8 @@ impl Example {
131133
text("Press the button to take a screenshot!").into()
132134
};
133135

134-
let image = container(image)
135-
.center_y(FillPortion(2))
136+
let image = center_y(image)
137+
.height(FillPortion(2))
136138
.padding(10)
137139
.style(container::rounded_box);
138140

@@ -211,7 +213,7 @@ impl Example {
211213
.spacing(40)
212214
};
213215

214-
let side_content = container(controls).center_y(Fill);
216+
let side_content = center_y(controls);
215217

216218
let content = row![side_content, image]
217219
.spacing(10)

examples/svg/src/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use iced::widget::{center, checkbox, column, container, svg};
1+
use iced::widget::{center, center_x, checkbox, column, svg};
22
use iced::{color, Element, Fill};
33

44
pub fn main() -> iced::Result {
@@ -46,12 +46,8 @@ impl Tiger {
4646
checkbox("Apply a color filter", self.apply_color_filter)
4747
.on_toggle(Message::ToggleColorFilter);
4848

49-
center(
50-
column![svg, container(apply_color_filter).center_x(Fill)]
51-
.spacing(20)
52-
.height(Fill),
53-
)
54-
.padding(20)
55-
.into()
49+
center(column![svg, center_x(apply_color_filter)].spacing(20))
50+
.padding(20)
51+
.into()
5652
}
5753
}

examples/todos/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use iced::keyboard;
22
use iced::widget::{
3-
self, button, center, checkbox, column, container, keyed_column, row,
3+
self, button, center, center_x, checkbox, column, keyed_column, row,
44
scrollable, text, text_input, Text,
55
};
66
use iced::window;
@@ -237,7 +237,7 @@ impl Todos {
237237
.spacing(20)
238238
.max_width(800);
239239

240-
scrollable(container(content).center_x(Fill).padding(40)).into()
240+
scrollable(center_x(content).padding(40)).into()
241241
}
242242
}
243243
}

examples/tour/src/main.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use iced::widget::{
2-
button, checkbox, column, container, horizontal_space, image, radio, row,
3-
scrollable, slider, text, text_input, toggler, vertical_space,
2+
button, center_x, center_y, checkbox, column, horizontal_space, image,
3+
radio, row, scrollable, slider, text, text_input, toggler, vertical_space,
44
};
55
use iced::widget::{Button, Column, Container, Slider};
66
use iced::{Center, Color, Element, Fill, Font, Pixels};
@@ -166,16 +166,13 @@ impl Tour {
166166
.padding(20)
167167
.into();
168168

169-
let scrollable = scrollable(
170-
container(if self.debug {
171-
content.explain(Color::BLACK)
172-
} else {
173-
content
174-
})
175-
.center_x(Fill),
176-
);
169+
let scrollable = scrollable(center_x(if self.debug {
170+
content.explain(Color::BLACK)
171+
} else {
172+
content
173+
}));
177174

178-
container(scrollable).center_y(Fill).into()
175+
center_y(scrollable).into()
179176
}
180177

181178
fn can_continue(&self) -> bool {
@@ -543,7 +540,7 @@ fn ferris<'a>(
543540
width: u16,
544541
filter_method: image::FilterMethod,
545542
) -> Container<'a, Message> {
546-
container(
543+
center_x(
547544
// This should go away once we unify resource loading on native
548545
// platforms
549546
if cfg!(target_arch = "wasm32") {
@@ -554,7 +551,6 @@ fn ferris<'a>(
554551
.filter_method(filter_method)
555552
.width(width),
556553
)
557-
.center_x(Fill)
558554
}
559555

560556
fn padded_button<Message: Clone>(label: &str) -> Button<'_, Message> {

0 commit comments

Comments
 (0)