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

View file

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

View file

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

View file

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

View file

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

View file

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