diff options
-rw-r--r-- | core/Cargo.toml | 5 | ||||
-rw-r--r-- | core/src/align.rs | 26 | ||||
-rw-r--r-- | core/src/justify.rs | 17 | ||||
-rw-r--r-- | native/Cargo.toml | 2 | ||||
-rw-r--r-- | native/src/style.rs | 110 |
5 files changed, 49 insertions, 111 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index 40a8bcfb..a244bcba 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,8 +6,3 @@ edition = "2018" description = "The essential concepts of Iced" license = "MIT" repository = "https://github.com/hecrj/iced" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -stretch = { version = "0.2", optional = true } diff --git a/core/src/align.rs b/core/src/align.rs index 5876e0f8..5dbd658d 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -19,29 +19,3 @@ pub enum Align { /// Stretch over the cross axis. Stretch, } - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From<Align> for stretch::style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => stretch::style::AlignItems::FlexStart, - Align::Center => stretch::style::AlignItems::Center, - Align::End => stretch::style::AlignItems::FlexEnd, - Align::Stretch => stretch::style::AlignItems::Stretch, - } - } -} - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From<Align> for stretch::style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => stretch::style::AlignSelf::FlexStart, - Align::Center => stretch::style::AlignSelf::Center, - Align::End => stretch::style::AlignSelf::FlexEnd, - Align::Stretch => stretch::style::AlignSelf::Stretch, - } - } -} diff --git a/core/src/justify.rs b/core/src/justify.rs index 878573b0..53aa7319 100644 --- a/core/src/justify.rs +++ b/core/src/justify.rs @@ -25,20 +25,3 @@ pub enum Justify { /// Place items with evenly distributed space. SpaceEvenly, } - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From<Justify> for stretch::style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => stretch::style::JustifyContent::FlexStart, - Justify::Center => stretch::style::JustifyContent::Center, - Justify::End => stretch::style::JustifyContent::FlexEnd, - Justify::SpaceBetween => { - stretch::style::JustifyContent::SpaceBetween - } - Justify::SpaceAround => stretch::style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => stretch::style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/native/Cargo.toml b/native/Cargo.toml index 1dbccdee..5f7e5e41 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/hecrj/iced" features = ["winit"] [dependencies] -iced_core = { version = "0.1.0-alpha", features = ["stretch"], path = "../core" } +iced_core = { version = "0.1.0-alpha", path = "../core" } stretch = "0.2" twox-hash = "1.5" diff --git a/native/src/style.rs b/native/src/style.rs index 3a75c925..b1c49fd4 100644 --- a/native/src/style.rs +++ b/native/src/style.rs @@ -1,7 +1,6 @@ use crate::{Align, Justify, Length}; -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; +use stretch::style; /// The appearance of a [`Node`]. /// @@ -9,12 +8,29 @@ use stretch::{geometry, style}; #[derive(Debug, Clone, Copy)] pub struct Style(pub(crate) style::Style); +impl Default for Style { + fn default() -> Style { + Style::new() + } +} + impl Style { + /// Creates a new [`Style`]. + /// + /// [`Style`]: struct.Style.html + pub fn new() -> Self { + Style(style::Style { + align_items: style::AlignItems::FlexStart, + justify_content: style::JustifyContent::FlexStart, + ..style::Style::default() + }) + } + /// Defines the width of a [`Node`]. /// /// [`Node`]: struct.Node.html pub fn width(mut self, width: Length) -> Self { - self.0.size.width = length_to_dimension(width); + self.0.size.width = into_dimension(width); self } @@ -22,7 +38,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn height(mut self, height: Length) -> Self { - self.0.size.height = length_to_dimension(height); + self.0.size.height = into_dimension(height); self } @@ -30,7 +46,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn min_width(mut self, min_width: Length) -> Self { - self.0.min_size.width = length_to_dimension(min_width); + self.0.min_size.width = into_dimension(min_width); self } @@ -38,7 +54,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn max_width(mut self, max_width: Length) -> Self { - self.0.max_size.width = length_to_dimension(max_width); + self.0.max_size.width = into_dimension(max_width); self } @@ -46,7 +62,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn min_height(mut self, min_height: Length) -> Self { - self.0.min_size.height = length_to_dimension(min_height); + self.0.min_size.height = into_dimension(min_height); self } @@ -54,17 +70,17 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn max_height(mut self, max_height: Length) -> Self { - self.0.max_size.height = length_to_dimension(max_height); + self.0.max_size.height = into_dimension(max_height); self } pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); + self.0.align_items = into_align_items(align); self } pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); + self.0.justify_content = into_justify_content(justify); self } @@ -80,7 +96,7 @@ impl Style { /// [`Row`]: widget/struct.Row.html pub fn align_self(mut self, align: Option<Align>) -> Self { self.0.align_self = match align { - Some(align) => align.into(), + Some(align) => into_align_self(align), None => stretch::style::AlignSelf::Auto, }; @@ -102,7 +118,7 @@ impl Style { } } -fn length_to_dimension(length: Length) -> style::Dimension { +fn into_dimension(length: Length) -> style::Dimension { match length { Length::Shrink => style::Dimension::Undefined, Length::Fill => style::Dimension::Percent(1.0), @@ -110,61 +126,31 @@ fn length_to_dimension(length: Length) -> style::Dimension { } } -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) +fn into_align_items(align: Align) -> style::AlignItems { + match align { + Align::Start => style::AlignItems::FlexStart, + Align::Center => style::AlignItems::Center, + Align::End => style::AlignItems::FlexEnd, + Align::Stretch => style::AlignItems::Stretch, } } -impl Hash for Style { - fn hash<H: Hasher>(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); +fn into_align_self(align: Align) -> style::AlignSelf { + match align { + Align::Start => style::AlignSelf::FlexStart, + Align::Center => style::AlignSelf::Center, + Align::End => style::AlignSelf::FlexEnd, + Align::Stretch => style::AlignSelf::Stretch, } } -fn hash_size<H: Hasher>( - size: &geometry::Size<style::Dimension>, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect<H: Hasher>( - rect: &geometry::Rect<style::Dimension>, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension<H: Hasher>(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } +fn into_justify_content(justify: Justify) -> style::JustifyContent { + match justify { + Justify::Start => style::JustifyContent::FlexStart, + Justify::Center => style::JustifyContent::Center, + Justify::End => style::JustifyContent::FlexEnd, + Justify::SpaceBetween => style::JustifyContent::SpaceBetween, + Justify::SpaceAround => style::JustifyContent::SpaceAround, + Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, } } |