From 32561bd85c6db0d7e6d9d12c87b87f4b50f1d43f Mon Sep 17 00:00:00 2001 From: Billy Messenger Date: Sun, 16 Aug 2020 10:10:32 -0500 Subject: [PATCH] added FillMode enum style for the Rule widget --- examples/styling/src/main.rs | 2 +- glow/src/widget/rule.rs | 2 +- graphics/src/widget/rule.rs | 101 ++++++++++++++++++++++++++++------- style/src/rule.rs | 28 ++++++++-- wgpu/src/widget/rule.rs | 2 +- 5 files changed, 108 insertions(+), 27 deletions(-) diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index c969526c..0c2cfc57 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -537,7 +537,7 @@ mod style { color: SURFACE, width: 2, radius: 1, - fill_percent: 90, + fill_mode: rule::FillMode::Padded(15), } } } diff --git a/glow/src/widget/rule.rs b/glow/src/widget/rule.rs index 16eaa267..faa2be86 100644 --- a/glow/src/widget/rule.rs +++ b/glow/src/widget/rule.rs @@ -2,7 +2,7 @@ use crate::Renderer; -pub use iced_graphics::rule::{Style, StyleSheet}; +pub use iced_graphics::rule::{FillMode, Style, StyleSheet}; /// Display a horizontal or vertical rule for dividing content. /// 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 { diff --git a/style/src/rule.rs b/style/src/rule.rs index dbd72d41..aa095d3b 100644 --- a/style/src/rule.rs +++ b/style/src/rule.rs @@ -2,6 +2,23 @@ use iced_core::Color; +/// The fill mode of a rule. +#[derive(Debug, Clone, Copy)] +pub enum FillMode { + /// Fill the whole length of the container. + Full, + /// Fill a percent of the length of the container. The rule + /// will be centered in that container. + /// + /// The range is `[0.0, 100.0]`. + Percent(f32), + /// Uniform offset from each end, length units. + Padded(u16), + /// Different offset on each end of the rule, length units. + /// First = top or left. + AsymmetricPadding(u16, u16), +} + /// The appearance of a rule. #[derive(Debug, Clone, Copy)] pub struct Style { @@ -11,9 +28,10 @@ pub struct Style { pub width: u16, /// The radius of the rectangle corners. pub radius: u16, - /// The percent from [0, 100] of the filled space the rule - /// will be drawn. - pub fill_percent: u16, + /// The [`FillMode`] of the rule. + /// + /// [`FillMode`]: enum.FillMode.html + pub fill_mode: FillMode, } /// A set of rules that dictate the style of a rule. @@ -27,10 +45,10 @@ struct Default; impl StyleSheet for Default { fn style(&self) -> Style { Style { - color: [0.6, 0.6, 0.6, 0.49].into(), + color: [0.6, 0.6, 0.6, 0.51].into(), width: 1, radius: 0, - fill_percent: 90, + fill_mode: FillMode::Percent(90.0), } } } diff --git a/wgpu/src/widget/rule.rs b/wgpu/src/widget/rule.rs index 630a6f33..3f7bc67a 100644 --- a/wgpu/src/widget/rule.rs +++ b/wgpu/src/widget/rule.rs @@ -2,7 +2,7 @@ use crate::Renderer; -pub use iced_graphics::rule::{Style, StyleSheet}; +pub use iced_graphics::rule::{FillMode, Style, StyleSheet}; /// Display a horizontal or vertical rule for dividing content. ///