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 --- style/src/rule.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 style/src/rule.rs (limited to 'style/src/rule.rs') diff --git a/style/src/rule.rs b/style/src/rule.rs new file mode 100644 index 00000000..dbd72d41 --- /dev/null +++ b/style/src/rule.rs @@ -0,0 +1,51 @@ +//! Display a horizontal or vertical rule for dividing content. + +use iced_core::Color; + +/// The appearance of a rule. +#[derive(Debug, Clone, Copy)] +pub struct Style { + /// The color of the rule. + pub color: Color, + /// The width (thickness) of the rule line. + 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, +} + +/// A set of rules that dictate the style of a rule. +pub trait StyleSheet { + /// Produces the style of a rule. + fn style(&self) -> Style; +} + +struct Default; + +impl StyleSheet for Default { + fn style(&self) -> Style { + Style { + color: [0.6, 0.6, 0.6, 0.49].into(), + width: 1, + radius: 0, + fill_percent: 90, + } + } +} + +impl std::default::Default for Box { + fn default() -> Self { + Box::new(Default) + } +} + +impl From for Box +where + T: 'static + StyleSheet, +{ + fn from(style: T) -> Self { + Box::new(style) + } +} -- 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 --- style/src/rule.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'style/src/rule.rs') 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), } } } -- 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() --- style/src/rule.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'style/src/rule.rs') diff --git a/style/src/rule.rs b/style/src/rule.rs index aa095d3b..6ba54e33 100644 --- a/style/src/rule.rs +++ b/style/src/rule.rs @@ -19,6 +19,53 @@ pub enum FillMode { AsymmetricPadding(u16, u16), } +impl FillMode { + /// Return the starting offset and length of the rule. + /// + /// * `space` - The space to fill. + /// + /// # Returns + /// + /// * (starting_offset, length) + pub fn fill(&self, space: f32) -> (f32, f32) { + match *self { + FillMode::Full => (0.0, space), + FillMode::Percent(percent) => { + if percent >= 100.0 { + (0.0, space) + } else { + let percent_width = (space * percent / 100.0).round(); + + (((space - percent_width) / 2.0).round(), percent_width) + } + } + FillMode::Padded(padding) => { + if padding == 0 { + (0.0, space) + } else { + let padding = padding as f32; + let mut line_width = space - (padding * 2.0); + if line_width < 0.0 { + line_width = 0.0; + } + + (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 = space - first_pad - second_pad; + if line_width < 0.0 { + line_width = 0.0; + } + + (first_pad, line_width) + } + } + } +} + /// The appearance of a rule. #[derive(Debug, Clone, Copy)] pub struct Style { @@ -26,7 +73,7 @@ pub struct Style { pub color: Color, /// The width (thickness) of the rule line. pub width: u16, - /// The radius of the rectangle corners. + /// The radius of the line corners. pub radius: u16, /// The [`FillMode`] of the rule. /// -- cgit