@@ -169,6 +169,13 @@ impl Application {
169169 self
170170 }
171171
172+ /// Configures when the application should automatically quit.
173+ /// By default, [`QuitMode::Default`] is used.
174+ pub fn with_quit_mode ( self , mode : QuitMode ) -> Self {
175+ self . 0 . borrow_mut ( ) . quit_mode = mode;
176+ self
177+ }
178+
172179 /// Start the application. The provided callback will be called once the
173180 /// app is fully launched.
174181 pub fn run < F > ( self , on_finish_launching : F )
@@ -238,6 +245,18 @@ type WindowClosedHandler = Box<dyn FnMut(&mut App)>;
238245type ReleaseListener = Box < dyn FnOnce ( & mut dyn Any , & mut App ) + ' static > ;
239246type NewEntityListener = Box < dyn FnMut ( AnyEntity , & mut Option < & mut Window > , & mut App ) + ' static > ;
240247
248+ /// Defines when the application should automatically quit.
249+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Default ) ]
250+ pub enum QuitMode {
251+ /// Use [`QuitMode::Explicit`] on macOS and [`QuitMode::LastWindowClosed`] on other platforms.
252+ #[ default]
253+ Default ,
254+ /// Quit automatically when the last window is closed.
255+ LastWindowClosed ,
256+ /// Quit only when requested via [`App::quit`].
257+ Explicit ,
258+ }
259+
241260#[ doc( hidden) ]
242261#[ derive( Clone , PartialEq , Eq ) ]
243262pub struct SystemWindowTab {
@@ -588,6 +607,7 @@ pub struct App {
588607 pub ( crate ) inspector_element_registry : InspectorElementRegistry ,
589608 #[ cfg( any( test, feature = "test-support" , debug_assertions) ) ]
590609 pub ( crate ) name : Option < & ' static str > ,
610+ quit_mode : QuitMode ,
591611 quitting : bool ,
592612}
593613
@@ -659,6 +679,7 @@ impl App {
659679 inspector_renderer : None ,
660680 #[ cfg( any( feature = "inspector" , debug_assertions) ) ]
661681 inspector_element_registry : InspectorElementRegistry :: default ( ) ,
682+ quit_mode : QuitMode :: default ( ) ,
662683 quitting : false ,
663684
664685 #[ cfg( any( test, feature = "test-support" , debug_assertions) ) ]
@@ -1172,6 +1193,12 @@ impl App {
11721193 self . http_client = new_client;
11731194 }
11741195
1196+ /// Configures when the application should automatically quit.
1197+ /// By default, [`QuitMode::Default`] is used.
1198+ pub fn set_quit_mode ( & mut self , mode : QuitMode ) {
1199+ self . quit_mode = mode;
1200+ }
1201+
11751202 /// Returns the SVG renderer used by the application.
11761203 pub fn svg_renderer ( & self ) -> SvgRenderer {
11771204 self . svg_renderer . clone ( )
@@ -1379,6 +1406,16 @@ impl App {
13791406 callback ( cx) ;
13801407 true
13811408 } ) ;
1409+
1410+ let quit_on_empty = match cx. quit_mode {
1411+ QuitMode :: Explicit => false ,
1412+ QuitMode :: LastWindowClosed => true ,
1413+ QuitMode :: Default => !cfg ! ( macos) ,
1414+ } ;
1415+
1416+ if quit_on_empty && cx. windows . is_empty ( ) {
1417+ cx. quit ( ) ;
1418+ }
13821419 } else {
13831420 cx. windows . get_mut ( id) ?. replace ( window) ;
13841421 }
0 commit comments