Rename horizontal_rule to rule and introduce Rule::vertical
This commit is contained in:
parent
c70ce5af89
commit
c684fbd6af
7 changed files with 63 additions and 86 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue