From 8d68c8584ea99d3974571cd92edcb31999ebb8fa Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Thu, 13 Aug 2020 12:54:34 -0500 Subject: widget Rule added --- graphics/src/widget.rs | 3 ++ graphics/src/widget/rule.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 graphics/src/widget/rule.rs (limited to 'graphics/src') diff --git a/graphics/src/widget.rs b/graphics/src/widget.rs index 94a65011..f87b558a 100644 --- a/graphics/src/widget.rs +++ b/graphics/src/widget.rs @@ -15,6 +15,7 @@ pub mod pane_grid; pub mod pick_list; pub mod progress_bar; pub mod radio; +pub mod rule; pub mod scrollable; pub mod slider; pub mod svg; @@ -40,6 +41,8 @@ pub use progress_bar::ProgressBar; #[doc(no_inline)] pub use radio::Radio; #[doc(no_inline)] +pub use rule::Rule; +#[doc(no_inline)] pub use scrollable::Scrollable; #[doc(no_inline)] pub use slider::Slider; diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs new file mode 100644 index 00000000..5ff5197d --- /dev/null +++ b/graphics/src/widget/rule.rs @@ -0,0 +1,89 @@ +//! Display a horizontal or vertical rule for dividing content. + +use crate::{Backend, Primitive, Renderer}; +use iced_native::mouse; +use iced_native::rule; +use iced_native::{Background, Color, Rectangle}; + +pub use iced_style::rule::{Style, StyleSheet}; + +/// Display a horizontal or vertical rule for dividing content. +/// +/// This is an alias of an `iced_native` rule with an `iced_graphics::Renderer`. +pub type Rule = iced_native::Rule>; + +impl rule::Renderer for Renderer +where + B: Backend, +{ + type Style = Box; + + fn draw( + &mut self, + bounds: Rectangle, + style_sheet: &Self::Style, + is_horizontal: bool, + ) -> Self::Output { + let style = style_sheet.style(); + + let line = if is_horizontal { + let line_y = (bounds.y + (bounds.height / 2.0) + - (style.width as f32 / 2.0)) + .round(); + + let (line_x, line_width) = if style.fill_percent >= 100 { + (bounds.x, bounds.width) + } else { + let percent_width = + (bounds.width * style.fill_percent as f32 / 100.0).round(); + ( + bounds.x + ((bounds.width - percent_width) / 2.0).round(), + percent_width, + ) + }; + + Primitive::Quad { + bounds: Rectangle { + x: line_x, + y: line_y, + width: line_width, + height: style.width as f32, + }, + background: Background::Color(style.color), + border_radius: style.radius, + border_width: 0, + border_color: Color::TRANSPARENT, + } + } else { + let line_x = (bounds.x + (bounds.width / 2.0) + - (style.width as f32 / 2.0)) + .round(); + + let (line_y, line_height) = if style.fill_percent >= 100 { + (bounds.y, bounds.height) + } else { + let percent_height = + (bounds.height * style.fill_percent as f32 / 100.0).round(); + ( + bounds.y + ((bounds.height - percent_height) / 2.0).round(), + percent_height, + ) + }; + + Primitive::Quad { + bounds: Rectangle { + x: line_x, + y: line_y, + width: style.width as f32, + height: line_height, + }, + background: Background::Color(style.color), + border_radius: style.radius, + border_width: 0, + border_color: Color::TRANSPARENT, + } + }; + + (line, mouse::Interaction::default()) + } +} -- cgit From 32561bd85c6db0d7e6d9d12c87b87f4b50f1d43f Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sun, 16 Aug 2020 10:10:32 -0500 Subject: added FillMode enum style for the Rule widget --- graphics/src/widget/rule.rs | 101 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 19 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index 5ff5197d..e058dffe 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -5,7 +5,7 @@ use iced_native::mouse; use iced_native::rule; use iced_native::{Background, Color, Rectangle}; -pub use iced_style::rule::{Style, StyleSheet}; +pub use iced_style::rule::{FillMode, Style, StyleSheet}; /// Display a horizontal or vertical rule for dividing content. /// @@ -31,15 +31,46 @@ where - (style.width as f32 / 2.0)) .round(); - let (line_x, line_width) = if style.fill_percent >= 100 { - (bounds.x, bounds.width) - } else { - let percent_width = - (bounds.width * style.fill_percent as f32 / 100.0).round(); - ( - bounds.x + ((bounds.width - percent_width) / 2.0).round(), - percent_width, - ) + let (line_x, line_width) = match style.fill_mode { + FillMode::Full => (bounds.x, bounds.width), + FillMode::Percent(percent) => { + if percent >= 100.0 { + (bounds.x, bounds.width) + } else { + let percent_width = + (bounds.width * percent / 100.0).round(); + + ( + bounds.x + + ((bounds.width - percent_width) / 2.0) + .round(), + percent_width, + ) + } + } + FillMode::Padded(padding) => { + if padding == 0 { + (bounds.x, bounds.width) + } else { + let padding = padding as f32; + let mut line_width = bounds.width - (padding * 2.0); + if line_width < 0.0 { + line_width = 0.0; + } + + (bounds.x + padding, line_width) + } + } + FillMode::AsymmetricPadding(first_pad, second_pad) => { + let first_pad = first_pad as f32; + let second_pad = second_pad as f32; + let mut line_width = bounds.width - first_pad - second_pad; + if line_width < 0.0 { + line_width = 0.0; + } + + (bounds.x + first_pad, line_width) + } }; Primitive::Quad { @@ -59,15 +90,47 @@ where - (style.width as f32 / 2.0)) .round(); - let (line_y, line_height) = if style.fill_percent >= 100 { - (bounds.y, bounds.height) - } else { - let percent_height = - (bounds.height * style.fill_percent as f32 / 100.0).round(); - ( - bounds.y + ((bounds.height - percent_height) / 2.0).round(), - percent_height, - ) + let (line_y, line_height) = match style.fill_mode { + FillMode::Full => (bounds.y, bounds.height), + FillMode::Percent(percent) => { + if percent >= 100.0 { + (bounds.y, bounds.height) + } else { + let percent_height = + (bounds.height * percent / 100.0).round(); + + ( + bounds.y + + ((bounds.height - percent_height) / 2.0) + .round(), + percent_height, + ) + } + } + FillMode::Padded(padding) => { + if padding == 0 { + (bounds.y, bounds.height) + } else { + let padding = padding as f32; + let mut line_height = bounds.height - (padding * 2.0); + if line_height < 0.0 { + line_height = 0.0; + } + + (bounds.y + padding, line_height) + } + } + FillMode::AsymmetricPadding(first_pad, second_pad) => { + let first_pad = first_pad as f32; + let second_pad = second_pad as f32; + let mut line_height = + bounds.height - first_pad - second_pad; + if line_height < 0.0 { + line_height = 0.0; + } + + (bounds.y + first_pad, line_height) + } }; Primitive::Quad { -- cgit From fed30ef7753bbe33af026f1f46a09d8381682284 Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sun, 16 Aug 2020 19:20:02 -0500 Subject: added FillMode::fill() --- graphics/src/widget/rule.rs | 87 +++------------------------------------------ 1 file changed, 4 insertions(+), 83 deletions(-) (limited to 'graphics/src') diff --git a/graphics/src/widget/rule.rs b/graphics/src/widget/rule.rs index e058dffe..a7a5d0e7 100644 --- a/graphics/src/widget/rule.rs +++ b/graphics/src/widget/rule.rs @@ -31,47 +31,8 @@ where - (style.width as f32 / 2.0)) .round(); - let (line_x, line_width) = match style.fill_mode { - FillMode::Full => (bounds.x, bounds.width), - FillMode::Percent(percent) => { - if percent >= 100.0 { - (bounds.x, bounds.width) - } else { - let percent_width = - (bounds.width * percent / 100.0).round(); - - ( - bounds.x - + ((bounds.width - percent_width) / 2.0) - .round(), - percent_width, - ) - } - } - FillMode::Padded(padding) => { - if padding == 0 { - (bounds.x, bounds.width) - } else { - let padding = padding as f32; - let mut line_width = bounds.width - (padding * 2.0); - if line_width < 0.0 { - line_width = 0.0; - } - - (bounds.x + padding, line_width) - } - } - FillMode::AsymmetricPadding(first_pad, second_pad) => { - let first_pad = first_pad as f32; - let second_pad = second_pad as f32; - let mut line_width = bounds.width - first_pad - second_pad; - if line_width < 0.0 { - line_width = 0.0; - } - - (bounds.x + first_pad, line_width) - } - }; + let (offset, line_width) = style.fill_mode.fill(bounds.width); + let line_x = bounds.x + offset; Primitive::Quad { bounds: Rectangle { @@ -90,48 +51,8 @@ where - (style.width as f32 / 2.0)) .round(); - let (line_y, line_height) = match style.fill_mode { - FillMode::Full => (bounds.y, bounds.height), - FillMode::Percent(percent) => { - if percent >= 100.0 { - (bounds.y, bounds.height) - } else { - let percent_height = - (bounds.height * percent / 100.0).round(); - - ( - bounds.y - + ((bounds.height - percent_height) / 2.0) - .round(), - percent_height, - ) - } - } - FillMode::Padded(padding) => { - if padding == 0 { - (bounds.y, bounds.height) - } else { - let padding = padding as f32; - let mut line_height = bounds.height - (padding * 2.0); - if line_height < 0.0 { - line_height = 0.0; - } - - (bounds.y + padding, line_height) - } - } - FillMode::AsymmetricPadding(first_pad, second_pad) => { - let first_pad = first_pad as f32; - let second_pad = second_pad as f32; - let mut line_height = - bounds.height - first_pad - second_pad; - if line_height < 0.0 { - line_height = 0.0; - } - - (bounds.y + first_pad, line_height) - } - }; + let (offset, line_height) = style.fill_mode.fill(bounds.height); + let line_y = bounds.y + offset; Primitive::Quad { bounds: Rectangle { -- cgit