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

@ -3,8 +3,7 @@ use iced::keyboard;
use iced::mouse;
use iced::widget::{
button, canvas, center, center_y, checkbox, column, container,
horizontal_rule, horizontal_space, pick_list, pin, row, scrollable, stack,
text, vertical_rule,
horizontal_space, pick_list, pin, row, rule, scrollable, stack, text,
};
use iced::{
Center, Element, Fill, Font, Length, Point, Rectangle, Renderer, Shrink,
@ -295,7 +294,7 @@ fn quotes<'a>() -> Element<'a, Message> {
fn quote<'a>(
content: impl Into<Element<'a, Message>>,
) -> Element<'a, Message> {
row![vertical_rule(1), content.into()]
row![rule(1).vertical(), content.into()]
.spacing(10)
.height(Shrink)
.into()
@ -313,7 +312,7 @@ fn quotes<'a>() -> Element<'a, Message> {
reply("This is the original message", "This is a reply"),
"This is another reply",
),
horizontal_rule(1),
rule(1),
text("A separator ↑"),
]
.width(Shrink)

View file

@ -1,8 +1,8 @@
use iced::keyboard;
use iced::widget::{
button, center_x, center_y, checkbox, column, container, horizontal_rule,
pick_list, progress_bar, row, scrollable, slider, text, text_input,
toggler, vertical_rule, vertical_space,
button, center_x, center_y, checkbox, column, container, pick_list,
progress_bar, row, rule, scrollable, slider, text, text_input, toggler,
vertical_space,
};
use iced::{Center, Element, Fill, Shrink, Subscription, Theme};
@ -162,14 +162,14 @@ impl Styling {
let content = column![
choose_theme,
horizontal_rule(1),
rule(1),
text_input,
buttons,
slider(),
progress_bar(),
row![
scroll_me,
vertical_rule(1),
rule(1).vertical(),
column![check, check_disabled, toggle, disabled_toggle]
.spacing(10)
]

View file

@ -172,7 +172,7 @@ mod toast {
use iced::theme;
use iced::time::{self, Duration, Instant};
use iced::widget::{
button, column, container, horizontal_rule, horizontal_space, row, text,
button, column, container, horizontal_space, row, rule, text,
};
use iced::window;
use iced::{
@ -254,7 +254,7 @@ mod toast {
Status::Success => success,
Status::Danger => danger,
}),
horizontal_rule(1),
rule(1),
container(text(toast.body.as_str()))
.width(Fill)
.padding(5)

View file

@ -28,7 +28,7 @@ use crate::test::instruction;
use crate::test::{Emulator, Ice, Instruction};
use crate::widget::{
button, center, column, combo_box, container, horizontal_space, pick_list,
row, scrollable, text, text_editor, text_input, themer,
row, rule, scrollable, text, text_editor, text_input, themer,
};
/// Attaches a [`Tester`] to the given [`Program`].
@ -632,10 +632,13 @@ impl<P: Program + 'static> Tester<P> {
row![
center(column![status, viewport].spacing(10).align_x(Right))
.padding(10),
rule(1).vertical().style(rule::weak),
container(self.controls().map(Tick::Tester))
.width(250)
.padding(10)
.style(container::dark)
.style(|theme| container::Style::default().background(
theme.extended_palette().background.weakest.color
)),
]
.into()
}

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(