 f45e78e658
			
		
	
	
		f45e78e658
		
			
		
	
	
	
	
		
			
			# Objective - Contributes to #15460 ## Solution - Added the following features: - `std` (default) - `bevy_tasks` (default) - `downcast ` (default) - `portable-atomic` - `critical-section` - `downcast` and `bevy_tasks` are now optional dependencies for `bevy_app`. ## Testing - CI - Personal UEFI and Raspberry Pi Pico demo applications compile and run against this branch ## Draft Release Notes Bevy's application framework now supports `no_std` platforms. Following up on `bevy_ecs` gaining `no_std` support, `bevy_app` extends the functionality available on these targets to include the powerful `App` and `Plugin` abstractions. With this, library authors now have the option of making their plugins `no_std` compatible, or even offering plugins specifically to improve Bevy on certain embedded platforms! To start making a `no_std` compatible plugin, simply disable default features when including `bevy_app`: ```toml [dependencies] bevy_app = { version = "0.16", default-features = false } ``` We encourage library authors to do this anyway, as it can also help with compile times and binary size on all platforms. Keep an eye out for future `no_std` updates as we continue to improve the parity between `std` and `no_std`. We look forward to seeing what kinds of applications are now possible with Bevy! ## Notes - `downcast-rs` is optional as it isn't compatible with `portable-atomic`. I will investigate making a PR upstream to add support for this functionality, as it should be very straightforward. - In line with the `bevy_ecs` no-std-ification, I've added documentation to all features, and grouped them as well. - ~~Creating this PR in draft while CI runs and so I can polish before review.~~ --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
		
			
				
	
	
		
			74 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| use core::sync::atomic::{AtomicBool, Ordering};
 | |
| 
 | |
| use bevy_ecs::event::EventWriter;
 | |
| 
 | |
| use crate::{App, AppExit, Plugin, Update};
 | |
| 
 | |
| pub use ctrlc;
 | |
| 
 | |
| /// Indicates that all [`App`]'s should exit.
 | |
| static SHOULD_EXIT: AtomicBool = AtomicBool::new(false);
 | |
| 
 | |
| /// Gracefully handles `Ctrl+C` by emitting a [`AppExit`] event. This plugin is part of the `DefaultPlugins`.
 | |
| ///
 | |
| /// ```no_run
 | |
| /// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup, TerminalCtrlCHandlerPlugin};
 | |
| /// fn main() {
 | |
| ///     App::new()
 | |
| ///         .add_plugins(MinimalPlugins)
 | |
| ///         .add_plugins(TerminalCtrlCHandlerPlugin)
 | |
| ///         .run();
 | |
| /// }
 | |
| /// ```
 | |
| ///
 | |
| /// If you want to setup your own `Ctrl+C` handler, you should call the
 | |
| /// [`TerminalCtrlCHandlerPlugin::gracefully_exit`] function in your handler if you want bevy to gracefully exit.
 | |
| /// ```no_run
 | |
| /// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, TerminalCtrlCHandlerPlugin, ctrlc};
 | |
| /// fn main() {
 | |
| ///     // Your own `Ctrl+C` handler
 | |
| ///     ctrlc::set_handler(move || {
 | |
| ///         // Other clean up code ...
 | |
| ///
 | |
| ///         TerminalCtrlCHandlerPlugin::gracefully_exit();
 | |
| ///     });
 | |
| ///
 | |
| ///     App::new()
 | |
| ///         .add_plugins(DefaultPlugins)
 | |
| ///         .run();
 | |
| /// }
 | |
| /// ```
 | |
| #[derive(Default)]
 | |
| pub struct TerminalCtrlCHandlerPlugin;
 | |
| 
 | |
| impl TerminalCtrlCHandlerPlugin {
 | |
|     /// Sends the [`AppExit`] event to all apps using this plugin to make them gracefully exit.
 | |
|     pub fn gracefully_exit() {
 | |
|         SHOULD_EXIT.store(true, Ordering::Relaxed);
 | |
|     }
 | |
| 
 | |
|     /// Sends a [`AppExit`] event when the user presses `Ctrl+C` on the terminal.
 | |
|     pub fn exit_on_flag(mut events: EventWriter<AppExit>) {
 | |
|         if SHOULD_EXIT.load(Ordering::Relaxed) {
 | |
|             events.send(AppExit::from_code(130));
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| impl Plugin for TerminalCtrlCHandlerPlugin {
 | |
|     fn build(&self, app: &mut App) {
 | |
|         let result = ctrlc::try_set_handler(move || {
 | |
|             Self::gracefully_exit();
 | |
|         });
 | |
|         match result {
 | |
|             Ok(()) => {}
 | |
|             Err(ctrlc::Error::MultipleHandlers) => {
 | |
|                 log::info!("Skipping installing `Ctrl+C` handler as one was already installed. Please call `TerminalCtrlCHandlerPlugin::gracefully_exit` in your own `Ctrl+C` handler if you want Bevy to gracefully exit on `Ctrl+C`.");
 | |
|             }
 | |
|             Err(err) => log::warn!("Failed to set `Ctrl+C` handler: {err}"),
 | |
|         }
 | |
| 
 | |
|         app.add_systems(Update, TerminalCtrlCHandlerPlugin::exit_on_flag);
 | |
|     }
 | |
| }
 |