From 7e9d4562e2648ba2777978bb1fadc6ee6bc235ab Mon Sep 17 00:00:00 2001
From: Jacob LeCoq <lecoqjacob@gmail.com>
Date: Sun, 20 Nov 2022 10:35:07 -0600
Subject: [PATCH] Fix app not updating if camera width/height changes

---
 examples/test_no_startup.rs | 49 +++++++++++++++++++++++++++++++++++++
 src/widgets/app.rs          | 20 +++++++++------
 2 files changed, 61 insertions(+), 8 deletions(-)
 create mode 100644 examples/test_no_startup.rs

diff --git a/examples/test_no_startup.rs b/examples/test_no_startup.rs
new file mode 100644
index 0000000..8567aa2
--- /dev/null
+++ b/examples/test_no_startup.rs
@@ -0,0 +1,49 @@
+use bevy::prelude::*;
+use kayak_ui::prelude::{widgets::*, *};
+
+#[derive(Default, Clone, Copy, PartialEq, Hash, Eq, Debug)]
+pub enum GameState {
+    #[default]
+    First,
+    Second,
+}
+
+fn first_sys(mut state: ResMut<State<GameState>>) {
+    state.overwrite_replace(GameState::Second).unwrap();
+}
+
+fn second_sys(
+    mut commands: Commands,
+    asset_server: Res<AssetServer>,
+    mut font_mapping: ResMut<FontMapping>,
+) {
+    font_mapping.set_default(asset_server.load("roboto.kayak_font"));
+
+    let mut widget_context = KayakRootContext::new();
+    widget_context.add_plugin(KayakWidgetsContextPlugin);
+    let parent_id = None;
+    rsx! {
+        <KayakAppBundle>
+            <TextWidgetBundle
+                text={TextProps {
+                    content: "Hello World".into(),
+                    size: 20.0,
+                    ..Default::default()
+                }}
+            />
+        </KayakAppBundle>
+    }
+
+    commands.spawn(UICameraBundle::new(widget_context));
+}
+
+fn main() {
+    App::new()
+        .add_plugins(DefaultPlugins)
+        .add_plugin(KayakContextPlugin)
+        .add_plugin(KayakWidgets)
+        .add_state(GameState::First)
+        .add_system_set(SystemSet::on_enter(GameState::First).with_system(first_sys))
+        .add_system_set(SystemSet::on_enter(GameState::Second).with_system(second_sys))
+        .run()
+}
diff --git a/src/widgets/app.rs b/src/widgets/app.rs
index c234a92..72cde9b 100644
--- a/src/widgets/app.rs
+++ b/src/widgets/app.rs
@@ -41,18 +41,22 @@ impl Default for KayakAppBundle {
 
 pub fn app_update(
     In((widget_context, entity, previous_props_entity)): In<(KayakWidgetContext, Entity, Entity)>,
-    windows: Res<Windows>,
     widget_param: WidgetParam<KayakApp, EmptyState>,
+    camera: Query<&Camera, With<CameraUIKayak>>,
 ) -> bool {
-    let primary_window = windows.get_primary().unwrap();
-
     let mut window_change = false;
     if let Ok(app_style) = widget_param.style_query.get(entity) {
-        if app_style.width != StyleProp::Value(Units::Pixels(primary_window.width())) {
-            window_change = true;
-        }
-        if app_style.height != StyleProp::Value(Units::Pixels(primary_window.height())) {
-            window_change = true;
+        if let Some(camera_entity) = widget_context.camera_entity {
+            if let Ok(camera) = camera.get(camera_entity) {
+                if let Some(size) = camera.logical_viewport_size() {
+                    if app_style.width != StyleProp::Value(Units::Pixels(size.x)) {
+                        window_change = true;
+                    }
+                    if app_style.height != StyleProp::Value(Units::Pixels(size.y)) {
+                        window_change = true;
+                    }
+                }
+            }
         }
     }
 
-- 
GitLab