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

View file

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

View file

@ -141,8 +141,7 @@ where
font: Font,
bounds: Size,
) -> (f32, f32) {
self.backend()
.measure(content, f32::from(size), font, bounds)
self.backend().measure(content, size, font, bounds)
}
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;
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.
pub fn pad(&self, padding: Padding) -> Limits {
self.shrink(Size::new(
padding.horizontal() as f32,
padding.vertical() as f32,
))
self.shrink(Size::new(padding.horizontal(), padding.vertical()))
}
/// Shrinks the current [`Limits`] by the given [`Size`].

View file

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

View file

@ -76,7 +76,7 @@ where
on_press: None,
width: Length::Shrink,
height: Length::Shrink,
padding: Padding::new(5),
padding: Padding::new(5.0),
style: <Renderer::Theme as StyleSheet>::Style::default(),
}
}
@ -434,7 +434,7 @@ pub fn layout<Renderer>(
let padding = padding.fit(content.size(), limits.max());
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])
}

View file

@ -310,7 +310,7 @@ pub fn layout<Renderer>(
let padding = padding.fit(content.size(), limits.max());
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(
Alignment::from(horizontal_alignment),
Alignment::from(vertical_alignment),

View file

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

View file

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

View file

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

View file

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