From be52a059602c023ba00f1a9b389f4be38c8d2745 Mon Sep 17 00:00:00 2001 From: astar76 Date: Sat, 24 Oct 2026 18:49:02 -0500 Subject: [PATCH] Ctrl+V pastes files copied in a file manager; drop hint overlay; XWayland opt-in Co-Authored-By: Claude Sonnet 5.5 --- src/app.rs | 12 ++++++++++++ src/config.rs | 2 +- src/main.rs | 3 +++ src/ui/layout.rs | 10 ++++++++++ src/ui/menus.rs | 2 ++ src/util.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/clip.rs | 12 ++++++++++++ 7 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/clip.rs diff --git a/src/app.rs b/src/app.rs index 72e4d19..5366edb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -901,6 +901,7 @@ impl App { if i.consume_key(cs, Key::S) { act = Some("export_all"); } else if i.consume_key(c, Key::S) { act = Some("export"); } if i.consume_key(c, Key::H) { act = Some("toggle_shuffle"); } + if !typing && i.consume_key(c, Key::V) { act = Some("paste_files"); } if i.consume_key(cs, Key::O) { act = Some("open_folder"); } else if i.consume_key(c, Key::O) { act = Some("open"); } if i.consume_key(Modifiers::COMMAND | Modifiers::ALT, Key::V) { act = Some("stop_after"); } @@ -1007,6 +1008,17 @@ impl App { "shape" => { let v = !settings().get_bool("appearance/floating_shape"); settings().set("appearance/floating_shape", v); } "shape_morph" => {} "about" => self.dialog = Some(Dialog::About), + "paste_files" => { + let bus = self.bus.clone(); + std::thread::spawn(move || { + let paths = util::clipboard_files(); + if paths.is_empty() { + bus.toast("No files on the clipboard (copy them in your file manager first)"); + } else { + bus.send(Msg::Picked { purpose: "paste_files".into(), paths }); + } + }); + } "noop" => {} "selall" => { let pl = self.mgr.current_mut(); diff --git a/src/config.rs b/src/config.rs index 5ce1761..52f2dda 100644 --- a/src/config.rs +++ b/src/config.rs @@ -92,7 +92,7 @@ fn defaults() -> BTreeMap<&'static str, Value> { ("appearance/font", json!("")), ("appearance/notifications", json!(true)), ("appearance/tray", json!(true)), - ("appearance/use_x11", json!(true)), + ("appearance/use_x11", json!(false)), ("appearance/floating_shape", json!(true)), ("appearance/max_fps", json!(30)), ("lastfm/enabled", json!(false)), diff --git a/src/main.rs b/src/main.rs index d7c2fc5..0744199 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,9 @@ fn main() -> eframe::Result { } // winit cannot receive file drops from file managers on Wayland; through XWayland it can if config::settings().get_bool("appearance/use_x11") && std::env::var_os("DISPLAY").is_some() && std::env::var_os("WAYLAND_DISPLAY").is_some() { + if let Some(w) = std::env::var_os("WAYLAND_DISPLAY") { + std::env::set_var("MULBERRY_WAYLAND_DISPLAY", w); + } std::env::remove_var("WAYLAND_DISPLAY"); } let viewport = egui::ViewportBuilder::default() diff --git a/src/ui/layout.rs b/src/ui/layout.rs index 8f248ff..725e448 100644 --- a/src/ui/layout.rs +++ b/src/ui/layout.rs @@ -1,5 +1,6 @@ //! Top-level window layout: menu bar, sidebar, hero, playlists, bottom bar and overlays. +use super::gfx::cr; use super::widgets::*; use super::{hero, menus, playlist_view, sidebar}; use crate::app::App; @@ -79,6 +80,15 @@ pub fn draw(app: &mut App, ui: &mut Ui) { } } + // ---- drop hint while a file is dragged over the window + if ctx.input(|i| !i.raw.hovered_files.is_empty()) { + let r = ctx.content_rect(); + let p = ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, Id::new("drop-hint"))); + p.rect_filled(r, 0.0, with_alpha(t.surface, 0.55)); + p.rect_stroke(r.shrink(10.0), cr(24.0), egui::Stroke::new(3.0, t.primary), egui::StrokeKind::Inside); + p.text(r.center(), Align2::CENTER_CENTER, "Drop to add to the playlist", egui::FontId::proportional(24.0), t.primary); + } + // ---- popups menus::show(app, &ctx); if app.area.drag.is_some() && ctx.input(|i| i.pointer.any_released()) { diff --git a/src/ui/menus.rs b/src/ui/menus.rs index 5f0f22a..75e4c4a 100644 --- a/src/ui/menus.rs +++ b/src/ui/menus.rs @@ -240,6 +240,7 @@ fn bar_menu(app: &mut App, ui: &mut Ui, i: usize, act: &mut Option) { } 1 => { item(ui, g, act, Some("playlist_add"), "New playlist", "Ctrl+T", Act::Do("newpl"), None); + item(ui, g, act, Some("content_copy"), "Paste files from file manager", "Ctrl+V", Act::Do("paste_files"), None); item(ui, g, act, Some("file_open"), "Import playlist…", "Ctrl+I", Act::Do("import"), None); item(ui, g, act, Some("save"), "Export playlist…", "Ctrl+S", Act::Do("export"), None); item(ui, g, act, Some("download"), "Export all playlists…", "Ctrl+Shift+S", Act::Do("export_all"), None); @@ -746,6 +747,7 @@ impl App { pub fn on_picked(&mut self, purpose: &str, paths: Vec) { match purpose { "open_files" => self.add_paths(paths, AddMode::Play), + "paste_files" => self.add_paths(paths, AddMode::Append), "open_folder" => self.add_paths(paths, AddMode::Play), "add_library_folder" => { if let Some(d) = paths.first() { diff --git a/src/util.rs b/src/util.rs index f6f18e8..37699cd 100644 --- a/src/util.rs +++ b/src/util.rs @@ -93,3 +93,46 @@ pub fn themed_icon(bg: eframe::egui::Color32, fg: eframe::egui::Color32, size: u } 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 { + let wl = std::env::var_os("MULBERRY_WAYLAND_DISPLAY").or_else(|| std::env::var_os("WAYLAND_DISPLAY")); + let run = |prog: &str, args: &[&str]| -> Option { + 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() +} diff --git a/tests/clip.rs b/tests/clip.rs new file mode 100644 index 0000000..6031c1b --- /dev/null +++ b/tests/clip.rs @@ -0,0 +1,12 @@ +#[test] +fn reads_copied_files_from_clipboard() { + if std::env::var_os("WAYLAND_DISPLAY").is_none() || std::process::Command::new("wl-copy").arg("--version").output().is_err() { + return; + } + let mut c = std::process::Command::new("wl-copy").args(["-t", "text/uri-list"]).stdin(std::process::Stdio::piped()).spawn().unwrap(); + use std::io::Write; + c.stdin.take().unwrap().write_all(b"file:///tmp/a%20b.flac\r\nfile:///tmp/c.opus\r\n").unwrap(); + c.wait().unwrap(); + std::thread::sleep(std::time::Duration::from_millis(200)); + assert_eq!(mulberry::util::clipboard_files(), vec!["/tmp/a b.flac", "/tmp/c.opus"]); +}