From d77b2bae7149228da33611c9e42a93ab22883760 Mon Sep 17 00:00:00 2001 From: shweet Date: Wed, 21 Oct 2026 01:18:05 -0400 Subject: [PATCH] Open Directory selects the file in the file manager Showing a spritesheet, sound, library (or Explore XML Location's document) opens its folder with the file selected: explorer /select on Windows, the freedesktop FileManager1 ShowItems call on Linux (Nautilus, Dolphin, Nemo, Caja, Thunar...), falling back to opening the folder. Paths go to the shell single-quoted. Co-Authored-By: Claude Opus 5.5 --- src/dialog.cpp | 47 +++++++++++++++++++++++++++++++++++--- src/imgui/taskbar.cpp | 2 +- src/imgui/window/panel.cpp | 4 +++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/dialog.cpp b/src/dialog.cpp index e5013e4..7f6ae52 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -9,6 +9,7 @@ #include "toast.hpp" #endif +#include #include #include #include @@ -76,14 +77,54 @@ namespace anm2ed this->type = type; } +#ifdef __unix__ + // A string as one single-quoted shell word. + std::string shell_quote(std::string_view text) + { + std::string quoted = "'"; + for (auto character : text) + quoted += character == '\'' ? std::string("'\\''") : std::string(1, character); + return quoted + "'"; + } + + // A file:// URI; everything but unreserved characters and slashes is percent-encoded (commas too, which dbus-send + // would otherwise take as array separators). + std::string file_uri_get(const std::filesystem::path& path) + { + std::string uri = "file://"; + for (unsigned char character : path::to_utf8(path)) + if (std::isalnum(character) || std::string_view("-._~/").contains((char)character)) + uri += (char)character; + else + uri += std::format("%{:02X}", character); + return uri; + } +#endif + + // Shows a folder in the system's file manager; a file is shown selected in its folder. void Dialog::file_explorer_open(const std::filesystem::path& path) { if (path.empty()) return; + std::error_code ec{}; + auto absolutePath = std::filesystem::absolute(path, ec); + auto isFile = std::filesystem::is_regular_file(absolutePath, ec); #ifdef _WIN32 - ShellExecuteW(nullptr, L"open", path.native().c_str(), nullptr, nullptr, SW_SHOWNORMAL); + if (isFile) + ShellExecuteW(nullptr, L"open", L"explorer.exe", (L"/select,\"" + absolutePath.native() + L"\"").c_str(), nullptr, + SW_SHOWNORMAL); + else + ShellExecuteW(nullptr, L"open", absolutePath.native().c_str(), nullptr, nullptr, SW_SHOWNORMAL); #elif __unix__ - auto pathUtf8 = path::to_utf8(path); - system(std::format("xdg-open \"{}\" &", pathUtf8).c_str()); + // File managers that implement org.freedesktop.FileManager1 (Nautilus, Dolphin, Nemo, Caja, Thunar...) select the + // file; anything else opens its folder. + auto folder = shell_quote(path::to_utf8(isFile ? absolutePath.parent_path() : absolutePath)); + auto command = isFile ? std::format("(dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 " + "--type=method_call /org/freedesktop/FileManager1 " + "org.freedesktop.FileManager1.ShowItems array:string:{} string:'' " + ">/dev/null 2>&1 || xdg-open {}) &", + shell_quote(file_uri_get(absolutePath)), folder) + : std::format("xdg-open {} &", folder); + system(command.c_str()); #else toast_log(Level::WARNING, TOAST_NOT_SUPPORTED); #endif diff --git a/src/imgui/taskbar.cpp b/src/imgui/taskbar.cpp index b51a9b3..04b252d 100644 --- a/src/imgui/taskbar.cpp +++ b/src/imgui/taskbar.cpp @@ -129,7 +129,7 @@ namespace anm2ed::imgui if (ImGui::MenuItem(localize.get(LABEL_SAVE_AS), settings.shortcutSaveAs.c_str(), false, document)) dialog.file_save(Dialog::ANM2_SAVE); if (ImGui::MenuItem(localize.get(LABEL_EXPLORE_XML_LOCATION), nullptr, false, document)) - dialog.file_explorer_open(document->directory_get()); + dialog.file_explorer_open(document->path); // Which game this document is saved for: the editor's format alone, or Rebirth's (with the editor's copy). auto isExtendedFormat = document && document->model.isExtendedFormat; diff --git a/src/imgui/window/panel.cpp b/src/imgui/window/panel.cpp index 29f20f9..2780fc3 100644 --- a/src/imgui/window/panel.cpp +++ b/src/imgui/window/panel.cpp @@ -342,13 +342,15 @@ namespace anm2ed::imgui return path::backslash_replace(path::make_relative(path::backslash_handle(path), document.directory_get())); } + // Shows a resource's file selected in the file manager (or the nearest folder there is, when it's missing). void directory_open(Dialog& dialog, Document& document, const std::filesystem::path& path) { if (path.empty()) return; std::error_code ec{}; auto absolutePath = std::filesystem::weakly_canonical(document.directory_get() / path, ec); if (ec) absolutePath = document.directory_get() / path; - auto target = std::filesystem::is_directory(absolutePath) ? absolutePath + auto target = std::filesystem::is_regular_file(absolutePath, ec) || std::filesystem::is_directory(absolutePath, ec) + ? absolutePath : std::filesystem::is_directory(absolutePath.parent_path()) ? absolutePath.parent_path() : document.directory_get(); dialog.file_explorer_open(target);