From 3a0d34c0240f4421737a6a08761f99d6f8140d02 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Mar 2023 05:37:11 +0100 Subject: Create `iced_widget` subcrate and re-organize the whole codebase --- widget/src/rule.rs | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 widget/src/rule.rs (limited to 'widget/src/rule.rs') diff --git a/widget/src/rule.rs b/widget/src/rule.rs new file mode 100644 index 00000000..3749d7ce --- /dev/null +++ b/widget/src/rule.rs @@ -0,0 +1,147 @@ +//! Display a horizontal or vertical rule for dividing content. +use crate::core::layout; +use crate::core::renderer; +use crate::core::widget::Tree; +use crate::core::{ + Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, +}; + +pub use crate::style::rule::{Appearance, FillMode, StyleSheet}; + +/// Display a horizontal or vertical rule for dividing content. +#[allow(missing_debug_implementations)] +pub struct Rule +where + Renderer: crate::core::Renderer, + Renderer::Theme: StyleSheet, +{ + width: Length, + height: Length, + is_horizontal: bool, + style: ::Style, +} + +impl Rule +where + Renderer: crate::core::Renderer, + Renderer::Theme: StyleSheet, +{ + /// Creates a horizontal [`Rule`] with the given height. + pub fn horizontal(height: impl Into) -> Self { + Rule { + width: Length::Fill, + height: Length::Fixed(height.into().0), + is_horizontal: true, + style: Default::default(), + } + } + + /// Creates a vertical [`Rule`] with the given width. + pub fn vertical(width: impl Into) -> Self { + Rule { + width: Length::Fixed(width.into().0), + height: Length::Fill, + is_horizontal: false, + style: Default::default(), + } + } + + /// Sets the style of the [`Rule`]. + pub fn style( + mut self, + style: impl Into<::Style>, + ) -> Self { + self.style = style.into(); + self + } +} + +impl Widget for Rule +where + Renderer: crate::core::Renderer, + Renderer::Theme: StyleSheet, +{ + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + _renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + + layout::Node::new(limits.resolve(Size::ZERO)) + } + + fn draw( + &self, + _state: &Tree, + renderer: &mut Renderer, + theme: &Renderer::Theme, + _style: &renderer::Style, + layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + ) { + let bounds = layout.bounds(); + let style = theme.appearance(&self.style); + + let bounds = if self.is_horizontal { + let line_y = (bounds.y + (bounds.height / 2.0) + - (style.width as f32 / 2.0)) + .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: style.width as f32, + } + } else { + let line_x = (bounds.x + (bounds.width / 2.0) + - (style.width as f32 / 2.0)) + .round(); + + let (offset, line_height) = style.fill_mode.fill(bounds.height); + let line_y = bounds.y + offset; + + Rectangle { + x: line_x, + y: line_y, + width: style.width as f32, + height: line_height, + } + }; + + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: style.radius.into(), + border_width: 0.0, + border_color: Color::TRANSPARENT, + }, + style.color, + ); + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Message: 'a, + Renderer: 'a + crate::core::Renderer, + Renderer::Theme: StyleSheet, +{ + fn from(rule: Rule) -> Element<'a, Message, Renderer> { + Element::new(rule) + } +} -- cgit From 1234d528121265698f9f426ca89fc687dc95dc01 Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Tue, 23 May 2023 15:28:45 +0200 Subject: clippy --- widget/src/rule.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'widget/src/rule.rs') diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 3749d7ce..272bd2b3 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -125,7 +125,7 @@ where renderer.fill_quad( renderer::Quad { bounds, - border_radius: style.radius.into(), + border_radius: style.radius, border_width: 0.0, border_color: Color::TRANSPARENT, }, -- cgit From 34451bff185d8875f55747ee97ed746828e30f40 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 8 Jun 2023 20:11:59 +0200 Subject: Implement basic cursor availability --- widget/src/rule.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'widget/src/rule.rs') diff --git a/widget/src/rule.rs b/widget/src/rule.rs index 272bd2b3..d703e6ae 100644 --- a/widget/src/rule.rs +++ b/widget/src/rule.rs @@ -1,9 +1,10 @@ //! Display a horizontal or vertical rule for dividing content. use crate::core::layout; +use crate::core::mouse; use crate::core::renderer; use crate::core::widget::Tree; use crate::core::{ - Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, + Color, Element, Layout, Length, Pixels, Rectangle, Size, Widget, }; pub use crate::style::rule::{Appearance, FillMode, StyleSheet}; @@ -86,7 +87,7 @@ where theme: &Renderer::Theme, _style: &renderer::Style, layout: Layout<'_>, - _cursor_position: Point, + _cursor: mouse::Cursor, _viewport: &Rectangle, ) { let bounds = layout.bounds(); -- cgit