From 5b029ae61cc56d8950aa25e40d5d1f76c9f3aaa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 18 Nov 2025 21:33:13 +0100 Subject: [PATCH] Improve consistency of `Padding` API --- core/src/padding.rs | 52 +++++++++++++++++++++++++++----------- core/src/rectangle.rs | 8 +++--- widget/src/overlay/menu.rs | 8 +++--- widget/src/pick_list.rs | 2 +- widget/src/text/rich.rs | 5 +--- widget/src/text_editor.rs | 2 +- 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/core/src/padding.rs b/core/src/padding.rs index b4b62f37..91f7a3b7 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -69,20 +69,14 @@ pub fn right(padding: impl Into) -> Padding { Padding::default().right(padding) } -/// Create a [`Padding`] with equal left and right sides. +/// Create some [`Padding`] with equal left and right sides. pub fn horizontal(padding: impl Into) -> Padding { - let padding: Pixels = padding.into(); - Padding::default() - .left(padding.clone()) - .right(padding) + Padding::default().horizontal(padding) } -/// Create a [`Padding`] with equal top and bottom sides. +/// Create some [`Padding`] with equal top and bottom sides. pub fn vertical(padding: impl Into) -> Padding { - let padding: Pixels = padding.into(); - Padding::default() - .top(padding.clone()) - .bottom(padding) + Padding::default().vertical(padding) } impl Padding { @@ -144,16 +138,44 @@ impl Padding { } } - /// Returns the total amount of vertical [`Padding`]. - pub fn vertical(self) -> f32 { - self.top + self.bottom + /// Sets the [`left`] and [`right`] of the [`Padding`]. + /// + /// [`left`]: Self::left + /// [`right`]: Self::right + pub fn horizontal(self, horizontal: impl Into) -> 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) -> Self { + let vertical = vertical.into(); + + Self { + top: vertical.0, + bottom: vertical.0, + ..self + } } /// Returns the total amount of horizontal [`Padding`]. - pub fn horizontal(self) -> f32 { + pub fn x(self) -> f32 { 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`]. pub fn fit(self, inner: Size, outer: Size) -> Self { let available = (outer - inner).max(Size::ZERO); @@ -215,7 +237,7 @@ impl From<[f32; 2]> for Padding { impl From for Size { fn from(padding: Padding) -> Self { - Self::new(padding.horizontal(), padding.vertical()) + Self::new(padding.x(), padding.y()) } } diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 9f2808dd..0a08bbe4 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -272,8 +272,8 @@ impl Rectangle { Self { x: self.x - padding.left, y: self.y - padding.top, - width: self.width + padding.horizontal(), - height: self.height + padding.vertical(), + width: self.width + padding.x(), + height: self.height + padding.y(), } } @@ -284,8 +284,8 @@ impl Rectangle { Self { x: self.x + padding.left, y: self.y + padding.top, - width: self.width - padding.horizontal(), - height: self.height - padding.vertical(), + width: self.width - padding.x(), + height: self.height - padding.y(), } } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index fb2fe9e9..7092f9ba 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -383,7 +383,7 @@ where let size = { let intrinsic = Size::new( 0.0, - (f32::from(text_line_height) + self.padding.vertical()) + (f32::from(text_line_height) + self.padding.y()) * self.options.len() as f32, ); @@ -424,7 +424,7 @@ where let option_height = f32::from(self.text_line_height.to_absolute(text_size)) - + self.padding.vertical(); + + self.padding.y(); let new_hovered_option = (cursor_position.y / option_height) as usize; @@ -454,7 +454,7 @@ where let option_height = f32::from(self.text_line_height.to_absolute(text_size)) - + self.padding.vertical(); + + self.padding.y(); *self.hovered_option = Some((cursor_position.y / option_height) as usize); @@ -515,7 +515,7 @@ where self.text_size.unwrap_or_else(|| renderer.default_size()); let option_height = f32::from(self.text_line_height.to_absolute(text_size)) - + self.padding.vertical(); + + self.padding.y(); let offset = viewport.y - bounds.y; let start = (offset / option_height) as usize; diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 06e938ec..5a129f9b 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -665,7 +665,7 @@ where line_height: self.text_line_height, font, bounds: Size::new( - bounds.width - self.padding.horizontal(), + bounds.width - self.padding.x(), f32::from(self.text_line_height.to_absolute(text_size)), ), align_x: text::Alignment::Default, diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 648ffe82..adf87e2e 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -287,10 +287,7 @@ where span.padding.top, ), bounds.size() - + Size::new( - span.padding.horizontal(), - span.padding.vertical(), - ), + + Size::new(span.padding.x(), span.padding.y()), ); renderer.fill_quad( diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 6141ee59..cf185676 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -654,7 +654,7 @@ where limits .height(min_bounds.height) .max() - .expand(Size::new(0.0, self.padding.vertical())), + .expand(Size::new(0.0, self.padding.y())), ) } }