Skip to content

Commit bd6656d

Browse files
Add config option for auto saver timeout
1 parent f6a80d9 commit bd6656d

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

i18n/en/cosmic_edit.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ prompt-save-changes-title = Unsaved changes
3030
prompt-unsaved-changes = You have unsaved changes. Save?
3131
discard = Discard changes
3232
33+
## Session
34+
session = Session
35+
seconds = seconds
36+
auto-save-secs = Auto save (seconds)
37+
3338
## Settings
3439
settings = Settings
3540

src/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ impl Default for Config {
5757
tab_width: 4,
5858
vim_bindings: false,
5959
word_wrap: true,
60-
// TODO: Set this back to None before PR
61-
auto_save_secs: Some(NonZeroU64::new(3).unwrap())
60+
auto_save_secs: None
6261
}
6362
}
6463
}

src/main.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use std::{
2626
any::TypeId,
2727
collections::HashMap,
2828
env, fs, io,
29+
num::NonZeroU64,
2930
path::{Path, PathBuf},
3031
process,
3132
sync::{Mutex, OnceLock},
@@ -307,6 +308,7 @@ impl PartialEq for WatcherWrapper {
307308
pub enum Message {
308309
AppTheme(AppTheme),
309310
AutoSaveSender(futures::channel::mpsc::Sender<AutoSaveEvent>),
311+
AutoSaveTimeout(Option<NonZeroU64>),
310312
Config(Config),
311313
ConfigState(ConfigState),
312314
CloseFile,
@@ -1148,6 +1150,11 @@ impl App {
11481150
.font_sizes
11491151
.iter()
11501152
.position(|font_size| font_size == &self.config.font_size);
1153+
let save_seconds = self
1154+
.config
1155+
.auto_save_secs
1156+
.map(|secs| secs.to_string())
1157+
.unwrap_or_default();
11511158
widget::settings::view_column(vec![
11521159
widget::settings::view_section(fl!("appearance"))
11531160
.add(
@@ -1198,6 +1205,16 @@ impl App {
11981205
.toggler(self.config.vim_bindings, Message::VimBindings),
11991206
)
12001207
.into(),
1208+
widget::settings::view_section(fl!("session"))
1209+
.add(
1210+
widget::settings::item::builder(fl!("auto-save-secs")).control(
1211+
widget::text_input(fl!("seconds"), save_seconds).on_input(|s| {
1212+
let secs = s.parse().ok();
1213+
Message::AutoSaveTimeout(secs)
1214+
}),
1215+
),
1216+
)
1217+
.into(),
12011218
])
12021219
.into()
12031220
}
@@ -1450,6 +1467,16 @@ impl Application for App {
14501467
Message::AutoSaveSender(sender) => {
14511468
self.auto_save_sender = Some(sender);
14521469
}
1470+
Message::AutoSaveTimeout(timeout) => {
1471+
self.config.auto_save_secs = timeout;
1472+
if let Some(timeout) = timeout {
1473+
return Command::batch([
1474+
self.save_config(),
1475+
self.update_auto_saver(AutoSaveEvent::UpdateTimeout(timeout)),
1476+
]);
1477+
}
1478+
return self.save_config();
1479+
}
14531480
Message::Config(config) => {
14541481
if config != self.config {
14551482
log::info!("update config");
@@ -2133,7 +2160,10 @@ impl Application for App {
21332160
]);
21342161
}
21352162

2136-
return self.update_tab();
2163+
return Command::batch([
2164+
self.update_tab(),
2165+
self.update_auto_saver(AutoSaveEvent::Cancel(entity)),
2166+
]);
21372167
}
21382168
Message::TabContextAction(entity, action) => {
21392169
if let Some(Tab::Editor(tab)) = self.tab_model.data_mut::<Tab>(entity) {
@@ -2701,13 +2731,14 @@ impl Application for App {
27012731
Some(dialog) => dialog.subscription(),
27022732
None => subscription::Subscription::none(),
27032733
},
2704-
auto_save_subscription(self.config.auto_save_secs.unwrap()).map(
2705-
|update| match update {
2734+
match self.config.auto_save_secs {
2735+
Some(secs) => auto_save_subscription(secs).map(|update| match update {
27062736
AutoSaveEvent::Ready(sender) => Message::AutoSaveSender(sender),
27072737
AutoSaveEvent::Save(entity) => Message::SaveAny(entity),
27082738
_ => unreachable!(),
2709-
},
2710-
),
2739+
}),
2740+
None => subscription::Subscription::none(),
2741+
},
27112742
])
27122743
}
27132744
}

0 commit comments

Comments
 (0)