Rename horizontal_rule to rule and introduce Rule::vertical

This commit is contained in:
Héctor Ramón Jiménez 2025-09-11 06:51:53 +02:00
parent c70ce5af89
commit c684fbd6af
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
7 changed files with 63 additions and 86 deletions

View file

@ -1752,7 +1752,7 @@ pub fn vertical_space() -> Space {
/// # mod iced { pub mod widget { pub use iced_widget::*; } }
/// # pub type State = ();
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
/// use iced::widget::horizontal_rule;
/// use iced::widget::rule;
///
/// #[derive(Clone)]
/// enum Message {
@ -1760,39 +1760,14 @@ pub fn vertical_space() -> Space {
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
/// horizontal_rule(2).into()
/// rule(2).into()
/// }
/// ```
pub fn horizontal_rule<'a, Theme>(height: impl Into<Pixels>) -> Rule<'a, Theme>
pub fn rule<'a, Theme>(height: impl Into<Pixels>) -> Rule<'a, Theme>
where
Theme: rule::Catalog + 'a,
{
Rule::horizontal(height)
}
/// Creates a vertical [`Rule`] with the given width.
///
/// # Example
/// ```no_run
/// # mod iced { pub mod widget { pub use iced_widget::*; } }
/// # pub type State = ();
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
/// use iced::widget::vertical_rule;
///
/// #[derive(Clone)]
/// enum Message {
/// // ...,
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
/// vertical_rule(2).into()
/// }
/// ```
pub fn vertical_rule<'a, Theme>(width: impl Into<Pixels>) -> Rule<'a, Theme>
where
Theme: rule::Catalog + 'a,
{
Rule::vertical(width)
Rule::new(height)
}
/// Creates a new [`ProgressBar`].

View file

@ -51,10 +51,7 @@ use crate::core::theme;
use crate::core::{
self, Color, Element, Length, Padding, Pixels, Theme, color,
};
use crate::{
column, container, horizontal_rule, rich_text, row, rule, scrollable, span,
text, vertical_rule,
};
use crate::{column, container, rich_text, row, scrollable, span, text};
use std::borrow::BorrowMut;
use std::cell::{Cell, RefCell};
@ -1391,7 +1388,7 @@ where
Renderer: core::text::Renderer<Font = Font> + 'a,
{
row![
vertical_rule(4),
crate::rule(4).vertical(),
column(
contents
.iter()
@ -1413,7 +1410,7 @@ where
Theme: Catalog + 'a,
Renderer: core::text::Renderer<Font = Font> + 'a,
{
horizontal_rule(2).into()
crate::rule(2).into()
}
/// Displays a table using the default look.
@ -1637,8 +1634,8 @@ where
pub trait Catalog:
container::Catalog
+ scrollable::Catalog
+ rule::Catalog
+ text::Catalog
+ crate::rule::Catalog
+ crate::table::Catalog
{
/// The styling class of a Markdown code block.

View file

@ -5,7 +5,7 @@
//! # mod iced { pub mod widget { pub use iced_widget::*; } }
//! # pub type State = ();
//! # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
//! use iced::widget::horizontal_rule;
//! use iced::widget::rule;
//!
//! #[derive(Clone)]
//! enum Message {
@ -13,7 +13,7 @@
//! }
//!
//! fn view(state: &State) -> Element<'_, Message> {
//! horizontal_rule(2).into()
//! rule(2).into()
//! }
//! ```
use crate::core;
@ -33,7 +33,7 @@ use crate::core::{
/// # mod iced { pub mod widget { pub use iced_widget::*; } }
/// # pub type State = ();
/// # pub type Element<'a, Message> = iced_widget::core::Element<'a, Message, iced_widget::Theme, iced_widget::Renderer>;
/// use iced::widget::horizontal_rule;
/// use iced::widget::rule;
///
/// #[derive(Clone)]
/// enum Message {
@ -41,7 +41,7 @@ use crate::core::{
/// }
///
/// fn view(state: &State) -> Element<'_, Message> {
/// horizontal_rule(2).into()
/// rule(2).into()
/// }
/// ```
#[allow(missing_debug_implementations)]
@ -49,9 +49,8 @@ pub struct Rule<'a, Theme = crate::Theme>
where
Theme: Catalog,
{
width: Length,
height: Length,
is_horizontal: bool,
thickness: Length,
is_vertical: bool,
class: Theme::Class<'a>,
}
@ -59,24 +58,19 @@ impl<'a, Theme> Rule<'a, Theme>
where
Theme: Catalog,
{
/// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: impl Into<Pixels>) -> Self {
/// Creates a horizontal [`Rule`] with the given thickness.
pub fn new(thickness: impl Into<Pixels>) -> Self {
Rule {
width: Length::Fill,
height: Length::Fixed(height.into().0),
is_horizontal: true,
thickness: Length::Fixed(thickness.into().0),
is_vertical: false,
class: Theme::default(),
}
}
/// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: impl Into<Pixels>) -> Self {
Rule {
width: Length::Fixed(width.into().0),
height: Length::Fill,
is_horizontal: false,
class: Theme::default(),
}
/// Turns the [`Rule`] into a vertical one.
pub fn vertical(mut self) -> Self {
self.is_vertical = true;
self
}
/// Sets the style of the [`Rule`].
@ -105,9 +99,16 @@ where
Theme: Catalog,
{
fn size(&self) -> Size<Length> {
Size {
width: self.width,
height: self.height,
if self.is_vertical {
Size {
width: self.thickness,
height: Length::Fill,
}
} else {
Size {
width: Length::Fill,
height: self.thickness,
}
}
}
@ -117,7 +118,9 @@ where
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout::atomic(limits, self.width, self.height)
let size = <Self as Widget<(), Theme, Renderer>>::size(self);
layout::atomic(limits, size.width, size.height)
}
fn draw(
@ -133,19 +136,7 @@ where
let bounds = layout.bounds();
let style = theme.style(&self.class);
let bounds = if self.is_horizontal {
let line_y = bounds.y.round();
let (offset, line_width) = style.fill_mode.fill(bounds.width);
let line_x = bounds.x + offset;
Rectangle {
x: line_x,
y: line_y,
width: line_width,
height: bounds.height,
}
} else {
let bounds = if self.is_vertical {
let line_x = bounds.x.round();
let (offset, line_height) = style.fill_mode.fill(bounds.height);
@ -157,6 +148,18 @@ where
width: bounds.width,
height: line_height,
}
} else {
let line_y = bounds.y.round();
let (offset, line_width) = style.fill_mode.fill(bounds.width);
let line_x = bounds.x + offset;
Rectangle {
x: line_x,
y: line_y,
width: line_width,
height: bounds.height,
}
};
renderer.fill_quad(