Skip to content

Commit 556f310

Browse files
Auto saver clean up and bug fixes
* Update tab names after auto save * Register tabs when auto saver is enabled via settings * Unregister tabs when auto saver is disabled
1 parent bd6656d commit 556f310

File tree

2 files changed

+161
-117
lines changed

2 files changed

+161
-117
lines changed

src/main.rs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use self::search::ProjectSearchResult;
6363
mod search;
6464

6565
mod session;
66-
use session::{auto_save_subscription, AutoSaveEvent};
66+
use session::{auto_save_subscription, AutoSaveMessage};
6767

6868
use self::tab::{EditorTab, GitDiffTab, Tab};
6969
mod tab;
@@ -307,7 +307,7 @@ impl PartialEq for WatcherWrapper {
307307
#[derive(Clone, Debug)]
308308
pub enum Message {
309309
AppTheme(AppTheme),
310-
AutoSaveSender(futures::channel::mpsc::Sender<AutoSaveEvent>),
310+
AutoSaveSender(futures::channel::mpsc::Sender<AutoSaveMessage>),
311311
AutoSaveTimeout(Option<NonZeroU64>),
312312
Config(Config),
313313
ConfigState(ConfigState),
@@ -437,7 +437,7 @@ pub struct App {
437437
project_search_value: String,
438438
project_search_result: Option<ProjectSearchResult>,
439439
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>>,
441441
modifiers: Modifiers,
442442
}
443443

@@ -729,7 +729,7 @@ impl App {
729729
}
730730

731731
// 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> {
733733
if let Some(mut sender) = self
734734
.config
735735
// Auto saving is enabled if a timeout is set
@@ -1470,12 +1470,32 @@ impl Application for App {
14701470
Message::AutoSaveTimeout(timeout) => {
14711471
self.config.auto_save_secs = timeout;
14721472
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.
14731489
return Command::batch([
14741490
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)),
14761493
]);
14771494
}
1478-
return self.save_config();
1495+
return Command::batch([
1496+
self.save_config(),
1497+
self.update_auto_saver(AutoSaveMessage::CancelAll),
1498+
]);
14791499
}
14801500
Message::Config(config) => {
14811501
if config != self.config {
@@ -1977,13 +1997,19 @@ impl Application for App {
19771997
self.tab_model.text_set(self.tab_model.active(), title);
19781998
}
19791999

1980-
// Remove saved tab from auto saver
2000+
// Remove saved tab from auto saver to avoid double saves
19812001
let entity = self.tab_model.active();
1982-
return self.update_auto_saver(AutoSaveEvent::Cancel(entity));
2002+
return self.update_auto_saver(AutoSaveMessage::Cancel(entity));
19832003
}
19842004
Message::SaveAny(entity) => {
2005+
// TODO: This variant and code should be updated to save backups instead of overwriting
2006+
// the open file
19852007
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+
}
19872013
}
19882014
}
19892015
Message::SaveAsDialog => {
@@ -2094,7 +2120,7 @@ impl Application for App {
20942120
self.tab_model.text_set(entity, title);
20952121
// Register tab with the auto saver
20962122
if has_path {
2097-
return self.update_auto_saver(AutoSaveEvent::Register(entity));
2123+
return self.update_auto_saver(AutoSaveMessage::Register(entity));
20982124
}
20992125
}
21002126
}
@@ -2156,13 +2182,13 @@ impl Application for App {
21562182
entity,
21572183
))),
21582184
self.update_tab(),
2159-
self.update_auto_saver(AutoSaveEvent::Cancel(entity)),
2185+
self.update_auto_saver(AutoSaveMessage::Cancel(entity)),
21602186
]);
21612187
}
21622188

21632189
return Command::batch([
21642190
self.update_tab(),
2165-
self.update_auto_saver(AutoSaveEvent::Cancel(entity)),
2191+
self.update_auto_saver(AutoSaveMessage::Cancel(entity)),
21662192
]);
21672193
}
21682194
Message::TabContextAction(entity, action) => {
@@ -2172,7 +2198,7 @@ impl Application for App {
21722198
// Run action's message
21732199
return Command::batch([
21742200
self.update(action.message()),
2175-
self.update_auto_saver(AutoSaveEvent::Cancel(entity)),
2201+
self.update_auto_saver(AutoSaveMessage::Cancel(entity)),
21762202
]);
21772203
}
21782204
}
@@ -2731,14 +2757,18 @@ impl Application for App {
27312757
Some(dialog) => dialog.subscription(),
27322758
None => subscription::Subscription::none(),
27332759
},
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+
}),
27422772
])
27432773
}
27442774
}

0 commit comments

Comments
 (0)