@@ -40,6 +40,7 @@ use tauri_utils::PackageInfo;
40
40
41
41
use std:: {
42
42
collections:: HashMap ,
43
+ fmt,
43
44
path:: { Path , PathBuf } ,
44
45
sync:: { mpsc:: Sender , Arc , Weak } ,
45
46
} ;
@@ -526,9 +527,10 @@ impl<R: Runtime> ManagerBase<R> for AppHandle<R> {
526
527
///
527
528
/// This type implements [`Manager`] which allows for manipulation of global application items.
528
529
#[ default_runtime( crate :: Wry , wry) ]
529
- #[ derive( Debug ) ]
530
530
pub struct App < R : Runtime > {
531
531
runtime : Option < R > ,
532
+ pending_windows : Option < Vec < PendingWindow < EventLoopMessage , R > > > ,
533
+ setup : Option < SetupHook < R > > ,
532
534
manager : WindowManager < R > ,
533
535
#[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
534
536
global_shortcut_manager : R :: GlobalShortcutManager ,
@@ -537,14 +539,34 @@ pub struct App<R: Runtime> {
537
539
handle : AppHandle < R > ,
538
540
}
539
541
542
+ impl < R : Runtime > fmt:: Debug for App < R > {
543
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
544
+ let mut d = f. debug_struct ( "App" ) ;
545
+ d. field ( "runtime" , & self . runtime )
546
+ . field ( "manager" , & self . manager )
547
+ . field ( "handle" , & self . handle ) ;
548
+
549
+ #[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
550
+ d. field ( "global_shortcut_manager" , & self . global_shortcut_manager ) ;
551
+ #[ cfg( feature = "clipboard" ) ]
552
+ d. field ( "clipboard_manager" , & self . clipboard_manager ) ;
553
+
554
+ d. finish ( )
555
+ }
556
+ }
557
+
540
558
impl < R : Runtime > Manager < R > for App < R > { }
541
559
impl < R : Runtime > ManagerBase < R > for App < R > {
542
560
fn manager ( & self ) -> & WindowManager < R > {
543
561
& self . manager
544
562
}
545
563
546
564
fn runtime ( & self ) -> RuntimeOrDispatch < ' _ , R > {
547
- RuntimeOrDispatch :: Runtime ( self . runtime . as_ref ( ) . unwrap ( ) )
565
+ if let Some ( runtime) = self . runtime . as_ref ( ) {
566
+ RuntimeOrDispatch :: Runtime ( runtime)
567
+ } else {
568
+ self . handle . runtime ( )
569
+ }
548
570
}
549
571
550
572
fn managed_app_handle ( & self ) -> AppHandle < R > {
@@ -775,6 +797,17 @@ impl<R: Runtime> App<R> {
775
797
let app_handle = self . handle ( ) ;
776
798
let manager = self . manager . clone ( ) ;
777
799
self . runtime . take ( ) . unwrap ( ) . run ( move |event| match event {
800
+ RuntimeRunEvent :: Ready => {
801
+ if let Err ( e) = setup ( & mut self ) {
802
+ panic ! ( "Failed to setup app: {}" , e) ;
803
+ }
804
+ on_event_loop_event (
805
+ & app_handle,
806
+ RuntimeRunEvent :: Ready ,
807
+ & manager,
808
+ Some ( & mut callback) ,
809
+ ) ;
810
+ }
778
811
RuntimeRunEvent :: Exit => {
779
812
on_event_loop_event (
780
813
& app_handle,
@@ -1460,8 +1493,11 @@ impl<R: Runtime> Builder<R> {
1460
1493
#[ cfg( feature = "clipboard" ) ]
1461
1494
let clipboard_manager = runtime. clipboard_manager ( ) ;
1462
1495
1496
+ #[ allow( unused_mut) ]
1463
1497
let mut app = App {
1464
1498
runtime : Some ( runtime) ,
1499
+ pending_windows : Some ( self . pending_windows ) ,
1500
+ setup : Some ( self . setup ) ,
1465
1501
manager : manager. clone ( ) ,
1466
1502
#[ cfg( all( desktop, feature = "global-shortcut" ) ) ]
1467
1503
global_shortcut_manager : global_shortcut_manager. clone ( ) ,
@@ -1551,26 +1587,6 @@ impl<R: Runtime> Builder<R> {
1551
1587
1552
1588
app. manager . initialize_plugins ( & app. handle ( ) ) ?;
1553
1589
1554
- let window_labels = self
1555
- . pending_windows
1556
- . iter ( )
1557
- . map ( |p| p. label . clone ( ) )
1558
- . collect :: < Vec < _ > > ( ) ;
1559
-
1560
- for pending in self . pending_windows {
1561
- let pending =
1562
- app
1563
- . manager
1564
- . prepare_window ( app. handle . clone ( ) , pending, & window_labels, None ) ?;
1565
- let detached = app. runtime . as_ref ( ) . unwrap ( ) . create_window ( pending) ?;
1566
- let _window = app. manager . attach_window ( app. handle ( ) , detached) ;
1567
- }
1568
-
1569
- ( self . setup ) ( & mut app) . map_err ( |e| crate :: Error :: Setup ( e. into ( ) ) ) ?;
1570
-
1571
- #[ cfg( updater) ]
1572
- app. run_updater ( ) ;
1573
-
1574
1590
Ok ( app)
1575
1591
}
1576
1592
@@ -1593,6 +1609,38 @@ unsafe impl HasRawDisplayHandle for App {
1593
1609
}
1594
1610
}
1595
1611
1612
+ fn setup < R : Runtime > ( app : & mut App < R > ) -> crate :: Result < ( ) > {
1613
+ let pending_windows = app. pending_windows . take ( ) ;
1614
+ if let Some ( pending_windows) = pending_windows {
1615
+ let window_labels = pending_windows
1616
+ . iter ( )
1617
+ . map ( |p| p. label . clone ( ) )
1618
+ . collect :: < Vec < _ > > ( ) ;
1619
+
1620
+ for pending in pending_windows {
1621
+ let pending =
1622
+ app
1623
+ . manager
1624
+ . prepare_window ( app. handle . clone ( ) , pending, & window_labels, None ) ?;
1625
+ let detached = if let RuntimeOrDispatch :: RuntimeHandle ( runtime) = app. handle ( ) . runtime ( ) {
1626
+ runtime. create_window ( pending) ?
1627
+ } else {
1628
+ // the AppHandle's runtime is always RuntimeOrDispatch::RuntimeHandle
1629
+ unreachable ! ( )
1630
+ } ;
1631
+ let _window = app. manager . attach_window ( app. handle ( ) , detached) ;
1632
+ }
1633
+ }
1634
+
1635
+ if let Some ( setup) = app. setup . take ( ) {
1636
+ ( setup) ( app) . map_err ( |e| crate :: Error :: Setup ( e. into ( ) ) ) ?;
1637
+ }
1638
+
1639
+ #[ cfg( updater) ]
1640
+ app. run_updater ( ) ;
1641
+ Ok ( ( ) )
1642
+ }
1643
+
1596
1644
fn on_event_loop_event < R : Runtime , F : FnMut ( & AppHandle < R > , RunEvent ) + ' static > (
1597
1645
app_handle : & AppHandle < R > ,
1598
1646
event : RuntimeRunEvent < EventLoopMessage > ,
0 commit comments