From 4aa5870725ebf2b6ce8d7abba1d86ffa32fd23c2 Mon Sep 17 00:00:00 2001
From: Maccesch <maccesch@synphonyte.com>
Date: Thu, 24 Nov 2022 19:01:56 +0000
Subject: [PATCH] added layout example

---
 examples/layout.rs  | 143 ++++++++++++++++++++++++++++++++++++++++++++
 src/node.rs         |  10 +---
 src/styles/style.rs |   8 +--
 3 files changed, 149 insertions(+), 12 deletions(-)
 create mode 100644 examples/layout.rs

diff --git a/examples/layout.rs b/examples/layout.rs
new file mode 100644
index 0000000..41e335a
--- /dev/null
+++ b/examples/layout.rs
@@ -0,0 +1,143 @@
+use bevy::prelude::*;
+use kayak_ui::prelude::{widgets::*, *};
+
+fn startup(
+    mut commands: Commands,
+    mut font_mapping: ResMut<FontMapping>,
+    asset_server: Res<AssetServer>,
+) {
+    font_mapping.set_default(asset_server.load("roboto.kayak_font"));
+
+    // Camera 2D forces a clear pass in bevy.
+    // We do this because our scene is not rendering anything else.
+    commands.spawn(Camera2dBundle::default());
+
+    let mut widget_context = KayakRootContext::new();
+    widget_context.add_plugin(KayakWidgetsContextPlugin);
+    let parent_id = None;
+
+    rsx! {
+        <KayakAppBundle>
+            <WindowBundle
+                window={KWindow {
+                    title: "Layout example".into(),
+                    draggable: true,
+                    initial_position: Vec2::new(10.0, 10.0),
+                    size: Vec2::new(512.0, 512.0),
+                    ..KWindow::default()
+                }}
+            >
+                <ElementBundle
+                    styles={KStyle{
+                        layout_type: LayoutType::Grid.into(),
+                        grid_rows: vec![Units::Stretch(1.0), Units::Stretch(2.0), Units::Stretch(5.0)].into(),
+                        grid_cols: vec![Units::Stretch(1.0), Units::Stretch(1.0)].into(),
+                        ..default()
+                    }}
+                >
+                    <BackgroundBundle
+                        styles={KStyle{
+                            background_color: Color::rgb(0.4, 0.9, 0.4).into(),
+                            color: Color::rgb(0.0, 0.0, 0.0).into(),
+                            padding: Edge::all(Units::Pixels(5.0)).into(),
+                            border_radius: Corner::all(10.0).into(),
+                            row_index: 0.into(),
+                            col_index: 0.into(),
+                            col_span: 2.into(),
+                            layout_type: LayoutType::Row.into(),
+                            col_between: Units::Pixels(5.0).into(),
+                            ..default()
+                        }}
+                    >
+                        <TextWidgetBundle
+                            text={TextProps {
+                                content: "A".into(),
+                                ..default()
+                            }}
+                        />
+                        <TextWidgetBundle
+                            text={TextProps {
+                                content: "B".into(),
+                                ..default()
+                            }}
+                        />
+                        <TextWidgetBundle
+                            text={TextProps {
+                                content: "C".into(),
+                                ..default()
+                            }}
+                        />
+                        <TextWidgetBundle
+                            text={TextProps {
+                                content: "D".into(),
+                                ..default()
+                            }}
+                        />
+                        <TextWidgetBundle
+                            text={TextProps {
+                                content: "E".into(),
+                                ..default()
+                            }}
+                        />
+
+                    </BackgroundBundle>
+                    <TextWidgetBundle
+                        text={TextProps {
+                            content: "R1 C0".into(),
+                            ..default()
+                        }}
+                        styles={KStyle{
+                            row_index: 1.into(),
+                            col_index: 0.into(),
+                            ..default()
+                        }}
+                    />
+                    <TextWidgetBundle
+                        text={TextProps {
+                            content: "R1 C1".into(),
+                            ..default()
+                        }}
+                        styles={KStyle{
+                            row_index: 1.into(),
+                            col_index: 1.into(),
+                            ..default()
+                        }}
+                    />
+                    <TextWidgetBundle
+                        text={TextProps {
+                            content: "R2 C0".into(),
+                            ..default()
+                        }}
+                        styles={KStyle{
+                            row_index: 2.into(),
+                            col_index: 0.into(),
+                            ..default()
+                        }}
+                    />
+                    <TextWidgetBundle
+                        text={TextProps {
+                            content: "R2 C1".into(),
+                            ..default()
+                        }}
+                        styles={KStyle{
+                            row_index: 2.into(),
+                            col_index: 1.into(),
+                            ..default()
+                        }}
+                    />
+                </ElementBundle>
+            </WindowBundle>
+        </KayakAppBundle>
+    }
+
+    commands.spawn(UICameraBundle::new(widget_context));
+}
+
+fn main() {
+    App::new()
+        .add_plugins(DefaultPlugins)
+        .add_plugin(KayakContextPlugin)
+        .add_plugin(KayakWidgets)
+        .add_startup_system(startup)
+        .run()
+}
diff --git a/src/node.rs b/src/node.rs
index dfd3165..b307207 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -415,14 +415,8 @@ impl<'a> morphorm::Node<'a> for WrappedIndex {
     fn col_index(&self, store: &'_ Self::Data) -> Option<usize> {
         if let Ok(node) = store.get(self.0) {
             return match node.resolved_styles.col_index {
-                StyleProp::Default => {
-                    println!("col_index default");
-                    Some(0)
-                },
-                StyleProp::Value(prop) => {
-                    println!("col_index value {prop}");
-                    Some(prop)
-                },
+                StyleProp::Default => Some(0),
+                StyleProp::Value(prop) => Some(prop),
                 _ => Some(0),
             };
         }
diff --git a/src/styles/style.rs b/src/styles/style.rs
index 7277196..5dba847 100644
--- a/src/styles/style.rs
+++ b/src/styles/style.rs
@@ -54,8 +54,8 @@ pub enum StyleProp<T: Default + Clone + Reflect + FromReflect> {
 }
 
 impl<T> Default for StyleProp<T>
-    where
-        T: Default + Clone + Reflect + FromReflect,
+where
+    T: Default + Clone + Reflect + FromReflect,
 {
     fn default() -> Self {
         Self::Unset
@@ -63,8 +63,8 @@ impl<T> Default for StyleProp<T>
 }
 
 impl<T> StyleProp<T>
-    where
-        T: Default + Clone + Reflect + FromReflect,
+where
+    T: Default + Clone + Reflect + FromReflect,
 {
     /// Resolves this style property into a concrete value.
     ///
-- 
GitLab