desktop: Allow starting bookmarks from the bookmarks dialog

This commit is contained in:
Tom Schuster 2024-08-25 18:22:52 +02:00
parent aff78a5d8e
commit 48170bd8e6
2 changed files with 44 additions and 10 deletions

View File

@ -19,6 +19,8 @@ use winit::event_loop::EventLoopProxy;
pub struct Dialogs {
window: Weak<winit::window::Window>,
event_loop: EventLoopProxy<RuffleEvent>,
preferences_dialog: Option<PreferencesDialog>,
bookmarks_dialog: Option<BookmarksDialog>,
bookmark_add_dialog: Option<BookmarkAddDialog>,
@ -47,7 +49,12 @@ impl Dialogs {
bookmarks_dialog: None,
bookmark_add_dialog: None,
open_dialog: OpenDialog::new(player_options, default_path, window.clone(), event_loop),
open_dialog: OpenDialog::new(
player_options,
default_path,
window.clone(),
event_loop.clone(),
),
is_open_dialog_visible: false,
volume_controls: VolumeControls::new(&preferences),
@ -56,6 +63,7 @@ impl Dialogs {
is_about_visible: false,
window,
event_loop,
preferences,
}
}
@ -82,6 +90,7 @@ impl Dialogs {
self.bookmarks_dialog = Some(BookmarksDialog::new(
self.preferences.clone(),
self.window.clone(),
self.event_loop.clone(),
));
}

View File

@ -1,12 +1,14 @@
use crate::gui::text;
use crate::gui::widgets::PathOrUrlField;
use crate::preferences::GlobalPreferences;
use crate::{custom_event::RuffleEvent, player::LaunchOptions};
use egui::{Align2, Button, Grid, Label, Layout, Sense, Ui, Widget, Window};
use egui_extras::{Column, TableBuilder};
use ruffle_frontend_utils::bookmarks::Bookmark;
use std::sync::Weak;
use unic_langid::LanguageIdentifier;
use url::Url;
use winit::event_loop::EventLoopProxy;
pub struct BookmarkAddDialog {
preferences: GlobalPreferences,
@ -99,14 +101,20 @@ struct SelectedBookmark {
pub struct BookmarksDialog {
window: Weak<winit::window::Window>,
event_loop: EventLoopProxy<RuffleEvent>,
preferences: GlobalPreferences,
selected_bookmark: Option<SelectedBookmark>,
}
impl BookmarksDialog {
pub fn new(preferences: GlobalPreferences, window: Weak<winit::window::Window>) -> Self {
pub fn new(
preferences: GlobalPreferences,
window: Weak<winit::window::Window>,
event_loop: EventLoopProxy<RuffleEvent>,
) -> Self {
Self {
window,
event_loop,
preferences,
selected_bookmark: None,
}
@ -114,7 +122,7 @@ impl BookmarksDialog {
pub fn show(&mut self, locale: &LanguageIdentifier, egui_ctx: &egui::Context) -> bool {
let mut keep_open = true;
let should_close = false;
let mut should_close = false;
Window::new(text(locale, "bookmarks-dialog"))
.open(&mut keep_open)
@ -129,7 +137,7 @@ impl BookmarksDialog {
.min_height(100.0)
.show_inside(ui, |ui| {
if self.preferences.have_bookmarks() {
self.show_bookmark_table(locale, ui);
should_close = self.show_bookmark_table(locale, ui);
} else {
ui.centered_and_justified(|ui| {
ui.label(text(locale, "bookmarks-dialog-no-bookmarks"));
@ -143,7 +151,7 @@ impl BookmarksDialog {
keep_open && !should_close
}
fn show_bookmark_table(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) {
fn show_bookmark_table(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) -> bool {
let text_height = egui::TextStyle::Body
.resolve(ui.style())
.size
@ -151,6 +159,7 @@ impl BookmarksDialog {
enum BookmarkAction {
Remove(usize),
Start(Url),
}
let mut action = None;
@ -198,6 +207,10 @@ impl BookmarksDialog {
let response = row.response();
response.context_menu(|ui| {
if ui.button(text(locale, "start")).clicked() {
ui.close_menu();
action = Some(BookmarkAction::Start(bookmark.url.clone()))
}
if ui.button(text(locale, "remove")).clicked() {
ui.close_menu();
action = Some(BookmarkAction::Remove(index));
@ -215,21 +228,33 @@ impl BookmarksDialog {
),
});
}
if response.double_clicked() {
action = Some(BookmarkAction::Start(bookmark.url.clone()));
}
});
}
});
});
if let Some(action) = action {
if let Err(e) = self.preferences.write_bookmarks(|writer| match action {
BookmarkAction::Remove(index) => {
match action {
Some(BookmarkAction::Remove(index)) => {
if let Err(e) = self.preferences.write_bookmarks(|writer| {
// TODO: Recalculate the index for the selected bookmark, if it survives, otherwise just set to None.
self.selected_bookmark = None;
writer.remove(index);
}) {
tracing::warn!("Couldn't update bookmarks: {e}");
}
}) {
tracing::warn!("Couldn't update bookmarks: {e}");
false
}
Some(BookmarkAction::Start(url)) => {
let _ = self.event_loop.send_event(RuffleEvent::OpenURL(
url,
Box::new(LaunchOptions::from(&self.preferences)),
));
true
}
None => false,
}
}