Guard editor shutdown and track clean savepoints
This commit is contained in:
1 parent
62b538999d
commit
4b33f32357
19 files changed
+1944
-129
No files matched your search
@@ -1631,7 +1631,7 @@ pub fn apply_command_undo(world: &mut World) {
|
||||
undo_command(world, &mut command);
|
||||
world.resource_mut::<EditorHistory>().push_redo(command);
|
||||
world.resource_mut::<EditorHistory>().set_redo_status(label);
|
||||
mark_dirty(world);
|
||||
crate::scene_io::reconcile_active_dirty_with_checkpoint(world);
|
||||
}
|
||||
|
||||
pub fn apply_command_redo(world: &mut World) {
|
||||
@@ -1652,7 +1652,7 @@ pub fn apply_command_redo(world: &mut World) {
|
||||
redo_command(world, &mut command);
|
||||
world.resource_mut::<EditorHistory>().push_undo(command);
|
||||
world.resource_mut::<EditorHistory>().set_undo_status(label);
|
||||
mark_dirty(world);
|
||||
crate::scene_io::reconcile_active_dirty_with_checkpoint(world);
|
||||
}
|
||||
|
||||
fn prepare_prefab_source_history(
|
||||
|
||||
@@ -30,6 +30,7 @@ pub use project::project_io;
|
||||
pub use project::samples;
|
||||
pub use project::session;
|
||||
pub use project::settings_ui;
|
||||
pub use project::shutdown;
|
||||
pub use scene::scene_io;
|
||||
pub use scene::scene_schema;
|
||||
pub use scene::scene_view;
|
||||
@@ -76,6 +77,7 @@ use play::PlaySessionPlugin;
|
||||
use project::collaboration::CollaborationPlugin;
|
||||
use project::native_dialog::NativeDialogPlugin;
|
||||
use project::samples::SampleCatalogPlugin;
|
||||
use project::shutdown::ShutdownPlugin;
|
||||
use project_io::ProjectIoPlugin;
|
||||
use render_view::RenderViewPlugin;
|
||||
use scene_io::SceneIoPlugin;
|
||||
@@ -102,6 +104,7 @@ impl PluginGroup for EditorPluginGroup {
|
||||
.add(ProjectIoPlugin)
|
||||
.add(SampleCatalogPlugin)
|
||||
.add(NativeDialogPlugin)
|
||||
.add(ShutdownPlugin)
|
||||
.add(scene_schema::SceneSchemaPlugin)
|
||||
.add(net_editor::NetEditorPlugin)
|
||||
.add(AssetDbPlugin)
|
||||
@@ -143,7 +146,7 @@ impl PluginGroup for EditorPluginGroup {
|
||||
|
||||
/// Shared Bevy app wiring for the in-process editor (game sim + egui shell).
|
||||
pub fn configure_editor_app(app: &mut App) {
|
||||
app.add_plugins(launch::default_plugins("Bevy FPS Editor"))
|
||||
app.add_plugins(launch::editor_plugins("Bevy FPS Editor"))
|
||||
.insert_resource(GameInputEnabled(false))
|
||||
.insert_resource(GameInputFocused(false))
|
||||
.insert_resource(SimEnabled(false))
|
||||
|
||||
@@ -8,3 +8,4 @@ pub mod project_io;
|
||||
pub mod samples;
|
||||
pub mod session;
|
||||
pub mod settings_ui;
|
||||
pub mod shutdown;
|
||||
@@ -11,6 +11,7 @@ use crate::project_io::{ProjectWorkspace, UserPreferences};
|
||||
use crate::scene::recovery::{atomic_write, default_state_root};
|
||||
use crate::scene_io::{SceneIo, SceneIoRequest};
|
||||
use crate::settings_ui::ProjectSettingsPanel;
|
||||
use crate::shutdown::ShutdownCoordinator;
|
||||
use crate::ui::{BrushDiagnosticsPanel, DiagnosticsPanel};
|
||||
use crate::viewport::rendering_diagnostics::RenderingDiagnosticsPanel;
|
||||
use crate::viewport::CameraBookmarks;
|
||||
@@ -109,8 +110,8 @@ impl Plugin for EditorSessionPlugin {
|
||||
.add_systems(Startup, initialize_session)
|
||||
.add_systems(Update, persist_running_session)
|
||||
.add_systems(
|
||||
Last,
|
||||
persist_clean_session_on_exit.after(bevy::window::ExitSystems),
|
||||
crate::shutdown::EditorShutdownFinalize,
|
||||
persist_clean_session_on_exit.after(crate::shutdown::finalize_authorized_shutdown),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -311,6 +312,7 @@ fn persist_running_session(world: &mut World) {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn persist_clean_session_on_exit(
|
||||
mut exits: MessageReader<AppExit>,
|
||||
shutdown: Option<Res<ShutdownCoordinator>>,
|
||||
scene_io: Res<SceneIo>,
|
||||
workspace: Res<ProjectWorkspace>,
|
||||
prefs: Res<UserPreferences>,
|
||||
@@ -323,6 +325,10 @@ fn persist_clean_session_on_exit(
|
||||
if exits.read().next().is_none() {
|
||||
return;
|
||||
}
|
||||
if !shutdown.is_some_and(|shutdown| shutdown.authorized_exit_sent()) {
|
||||
warn!("Ignoring an unauthorized AppExit for clean-session persistence");
|
||||
return;
|
||||
}
|
||||
let Some(path) = session_path() else {
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,752 @@
|
||||
//! Guarded editor shutdown and the handoff to scene persistence.
|
||||
|
||||
use bevy::app::{AppExit, MainScheduleOrder};
|
||||
use bevy::ecs::schedule::ScheduleLabel;
|
||||
use bevy::prelude::*;
|
||||
use bevy::window::{PrimaryWindow, WindowCloseRequested};
|
||||
|
||||
use crate::native_dialog::NativeDialogBroker;
|
||||
use crate::scene_io::SceneIo;
|
||||
|
||||
const SAVE_ALL_LABEL: &str = "Save All";
|
||||
const DISCARD_LABEL: &str = "Discard";
|
||||
const CANCEL_LABEL: &str = "Cancel";
|
||||
|
||||
/// The user or editor surface that initiated a guarded shutdown.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ShutdownSource {
|
||||
NativeWindow,
|
||||
FileMenu,
|
||||
Programmatic,
|
||||
SwitchProject,
|
||||
}
|
||||
|
||||
/// Observable state of the single editor shutdown flow.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum ShutdownIntent {
|
||||
#[default]
|
||||
Idle,
|
||||
WaitingForBroker {
|
||||
source: ShutdownSource,
|
||||
},
|
||||
Confirming {
|
||||
source: ShutdownSource,
|
||||
},
|
||||
Saving {
|
||||
source: ShutdownSource,
|
||||
},
|
||||
Authorized {
|
||||
source: ShutdownSource,
|
||||
require_clean_scenes: bool,
|
||||
},
|
||||
ExitSent {
|
||||
source: ShutdownSource,
|
||||
},
|
||||
}
|
||||
|
||||
impl ShutdownIntent {
|
||||
pub fn source(self) -> Option<ShutdownSource> {
|
||||
match self {
|
||||
Self::Idle => None,
|
||||
Self::WaitingForBroker { source }
|
||||
| Self::Confirming { source }
|
||||
| Self::Saving { source }
|
||||
| Self::Authorized { source, .. }
|
||||
| Self::ExitSent { source } => Some(source),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Coordinates all editor exit requests so only an authorized flow emits [`AppExit`].
|
||||
#[derive(Resource, Debug, Default)]
|
||||
pub struct ShutdownCoordinator {
|
||||
intent: ShutdownIntent,
|
||||
coalesced_requests: u32,
|
||||
last_blocker: Option<String>,
|
||||
}
|
||||
|
||||
impl ShutdownCoordinator {
|
||||
pub fn intent(&self) -> ShutdownIntent {
|
||||
self.intent
|
||||
}
|
||||
|
||||
pub fn coalesced_requests(&self) -> u32 {
|
||||
self.coalesced_requests
|
||||
}
|
||||
|
||||
pub fn last_blocker(&self) -> Option<&str> {
|
||||
self.last_blocker.as_deref()
|
||||
}
|
||||
|
||||
pub fn authorized_exit_sent(&self) -> bool {
|
||||
matches!(self.intent, ShutdownIntent::ExitSent { .. })
|
||||
}
|
||||
|
||||
/// Queue a shutdown if no shutdown flow is already active.
|
||||
///
|
||||
/// Repeated window, menu, or programmatic requests are coalesced until the
|
||||
/// current flow is cancelled, fails to save, or sends the authorized exit.
|
||||
pub fn request(&mut self, source: ShutdownSource) -> bool {
|
||||
if self.intent != ShutdownIntent::Idle {
|
||||
self.coalesced_requests = self.coalesced_requests.saturating_add(1);
|
||||
return false;
|
||||
}
|
||||
self.intent = ShutdownIntent::WaitingForBroker { source };
|
||||
self.coalesced_requests = 0;
|
||||
self.last_blocker = None;
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn set_intent_for_test(&mut self, intent: ShutdownIntent) {
|
||||
self.intent = intent;
|
||||
}
|
||||
}
|
||||
|
||||
/// Emitted once when the guarded dialog chooses `Save All`.
|
||||
///
|
||||
/// Scene persistence owns the actual save sequence and must finish it with
|
||||
/// [`complete_shutdown_save`].
|
||||
#[derive(Message, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ShutdownSaveAllRequested {
|
||||
pub source: ShutdownSource,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum ShutdownSaveOutcome {
|
||||
Saved,
|
||||
Cancelled,
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ShutdownDecision {
|
||||
SaveAll,
|
||||
Discard,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
/// Runs after Bevy's `Last` schedule so no editor authoring system can mutate
|
||||
/// a document after the final dirty-state check in the same frame.
|
||||
#[derive(ScheduleLabel, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct EditorShutdownFinalize;
|
||||
|
||||
/// Queue a guarded shutdown from an exclusive-world UI or workflow callback.
|
||||
pub fn request_editor_shutdown(world: &mut World, source: ShutdownSource) -> bool {
|
||||
world
|
||||
.get_resource_mut::<ShutdownCoordinator>()
|
||||
.is_some_and(|mut coordinator| coordinator.request(source))
|
||||
}
|
||||
|
||||
/// Queue a project-browser handoff through the same dirty-document guard as editor exit.
|
||||
pub fn request_project_switch(world: &mut World) -> bool {
|
||||
request_editor_shutdown(world, ShutdownSource::SwitchProject)
|
||||
}
|
||||
|
||||
/// Resolve the dirty-scene confirmation through the shared coordinator.
|
||||
pub fn resolve_shutdown_decision(world: &mut World, decision: ShutdownDecision) -> bool {
|
||||
let (save_request, status) = {
|
||||
let Some(mut coordinator) = world.get_resource_mut::<ShutdownCoordinator>() else {
|
||||
return false;
|
||||
};
|
||||
let ShutdownIntent::Confirming { source } = coordinator.intent else {
|
||||
return false;
|
||||
};
|
||||
match decision {
|
||||
ShutdownDecision::SaveAll => {
|
||||
coordinator.intent = ShutdownIntent::Saving { source };
|
||||
(
|
||||
Some(source),
|
||||
Some("Saving all modified scene tabs before shutdown"),
|
||||
)
|
||||
}
|
||||
ShutdownDecision::Discard => {
|
||||
coordinator.intent = ShutdownIntent::Authorized {
|
||||
source,
|
||||
require_clean_scenes: false,
|
||||
};
|
||||
(None, None)
|
||||
}
|
||||
ShutdownDecision::Cancel => {
|
||||
coordinator.intent = ShutdownIntent::Idle;
|
||||
(
|
||||
None,
|
||||
Some("Shutdown cancelled; unsaved scene tabs retained"),
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(status) = status {
|
||||
if let Some(mut scene_io) = world.get_resource_mut::<SceneIo>() {
|
||||
scene_io.set_status(status);
|
||||
}
|
||||
}
|
||||
if let Some(source) = save_request {
|
||||
world.write_message(ShutdownSaveAllRequested { source });
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Finish the save-all handoff. A reported success is authorized only after
|
||||
/// every scene tab is observably clean.
|
||||
pub fn complete_shutdown_save(world: &mut World, outcome: ShutdownSaveOutcome) -> bool {
|
||||
let scenes_are_clean = !scene_has_unsaved_changes(world);
|
||||
let Some(mut coordinator) = world.get_resource_mut::<ShutdownCoordinator>() else {
|
||||
return false;
|
||||
};
|
||||
let ShutdownIntent::Saving { source } = coordinator.intent else {
|
||||
return false;
|
||||
};
|
||||
|
||||
match outcome {
|
||||
ShutdownSaveOutcome::Saved if scenes_are_clean => {
|
||||
coordinator.intent = ShutdownIntent::Authorized {
|
||||
source,
|
||||
require_clean_scenes: true,
|
||||
};
|
||||
coordinator.last_blocker = None;
|
||||
true
|
||||
}
|
||||
ShutdownSaveOutcome::Saved => {
|
||||
coordinator.intent = ShutdownIntent::Idle;
|
||||
coordinator.last_blocker =
|
||||
Some("Save All completed while unsaved scene tabs remain".into());
|
||||
false
|
||||
}
|
||||
ShutdownSaveOutcome::Cancelled => {
|
||||
coordinator.intent = ShutdownIntent::Idle;
|
||||
coordinator.last_blocker = None;
|
||||
false
|
||||
}
|
||||
ShutdownSaveOutcome::Failed(error) => {
|
||||
coordinator.intent = ShutdownIntent::Idle;
|
||||
coordinator.last_blocker = Some(error);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ShutdownPlugin;
|
||||
|
||||
impl Plugin for ShutdownPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_schedule(EditorShutdownFinalize);
|
||||
app.world_mut()
|
||||
.resource_mut::<MainScheduleOrder>()
|
||||
.insert_after(Last, EditorShutdownFinalize);
|
||||
app.init_resource::<ShutdownCoordinator>()
|
||||
.add_message::<ShutdownSaveAllRequested>()
|
||||
.add_systems(
|
||||
Update,
|
||||
(capture_native_close_requests, drive_shutdown).chain(),
|
||||
)
|
||||
.add_systems(EditorShutdownFinalize, finalize_authorized_shutdown);
|
||||
}
|
||||
}
|
||||
|
||||
fn capture_native_close_requests(
|
||||
mut commands: Commands,
|
||||
mut close_requests: MessageReader<WindowCloseRequested>,
|
||||
primary_window: Query<Entity, With<PrimaryWindow>>,
|
||||
mut coordinator: ResMut<ShutdownCoordinator>,
|
||||
) {
|
||||
let Ok(primary_window) = primary_window.single() else {
|
||||
return;
|
||||
};
|
||||
for request in close_requests.read() {
|
||||
if request.window == primary_window {
|
||||
coordinator.request(ShutdownSource::NativeWindow);
|
||||
} else {
|
||||
commands.entity(request.window).despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn drive_shutdown(world: &mut World) {
|
||||
let intent = world.resource::<ShutdownCoordinator>().intent;
|
||||
match intent {
|
||||
ShutdownIntent::Idle
|
||||
| ShutdownIntent::Confirming { .. }
|
||||
| ShutdownIntent::Saving { .. }
|
||||
| ShutdownIntent::ExitSent { .. } => {}
|
||||
ShutdownIntent::WaitingForBroker { source } => {
|
||||
let unsaved_count = unsaved_scene_count(world);
|
||||
if unsaved_count == 0 {
|
||||
world.resource_mut::<ShutdownCoordinator>().intent = ShutdownIntent::Authorized {
|
||||
source,
|
||||
require_clean_scenes: true,
|
||||
};
|
||||
return;
|
||||
}
|
||||
if world.resource::<NativeDialogBroker>().is_pending() {
|
||||
return;
|
||||
}
|
||||
|
||||
let action = if source == ShutdownSource::SwitchProject {
|
||||
"switching projects"
|
||||
} else {
|
||||
"quitting"
|
||||
};
|
||||
let description = if unsaved_count == 1 {
|
||||
format!("One scene tab has unsaved changes. Save it before {action}?")
|
||||
} else {
|
||||
format!(
|
||||
"{unsaved_count} scene tabs have unsaved changes. Save all before {action}?"
|
||||
)
|
||||
};
|
||||
let title = if source == ShutdownSource::SwitchProject {
|
||||
"Switch Project"
|
||||
} else {
|
||||
"Quit Blacksite Editor"
|
||||
};
|
||||
let request = world.resource::<NativeDialogBroker>().request(
|
||||
move || {
|
||||
rfd::MessageDialog::new()
|
||||
.set_title(title)
|
||||
.set_description(description)
|
||||
.set_level(rfd::MessageLevel::Warning)
|
||||
.set_buttons(rfd::MessageButtons::YesNoCancelCustom(
|
||||
SAVE_ALL_LABEL.into(),
|
||||
DISCARD_LABEL.into(),
|
||||
CANCEL_LABEL.into(),
|
||||
))
|
||||
.show()
|
||||
},
|
||||
|world, result| {
|
||||
resolve_shutdown_decision(world, decision_from_dialog(result));
|
||||
},
|
||||
);
|
||||
if request.is_ok() {
|
||||
world.resource_mut::<ShutdownCoordinator>().intent =
|
||||
ShutdownIntent::Confirming { source };
|
||||
}
|
||||
}
|
||||
ShutdownIntent::Authorized { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Final dirty-state recheck after editor UI and authoring schedules have completed.
|
||||
pub(crate) fn finalize_authorized_shutdown(world: &mut World) {
|
||||
let (source, require_clean_scenes) = {
|
||||
let coordinator = world.resource::<ShutdownCoordinator>();
|
||||
let ShutdownIntent::Authorized {
|
||||
source,
|
||||
require_clean_scenes,
|
||||
} = coordinator.intent
|
||||
else {
|
||||
return;
|
||||
};
|
||||
(source, require_clean_scenes)
|
||||
};
|
||||
if require_clean_scenes && scene_has_unsaved_changes(world) {
|
||||
world.resource_mut::<ShutdownCoordinator>().intent =
|
||||
ShutdownIntent::WaitingForBroker { source };
|
||||
return;
|
||||
}
|
||||
if source == ShutdownSource::SwitchProject {
|
||||
if let Err(error) = crate::launcher::spawn_project_launcher_process() {
|
||||
let message = format!("Switch project failed: {error}");
|
||||
{
|
||||
let mut coordinator = world.resource_mut::<ShutdownCoordinator>();
|
||||
coordinator.intent = ShutdownIntent::Idle;
|
||||
coordinator.last_blocker = Some(message.clone());
|
||||
}
|
||||
if let Some(mut scene_io) = world.get_resource_mut::<SceneIo>() {
|
||||
scene_io.set_status(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
world.resource_mut::<ShutdownCoordinator>().intent = ShutdownIntent::ExitSent { source };
|
||||
debug!(?source, "Editor shutdown authorized");
|
||||
world.write_message(AppExit::Success);
|
||||
}
|
||||
|
||||
fn scene_has_unsaved_changes(world: &World) -> bool {
|
||||
world
|
||||
.get_resource::<SceneIo>()
|
||||
.is_some_and(SceneIo::has_unsaved_tabs)
|
||||
}
|
||||
|
||||
fn unsaved_scene_count(world: &World) -> usize {
|
||||
let Some(scene_io) = world.get_resource::<SceneIo>() else {
|
||||
return 0;
|
||||
};
|
||||
let tab_count = scene_io.tabs.iter().filter(|tab| tab.dirty).count();
|
||||
if scene_io.dirty
|
||||
&& scene_io
|
||||
.tabs
|
||||
.get(scene_io.active_tab)
|
||||
.is_none_or(|tab| !tab.dirty)
|
||||
{
|
||||
tab_count + 1
|
||||
} else {
|
||||
tab_count
|
||||
}
|
||||
}
|
||||
|
||||
fn decision_from_dialog(result: rfd::MessageDialogResult) -> ShutdownDecision {
|
||||
match result {
|
||||
rfd::MessageDialogResult::Custom(label) if label == SAVE_ALL_LABEL => {
|
||||
ShutdownDecision::SaveAll
|
||||
}
|
||||
rfd::MessageDialogResult::Custom(label) if label == DISCARD_LABEL => {
|
||||
ShutdownDecision::Discard
|
||||
}
|
||||
rfd::MessageDialogResult::Yes => ShutdownDecision::SaveAll,
|
||||
rfd::MessageDialogResult::No => ShutdownDecision::Discard,
|
||||
_ => ShutdownDecision::Cancel,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::mpsc;
|
||||
|
||||
use bevy::ecs::message::MessageCursor;
|
||||
|
||||
use super::*;
|
||||
use crate::native_dialog::NativeDialogPlugin;
|
||||
|
||||
fn confirming_world() -> World {
|
||||
let mut world = World::new();
|
||||
world.init_resource::<Messages<ShutdownSaveAllRequested>>();
|
||||
world.insert_resource(ShutdownCoordinator {
|
||||
intent: ShutdownIntent::Confirming {
|
||||
source: ShutdownSource::FileMenu,
|
||||
},
|
||||
..default()
|
||||
});
|
||||
world
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_requests_are_coalesced_behind_the_first_source() {
|
||||
let mut coordinator = ShutdownCoordinator::default();
|
||||
|
||||
assert!(coordinator.request(ShutdownSource::NativeWindow));
|
||||
assert!(!coordinator.request(ShutdownSource::FileMenu));
|
||||
assert_eq!(coordinator.coalesced_requests(), 1);
|
||||
assert_eq!(
|
||||
coordinator.intent(),
|
||||
ShutdownIntent::WaitingForBroker {
|
||||
source: ShutdownSource::NativeWindow
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_returns_to_idle_and_discard_authorizes_exit() {
|
||||
let mut cancel_world = confirming_world();
|
||||
assert!(resolve_shutdown_decision(
|
||||
&mut cancel_world,
|
||||
ShutdownDecision::Cancel
|
||||
));
|
||||
assert_eq!(
|
||||
cancel_world.resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::Idle
|
||||
);
|
||||
|
||||
let mut discard_world = confirming_world();
|
||||
assert!(resolve_shutdown_decision(
|
||||
&mut discard_world,
|
||||
ShutdownDecision::Discard
|
||||
));
|
||||
assert_eq!(
|
||||
discard_world.resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::Authorized {
|
||||
source: ShutdownSource::FileMenu,
|
||||
require_clean_scenes: false,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dirty_cancel_preserves_the_session_and_discard_emits_one_exit() {
|
||||
let mut app = App::new();
|
||||
app.add_message::<AppExit>()
|
||||
.init_resource::<SceneIo>()
|
||||
.init_resource::<ShutdownCoordinator>();
|
||||
app.world_mut().resource_mut::<SceneIo>().mark_dirty();
|
||||
app.world_mut()
|
||||
.resource_mut::<ShutdownCoordinator>()
|
||||
.set_intent_for_test(ShutdownIntent::Confirming {
|
||||
source: ShutdownSource::Programmatic,
|
||||
});
|
||||
|
||||
assert!(resolve_shutdown_decision(
|
||||
app.world_mut(),
|
||||
ShutdownDecision::Cancel
|
||||
));
|
||||
drive_shutdown(app.world_mut());
|
||||
finalize_authorized_shutdown(app.world_mut());
|
||||
assert_eq!(app.should_exit(), None);
|
||||
assert!(app.world().resource::<SceneIo>().has_unsaved_tabs());
|
||||
|
||||
app.world_mut()
|
||||
.resource_mut::<ShutdownCoordinator>()
|
||||
.set_intent_for_test(ShutdownIntent::Confirming {
|
||||
source: ShutdownSource::Programmatic,
|
||||
});
|
||||
assert!(resolve_shutdown_decision(
|
||||
app.world_mut(),
|
||||
ShutdownDecision::Discard
|
||||
));
|
||||
drive_shutdown(app.world_mut());
|
||||
finalize_authorized_shutdown(app.world_mut());
|
||||
assert_eq!(app.should_exit(), Some(AppExit::Success));
|
||||
assert!(app.world().resource::<SceneIo>().has_unsaved_tabs());
|
||||
assert!(app
|
||||
.world()
|
||||
.resource::<ShutdownCoordinator>()
|
||||
.authorized_exit_sent());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_menu_and_programmatic_clean_exit_use_the_same_coordinator_path() {
|
||||
for source in [ShutdownSource::FileMenu, ShutdownSource::Programmatic] {
|
||||
let mut app = App::new();
|
||||
app.add_message::<AppExit>()
|
||||
.init_resource::<SceneIo>()
|
||||
.init_resource::<ShutdownCoordinator>()
|
||||
.init_resource::<NativeDialogBroker>();
|
||||
|
||||
assert!(request_editor_shutdown(app.world_mut(), source));
|
||||
drive_shutdown(app.world_mut());
|
||||
finalize_authorized_shutdown(app.world_mut());
|
||||
|
||||
assert_eq!(app.should_exit(), Some(AppExit::Success));
|
||||
assert_eq!(
|
||||
app.world().resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::ExitSent { source }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_all_emits_one_persistence_handoff() {
|
||||
let mut world = confirming_world();
|
||||
let mut cursor = MessageCursor::<ShutdownSaveAllRequested>::default();
|
||||
|
||||
assert!(resolve_shutdown_decision(
|
||||
&mut world,
|
||||
ShutdownDecision::SaveAll
|
||||
));
|
||||
assert!(!resolve_shutdown_decision(
|
||||
&mut world,
|
||||
ShutdownDecision::SaveAll
|
||||
));
|
||||
|
||||
let messages = world.resource::<Messages<ShutdownSaveAllRequested>>();
|
||||
let requests: Vec<_> = cursor.read(messages).copied().collect();
|
||||
assert_eq!(
|
||||
requests,
|
||||
vec![ShutdownSaveAllRequested {
|
||||
source: ShutdownSource::FileMenu
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_native_close_emits_the_authorized_exit() {
|
||||
let mut app = App::new();
|
||||
app.add_message::<WindowCloseRequested>()
|
||||
.add_plugins((NativeDialogPlugin, ShutdownPlugin))
|
||||
.init_resource::<SceneIo>();
|
||||
let window = app.world_mut().spawn(PrimaryWindow).id();
|
||||
app.world_mut()
|
||||
.write_message(WindowCloseRequested { window });
|
||||
|
||||
app.update();
|
||||
|
||||
assert_eq!(app.should_exit(), Some(AppExit::Success));
|
||||
assert_eq!(
|
||||
app.world().resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::ExitSent {
|
||||
source: ShutdownSource::NativeWindow
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn secondary_window_close_does_not_request_editor_shutdown() {
|
||||
let mut app = App::new();
|
||||
app.add_message::<WindowCloseRequested>()
|
||||
.add_plugins((NativeDialogPlugin, ShutdownPlugin))
|
||||
.init_resource::<SceneIo>();
|
||||
app.world_mut().spawn(PrimaryWindow);
|
||||
let secondary = app.world_mut().spawn_empty().id();
|
||||
app.world_mut()
|
||||
.write_message(WindowCloseRequested { window: secondary });
|
||||
|
||||
app.update();
|
||||
|
||||
assert_eq!(app.should_exit(), None);
|
||||
assert_eq!(
|
||||
app.world().resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::Idle
|
||||
);
|
||||
assert!(app.world().get_entity(secondary).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn occupied_dialog_broker_keeps_shutdown_waiting() {
|
||||
let mut app = App::new();
|
||||
app.add_message::<WindowCloseRequested>()
|
||||
.add_plugins((NativeDialogPlugin, ShutdownPlugin))
|
||||
.init_resource::<SceneIo>();
|
||||
app.world_mut().resource_mut::<SceneIo>().mark_dirty();
|
||||
let (release_sender, release_receiver) = mpsc::channel();
|
||||
app.world()
|
||||
.resource::<NativeDialogBroker>()
|
||||
.request(
|
||||
move || release_receiver.recv().expect("release sender dropped"),
|
||||
|_, _: ()| {},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(request_editor_shutdown(
|
||||
app.world_mut(),
|
||||
ShutdownSource::Programmatic
|
||||
));
|
||||
|
||||
app.update();
|
||||
|
||||
assert_eq!(
|
||||
app.world().resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::WaitingForBroker {
|
||||
source: ShutdownSource::Programmatic
|
||||
}
|
||||
);
|
||||
release_sender.send(()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn successful_save_requires_every_scene_tab_to_be_clean() {
|
||||
let mut world = World::new();
|
||||
world.init_resource::<SceneIo>();
|
||||
world.insert_resource(ShutdownCoordinator {
|
||||
intent: ShutdownIntent::Saving {
|
||||
source: ShutdownSource::Programmatic,
|
||||
},
|
||||
..default()
|
||||
});
|
||||
|
||||
assert!(complete_shutdown_save(
|
||||
&mut world,
|
||||
ShutdownSaveOutcome::Saved
|
||||
));
|
||||
assert_eq!(
|
||||
world.resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::Authorized {
|
||||
source: ShutdownSource::Programmatic,
|
||||
require_clean_scenes: true,
|
||||
}
|
||||
);
|
||||
|
||||
world.resource_mut::<SceneIo>().mark_dirty();
|
||||
world.resource_mut::<ShutdownCoordinator>().intent = ShutdownIntent::Saving {
|
||||
source: ShutdownSource::Programmatic,
|
||||
};
|
||||
assert!(!complete_shutdown_save(
|
||||
&mut world,
|
||||
ShutdownSaveOutcome::Saved
|
||||
));
|
||||
assert_eq!(
|
||||
world.resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::Idle
|
||||
);
|
||||
assert!(world
|
||||
.resource::<ShutdownCoordinator>()
|
||||
.last_blocker()
|
||||
.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_new_edit_after_save_reopens_the_guard_instead_of_exiting() {
|
||||
let mut world = World::new();
|
||||
world.init_resource::<Messages<AppExit>>();
|
||||
world.init_resource::<SceneIo>();
|
||||
world.insert_resource(ShutdownCoordinator {
|
||||
intent: ShutdownIntent::Authorized {
|
||||
source: ShutdownSource::Programmatic,
|
||||
require_clean_scenes: true,
|
||||
},
|
||||
..default()
|
||||
});
|
||||
world.resource_mut::<SceneIo>().mark_dirty();
|
||||
|
||||
finalize_authorized_shutdown(&mut world);
|
||||
|
||||
assert_eq!(
|
||||
world.resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::WaitingForBroker {
|
||||
source: ShutdownSource::Programmatic
|
||||
}
|
||||
);
|
||||
let messages = world.resource::<Messages<AppExit>>();
|
||||
assert_eq!(
|
||||
MessageCursor::<AppExit>::default().read(messages).count(),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn last_schedule_edit_is_seen_before_final_exit_authorization() {
|
||||
let mut app = App::new();
|
||||
app.add_message::<WindowCloseRequested>()
|
||||
.add_plugins((NativeDialogPlugin, ShutdownPlugin))
|
||||
.init_resource::<SceneIo>()
|
||||
.add_systems(Last, |mut scene_io: ResMut<SceneIo>| {
|
||||
scene_io.mark_dirty();
|
||||
});
|
||||
assert!(request_editor_shutdown(
|
||||
app.world_mut(),
|
||||
ShutdownSource::Programmatic
|
||||
));
|
||||
|
||||
app.update();
|
||||
|
||||
assert_eq!(app.should_exit(), None);
|
||||
assert!(app.world().resource::<SceneIo>().has_unsaved_tabs());
|
||||
assert_eq!(
|
||||
app.world().resource::<ShutdownCoordinator>().intent(),
|
||||
ShutdownIntent::WaitingForBroker {
|
||||
source: ShutdownSource::Programmatic
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_dialog_labels_map_to_shutdown_decisions() {
|
||||
assert_eq!(
|
||||
decision_from_dialog(rfd::MessageDialogResult::Custom(SAVE_ALL_LABEL.into())),
|
||||
ShutdownDecision::SaveAll
|
||||
);
|
||||
assert_eq!(
|
||||
decision_from_dialog(rfd::MessageDialogResult::Custom(DISCARD_LABEL.into())),
|
||||
ShutdownDecision::Discard
|
||||
);
|
||||
assert_eq!(
|
||||
decision_from_dialog(rfd::MessageDialogResult::Custom(CANCEL_LABEL.into())),
|
||||
ShutdownDecision::Cancel
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_final_coordinator_state_authorizes_clean_session_persistence() {
|
||||
let mut coordinator = ShutdownCoordinator::default();
|
||||
assert!(!coordinator.authorized_exit_sent());
|
||||
|
||||
coordinator.intent = ShutdownIntent::Authorized {
|
||||
source: ShutdownSource::Programmatic,
|
||||
require_clean_scenes: true,
|
||||
};
|
||||
assert!(!coordinator.authorized_exit_sent());
|
||||
|
||||
coordinator.intent = ShutdownIntent::ExitSent {
|
||||
source: ShutdownSource::Programmatic,
|
||||
};
|
||||
assert!(coordinator.authorized_exit_sent());
|
||||
}
|
||||
}
|
||||
+963
-106
File diff suppressed because it is too large.
Load diff
@@ -9,6 +9,7 @@ use crate::history::{apply_command_redo, apply_command_undo, EditorHistory};
|
||||
use crate::project::samples::{SampleCatalog, SampleCatalogEntry};
|
||||
use crate::scene_io::{SceneIo, SceneIoRequest};
|
||||
use crate::settings_ui::{open_project_settings_panel, ProjectSettingsPanel};
|
||||
use crate::shutdown::{request_editor_shutdown, ShutdownSource};
|
||||
use crate::state::{EditorMode, PlayPaused};
|
||||
|
||||
use super::diagnostics::{BrushDiagnosticsPanel, DiagnosticsPanel};
|
||||
@@ -148,6 +149,11 @@ pub fn top_menu_bar(
|
||||
}
|
||||
});
|
||||
}
|
||||
ui.separator();
|
||||
if menu_item(ui, "Quit", None, true).clicked() {
|
||||
request_editor_shutdown(world, ShutdownSource::FileMenu);
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
|
||||
ui.menu_button("Edit", |ui| {
|
||||
|
||||
Reference in new issue
Block a user