95 lines
3.5 KiB
Rust
95 lines
3.5 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
|
|
}
|