@@ -63,7 +63,7 @@ use self::search::ProjectSearchResult;
63
63
mod search;
64
64
65
65
mod session;
66
- use session:: { auto_save_subscription, AutoSaveEvent } ;
66
+ use session:: { auto_save_subscription, AutoSaveMessage } ;
67
67
68
68
use self :: tab:: { EditorTab , GitDiffTab , Tab } ;
69
69
mod tab;
@@ -307,7 +307,7 @@ impl PartialEq for WatcherWrapper {
307
307
#[ derive( Clone , Debug ) ]
308
308
pub enum Message {
309
309
AppTheme ( AppTheme ) ,
310
- AutoSaveSender ( futures:: channel:: mpsc:: Sender < AutoSaveEvent > ) ,
310
+ AutoSaveSender ( futures:: channel:: mpsc:: Sender < AutoSaveMessage > ) ,
311
311
AutoSaveTimeout ( Option < NonZeroU64 > ) ,
312
312
Config ( Config ) ,
313
313
ConfigState ( ConfigState ) ,
@@ -437,7 +437,7 @@ pub struct App {
437
437
project_search_value : String ,
438
438
project_search_result : Option < ProjectSearchResult > ,
439
439
watcher_opt : Option < notify:: RecommendedWatcher > ,
440
- auto_save_sender : Option < futures:: channel:: mpsc:: Sender < AutoSaveEvent > > ,
440
+ auto_save_sender : Option < futures:: channel:: mpsc:: Sender < AutoSaveMessage > > ,
441
441
modifiers : Modifiers ,
442
442
}
443
443
@@ -729,7 +729,7 @@ impl App {
729
729
}
730
730
731
731
// Send a message to the auto saver if enabled
732
- fn update_auto_saver ( & mut self , message : AutoSaveEvent ) -> Command < Message > {
732
+ fn update_auto_saver ( & mut self , message : AutoSaveMessage ) -> Command < Message > {
733
733
if let Some ( mut sender) = self
734
734
. config
735
735
// Auto saving is enabled if a timeout is set
@@ -1470,12 +1470,32 @@ impl Application for App {
1470
1470
Message :: AutoSaveTimeout ( timeout) => {
1471
1471
self . config . auto_save_secs = timeout;
1472
1472
if let Some ( timeout) = timeout {
1473
+ let entities: Vec < _ > = self
1474
+ . tab_model
1475
+ . iter ( )
1476
+ . filter_map ( |entity| {
1477
+ self . tab_model . data :: < Tab > ( entity) . map ( |tab| {
1478
+ if let Tab :: Editor ( tab) = tab {
1479
+ tab. changed ( ) . then_some ( entity)
1480
+ } else {
1481
+ None
1482
+ }
1483
+ } )
1484
+ } )
1485
+ . flatten ( )
1486
+ . collect ( ) ;
1487
+
1488
+ // Set new timeout and register all modified tabs.
1473
1489
return Command :: batch ( [
1474
1490
self . save_config ( ) ,
1475
- self . update_auto_saver ( AutoSaveEvent :: UpdateTimeout ( timeout) ) ,
1491
+ self . update_auto_saver ( AutoSaveMessage :: UpdateTimeout ( timeout) ) ,
1492
+ self . update_auto_saver ( AutoSaveMessage :: RegisterBatch ( entities) ) ,
1476
1493
] ) ;
1477
1494
}
1478
- return self . save_config ( ) ;
1495
+ return Command :: batch ( [
1496
+ self . save_config ( ) ,
1497
+ self . update_auto_saver ( AutoSaveMessage :: CancelAll ) ,
1498
+ ] ) ;
1479
1499
}
1480
1500
Message :: Config ( config) => {
1481
1501
if config != self . config {
@@ -1977,13 +1997,19 @@ impl Application for App {
1977
1997
self . tab_model . text_set ( self . tab_model . active ( ) , title) ;
1978
1998
}
1979
1999
1980
- // Remove saved tab from auto saver
2000
+ // Remove saved tab from auto saver to avoid double saves
1981
2001
let entity = self . tab_model . active ( ) ;
1982
- return self . update_auto_saver ( AutoSaveEvent :: Cancel ( entity) ) ;
2002
+ return self . update_auto_saver ( AutoSaveMessage :: Cancel ( entity) ) ;
1983
2003
}
1984
2004
Message :: SaveAny ( entity) => {
2005
+ // TODO: This variant and code should be updated to save backups instead of overwriting
2006
+ // the open file
1985
2007
if let Some ( Tab :: Editor ( tab) ) = self . tab_model . data_mut :: < Tab > ( entity) {
1986
- tab. save ( ) ;
2008
+ let title = tab. title ( ) ;
2009
+ if tab. path_opt . is_some ( ) {
2010
+ tab. save ( ) ;
2011
+ self . tab_model . text_set ( entity, title) ;
2012
+ }
1987
2013
}
1988
2014
}
1989
2015
Message :: SaveAsDialog => {
@@ -2094,7 +2120,7 @@ impl Application for App {
2094
2120
self . tab_model . text_set ( entity, title) ;
2095
2121
// Register tab with the auto saver
2096
2122
if has_path {
2097
- return self . update_auto_saver ( AutoSaveEvent :: Register ( entity) ) ;
2123
+ return self . update_auto_saver ( AutoSaveMessage :: Register ( entity) ) ;
2098
2124
}
2099
2125
}
2100
2126
}
@@ -2156,13 +2182,13 @@ impl Application for App {
2156
2182
entity,
2157
2183
) ) ) ,
2158
2184
self . update_tab ( ) ,
2159
- self . update_auto_saver ( AutoSaveEvent :: Cancel ( entity) ) ,
2185
+ self . update_auto_saver ( AutoSaveMessage :: Cancel ( entity) ) ,
2160
2186
] ) ;
2161
2187
}
2162
2188
2163
2189
return Command :: batch ( [
2164
2190
self . update_tab ( ) ,
2165
- self . update_auto_saver ( AutoSaveEvent :: Cancel ( entity) ) ,
2191
+ self . update_auto_saver ( AutoSaveMessage :: Cancel ( entity) ) ,
2166
2192
] ) ;
2167
2193
}
2168
2194
Message :: TabContextAction ( entity, action) => {
@@ -2172,7 +2198,7 @@ impl Application for App {
2172
2198
// Run action's message
2173
2199
return Command :: batch ( [
2174
2200
self . update ( action. message ( ) ) ,
2175
- self . update_auto_saver ( AutoSaveEvent :: Cancel ( entity) ) ,
2201
+ self . update_auto_saver ( AutoSaveMessage :: Cancel ( entity) ) ,
2176
2202
] ) ;
2177
2203
}
2178
2204
}
@@ -2731,14 +2757,18 @@ impl Application for App {
2731
2757
Some ( dialog) => dialog. subscription ( ) ,
2732
2758
None => subscription:: Subscription :: none ( ) ,
2733
2759
} ,
2734
- match self . config . auto_save_secs {
2735
- Some ( secs) => auto_save_subscription ( secs) . map ( |update| match update {
2736
- AutoSaveEvent :: Ready ( sender) => Message :: AutoSaveSender ( sender) ,
2737
- AutoSaveEvent :: Save ( entity) => Message :: SaveAny ( entity) ,
2738
- _ => unreachable ! ( ) ,
2739
- } ) ,
2740
- None => subscription:: Subscription :: none ( ) ,
2741
- } ,
2760
+ auto_save_subscription (
2761
+ self . config
2762
+ . auto_save_secs
2763
+ // Autosave won't be triggered until the user enables it regardless of passing
2764
+ // a timeout and starting the subscription.
2765
+ . unwrap_or ( NonZeroU64 :: new ( 1 ) . unwrap ( ) ) ,
2766
+ )
2767
+ . map ( |update| match update {
2768
+ AutoSaveMessage :: Ready ( sender) => Message :: AutoSaveSender ( sender) ,
2769
+ AutoSaveMessage :: Save ( entity) => Message :: SaveAny ( entity) ,
2770
+ _ => unreachable ! ( ) ,
2771
+ } ) ,
2742
2772
] )
2743
2773
}
2744
2774
}
0 commit comments