From 27b1f3da35de960914f0125c8daf48dfc0347461 Mon Sep 17 00:00:00 2001
From: MrGVSV <gino.valente.code@gmail.com>
Date: Tue, 15 Feb 2022 16:14:14 -0800
Subject: [PATCH] Added offset style property

---
 examples/clipping.rs         |  5 +----
 kayak_core/src/node.rs       | 24 ++++++++++++++++++++----
 kayak_core/src/styles/mod.rs | 24 ++++++++++++++++++------
 3 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/examples/clipping.rs b/examples/clipping.rs
index 0b07679..24691c9 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 3d098e8..7142d9a 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 4c7f2ce..3a82fe0 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,
-- 
GitLab