From 011c633a8a4a17b47e07ea3dabbd0a4560d7e2a8 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Sun, 2 Feb 2025 01:14:46 -0500 Subject: [PATCH] Open folder passed as command line arg Allows opening a folder on the command line rather than only a single file. This is useful for Freedesktop compliant file managers as they pass files URLs to media players. This currently only handles one URL whereas the spec allows multiple. --- src/main.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7dc5b7e..170c947 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only use cosmic::{ - app::{message, Command, Core, Settings}, + app::{command, message, Command, Core, Settings}, cosmic_config::{self, CosmicConfigEntry}, cosmic_theme, executor, font, iced::{ @@ -114,7 +114,7 @@ pub fn main() -> Result<(), Box> { Some(arg) => match url::Url::parse(&arg) { Ok(url) => Some(url), Err(_) => match fs::canonicalize(&arg) { - Ok(path) => match url::Url::from_file_path(&path) { + Ok(path) => match url::Url::from_file_path(&path).or_else(|_| url::Url::from_directory_path(&path)) { Ok(url) => Some(url), Err(()) => { log::warn!("failed to parse path {:?}", path); @@ -809,7 +809,15 @@ impl Application for App { .icon(widget::icon::from_name("folder-open-symbolic").size(16)) .text(fl!("open-folder")); - let command = app.load(); + let maybe_path = app + .flags + .url_opt + .as_ref() + .and_then(|url| url.to_file_path().ok()); + let command = match maybe_path { + Some(path) if path.is_dir() => command::message::app(Message::FolderLoad(path)), + _ => app.load(), + }; (app, command) }