Implement padding
support for Container
This commit is contained in:
parent
6e9ab1cd6f
commit
749a9588d7
@ -66,6 +66,7 @@ impl Application for Clock {
|
|||||||
Container::new(canvas)
|
Container::new(canvas)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
|
.padding(20)
|
||||||
.center_x()
|
.center_x()
|
||||||
.center_y()
|
.center_y()
|
||||||
.into()
|
.into()
|
||||||
|
@ -106,11 +106,10 @@ impl Sandbox for Example {
|
|||||||
.on_resize(Message::Resized)
|
.on_resize(Message::Resized)
|
||||||
.on_key_press(handle_hotkey);
|
.on_key_press(handle_hotkey);
|
||||||
|
|
||||||
Column::new()
|
Container::new(pane_grid)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.push(pane_grid)
|
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -213,9 +212,10 @@ impl Content {
|
|||||||
.push(Text::new(format!("Pane {}", id)).size(30))
|
.push(Text::new(format!("Pane {}", id)).size(30))
|
||||||
.push(controls);
|
.push(controls);
|
||||||
|
|
||||||
Container::new(Column::new().padding(5).push(content))
|
Container::new(content)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
|
.padding(5)
|
||||||
.center_y()
|
.center_y()
|
||||||
.style(style::Pane {
|
.style(style::Pane {
|
||||||
is_focused: focus.is_some(),
|
is_focused: focus.is_some(),
|
||||||
|
@ -225,7 +225,7 @@ enum Error {
|
|||||||
|
|
||||||
impl From<reqwest::Error> for Error {
|
impl From<reqwest::Error> for Error {
|
||||||
fn from(error: reqwest::Error) -> Error {
|
fn from(error: reqwest::Error) -> Error {
|
||||||
dbg!(&error);
|
dbg!(error);
|
||||||
|
|
||||||
Error::APIError
|
Error::APIError
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use iced::{Column, Container, Element, Length, Sandbox, Settings, Svg};
|
use iced::{Container, Element, Length, Sandbox, Settings, Svg};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -23,18 +23,17 @@ impl Sandbox for Tiger {
|
|||||||
fn update(&mut self, _message: ()) {}
|
fn update(&mut self, _message: ()) {}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<()> {
|
fn view(&mut self) -> Element<()> {
|
||||||
let content = Column::new().padding(20).push(
|
let svg = Svg::new(format!(
|
||||||
Svg::new(format!(
|
"{}/resources/tiger.svg",
|
||||||
"{}/resources/tiger.svg",
|
env!("CARGO_MANIFEST_DIR")
|
||||||
env!("CARGO_MANIFEST_DIR")
|
))
|
||||||
))
|
.width(Length::Fill)
|
||||||
.width(Length::Fill)
|
.height(Length::Fill);
|
||||||
.height(Length::Fill),
|
|
||||||
);
|
|
||||||
|
|
||||||
Container::new(content)
|
Container::new(svg)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
|
.padding(20)
|
||||||
.center_x()
|
.center_x()
|
||||||
.center_y()
|
.center_y()
|
||||||
.into()
|
.into()
|
||||||
|
@ -171,8 +171,5 @@ where
|
|||||||
let (width, height) = axis.pack(main - padding, cross);
|
let (width, height) = axis.pack(main - padding, cross);
|
||||||
let size = limits.resolve(Size::new(width, height));
|
let size = limits.resolve(Size::new(width, height));
|
||||||
|
|
||||||
Node::with_children(
|
Node::with_children(size.pad(padding), nodes)
|
||||||
Size::new(size.width + padding * 2.0, size.height + padding * 2.0),
|
|
||||||
nodes,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ use std::u32;
|
|||||||
/// It is normally used for alignment purposes.
|
/// It is normally used for alignment purposes.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Container<'a, Message, Renderer: self::Renderer> {
|
pub struct Container<'a, Message, Renderer: self::Renderer> {
|
||||||
|
padding: u16,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
max_width: u32,
|
max_width: u32,
|
||||||
@ -35,6 +36,7 @@ where
|
|||||||
T: Into<Element<'a, Message, Renderer>>,
|
T: Into<Element<'a, Message, Renderer>>,
|
||||||
{
|
{
|
||||||
Container {
|
Container {
|
||||||
|
padding: 0,
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
max_width: u32::MAX,
|
max_width: u32::MAX,
|
||||||
@ -46,6 +48,14 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the padding of the [`Container`].
|
||||||
|
///
|
||||||
|
/// [`Container`]: struct.Column.html
|
||||||
|
pub fn padding(mut self, units: u16) -> Self {
|
||||||
|
self.padding = units;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the width of the [`Container`].
|
/// Sets the width of the [`Container`].
|
||||||
///
|
///
|
||||||
/// [`Container`]: struct.Container.html
|
/// [`Container`]: struct.Container.html
|
||||||
@ -137,19 +147,23 @@ where
|
|||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
|
let padding = f32::from(self.padding);
|
||||||
|
|
||||||
let limits = limits
|
let limits = limits
|
||||||
.loose()
|
.loose()
|
||||||
.max_width(self.max_width)
|
.max_width(self.max_width)
|
||||||
.max_height(self.max_height)
|
.max_height(self.max_height)
|
||||||
.width(self.width)
|
.width(self.width)
|
||||||
.height(self.height);
|
.height(self.height)
|
||||||
|
.pad(padding);
|
||||||
|
|
||||||
let mut content = self.content.layout(renderer, &limits.loose());
|
let mut content = self.content.layout(renderer, &limits.loose());
|
||||||
let size = limits.resolve(content.size());
|
let size = limits.resolve(content.size());
|
||||||
|
|
||||||
|
content.move_to(Point::new(padding, padding));
|
||||||
content.align(self.horizontal_alignment, self.vertical_alignment, size);
|
content.align(self.horizontal_alignment, self.vertical_alignment, size);
|
||||||
|
|
||||||
layout::Node::with_children(size, vec![content])
|
layout::Node::with_children(size.pad(padding), vec![content])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
@ -191,6 +205,7 @@ where
|
|||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
std::any::TypeId::of::<Container<'_, (), Renderer>>().hash(state);
|
std::any::TypeId::of::<Container<'_, (), Renderer>>().hash(state);
|
||||||
|
|
||||||
|
self.padding.hash(state);
|
||||||
self.width.hash(state);
|
self.width.hash(state);
|
||||||
self.height.hash(state);
|
self.height.hash(state);
|
||||||
self.max_width.hash(state);
|
self.max_width.hash(state);
|
||||||
|
@ -8,6 +8,7 @@ pub use iced_style::container::{Style, StyleSheet};
|
|||||||
/// It is normally used for alignment purposes.
|
/// It is normally used for alignment purposes.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Container<'a, Message> {
|
pub struct Container<'a, Message> {
|
||||||
|
padding: u16,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
max_width: u32,
|
max_width: u32,
|
||||||
@ -29,6 +30,7 @@ impl<'a, Message> Container<'a, Message> {
|
|||||||
use std::u32;
|
use std::u32;
|
||||||
|
|
||||||
Container {
|
Container {
|
||||||
|
padding: 0,
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
max_width: u32::MAX,
|
max_width: u32::MAX,
|
||||||
@ -40,6 +42,14 @@ impl<'a, Message> Container<'a, Message> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the padding of the [`Container`].
|
||||||
|
///
|
||||||
|
/// [`Container`]: struct.Column.html
|
||||||
|
pub fn padding(mut self, units: u16) -> Self {
|
||||||
|
self.padding = units;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the width of the [`Container`].
|
/// Sets the width of the [`Container`].
|
||||||
///
|
///
|
||||||
/// [`Container`]: struct.Container.html
|
/// [`Container`]: struct.Container.html
|
||||||
@ -113,12 +123,15 @@ where
|
|||||||
|
|
||||||
let column_class = style_sheet.insert(bump, css::Rule::Column);
|
let column_class = style_sheet.insert(bump, css::Rule::Column);
|
||||||
|
|
||||||
|
let padding_class =
|
||||||
|
style_sheet.insert(bump, css::Rule::Padding(self.padding));
|
||||||
|
|
||||||
let style = self.style_sheet.style();
|
let style = self.style_sheet.style();
|
||||||
|
|
||||||
let node = div(bump)
|
let node = div(bump)
|
||||||
.attr(
|
.attr(
|
||||||
"class",
|
"class",
|
||||||
bumpalo::format!(in bump, "{}", column_class).into_bump_str(),
|
bumpalo::format!(in bump, "{} {}", column_class, padding_class).into_bump_str(),
|
||||||
)
|
)
|
||||||
.attr(
|
.attr(
|
||||||
"style",
|
"style",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user