diff --git a/examples/clipping.rs b/examples/clipping.rs index 0b0767978f6896cf9345c9355b2d5f25aae53bcf..24691c930070b7e5c01c00d98961358200c3de02 100644 --- a/examples/clipping.rs +++ b/examples/clipping.rs @@ -28,10 +28,7 @@ fn startup( let nine_patch_styles = Style { width: StyleProp::Value(Units::Pixels(512.0)), height: StyleProp::Value(Units::Pixels(512.0)), - left: StyleProp::Value(Units::Stretch(1.0)), - right: StyleProp::Value(Units::Stretch(1.0)), - top: StyleProp::Value(Units::Stretch(1.0)), - bottom: StyleProp::Value(Units::Stretch(1.0)), + offset: StyleProp::Value(Edge::all(Units::Stretch(1.0))), padding: StyleProp::Value(Edge::all(Units::Pixels(25.0))), ..Style::default() }; diff --git a/kayak_core/src/node.rs b/kayak_core/src/node.rs index 3d098e8d26fdc401c2d002cefcd15cb8fbe1dc09..7142d9a68671b4b2e64daec99379551a8b9fb4fe 100644 --- a/kayak_core/src/node.rs +++ b/kayak_core/src/node.rs @@ -175,7 +175,11 @@ impl<'a> morphorm::Node<'a> for Index { if let Some(node) = store.get(*self) { if let Some(node) = node { return match node.resolved_styles.left { - StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Default => match node.resolved_styles.offset { + StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Value(prop) => Some(prop.left), + _ => Some(morphorm::Units::Auto), + }, StyleProp::Value(prop) => Some(prop), _ => Some(morphorm::Units::Auto), }; @@ -188,7 +192,11 @@ impl<'a> morphorm::Node<'a> for Index { if let Some(node) = store.get(*self) { if let Some(node) = node { return match node.resolved_styles.right { - StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Default => match node.resolved_styles.offset { + StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Value(prop) => Some(prop.right), + _ => Some(morphorm::Units::Auto), + }, StyleProp::Value(prop) => Some(prop), _ => Some(morphorm::Units::Auto), }; @@ -201,7 +209,11 @@ impl<'a> morphorm::Node<'a> for Index { if let Some(node) = store.get(*self) { if let Some(node) = node { return match node.resolved_styles.top { - StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Default => match node.resolved_styles.offset { + StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Value(prop) => Some(prop.top), + _ => Some(morphorm::Units::Auto), + }, StyleProp::Value(prop) => Some(prop), _ => Some(morphorm::Units::Auto), }; @@ -214,7 +226,11 @@ impl<'a> morphorm::Node<'a> for Index { if let Some(node) = store.get(*self) { if let Some(node) = node { return match node.resolved_styles.bottom { - StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Default => match node.resolved_styles.offset { + StyleProp::Default => Some(morphorm::Units::Auto), + StyleProp::Value(prop) => Some(prop.bottom), + _ => Some(morphorm::Units::Auto), + }, StyleProp::Value(prop) => Some(prop), _ => Some(morphorm::Units::Auto), }; diff --git a/kayak_core/src/styles/mod.rs b/kayak_core/src/styles/mod.rs index 4c7f2ce13d4b28fa71fb93171513aff31f772481..3a82fe0adfd24f4b06ed5fa6c253d98f9d8a6ec6 100644 --- a/kayak_core/src/styles/mod.rs +++ b/kayak_core/src/styles/mod.rs @@ -28,8 +28,8 @@ pub enum StyleProp<T: Default + Clone> { } impl<T> Default for StyleProp<T> -where - T: Default + Clone, + where + T: Default + Clone, { fn default() -> Self { Self::Unset @@ -37,8 +37,8 @@ where } impl<T> StyleProp<T> -where - T: Default + Clone, + where + T: Default + Clone, { pub fn resolve(&self) -> T { match self { @@ -159,13 +159,24 @@ define_styles! { pub max_width: StyleProp<Units>, pub min_height: StyleProp<Units>, pub min_width: StyleProp<Units>, + /// The positional offset from this widget's default position + /// + /// This property has lower precedence than its more specific counterparts + /// ([`top`](Self::top), [`right`](Self::right), [`bottom`](Self::bottom), and [`left`](Self::left)), + /// allowing it to be overridden. + /// + /// For widgets with a [`position_type`](Self::position_type) of [`PositionType`](PositionType::ParentDirected) + /// this acts like margin around the widget. For [`PositionType`](PositionType::SelfDirected) this + /// acts as the actual position from the parent. + pub offset: StyleProp<Edge<Units>>, /// The inner padding between the edges of this widget and its children /// /// This property has lower precedence than its more specific counterparts - /// (`padding_top`, `padding_right`, `padding_bottom`, and `padding_left`), allowing it + /// ([`padding_top`](Self::padding_top), [`padding_right`](Self::padding_right), + /// [`padding_bottom`](Self::padding_bottom), and [`padding_left`](Self::padding_left)), allowing it /// to be overridden. /// - /// A child with their own padding properties set to anything other than `Units::Auto` will + /// A child with their own padding properties set to anything other than [`Units::Auto`] will /// override the padding set by this widget. pub padding: StyleProp<Edge<Units>>, pub padding_bottom: StyleProp<Units>, @@ -205,6 +216,7 @@ impl Style { max_width: StyleProp::Default, min_height: StyleProp::Default, min_width: StyleProp::Default, + offset: StyleProp::Default, padding: StyleProp::Default, padding_bottom: StyleProp::Default, padding_left: StyleProp::Default,