Use Pixels for spacing

This commit is contained in:
Héctor Ramón Jiménez 2023-02-17 16:23:29 +01:00
parent fd3a141024
commit a467a037c3
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 51 additions and 58 deletions

View file

@ -53,7 +53,7 @@ where
label: String, label: String,
width: Length, width: Length,
size: f32, size: f32,
spacing: u16, spacing: f32,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Renderer::Font,
icon: Icon<Renderer::Font>, icon: Icon<Renderer::Font>,
@ -69,7 +69,7 @@ where
const DEFAULT_SIZE: f32 = 20.0; const DEFAULT_SIZE: f32 = 20.0;
/// The default spacing of a [`Checkbox`]. /// The default spacing of a [`Checkbox`].
const DEFAULT_SPACING: u16 = 15; const DEFAULT_SPACING: f32 = 15.0;
/// Creates a new [`Checkbox`]. /// Creates a new [`Checkbox`].
/// ///
@ -114,8 +114,8 @@ where
} }
/// Sets the spacing between the [`Checkbox`] and the text. /// Sets the spacing between the [`Checkbox`] and the text.
pub fn spacing(mut self, spacing: u16) -> Self { pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
self.spacing = spacing; self.spacing = spacing.into().0;
self self
} }

View file

@ -13,7 +13,7 @@ use crate::{
/// A container that distributes its contents vertically. /// A container that distributes its contents vertically.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Column<'a, Message, Renderer> { pub struct Column<'a, Message, Renderer> {
spacing: u16, spacing: f32,
padding: Padding, padding: Padding,
width: Length, width: Length,
height: Length, height: Length,
@ -33,7 +33,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
children: Vec<Element<'a, Message, Renderer>>, children: Vec<Element<'a, Message, Renderer>>,
) -> Self { ) -> Self {
Column { Column {
spacing: 0, spacing: 0.0,
padding: Padding::ZERO, padding: Padding::ZERO,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
@ -48,8 +48,8 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> {
/// Custom margins per element do not exist in iced. You should use this /// Custom margins per element do not exist in iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self {
self.spacing = units; self.spacing = amount.into().0;
self self
} }
@ -135,7 +135,7 @@ where
renderer, renderer,
&limits, &limits,
self.padding, self.padding,
self.spacing as f32, self.spacing,
self.align_items, self.align_items,
&self.children, &self.children,
) )

View file

@ -42,8 +42,8 @@ use crate::widget;
use crate::widget::container; use crate::widget::container;
use crate::widget::tree::{self, Tree}; use crate::widget::tree::{self, Tree};
use crate::{ use crate::{
Clipboard, Color, Element, Layout, Length, Point, Rectangle, Shell, Size, Clipboard, Color, Element, Layout, Length, Pixels, Point, Rectangle, Shell,
Vector, Widget, Size, Vector, Widget,
}; };
/// A collection of panes distributed using either vertical or horizontal splits /// A collection of panes distributed using either vertical or horizontal splits
@ -104,10 +104,10 @@ where
contents: Contents<'a, Content<'a, Message, Renderer>>, contents: Contents<'a, Content<'a, Message, Renderer>>,
width: Length, width: Length,
height: Length, height: Length,
spacing: u16, spacing: f32,
on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -150,7 +150,7 @@ where
contents, contents,
width: Length::Fill, width: Length::Fill,
height: Length::Fill, height: Length::Fill,
spacing: 0, spacing: 0.0,
on_click: None, on_click: None,
on_drag: None, on_drag: None,
on_resize: None, on_resize: None,
@ -171,8 +171,8 @@ where
} }
/// Sets the spacing _between_ the panes of the [`PaneGrid`]. /// Sets the spacing _between_ the panes of the [`PaneGrid`].
pub fn spacing(mut self, units: u16) -> Self { pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self {
self.spacing = units; self.spacing = amount.into().0;
self self
} }
@ -205,11 +205,11 @@ where
/// The grabbable area of a split will have a length of `spacing + leeway`, /// The grabbable area of a split will have a length of `spacing + leeway`,
/// properly centered. In other words, a length of /// properly centered. In other words, a length of
/// `(spacing + leeway) / 2.0` on either side of the split line. /// `(spacing + leeway) / 2.0` on either side of the split line.
pub fn on_resize<F>(mut self, leeway: u16, f: F) -> Self pub fn on_resize<F>(mut self, leeway: impl Into<Pixels>, f: F) -> Self
where where
F: 'a + Fn(ResizeEvent) -> Message, F: 'a + Fn(ResizeEvent) -> Message,
{ {
self.on_resize = Some((leeway, Box::new(f))); self.on_resize = Some((leeway.into().0, Box::new(f)));
self self
} }
@ -485,14 +485,14 @@ pub fn layout<Renderer, T>(
node: &Node, node: &Node,
width: Length, width: Length,
height: Length, height: Length,
spacing: u16, spacing: f32,
contents: impl Iterator<Item = (Pane, T)>, contents: impl Iterator<Item = (Pane, T)>,
layout_content: impl Fn(T, &Renderer, &layout::Limits) -> layout::Node, layout_content: impl Fn(T, &Renderer, &layout::Limits) -> layout::Node,
) -> layout::Node { ) -> layout::Node {
let limits = limits.width(width).height(height); let limits = limits.width(width).height(height);
let size = limits.resolve(Size::ZERO); let size = limits.resolve(Size::ZERO);
let regions = node.pane_regions(f32::from(spacing), size); let regions = node.pane_regions(spacing, size);
let children = contents let children = contents
.filter_map(|(pane, content)| { .filter_map(|(pane, content)| {
let region = regions.get(&pane)?; let region = regions.get(&pane)?;
@ -522,11 +522,11 @@ pub fn update<'a, Message, T: Draggable>(
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: Point,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
spacing: u16, spacing: f32,
contents: impl Iterator<Item = (Pane, T)>, contents: impl Iterator<Item = (Pane, T)>,
on_click: &Option<Box<dyn Fn(Pane) -> Message + 'a>>, on_click: &Option<Box<dyn Fn(Pane) -> Message + 'a>>,
on_drag: &Option<Box<dyn Fn(DragEvent) -> Message + 'a>>, on_drag: &Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
on_resize: &Option<(u16, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>, on_resize: &Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
) -> event::Status { ) -> event::Status {
let mut event_status = event::Status::Ignored; let mut event_status = event::Status::Ignored;
@ -546,13 +546,13 @@ pub fn update<'a, Message, T: Draggable>(
); );
let splits = node.split_regions( let splits = node.split_regions(
f32::from(spacing), spacing,
Size::new(bounds.width, bounds.height), Size::new(bounds.width, bounds.height),
); );
let clicked_split = hovered_split( let clicked_split = hovered_split(
splits.iter(), splits.iter(),
f32::from(spacing + leeway), spacing + leeway,
relative_cursor, relative_cursor,
); );
@ -624,7 +624,7 @@ pub fn update<'a, Message, T: Draggable>(
let bounds = layout.bounds(); let bounds = layout.bounds();
let splits = node.split_regions( let splits = node.split_regions(
f32::from(spacing), spacing,
Size::new(bounds.width, bounds.height), Size::new(bounds.width, bounds.height),
); );
@ -698,8 +698,8 @@ pub fn mouse_interaction(
node: &Node, node: &Node,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: Point,
spacing: u16, spacing: f32,
resize_leeway: Option<u16>, resize_leeway: Option<f32>,
) -> Option<mouse::Interaction> { ) -> Option<mouse::Interaction> {
if action.picked_pane().is_some() { if action.picked_pane().is_some() {
return Some(mouse::Interaction::Grabbing); return Some(mouse::Interaction::Grabbing);
@ -710,20 +710,15 @@ pub fn mouse_interaction(
resize_leeway.and_then(|leeway| { resize_leeway.and_then(|leeway| {
let bounds = layout.bounds(); let bounds = layout.bounds();
let splits = let splits = node.split_regions(spacing, bounds.size());
node.split_regions(f32::from(spacing), bounds.size());
let relative_cursor = Point::new( let relative_cursor = Point::new(
cursor_position.x - bounds.x, cursor_position.x - bounds.x,
cursor_position.y - bounds.y, cursor_position.y - bounds.y,
); );
hovered_split( hovered_split(splits.iter(), spacing + leeway, relative_cursor)
splits.iter(), .map(|(_, axis, _)| axis)
f32::from(spacing + leeway),
relative_cursor,
)
.map(|(_, axis, _)| axis)
}) })
}); });
@ -747,8 +742,8 @@ pub fn draw<Renderer, T>(
theme: &Renderer::Theme, theme: &Renderer::Theme,
default_style: &renderer::Style, default_style: &renderer::Style,
viewport: &Rectangle, viewport: &Rectangle,
spacing: u16, spacing: f32,
resize_leeway: Option<u16>, resize_leeway: Option<f32>,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
contents: impl Iterator<Item = (Pane, T)>, contents: impl Iterator<Item = (Pane, T)>,
draw_pane: impl Fn( draw_pane: impl Fn(
@ -770,12 +765,11 @@ pub fn draw<Renderer, T>(
.and_then(|(split, axis)| { .and_then(|(split, axis)| {
let bounds = layout.bounds(); let bounds = layout.bounds();
let splits = node.split_regions(f32::from(spacing), bounds.size()); let splits = node.split_regions(spacing, bounds.size());
let (_axis, region, ratio) = splits.get(&split)?; let (_axis, region, ratio) = splits.get(&split)?;
let region = let region = axis.split_line_bounds(*region, *ratio, spacing);
axis.split_line_bounds(*region, *ratio, f32::from(spacing));
Some((axis, region + Vector::new(bounds.x, bounds.y), true)) Some((axis, region + Vector::new(bounds.x, bounds.y), true))
}) })
@ -788,12 +782,11 @@ pub fn draw<Renderer, T>(
cursor_position.y - bounds.y, cursor_position.y - bounds.y,
); );
let splits = let splits = node.split_regions(spacing, bounds.size());
node.split_regions(f32::from(spacing), bounds.size());
let (_split, axis, region) = hovered_split( let (_split, axis, region) = hovered_split(
splits.iter(), splits.iter(),
f32::from(spacing + leeway), spacing + leeway,
relative_cursor, relative_cursor,
)?; )?;

View file

@ -51,7 +51,7 @@ where
label: String, label: String,
width: Length, width: Length,
size: f32, size: f32,
spacing: u16, spacing: f32,
text_size: Option<f32>, text_size: Option<f32>,
font: Renderer::Font, font: Renderer::Font,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
@ -67,7 +67,7 @@ where
pub const DEFAULT_SIZE: f32 = 28.0; pub const DEFAULT_SIZE: f32 = 28.0;
/// The default spacing of a [`Radio`] button. /// The default spacing of a [`Radio`] button.
pub const DEFAULT_SPACING: u16 = 15; pub const DEFAULT_SPACING: f32 = 15.0;
/// Creates a new [`Radio`] button. /// Creates a new [`Radio`] button.
/// ///
@ -113,8 +113,8 @@ where
} }
/// Sets the spacing between the [`Radio`] button and the text. /// Sets the spacing between the [`Radio`] button and the text.
pub fn spacing(mut self, spacing: u16) -> Self { pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
self.spacing = spacing; self.spacing = spacing.into().0;
self self
} }

View file

@ -6,14 +6,14 @@ use crate::overlay;
use crate::renderer; use crate::renderer;
use crate::widget::{Operation, Tree}; use crate::widget::{Operation, Tree};
use crate::{ use crate::{
Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, Alignment, Clipboard, Element, Length, Padding, Pixels, Point, Rectangle,
Widget, Shell, Widget,
}; };
/// A container that distributes its contents horizontally. /// A container that distributes its contents horizontally.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Row<'a, Message, Renderer> { pub struct Row<'a, Message, Renderer> {
spacing: u16, spacing: f32,
padding: Padding, padding: Padding,
width: Length, width: Length,
height: Length, height: Length,
@ -32,7 +32,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
children: Vec<Element<'a, Message, Renderer>>, children: Vec<Element<'a, Message, Renderer>>,
) -> Self { ) -> Self {
Row { Row {
spacing: 0, spacing: 0.0,
padding: Padding::ZERO, padding: Padding::ZERO,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
@ -46,8 +46,8 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> {
/// Custom margins per element do not exist in iced. You should use this /// Custom margins per element do not exist in iced. You should use this
/// method instead! While less flexible, it helps you keep spacing between /// method instead! While less flexible, it helps you keep spacing between
/// elements consistent. /// elements consistent.
pub fn spacing(mut self, units: u16) -> Self { pub fn spacing(mut self, amount: impl Into<Pixels>) -> Self {
self.spacing = units; self.spacing = amount.into().0;
self self
} }
@ -124,7 +124,7 @@ where
renderer, renderer,
&limits, &limits,
self.padding, self.padding,
self.spacing as f32, self.spacing,
self.align_items, self.align_items,
&self.children, &self.children,
) )

View file

@ -41,7 +41,7 @@ where
size: f32, size: f32,
text_size: Option<f32>, text_size: Option<f32>,
text_alignment: alignment::Horizontal, text_alignment: alignment::Horizontal,
spacing: u16, spacing: f32,
font: Renderer::Font, font: Renderer::Font,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -78,7 +78,7 @@ where
size: Self::DEFAULT_SIZE, size: Self::DEFAULT_SIZE,
text_size: None, text_size: None,
text_alignment: alignment::Horizontal::Left, text_alignment: alignment::Horizontal::Left,
spacing: 0, spacing: 0.0,
font: Renderer::Font::default(), font: Renderer::Font::default(),
style: Default::default(), style: Default::default(),
} }
@ -109,8 +109,8 @@ where
} }
/// Sets the spacing between the [`Toggler`] and the text. /// Sets the spacing between the [`Toggler`] and the text.
pub fn spacing(mut self, spacing: u16) -> Self { pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
self.spacing = spacing; self.spacing = spacing.into().0;
self self
} }