desktop: Basic recents menu

This commit is contained in:
sleepycatcoding 2024-04-09 01:38:16 +03:00 committed by Nathan Adams
parent 7c80091488
commit a33c18b309
2 changed files with 57 additions and 0 deletions

View File

@ -8,6 +8,7 @@ mod widgets;
pub use controller::GuiController;
pub use movie::MovieView;
use ruffle_frontend_utils::recents::Recent;
use std::borrow::Cow;
use url::Url;
@ -206,6 +207,17 @@ impl RuffleGui {
mut player: MutexGuard<Player>,
) {
self.currently_opened = Some((movie_url.clone(), opt.clone()));
if let Err(e) = self.preferences.write_recents(|writer| {
writer.push(
Recent {
url: movie_url.clone(),
},
// TODO: Add user configurable recent limit.
10,
)
}) {
tracing::warn!("Couldn't update recents: {e}");
}
// Update dialog state to reflect the newly-opened movie's options.
self.is_open_dialog_visible = false;
@ -274,6 +286,19 @@ impl RuffleGui {
if ui.add_enabled(player.is_some(), Button::new(text(locale, "file-menu-close"))).clicked() {
self.close_movie(ui);
}
ui.separator();
ui.menu_button("Recents", |ui| {
// Since we store recents from oldest to newest iterate backwards.
self.preferences.recents(|recents| {
for recent in recents.iter().rev() {
if ui.button(recent.url.as_str()).clicked() {
ui.close_menu();
let _ = self.event_loop.send_event(RuffleEvent::OpenURL(recent.url.clone(), Box::new(self.default_player_options.clone())));
}
}
});
});
ui.separator();
if Button::new(text(locale, "file-menu-preferences"))

View File

@ -11,6 +11,7 @@ use anyhow::{Context, Error};
use ruffle_core::backend::ui::US_ENGLISH;
use ruffle_frontend_utils::bookmarks::{read_bookmarks, Bookmarks, BookmarksWriter};
use ruffle_frontend_utils::parse::DocumentHolder;
use ruffle_frontend_utils::recents::{read_recents, Recents, RecentsWriter};
use ruffle_render_wgpu::clap::{GraphicsBackend, PowerPreference};
use std::sync::{Arc, Mutex};
use sys_locale::get_locale;
@ -39,6 +40,8 @@ pub struct GlobalPreferences {
preferences: Arc<Mutex<DocumentHolder<SavedGlobalPreferences>>>,
bookmarks: Arc<Mutex<DocumentHolder<Bookmarks>>>,
recents: Arc<Mutex<DocumentHolder<Recents>>>,
}
impl GlobalPreferences {
@ -71,10 +74,24 @@ impl GlobalPreferences {
Default::default()
};
let recents_path = cli.config.join("recents.toml");
let recents = if recents_path.exists() {
let contents =
std::fs::read_to_string(&recents_path).context("Failed to read saved recents")?;
let result = read_recents(&contents);
for warning in result.warnings {
tracing::warn!("{warning}");
}
result.result
} else {
Default::default()
};
Ok(Self {
cli,
preferences: Arc::new(Mutex::new(preferences)),
bookmarks: Arc::new(Mutex::new(bookmarks)),
recents: Arc::new(Mutex::new(recents)),
})
}
@ -156,6 +173,10 @@ impl GlobalPreferences {
})
}
pub fn recents(&self, fun: impl FnOnce(&Recents)) {
fun(&self.recents.lock().expect("Recents is not reentrant"))
}
pub fn write_preferences(&self, fun: impl FnOnce(&mut PreferencesWriter)) -> Result<(), Error> {
let mut preferences = self
.preferences
@ -180,6 +201,17 @@ impl GlobalPreferences {
std::fs::write(self.cli.config.join("bookmarks.toml"), serialized)
.context("Could not write bookmarks to disk")
}
pub fn write_recents(&self, fun: impl FnOnce(&mut RecentsWriter)) -> Result<(), Error> {
let mut recents = self.recents.lock().expect("Recents is not reentrant");
let mut writer = RecentsWriter::new(&mut recents);
fun(&mut writer);
let serialized = recents.serialize();
std::fs::write(self.cli.config.join("recents.toml"), serialized)
.context("Could not write recents to disk")
}
}
#[derive(PartialEq, Debug)]