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 <noreply@anthropic.com>
This commit is contained in:
1 parent
a6f9b425e8
commit
d77b2bae71
3 files changed
+48
-5
No files matched your search
+44
-3
@@ -9,6 +9,7 @@
|
|||||||
#include "toast.hpp"
|
#include "toast.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <format>
|
#include <format>
|
||||||
@@ -76,14 +77,54 @@ namespace anm2ed
|
|||||||
this->type = type;
|
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)
|
void Dialog::file_explorer_open(const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
if (path.empty()) return;
|
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
|
#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__
|
#elif __unix__
|
||||||
auto pathUtf8 = path::to_utf8(path);
|
// File managers that implement org.freedesktop.FileManager1 (Nautilus, Dolphin, Nemo, Caja, Thunar...) select the
|
||||||
system(std::format("xdg-open \"{}\" &", pathUtf8).c_str());
|
// 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
|
#else
|
||||||
toast_log(Level::WARNING, TOAST_NOT_SUPPORTED);
|
toast_log(Level::WARNING, TOAST_NOT_SUPPORTED);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ namespace anm2ed::imgui
|
|||||||
if (ImGui::MenuItem(localize.get(LABEL_SAVE_AS), settings.shortcutSaveAs.c_str(), false, document))
|
if (ImGui::MenuItem(localize.get(LABEL_SAVE_AS), settings.shortcutSaveAs.c_str(), false, document))
|
||||||
dialog.file_save(Dialog::ANM2_SAVE);
|
dialog.file_save(Dialog::ANM2_SAVE);
|
||||||
if (ImGui::MenuItem(localize.get(LABEL_EXPLORE_XML_LOCATION), nullptr, false, document))
|
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).
|
// 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;
|
auto isExtendedFormat = document && document->model.isExtendedFormat;
|
||||||
|
|||||||
@@ -342,13 +342,15 @@ namespace anm2ed::imgui
|
|||||||
return path::backslash_replace(path::make_relative(path::backslash_handle(path), document.directory_get()));
|
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)
|
void directory_open(Dialog& dialog, Document& document, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
if (path.empty()) return;
|
if (path.empty()) return;
|
||||||
std::error_code ec{};
|
std::error_code ec{};
|
||||||
auto absolutePath = std::filesystem::weakly_canonical(document.directory_get() / path, ec);
|
auto absolutePath = std::filesystem::weakly_canonical(document.directory_get() / path, ec);
|
||||||
if (ec) absolutePath = document.directory_get() / path;
|
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()
|
: std::filesystem::is_directory(absolutePath.parent_path()) ? absolutePath.parent_path()
|
||||||
: document.directory_get();
|
: document.directory_get();
|
||||||
dialog.file_explorer_open(target);
|
dialog.file_explorer_open(target);
|
||||||
|
|||||||
Reference in new issue
Block a user