From d7cb178c2b63f7640105dadfc5c474c40708df60 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Fri, 7 Aug 2026 19:49:53 +0800 Subject: [PATCH] feat(ui): add show in file manager button to screenshot view - Add Tools::open_file_manager helper with Windows and Linux support. - Ensure screenshot directory is created before scanning. - Add localization keys for en_US and zh_CN. --- assets/lang/en_US.json | 3 +- assets/lang/zh_CN.json | 3 +- include/Cubed/tools/file_utils.hpp | 57 ++++++++++++++++++++++++++++++ src/ui/screenshot_ui.cpp | 17 ++++++--- 4 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 include/Cubed/tools/file_utils.hpp diff --git a/assets/lang/en_US.json b/assets/lang/en_US.json index 1efc672..0b2d4e4 100644 --- a/assets/lang/en_US.json +++ b/assets/lang/en_US.json @@ -48,5 +48,6 @@ "item.stone.name": "stone", "item.water.name": "water", "item.pig_spawn_egg.name": "pig spawn egg", - "screenshot.no_screenshot": "No Screenshot" + "screenshot.no_screenshot": "No Screenshot", + "button.show_in_file_manager": "Show In File Manager" } \ No newline at end of file diff --git a/assets/lang/zh_CN.json b/assets/lang/zh_CN.json index f86e61e..0b0185e 100644 --- a/assets/lang/zh_CN.json +++ b/assets/lang/zh_CN.json @@ -48,5 +48,6 @@ "item.stone.name": "石头", "item.water.name": "水", "item.pig_spawn_egg.name": "猪猪刷怪蛋", - "screenshot.no_screenshot": "没有截图" + "screenshot.no_screenshot": "没有截图", + "button.show_in_file_manager": "在文件管理器中显示" } \ No newline at end of file diff --git a/include/Cubed/tools/file_utils.hpp b/include/Cubed/tools/file_utils.hpp new file mode 100644 index 0000000..66f5d4a --- /dev/null +++ b/include/Cubed/tools/file_utils.hpp @@ -0,0 +1,57 @@ +#pragma once +#include "Cubed/tools/log.hpp" + +#include +#include + +#ifdef _WIN32 +// clang-format off +#include +#include +// clang-format on +#elif __linux__ +#include +#endif + +namespace Cubed::Tools { + +inline void open_file_manager(const std::filesystem::path& path) { +#ifdef _WIN32 + + ShellExecuteW(nullptr, L"open", path.wstring().c_str(), nullptr, nullptr, + SW_SHOWNORMAL); + +#elif __linux__ + + constexpr const char* FILE_MANAGERS[] = {"nautilus", "dolphin", "thunar", + "nemo", "pcmanfm"}; + + auto is_available = [](const char* exe) { + std::string cmd = std::string("command -v ") + exe + " >/dev/null 2>&1"; + return std::system(cmd.c_str()) == 0; + }; + + const std::string QUOTED = "\"" + path.string() + "\""; + for (const char* fm : FILE_MANAGERS) { + if (!is_available(fm)) { + continue; + } + std::string cmd = std::string(fm) + " " + QUOTED + " >/dev/null 2>&1 &"; + if (std::system(cmd.c_str()) == 0) { + return; + } + } + + // Fallback: let the system choose a handler. + if (is_available("xdg-open")) { + std::string cmd = "xdg-open " + QUOTED + " >/dev/null 2>&1 &"; + if (std::system(cmd.c_str()) == 0) { + return; + } + } + + Logger::warn("Failed to open file manager for {}", path.string()); +#endif +} + +} // namespace Cubed::Tools \ No newline at end of file diff --git a/src/ui/screenshot_ui.cpp b/src/ui/screenshot_ui.cpp index b410b4f..1779c5b 100644 --- a/src/ui/screenshot_ui.cpp +++ b/src/ui/screenshot_ui.cpp @@ -4,6 +4,7 @@ #include "Cubed/localization.hpp" #include "Cubed/render/renderer.hpp" #include "Cubed/scene/screenshot_scene.hpp" +#include "Cubed/tools/file_utils.hpp" #include "Cubed/ui/button.hpp" #include "Cubed/ui/column_layout.hpp" #include "Cubed/ui/row_layout.hpp" @@ -45,6 +46,7 @@ void ScreenshotUI::update_layout(int width, int height) { constexpr float IMAGE_SCALE = 0.25; ScrollView* sv = nullptr; + fs::create_directories(path); if (fs::exists(path)) { for (auto& entry : fs::recursive_directory_iterator(path)) { if (!fs::is_regular_file(entry)) { @@ -109,20 +111,27 @@ void ScreenshotUI::update_layout(int width, int height) { .set_color(Color::WHITE) .set_anchor(Anchor::CENTER); } - auto& return_button = bg->add_child