138 lines
5.1 KiB
Rust
138 lines
5.1 KiB
Rust
//! Small shared helpers.
|
|
|
|
use chrono::TimeZone;
|
|
|
|
pub fn format_local(ts: i64, fmt: &str) -> String {
|
|
match chrono::Local.timestamp_opt(ts, 0) {
|
|
chrono::LocalResult::Single(t) => t.format(fmt).to_string(),
|
|
_ => String::new(),
|
|
}
|
|
}
|
|
|
|
/// Open a path or URL with the desktop's default handler.
|
|
pub fn open_url(url: &str) {
|
|
let _ = std::process::Command::new("xdg-open")
|
|
.arg(url)
|
|
.stdin(std::process::Stdio::null())
|
|
.stdout(std::process::Stdio::null())
|
|
.stderr(std::process::Stdio::null())
|
|
.spawn();
|
|
}
|
|
|
|
/// Reveal a file in the file manager (FileManager1 over D-Bus, else open the folder).
|
|
pub fn show_in_file_manager(path: &str) {
|
|
if !std::path::Path::new(path).exists() {
|
|
return;
|
|
}
|
|
let uri = format!("file://{}", urlencoding::encode(path).replace("%2F", "/"));
|
|
let ok = std::process::Command::new("dbus-send")
|
|
.args([
|
|
"--session",
|
|
"--print-reply",
|
|
"--dest=org.freedesktop.FileManager1",
|
|
"/org/freedesktop/FileManager1",
|
|
"org.freedesktop.FileManager1.ShowItems",
|
|
&format!("array:string:{uri}"),
|
|
"string:",
|
|
])
|
|
.stdout(std::process::Stdio::null())
|
|
.stderr(std::process::Stdio::null())
|
|
.status()
|
|
.map(|s| s.success())
|
|
.unwrap_or(false);
|
|
if !ok {
|
|
let dir = std::path::Path::new(path).parent().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
|
|
open_url(&dir);
|
|
}
|
|
}
|
|
|
|
pub fn clean_title(s: &str) -> String {
|
|
// strip "(feat. …)", "[Remastered]" etc.
|
|
let re = regex::Regex::new(r"\s*[\(\[][^\)\]]*(feat|ft\.|remaster|version|edit|mix|live)[^\)\]]*[\)\]]").unwrap();
|
|
re.replace_all(s, "").trim().to_string()
|
|
}
|
|
|
|
pub fn norm(s: &str) -> String {
|
|
s.chars().filter(|c| c.is_ascii_alphanumeric()).collect::<String>().to_lowercase()
|
|
}
|
|
|
|
/// The cookie-shaped app icon in the current accent colours (RGBA, straight alpha).
|
|
pub fn themed_icon(bg: eframe::egui::Color32, fg: eframe::egui::Color32, size: u32) -> Vec<u8> {
|
|
let hex = |c: eframe::egui::Color32| format!("#{:02x}{:02x}{:02x}", c.r(), c.g(), c.b());
|
|
let c = 128.0f32;
|
|
let r = 256.0 * 0.47;
|
|
let pts: Vec<String> = (0..120)
|
|
.map(|i| {
|
|
let t = i as f32 / 120.0 * std::f32::consts::TAU;
|
|
let rr = r * (0.92 + 0.08 * (9.0 * t).cos());
|
|
format!("{:.2},{:.2}", c + rr * t.cos(), c + rr * t.sin())
|
|
})
|
|
.collect();
|
|
let note = crate::icondata::ICONS.iter().find(|(n, _)| *n == "music_note").map(|x| x.1).unwrap_or("");
|
|
let s = 256.0 * 0.56 / 960.0;
|
|
let off = (256.0 - 256.0 * 0.56) / 2.0;
|
|
let svg = format!(
|
|
r#"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="256" height="256"><polygon fill="{}" points="{}"/><g transform="translate({off},{off}) scale({s}) translate(0,960)"><path fill="{}" d="{note}"/></g></svg>"#,
|
|
hex(bg),
|
|
pts.join(" "),
|
|
hex(fg)
|
|
);
|
|
let mut pm = resvg::tiny_skia::Pixmap::new(size, size).unwrap();
|
|
if let Ok(tree) = resvg::usvg::Tree::from_str(&svg, &resvg::usvg::Options::default()) {
|
|
let k = size as f32 / 256.0;
|
|
resvg::render(&tree, resvg::tiny_skia::Transform::from_scale(k, k), &mut pm.as_mut());
|
|
}
|
|
let mut rgba = pm.take();
|
|
for px in rgba.chunks_exact_mut(4) {
|
|
let a = px[3] as u32;
|
|
if a > 0 && a < 255 {
|
|
for ch in &mut px[..3] {
|
|
*ch = ((*ch as u32 * 255 + a / 2) / a).min(255) as u8;
|
|
}
|
|
}
|
|
}
|
|
rgba
|
|
}
|
|
|
|
/// Files copied in a file manager (Thunar, Dolphin, Nautilus…): read `text/uri-list` or
|
|
/// `x-special/gnome-copied-files` from the clipboard with wl-paste / xclip.
|
|
pub fn clipboard_files() -> Vec<String> {
|
|
let wl = std::env::var_os("MULBERRY_WAYLAND_DISPLAY").or_else(|| std::env::var_os("WAYLAND_DISPLAY"));
|
|
let run = |prog: &str, args: &[&str]| -> Option<String> {
|
|
let mut c = std::process::Command::new(prog);
|
|
c.args(args).stdin(std::process::Stdio::null()).stderr(std::process::Stdio::null());
|
|
if let Some(w) = &wl {
|
|
c.env("WAYLAND_DISPLAY", w);
|
|
}
|
|
let o = c.output().ok()?;
|
|
o.status.success().then(|| String::from_utf8_lossy(&o.stdout).into_owned())
|
|
};
|
|
let mut text = String::new();
|
|
if wl.is_some() {
|
|
if let Some(types) = run("wl-paste", &["-l"]) {
|
|
for ty in ["text/uri-list", "x-special/gnome-copied-files"] {
|
|
if types.lines().any(|l| l.trim() == ty) {
|
|
if let Some(t) = run("wl-paste", &["-t", ty]) {
|
|
text = t;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if text.is_empty() {
|
|
for ty in ["text/uri-list", "x-special/gnome-copied-files"] {
|
|
if let Some(t) = run("xclip", &["-selection", "clipboard", "-t", ty, "-o"]) {
|
|
if !t.trim().is_empty() {
|
|
text = t;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
text.lines()
|
|
.map(str::trim)
|
|
.filter_map(|l| l.strip_prefix("file://"))
|
|
.map(|p| crate::song::percent_decode(p))
|
|
.collect()
|
|
}
|