desktop: Add a way to clear recently played files
This commit is contained in:
parent
4422445f6a
commit
059abb17ce
|
@ -23,3 +23,4 @@ storage-backend-disk = Disk
|
||||||
storage-backend-memory = Memory
|
storage-backend-memory = Memory
|
||||||
|
|
||||||
recent-limit = Recent Limit
|
recent-limit = Recent Limit
|
||||||
|
recent-clear = Clear
|
|
@ -324,6 +324,7 @@ impl PreferencesDialog {
|
||||||
fn show_misc_preferences(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) {
|
fn show_misc_preferences(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) {
|
||||||
ui.label(text(locale, "recent-limit"));
|
ui.label(text(locale, "recent-limit"));
|
||||||
|
|
||||||
|
ui.horizontal(|ui| {
|
||||||
let previous = self.recent_limit;
|
let previous = self.recent_limit;
|
||||||
DragValue::new(&mut self.recent_limit).ui(ui);
|
DragValue::new(&mut self.recent_limit).ui(ui);
|
||||||
|
|
||||||
|
@ -331,6 +332,15 @@ impl PreferencesDialog {
|
||||||
self.recent_limit_changed = true;
|
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()
|
ui.end_row()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::parse::DocumentHolder;
|
use crate::parse::DocumentHolder;
|
||||||
use crate::recents::{Recent, Recents};
|
use crate::recents::{Recent, Recents};
|
||||||
use crate::write::TableExt;
|
use crate::write::TableExt;
|
||||||
use toml_edit::{value, Table};
|
use toml_edit::{value, ArrayOfTables, Table};
|
||||||
|
|
||||||
pub struct RecentsWriter<'a>(&'a mut DocumentHolder<Recents>);
|
pub struct RecentsWriter<'a>(&'a mut DocumentHolder<Recents>);
|
||||||
|
|
||||||
|
@ -10,11 +10,23 @@ impl<'a> RecentsWriter<'a> {
|
||||||
Self(recents)
|
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.
|
/// 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) {
|
pub fn push(&mut self, recent: Recent, limit: usize) {
|
||||||
self.0.edit(|values, toml_document| {
|
self.with_underlying_table(|values, array| {
|
||||||
let array = toml_document.get_or_create_array_of_tables("recent");
|
|
||||||
|
|
||||||
// First, lets check if we already have existing entry with the same URL and move it to the top.
|
// 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);
|
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(),
|
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");
|
}, 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(), "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue