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

View File

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