Merge pull request #2655 from hydra/add-horizontal-and-vertical-padding-to-api

Add horizontal and vertical padding methods.
This commit is contained in:
Héctor 2025-11-18 23:04:06 +01:00 • committed by GitHub
commit 08d1979975
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 54 additions and 19 deletions

View file

@ -69,6 +69,16 @@ pub fn right(padding: impl Into<Pixels>) -> Padding {
Padding::default().right(padding) Padding::default().right(padding)
} }
/// Create some [`Padding`] with equal left and right sides.
pub fn horizontal(padding: impl Into<Pixels>) -> Padding {
Padding::default().horizontal(padding)
}
/// Create some [`Padding`] with equal top and bottom sides.
pub fn vertical(padding: impl Into<Pixels>) -> Padding {
Padding::default().vertical(padding)
}
impl Padding { impl Padding {
/// Padding of zero /// Padding of zero
pub const ZERO: Padding = Padding { pub const ZERO: Padding = Padding {
@ -128,16 +138,44 @@ impl Padding {
} }
} }
/// Returns the total amount of vertical [`Padding`]. /// Sets the [`left`] and [`right`] of the [`Padding`].
pub fn vertical(self) -> f32 { ///
self.top + self.bottom /// [`left`]: Self::left
/// [`right`]: Self::right
pub fn horizontal(self, horizontal: impl Into<Pixels>) -> Self {
let horizontal = horizontal.into();
Self {
left: horizontal.0,
right: horizontal.0,
..self
}
}
/// Sets the [`top`] and [`bottom`] of the [`Padding`].
///
/// [`top`]: Self::top
/// [`bottom`]: Self::bottom
pub fn vertical(self, vertical: impl Into<Pixels>) -> Self {
let vertical = vertical.into();
Self {
top: vertical.0,
bottom: vertical.0,
..self
}
} }
/// Returns the total amount of horizontal [`Padding`]. /// Returns the total amount of horizontal [`Padding`].
pub fn horizontal(self) -> f32 { pub fn x(self) -> f32 {
self.left + self.right self.left + self.right
} }
/// Returns the total amount of vertical [`Padding`].
pub fn y(self) -> f32 {
self.top + self.bottom
}
/// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`]. /// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`].
pub fn fit(self, inner: Size, outer: Size) -> Self { pub fn fit(self, inner: Size, outer: Size) -> Self {
let available = (outer - inner).max(Size::ZERO); let available = (outer - inner).max(Size::ZERO);
@ -199,7 +237,7 @@ impl From<[f32; 2]> for Padding {
impl From<Padding> for Size { impl From<Padding> for Size {
fn from(padding: Padding) -> Self { fn from(padding: Padding) -> Self {
Self::new(padding.horizontal(), padding.vertical()) Self::new(padding.x(), padding.y())
} }
} }

View file

@ -272,8 +272,8 @@ impl Rectangle<f32> {
Self { Self {
x: self.x - padding.left, x: self.x - padding.left,
y: self.y - padding.top, y: self.y - padding.top,
width: self.width + padding.horizontal(), width: self.width + padding.x(),
height: self.height + padding.vertical(), height: self.height + padding.y(),
} }
} }
@ -284,8 +284,8 @@ impl Rectangle<f32> {
Self { Self {
x: self.x + padding.left, x: self.x + padding.left,
y: self.y + padding.top, y: self.y + padding.top,
width: self.width - padding.horizontal(), width: self.width - padding.x(),
height: self.height - padding.vertical(), height: self.height - padding.y(),
} }
} }

View file

@ -383,7 +383,7 @@ where
let size = { let size = {
let intrinsic = Size::new( let intrinsic = Size::new(
0.0, 0.0,
(f32::from(text_line_height) + self.padding.vertical()) (f32::from(text_line_height) + self.padding.y())
* self.options.len() as f32, * self.options.len() as f32,
); );
@ -424,7 +424,7 @@ where
let option_height = let option_height =
f32::from(self.text_line_height.to_absolute(text_size)) f32::from(self.text_line_height.to_absolute(text_size))
+ self.padding.vertical(); + self.padding.y();
let new_hovered_option = let new_hovered_option =
(cursor_position.y / option_height) as usize; (cursor_position.y / option_height) as usize;
@ -454,7 +454,7 @@ where
let option_height = let option_height =
f32::from(self.text_line_height.to_absolute(text_size)) f32::from(self.text_line_height.to_absolute(text_size))
+ self.padding.vertical(); + self.padding.y();
*self.hovered_option = *self.hovered_option =
Some((cursor_position.y / option_height) as usize); Some((cursor_position.y / option_height) as usize);
@ -515,7 +515,7 @@ where
self.text_size.unwrap_or_else(|| renderer.default_size()); self.text_size.unwrap_or_else(|| renderer.default_size());
let option_height = let option_height =
f32::from(self.text_line_height.to_absolute(text_size)) f32::from(self.text_line_height.to_absolute(text_size))
+ self.padding.vertical(); + self.padding.y();
let offset = viewport.y - bounds.y; let offset = viewport.y - bounds.y;
let start = (offset / option_height) as usize; let start = (offset / option_height) as usize;

View file

@ -665,7 +665,7 @@ where
line_height: self.text_line_height, line_height: self.text_line_height,
font, font,
bounds: Size::new( bounds: Size::new(
bounds.width - self.padding.horizontal(), bounds.width - self.padding.x(),
f32::from(self.text_line_height.to_absolute(text_size)), f32::from(self.text_line_height.to_absolute(text_size)),
), ),
align_x: text::Alignment::Default, align_x: text::Alignment::Default,

View file

@ -287,10 +287,7 @@ where
span.padding.top, span.padding.top,
), ),
bounds.size() bounds.size()
+ Size::new( + Size::new(span.padding.x(), span.padding.y()),
span.padding.horizontal(),
span.padding.vertical(),
),
); );
renderer.fill_quad( renderer.fill_quad(

View file

@ -654,7 +654,7 @@ where
limits limits
.height(min_bounds.height) .height(min_bounds.height)
.max() .max()
.expand(Size::new(0.0, self.padding.vertical())), .expand(Size::new(0.0, self.padding.y())),
) )
} }
} }