Use f32 for Padding

This commit is contained in:
Héctor Ramón Jiménez 2023-02-17 16:09:49 +01:00
parent 0872d078e2
commit 3320ac1126
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
12 changed files with 98 additions and 79 deletions

View file

@ -33,29 +33,29 @@ use crate::Size;
/// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right /// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right
/// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left /// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left
/// ``` /// ```
#[derive(Debug, Hash, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Padding { pub struct Padding {
/// Top padding /// Top padding
pub top: u16, pub top: f32,
/// Right padding /// Right padding
pub right: u16, pub right: f32,
/// Bottom padding /// Bottom padding
pub bottom: u16, pub bottom: f32,
/// Left padding /// Left padding
pub left: u16, pub left: f32,
} }
impl Padding { impl Padding {
/// Padding of zero /// Padding of zero
pub const ZERO: Padding = Padding { pub const ZERO: Padding = Padding {
top: 0, top: 0.0,
right: 0, right: 0.0,
bottom: 0, bottom: 0.0,
left: 0, left: 0.0,
}; };
/// Create a Padding that is equal on all sides /// Create a Padding that is equal on all sides
pub const fn new(padding: u16) -> Padding { pub const fn new(padding: f32) -> Padding {
Padding { Padding {
top: padding, top: padding,
right: padding, right: padding,
@ -65,12 +65,12 @@ impl Padding {
} }
/// Returns the total amount of vertical [`Padding`]. /// Returns the total amount of vertical [`Padding`].
pub fn vertical(self) -> u16 { pub fn vertical(self) -> f32 {
self.top + self.bottom self.top + self.bottom
} }
/// Returns the total amount of horizontal [`Padding`]. /// Returns the total amount of horizontal [`Padding`].
pub fn horizontal(self) -> u16 { pub fn horizontal(self) -> f32 {
self.left + self.right self.left + self.right
} }
@ -79,16 +79,49 @@ impl Padding {
let available = (outer - inner).max(Size::ZERO); let available = (outer - inner).max(Size::ZERO);
Padding { Padding {
top: self.top.min((available.height as u16) / 2), top: self.top.min(available.height / 2.0),
right: self.right.min((available.width as u16) / 2), right: self.right.min(available.width / 2.0),
bottom: self.bottom.min((available.height as u16) / 2), bottom: self.bottom.min(available.height / 2.0),
left: self.left.min((available.width as u16) / 2), left: self.left.min(available.width / 2.0),
} }
} }
} }
impl From<u16> for Padding { impl From<u16> for Padding {
fn from(p: u16) -> Self { fn from(p: u16) -> Self {
Padding {
top: f32::from(p),
right: f32::from(p),
bottom: f32::from(p),
left: f32::from(p),
}
}
}
impl From<[u16; 2]> for Padding {
fn from(p: [u16; 2]) -> Self {
Padding {
top: f32::from(p[0]),
right: f32::from(p[1]),
bottom: f32::from(p[0]),
left: f32::from(p[1]),
}
}
}
impl From<[u16; 4]> for Padding {
fn from(p: [u16; 4]) -> Self {
Padding {
top: f32::from(p[0]),
right: f32::from(p[1]),
bottom: f32::from(p[2]),
left: f32::from(p[3]),
}
}
}
impl From<f32> for Padding {
fn from(p: f32) -> Self {
Padding { Padding {
top: p, top: p,
right: p, right: p,
@ -98,8 +131,8 @@ impl From<u16> for Padding {
} }
} }
impl From<[u16; 2]> for Padding { impl From<[f32; 2]> for Padding {
fn from(p: [u16; 2]) -> Self { fn from(p: [f32; 2]) -> Self {
Padding { Padding {
top: p[0], top: p[0],
right: p[1], right: p[1],
@ -109,8 +142,8 @@ impl From<[u16; 2]> for Padding {
} }
} }
impl From<[u16; 4]> for Padding { impl From<[f32; 4]> for Padding {
fn from(p: [u16; 4]) -> Self { fn from(p: [f32; 4]) -> Self {
Padding { Padding {
top: p[0], top: p[0],
right: p[1], right: p[1],

View file

@ -29,8 +29,8 @@ impl Size {
/// Increments the [`Size`] to account for the given padding. /// Increments the [`Size`] to account for the given padding.
pub fn pad(&self, padding: Padding) -> Self { pub fn pad(&self, padding: Padding) -> Self {
Size { Size {
width: self.width + padding.horizontal() as f32, width: self.width + padding.horizontal(),
height: self.height + padding.vertical() as f32, height: self.height + padding.vertical(),
} }
} }

View file

@ -141,8 +141,7 @@ where
font: Font, font: Font,
bounds: Size, bounds: Size,
) -> (f32, f32) { ) -> (f32, f32) {
self.backend() self.backend().measure(content, size, font, bounds)
.measure(content, f32::from(size), font, bounds)
} }
fn hit_test( fn hit_test(

View file

@ -191,7 +191,7 @@ where
} }
} }
let pad = axis.pack(padding.left as f32, padding.top as f32); let pad = axis.pack(padding.left, padding.top);
let mut main = pad.0; let mut main = pad.0;
for (i, node) in nodes.iter_mut().enumerate() { for (i, node) in nodes.iter_mut().enumerate() {

View file

@ -114,10 +114,7 @@ impl Limits {
/// Shrinks the current [`Limits`] to account for the given padding. /// Shrinks the current [`Limits`] to account for the given padding.
pub fn pad(&self, padding: Padding) -> Limits { pub fn pad(&self, padding: Padding) -> Limits {
self.shrink(Size::new( self.shrink(Size::new(padding.horizontal(), padding.vertical()))
padding.horizontal() as f32,
padding.vertical() as f32,
))
} }
/// Shrinks the current [`Limits`] by the given [`Size`]. /// Shrinks the current [`Limits`] by the given [`Size`].

View file

@ -344,9 +344,7 @@ where
let size = { let size = {
let intrinsic = Size::new( let intrinsic = Size::new(
0.0, 0.0,
text_size text_size + self.padding.vertical() * self.options.len() as f32,
+ f32::from(self.padding.vertical())
* self.options.len() as f32,
); );
limits.resolve(intrinsic) limits.resolve(intrinsic)
@ -387,7 +385,7 @@ where
*self.hovered_option = Some( *self.hovered_option = Some(
((cursor_position.y - bounds.y) ((cursor_position.y - bounds.y)
/ (text_size + f32::from(self.padding.vertical()))) / (text_size + self.padding.vertical()))
as usize, as usize,
); );
} }
@ -402,7 +400,7 @@ where
*self.hovered_option = Some( *self.hovered_option = Some(
((cursor_position.y - bounds.y) ((cursor_position.y - bounds.y)
/ (text_size + f32::from(self.padding.vertical()))) / (text_size + self.padding.vertical()))
as usize, as usize,
); );
@ -451,8 +449,7 @@ where
let text_size = let text_size =
self.text_size.unwrap_or_else(|| renderer.default_size()); self.text_size.unwrap_or_else(|| renderer.default_size());
let option_height = let option_height = (text_size + self.padding.vertical()) as usize;
(text_size + f32::from(self.padding.vertical())) as usize;
let offset = viewport.y - bounds.y; let offset = viewport.y - bounds.y;
let start = (offset / option_height as f32) as usize; let start = (offset / option_height as f32) as usize;
@ -469,7 +466,7 @@ where
x: bounds.x, x: bounds.x,
y: bounds.y + (option_height * i) as f32, y: bounds.y + (option_height * i) as f32,
width: bounds.width, width: bounds.width,
height: text_size + f32::from(self.padding.vertical()), height: text_size + self.padding.vertical(),
}; };
if is_selected { if is_selected {
@ -487,12 +484,12 @@ where
renderer.fill_text(Text { renderer.fill_text(Text {
content: &option.to_string(), content: &option.to_string(),
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + self.padding.left as f32, x: bounds.x + self.padding.left,
y: bounds.center_y(), y: bounds.center_y(),
width: f32::INFINITY, width: f32::INFINITY,
..bounds ..bounds
}, },
size: f32::from(text_size), size: text_size,
font: self.font.clone(), font: self.font.clone(),
color: if is_selected { color: if is_selected {
appearance.selected_text_color appearance.selected_text_color

View file

@ -76,7 +76,7 @@ where
on_press: None, on_press: None,
width: Length::Shrink, width: Length::Shrink,
height: Length::Shrink, height: Length::Shrink,
padding: Padding::new(5), padding: Padding::new(5.0),
style: <Renderer::Theme as StyleSheet>::Style::default(), style: <Renderer::Theme as StyleSheet>::Style::default(),
} }
} }
@ -434,7 +434,7 @@ pub fn layout<Renderer>(
let padding = padding.fit(content.size(), limits.max()); let padding = padding.fit(content.size(), limits.max());
let size = limits.pad(padding).resolve(content.size()).pad(padding); let size = limits.pad(padding).resolve(content.size()).pad(padding);
content.move_to(Point::new(padding.left.into(), padding.top.into())); content.move_to(Point::new(padding.left, padding.top));
layout::Node::with_children(size, vec![content]) layout::Node::with_children(size, vec![content])
} }

View file

@ -310,7 +310,7 @@ pub fn layout<Renderer>(
let padding = padding.fit(content.size(), limits.max()); let padding = padding.fit(content.size(), limits.max());
let size = limits.pad(padding).resolve(content.size()); let size = limits.pad(padding).resolve(content.size());
content.move_to(Point::new(padding.left.into(), padding.top.into())); content.move_to(Point::new(padding.left, padding.top));
content.align( content.align(
Alignment::from(horizontal_alignment), Alignment::from(horizontal_alignment),
Alignment::from(vertical_alignment), Alignment::from(vertical_alignment),

View file

@ -249,10 +249,7 @@ where
) )
}; };
node.move_to(Point::new( node.move_to(Point::new(self.padding.left, self.padding.top));
self.padding.left.into(),
self.padding.top.into(),
));
layout::Node::with_children(node.size().pad(self.padding), vec![node]) layout::Node::with_children(node.size().pad(self.padding), vec![node])
} }

View file

@ -53,7 +53,7 @@ where
From<<Renderer::Theme as StyleSheet>::Style>, From<<Renderer::Theme as StyleSheet>::Style>,
{ {
/// The default padding of a [`PickList`]. /// The default padding of a [`PickList`].
pub const DEFAULT_PADDING: Padding = Padding::new(5); pub const DEFAULT_PADDING: Padding = Padding::new(5.0);
/// Creates a new [`PickList`] with the given list of options, the current /// Creates a new [`PickList`] with the given list of options, the current
/// selected value, and the message to produce when an option is selected. /// selected value, and the message to produce when an option is selected.
@ -358,7 +358,7 @@ where
let max_width = match width { let max_width = match width {
Length::Shrink => { Length::Shrink => {
let measure = |label: &str| -> u32 { let measure = |label: &str| -> f32 {
let (width, _) = renderer.measure( let (width, _) = renderer.measure(
label, label,
text_size, text_size,
@ -366,26 +366,25 @@ where
Size::new(f32::INFINITY, f32::INFINITY), Size::new(f32::INFINITY, f32::INFINITY),
); );
width.round() as u32 width.round()
}; };
let labels = options.iter().map(ToString::to_string); let labels = options.iter().map(ToString::to_string);
let labels_width = let labels_width = labels
labels.map(|label| measure(&label)).max().unwrap_or(100); .map(|label| measure(&label))
.fold(100.0, |candidate, current| current.max(candidate));
let placeholder_width = placeholder.map(measure).unwrap_or(100); let placeholder_width = placeholder.map(measure).unwrap_or(100.0);
labels_width.max(placeholder_width) labels_width.max(placeholder_width)
} }
_ => 0, _ => 0.0,
}; };
let size = { let size = {
let intrinsic = Size::new( let intrinsic =
max_width as f32 + f32::from(text_size) + f32::from(padding.left), Size::new(max_width + text_size + padding.left, text_size);
f32::from(text_size),
);
limits.resolve(intrinsic).pad(padding) limits.resolve(intrinsic).pad(padding)
}; };
@ -612,7 +611,7 @@ pub fn draw<'a, T, Renderer>(
}; };
if let Some((font, code_point, size)) = handle { if let Some((font, code_point, size)) = handle {
let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); let size = size.unwrap_or_else(|| renderer.default_size());
renderer.fill_text(Text { renderer.fill_text(Text {
content: &code_point.to_string(), content: &code_point.to_string(),
@ -620,7 +619,7 @@ pub fn draw<'a, T, Renderer>(
font, font,
color: style.handle_color, color: style.handle_color,
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + bounds.width - f32::from(padding.horizontal()), x: bounds.x + bounds.width - padding.horizontal(),
y: bounds.center_y() - size / 2.0, y: bounds.center_y() - size / 2.0,
height: size, height: size,
..bounds ..bounds
@ -633,8 +632,7 @@ pub fn draw<'a, T, Renderer>(
let label = selected.map(ToString::to_string); let label = selected.map(ToString::to_string);
if let Some(label) = label.as_deref().or(placeholder) { if let Some(label) = label.as_deref().or(placeholder) {
let text_size = let text_size = text_size.unwrap_or_else(|| renderer.default_size());
f32::from(text_size.unwrap_or_else(|| renderer.default_size()));
renderer.fill_text(Text { renderer.fill_text(Text {
content: label, content: label,
@ -646,9 +644,9 @@ pub fn draw<'a, T, Renderer>(
style.placeholder_color style.placeholder_color
}, },
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + f32::from(padding.left), x: bounds.x + padding.left,
y: bounds.center_y() - text_size / 2.0, y: bounds.center_y() - text_size / 2.0,
width: bounds.width - f32::from(padding.horizontal()), width: bounds.width - padding.horizontal(),
height: text_size, height: text_size,
}, },
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,

View file

@ -94,7 +94,7 @@ where
is_secure: false, is_secure: false,
font: Default::default(), font: Default::default(),
width: Length::Fill, width: Length::Fill,
padding: Padding::new(5), padding: Padding::new(5.0),
size: None, size: None,
on_change: Box::new(on_change), on_change: Box::new(on_change),
on_paste: None, on_paste: None,
@ -390,7 +390,7 @@ where
let limits = limits.width(width).pad(padding).height(text_size); let limits = limits.width(width).pad(padding).height(text_size);
let mut text = layout::Node::new(limits.resolve(Size::ZERO)); let mut text = layout::Node::new(limits.resolve(Size::ZERO));
text.move_to(Point::new(padding.left.into(), padding.top.into())); text.move_to(Point::new(padding.left, padding.top));
layout::Node::with_children(text.size().pad(padding), vec![text]) layout::Node::with_children(text.size().pad(padding), vec![text])
} }
@ -965,7 +965,7 @@ pub fn draw<Renderer>(
width: f32::INFINITY, width: f32::INFINITY,
..text_bounds ..text_bounds
}, },
size: f32::from(size), size,
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center, vertical_alignment: alignment::Vertical::Center,
}); });
@ -1197,7 +1197,7 @@ where
renderer renderer
.hit_test( .hit_test(
&value.to_string(), &value.to_string(),
size.into(), size,
font, font,
Size::INFINITY, Size::INFINITY,
Point::new(x + offset, text_bounds.height / 2.0), Point::new(x + offset, text_bounds.height / 2.0),

View file

@ -25,8 +25,8 @@ where
content: Element<'a, Message, Renderer>, content: Element<'a, Message, Renderer>,
tooltip: Text<'a, Renderer>, tooltip: Text<'a, Renderer>,
position: Position, position: Position,
gap: u16, gap: f32,
padding: u16, padding: f32,
snap_within_viewport: bool, snap_within_viewport: bool,
style: <Renderer::Theme as container::StyleSheet>::Style, style: <Renderer::Theme as container::StyleSheet>::Style,
} }
@ -37,7 +37,7 @@ where
Renderer::Theme: container::StyleSheet + widget::text::StyleSheet, Renderer::Theme: container::StyleSheet + widget::text::StyleSheet,
{ {
/// The default padding of a [`Tooltip`] drawn by this renderer. /// The default padding of a [`Tooltip`] drawn by this renderer.
const DEFAULT_PADDING: u16 = 5; const DEFAULT_PADDING: f32 = 5.0;
/// Creates a new [`Tooltip`]. /// Creates a new [`Tooltip`].
/// ///
@ -51,7 +51,7 @@ where
content: content.into(), content: content.into(),
tooltip: Text::new(tooltip), tooltip: Text::new(tooltip),
position, position,
gap: 0, gap: 0.0,
padding: Self::DEFAULT_PADDING, padding: Self::DEFAULT_PADDING,
snap_within_viewport: true, snap_within_viewport: true,
style: Default::default(), style: Default::default(),
@ -73,14 +73,14 @@ where
} }
/// Sets the gap between the content and its [`Tooltip`]. /// Sets the gap between the content and its [`Tooltip`].
pub fn gap(mut self, gap: u16) -> Self { pub fn gap(mut self, gap: impl Into<Pixels>) -> Self {
self.gap = gap; self.gap = gap.into().0;
self self
} }
/// Sets the padding of the [`Tooltip`]. /// Sets the padding of the [`Tooltip`].
pub fn padding(mut self, padding: u16) -> Self { pub fn padding(mut self, padding: impl Into<Pixels>) -> Self {
self.padding = padding; self.padding = padding.into().0;
self self
} }
@ -272,8 +272,8 @@ pub fn draw<Renderer>(
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle, viewport: &Rectangle,
position: Position, position: Position,
gap: u16, gap: f32,
padding: u16, padding: f32,
snap_within_viewport: bool, snap_within_viewport: bool,
style: &<Renderer::Theme as container::StyleSheet>::Style, style: &<Renderer::Theme as container::StyleSheet>::Style,
layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node,
@ -293,7 +293,6 @@ pub fn draw<Renderer>(
let bounds = layout.bounds(); let bounds = layout.bounds();
if bounds.contains(cursor_position) { if bounds.contains(cursor_position) {
let gap = f32::from(gap);
let style = theme.appearance(style); let style = theme.appearance(style);
let defaults = renderer::Style { let defaults = renderer::Style {
@ -311,7 +310,6 @@ pub fn draw<Renderer>(
.pad(Padding::new(padding)), .pad(Padding::new(padding)),
); );
let padding = f32::from(padding);
let text_bounds = text_layout.bounds(); let text_bounds = text_layout.bounds();
let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0; let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0;
let y_center = bounds.y + (bounds.height - text_bounds.height) / 2.0; let y_center = bounds.y + (bounds.height - text_bounds.height) / 2.0;