desktop: Add Open URL dialog

This commit is contained in:
Kamil Jarosz 2024-08-28 16:51:51 +02:00
parent 263ef4696e
commit 25570ef374
5 changed files with 85 additions and 1 deletions

View File

@ -1,2 +1,2 @@
dialog-ok = OK
dialog-cancel = Cancel
dialog-cancel = Cancel

View File

@ -0,0 +1,5 @@
open-url-dialog-title = Open Website?
open-url-dialog-message = The movie wants to open the following website, are you sure you want to go there?
open-url-dialog-open = Open

View File

@ -396,6 +396,10 @@ impl GuiController {
pub fn show_open_dialog(&mut self) {
self.gui.dialogs.open_file_advanced()
}
pub fn show_open_url_dialog(&mut self, url: Url) {
self.gui.dialogs.open_open_url(url);
}
}
fn create_wgpu_instance(

View File

@ -1,6 +1,7 @@
mod about_dialog;
mod bookmarks_dialog;
mod open_dialog;
mod open_url_dialog;
mod preferences_dialog;
mod volume_controls;
@ -9,6 +10,7 @@ use crate::player::LaunchOptions;
use crate::preferences::GlobalPreferences;
use bookmarks_dialog::{BookmarkAddDialog, BookmarksDialog};
use open_dialog::OpenDialog;
use open_url_dialog::OpenUrlDialog;
use preferences_dialog::PreferencesDialog;
use ruffle_core::Player;
use std::sync::Weak;
@ -26,6 +28,7 @@ pub struct Dialogs {
preferences_dialog: Option<PreferencesDialog>,
bookmarks_dialog: Option<BookmarksDialog>,
bookmark_add_dialog: Option<BookmarkAddDialog>,
open_url_dialog: Option<OpenUrlDialog>,
open_dialog: OpenDialog,
is_open_dialog_visible: bool,
@ -51,6 +54,7 @@ impl Dialogs {
preferences_dialog: None,
bookmarks_dialog: None,
bookmark_add_dialog: None,
open_url_dialog: None,
open_dialog: OpenDialog::new(
player_options,
@ -117,6 +121,10 @@ impl Dialogs {
self.is_about_visible = true;
}
pub fn open_open_url(&mut self, url: Url) {
self.open_url_dialog = Some(OpenUrlDialog::new(url));
}
pub fn show(
&mut self,
locale: &LanguageIdentifier,
@ -129,6 +137,7 @@ impl Dialogs {
self.bookmark_add_dialog(locale, egui_ctx);
self.volume_controls(locale, egui_ctx, player);
self.about_dialog(locale, egui_ctx);
self.open_url_dialog(locale, egui_ctx);
}
fn open_dialog(&mut self, locale: &LanguageIdentifier, egui_ctx: &egui::Context) {
@ -191,4 +200,15 @@ impl Dialogs {
self.is_about_visible = keep_open;
}
}
fn open_url_dialog(&mut self, locale: &LanguageIdentifier, egui_ctx: &egui::Context) {
let keep_open = if let Some(dialog) = &mut self.open_url_dialog {
dialog.show(locale, egui_ctx)
} else {
true
};
if !keep_open {
self.open_url_dialog = None;
}
}
}

View File

@ -0,0 +1,55 @@
use crate::{gui::text, util::open_url};
use egui::{Align2, Ui, Window};
use unic_langid::LanguageIdentifier;
use url::Url;
pub struct OpenUrlDialog {
url: Url,
}
impl OpenUrlDialog {
pub fn new(url: Url) -> Self {
Self { url }
}
pub fn show(&mut self, locale: &LanguageIdentifier, egui_ctx: &egui::Context) -> bool {
let mut keep_open = true;
let mut should_close = false;
Window::new(text(locale, "open-url-dialog-title"))
.open(&mut keep_open)
.anchor(Align2::CENTER_CENTER, egui::Vec2::ZERO)
.collapsible(false)
.resizable(false)
.show(egui_ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
should_close = self.render_window_contents(locale, ui)
});
});
keep_open && !should_close
}
pub fn render_window_contents(&mut self, locale: &LanguageIdentifier, ui: &mut Ui) -> bool {
let mut should_close = false;
ui.label(text(locale, "open-url-dialog-message"));
ui.label("");
ui.monospace(self.url.as_str());
ui.label("");
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui.button(text(locale, "open-url-dialog-open")).clicked() {
open_url(&self.url);
should_close = true;
}
if ui.button(text(locale, "dialog-cancel")).clicked() {
should_close = true;
}
})
});
should_close
}
}