Remove stretch optional dependency in core

This commit is contained in:
Héctor Ramón Jiménez 2019-09-24 14:38:40 +02:00
parent 0a93be78b8
commit a975754ab0
5 changed files with 49 additions and 111 deletions

View file

@ -6,8 +6,3 @@ edition = "2018"
description = "The essential concepts of Iced" description = "The essential concepts of Iced"
license = "MIT" license = "MIT"
repository = "https://github.com/hecrj/iced" 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 }

View file

@ -19,29 +19,3 @@ pub enum Align {
/// Stretch over the cross axis. /// Stretch over the cross axis.
Stretch, 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,
}
}
}

View file

@ -25,20 +25,3 @@ pub enum Justify {
/// Place items with evenly distributed space. /// Place items with evenly distributed space.
SpaceEvenly, 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,
}
}
}

View file

@ -11,7 +11,7 @@ repository = "https://github.com/hecrj/iced"
features = ["winit"] features = ["winit"]
[dependencies] [dependencies]
iced_core = { version = "0.1.0-alpha", features = ["stretch"], path = "../core" } iced_core = { version = "0.1.0-alpha", path = "../core" }
stretch = "0.2" stretch = "0.2"
twox-hash = "1.5" twox-hash = "1.5"

View file

@ -1,7 +1,6 @@
use crate::{Align, Justify, Length}; use crate::{Align, Justify, Length};
use std::hash::{Hash, Hasher}; use stretch::style;
use stretch::{geometry, style};
/// The appearance of a [`Node`]. /// The appearance of a [`Node`].
/// ///
@ -9,12 +8,29 @@ use stretch::{geometry, style};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Style(pub(crate) style::Style); pub struct Style(pub(crate) style::Style);
impl Default for Style {
fn default() -> Style {
Style::new()
}
}
impl Style { 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`]. /// Defines the width of a [`Node`].
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn width(mut self, width: Length) -> Self { pub fn width(mut self, width: Length) -> Self {
self.0.size.width = length_to_dimension(width); self.0.size.width = into_dimension(width);
self self
} }
@ -22,7 +38,7 @@ impl Style {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn height(mut self, height: Length) -> Self { pub fn height(mut self, height: Length) -> Self {
self.0.size.height = length_to_dimension(height); self.0.size.height = into_dimension(height);
self self
} }
@ -30,7 +46,7 @@ impl Style {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn min_width(mut self, min_width: Length) -> Self { 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 self
} }
@ -38,7 +54,7 @@ impl Style {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn max_width(mut self, max_width: Length) -> Self { 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 self
} }
@ -46,7 +62,7 @@ impl Style {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn min_height(mut self, min_height: Length) -> Self { 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 self
} }
@ -54,17 +70,17 @@ impl Style {
/// ///
/// [`Node`]: struct.Node.html /// [`Node`]: struct.Node.html
pub fn max_height(mut self, max_height: Length) -> Self { 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 self
} }
pub(crate) fn align_items(mut self, align: Align) -> 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 self
} }
pub(crate) fn justify_content(mut self, justify: Justify) -> 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 self
} }
@ -80,7 +96,7 @@ impl Style {
/// [`Row`]: widget/struct.Row.html /// [`Row`]: widget/struct.Row.html
pub fn align_self(mut self, align: Option<Align>) -> Self { pub fn align_self(mut self, align: Option<Align>) -> Self {
self.0.align_self = match align { self.0.align_self = match align {
Some(align) => align.into(), Some(align) => into_align_self(align),
None => stretch::style::AlignSelf::Auto, 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 { match length {
Length::Shrink => style::Dimension::Undefined, Length::Shrink => style::Dimension::Undefined,
Length::Fill => style::Dimension::Percent(1.0), Length::Fill => style::Dimension::Percent(1.0),
@ -110,61 +126,31 @@ fn length_to_dimension(length: Length) -> style::Dimension {
} }
} }
impl Default for Style { fn into_align_items(align: Align) -> style::AlignItems {
fn default() -> Style { match align {
Style(style::Style { Align::Start => style::AlignItems::FlexStart,
align_items: style::AlignItems::FlexStart, Align::Center => style::AlignItems::Center,
justify_content: style::JustifyContent::FlexStart, Align::End => style::AlignItems::FlexEnd,
..style::Style::default() Align::Stretch => style::AlignItems::Stretch,
})
} }
} }
impl Hash for Style { fn into_align_self(align: Align) -> style::AlignSelf {
fn hash<H: Hasher>(&self, state: &mut H) { match align {
hash_size(&self.0.size, state); Align::Start => style::AlignSelf::FlexStart,
hash_size(&self.0.min_size, state); Align::Center => style::AlignSelf::Center,
hash_size(&self.0.max_size, state); Align::End => style::AlignSelf::FlexEnd,
Align::Stretch => style::AlignSelf::Stretch,
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 hash_size<H: Hasher>( fn into_justify_content(justify: Justify) -> style::JustifyContent {
size: &geometry::Size<style::Dimension>, match justify {
state: &mut H, Justify::Start => style::JustifyContent::FlexStart,
) { Justify::Center => style::JustifyContent::Center,
hash_dimension(size.width, state); Justify::End => style::JustifyContent::FlexEnd,
hash_dimension(size.height, state); Justify::SpaceBetween => style::JustifyContent::SpaceBetween,
} Justify::SpaceAround => style::JustifyContent::SpaceAround,
Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly,
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);
}
} }
} }