desktop: Add a way to clear recently played files

This commit is contained in:
sleepycatcoding 2024-05-08 18:49:21 +03:00 committed by Nathan Adams
parent 4422445f6a
commit 059abb17ce
3 changed files with 38 additions and 10 deletions

View File

@ -23,3 +23,4 @@ storage-backend-disk = Disk
storage-backend-memory = Memory
recent-limit = Recent Limit
recent-clear = Clear

View File

@ -324,6 +324,7 @@ impl PreferencesDialog {
fn show_misc_preferences(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) {
ui.label(text(locale, "recent-limit"));
ui.horizontal(|ui| {
let previous = self.recent_limit;
DragValue::new(&mut self.recent_limit).ui(ui);
@ -331,6 +332,15 @@ impl PreferencesDialog {
self.recent_limit_changed = true;
}
if ui.button(text(locale, "recent-clear")).clicked() {
if let Err(e) = self.preferences.write_recents(|writer| {
writer.clear();
}) {
tracing::warn!("Couldn't update recents: {e}");
}
}
});
ui.end_row()
}

View File

@ -1,7 +1,7 @@
use crate::parse::DocumentHolder;
use crate::recents::{Recent, Recents};
use crate::write::TableExt;
use toml_edit::{value, Table};
use toml_edit::{value, ArrayOfTables, Table};
pub struct RecentsWriter<'a>(&'a mut DocumentHolder<Recents>);
@ -10,11 +10,23 @@ impl<'a> RecentsWriter<'a> {
Self(recents)
}
fn with_underlying_table(&mut self, fun: impl FnOnce(&mut Recents, &mut ArrayOfTables)) {
self.0.edit(|values, toml_document| {
let table = toml_document.get_or_create_array_of_tables("recent");
fun(values, table)
})
}
pub fn clear(&mut self) {
self.with_underlying_table(|values, array| {
array.clear();
values.clear();
});
}
/// Pushes a new recent entry on the entry stack, if same entry already exists, it will get moved to the top.
pub fn push(&mut self, recent: Recent, limit: usize) {
self.0.edit(|values, toml_document| {
let array = toml_document.get_or_create_array_of_tables("recent");
self.with_underlying_table(|values, array| {
// First, lets check if we already have existing entry with the same URL and move it to the top.
let existing = values.iter().position(|x| x.url == recent.url);
@ -92,4 +104,9 @@ mod tests {
url: Url::parse("file:///very_important_file.swf").unwrap(),
}, 3), "[[recent]]\nurl = \"file:///2.swf\"\n[[recent]]\nurl = \"file:///3.swf\"\n\n[[recent]]\nurl = \"file:///very_important_file.swf\"\n");
}
#[test]
fn clear() {
test("[[recent]]\nurl = \"file:///file_one.swf\"\n[[recent]]\nurl = \"file:///file_two.swf\"\n[[recent]]\nurl = \"file:///3.swf\"\n", |writer| writer.clear(), "");
}
}