From 7c10fa503aaf9f16693883cc70f1f36fd297baae Mon Sep 17 00:00:00 2001
From: zhouzihao <1042181618@qq.com>
Date: Tue, 5 Sep 2023 09:54:30 +0800
Subject: [PATCH] =?UTF-8?q?fix-=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=AD=89?=
 =?UTF-8?q?=E5=BE=85=E9=A1=B5=E9=9D=A2=E4=B8=AD=E7=9A=84=E5=85=B3=E9=97=AD?=
 =?UTF-8?q?=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/client/state_manager/menu.rs | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/client/state_manager/menu.rs b/src/client/state_manager/menu.rs
index 771fc3a..e1c65f3 100644
--- a/src/client/state_manager/menu.rs
+++ b/src/client/state_manager/menu.rs
@@ -3,10 +3,10 @@ use std::time::Duration;
 use bevy::{
     app::AppExit,
     prelude::{
-        in_state, Entity, EventWriter, IntoSystemConfigs, NextState, OnEnter, Plugin, Query, Res,
-        ResMut, Resource, States, Update, With,
+        in_state, not, Entity, EventReader, EventWriter, IntoSystemConfigs, NextState, OnEnter,
+        Plugin, Query, Res, ResMut, Resource, States, Update, With,
     },
-    window::{PrimaryWindow, Window},
+    window::{PrimaryWindow, Window, WindowCloseRequested},
 };
 use bevy_egui::{egui, EguiContext, EguiContexts, EguiUserTextures};
 
@@ -47,6 +47,11 @@ impl Plugin for MenuPlugin {
             Update,
             menu_multiplayer.run_if(in_state(MenuState::Multiplayer)),
         );
+
+        app.add_systems(
+            Update,
+            disconnect_on_close_without_connected.run_if(not(in_state(GameState::Game))),
+        );
     }
 }
 
@@ -192,3 +197,12 @@ fn test(
         }
     }
 }
+
+fn disconnect_on_close_without_connected(
+    mut exit: EventWriter<AppExit>,
+    mut closed: EventReader<WindowCloseRequested>,
+) {
+    for _ in closed.iter() {
+        exit.send(AppExit);
+    }
+}