frontend-utils: Move url_to_readable_name from desktop

This commit is contained in:
sleepycatcoding 2024-04-05 14:56:23 +03:00 committed by Nathan Adams
parent f737739c15
commit 7c7f383595
7 changed files with 20 additions and 14 deletions

1
Cargo.lock generated
View File

@ -4386,6 +4386,7 @@ dependencies = [
"toml_edit 0.22.9",
"tracing",
"url",
"urlencoding",
"zip",
]

View File

@ -19,7 +19,7 @@ impl BookmarkAddDialog {
preferences,
name: initial_url
.as_ref()
.map(|x| crate::util::url_to_readable_name(x).into_owned())
.map(|x| ruffle_frontend_utils::url_to_readable_name(x).into_owned())
.unwrap_or_default(),
// TODO: hint.
url: PathOrUrlField::new(initial_url, ""),

View File

@ -125,7 +125,9 @@ impl PlayingContent {
pub fn name(&self) -> String {
match self {
PlayingContent::DirectFile(url) => crate::util::url_to_readable_name(url).to_string(),
PlayingContent::DirectFile(url) => {
ruffle_frontend_utils::url_to_readable_name(url).to_string()
}
PlayingContent::Bundle(_, bundle) => bundle.information().name.to_string(),
}
}

View File

@ -93,7 +93,7 @@ pub fn read_bookmarks(input: &str) -> ParseDetails<Bookmarks> {
let name = match bookmark.parse_from_str(cx, "name") {
Some(value) => value,
// Fallback to using the URL as the name.
None => crate::util::url_to_readable_name(&url).into_owned(),
None => ruffle_frontend_utils::url_to_readable_name(&url).into_owned(),
};
result.push(Bookmark { url, name });

View File

@ -3,7 +3,6 @@ use anyhow::{anyhow, Error};
use gilrs::Button;
use rfd::FileDialog;
use ruffle_core::events::{GamepadButton, KeyCode, TextControlCode};
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use url::Url;
use winit::dpi::PhysicalSize;
@ -234,15 +233,6 @@ pub fn parse_url(path: &Path) -> Result<Url, Error> {
}
}
pub fn url_to_readable_name(url: &Url) -> Cow<'_, str> {
let name = url
.path_segments()
.and_then(|segments| segments.last())
.unwrap_or_else(|| url.as_str());
urlencoding::decode(name).unwrap_or(Cow::Borrowed(name))
}
fn actually_pick_file(dir: Option<PathBuf>) -> Option<PathBuf> {
let mut dialog = FileDialog::new()
.add_filter("Flash Files", &["swf", "spl", "ruf"])

View File

@ -16,6 +16,7 @@ url = { workspace = true }
tracing = { workspace = true }
thiserror = { workspace = true }
zip = "0.6.6"
urlencoding = "2.1.3"
[dev-dependencies]
tempfile = "3"
tempfile = "3"

View File

@ -2,3 +2,15 @@ pub mod bookmarks;
pub mod bundle;
pub mod parse;
pub mod write;
use std::borrow::Cow;
use url::Url;
pub fn url_to_readable_name(url: &Url) -> Cow<'_, str> {
let name = url
.path_segments()
.and_then(|segments| segments.last())
.unwrap_or_else(|| url.as_str());
urlencoding::decode(name).unwrap_or(Cow::Borrowed(name))
}