Route dialog work directory picker through platform services

This commit is contained in:
2026-06-04 18:14:02 +02:00
parent 52cf7628da
commit b1acd5118b
11 changed files with 164 additions and 78 deletions

View File

@@ -184,6 +184,8 @@ public:
void pick_file_save(const std::string& type, const std::string& default_name,
std::function<void(std::string path)> writer, std::function<void(const std::string& path, bool saved)> callback);
void pick_file_save(std::vector<std::string> types, std::function<void(std::string path)> callback);
[[nodiscard]] bool supports_working_directory_picker() const;
[[nodiscard]] std::string format_working_directory_path(std::string_view path) const;
[[nodiscard]] bool uses_prepared_file_writes() const;
[[nodiscard]] bool uses_work_directory_document_export_collections() const;
[[nodiscard]] bool disables_network_tls_verification() const;

View File

@@ -217,6 +217,16 @@ void App::pick_dir(std::function<void(std::string path)> callback)
active_platform_services().pick_directory(std::move(callback));
}
bool App::supports_working_directory_picker() const
{
return active_platform_services().supports_working_directory_picker();
}
std::string App::format_working_directory_path(std::string_view path) const
{
return active_platform_services().format_working_directory_path(path);
}
void App::display_file(std::string path)
{
if (pp::app::plan_display_file(path) == pp::app::DisplayFileAction::ignore_empty_path)

View File

@@ -69,32 +69,28 @@ void NodeDialogBrowse::init_controls()
};
container = find<Node>("files-list");
init_list();
#if defined(_WIN32) || defined(__OSX__)
static char path_buffer[256];
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*){
App::I->pick_dir([this](std::string path){
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
#ifdef _WIN32
GetFullPathNameA(path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(path.c_str(), path_buffer);
#endif
working_path->set_text_format("Destination dir: %s", path_buffer);
search_paths = {path};
clear_list();
init_list();
});
};
working_path = find<NodeText>("path");
#ifdef _WIN32
GetFullPathNameA(App::I->work_path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(App::I->work_path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
#endif
if (App::I->supports_working_directory_picker())
{
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*) {
App::I->pick_dir([this](std::string path) {
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
const auto display_path = App::I->format_working_directory_path(path);
working_path->set_text_format(
"Destination dir: %s",
display_path.c_str());
search_paths = { path };
clear_list();
init_list();
});
};
working_path = find<NodeText>("path");
const auto display_path = App::I->format_working_directory_path(App::I->work_path);
working_path->set_text_format(
"Working dir: %s",
display_path.c_str());
}
// if (auto* first = (NodeDialogBrowseItem*)container->get_child_at(0))
// {
// first->on_selected(first);

View File

@@ -188,29 +188,25 @@ void NodeDialogSave::init_controls()
if (btn_ok->on_click)
btn_ok->on_click(btn_ok);
};
#if defined(_WIN32) || defined(__OSX__)
static char path_buffer[256];
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*){
App::I->pick_dir([this](std::string path){
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
#ifdef _WIN32
GetFullPathNameA(path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
});
};
working_path = find<NodeText>("path");
#ifdef _WIN32
GetFullPathNameA(App::I->work_path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(App::I->work_path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
#endif
if (App::I->supports_working_directory_picker())
{
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*) {
App::I->pick_dir([this](std::string path) {
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
const auto display_path = App::I->format_working_directory_path(path);
working_path->set_text_format(
"Working dir: %s",
display_path.c_str());
});
};
working_path = find<NodeText>("path");
const auto display_path = App::I->format_working_directory_path(App::I->work_path);
working_path->set_text_format(
"Working dir: %s",
display_path.c_str());
}
}
void NodeDialogSave::loaded()
{
@@ -253,29 +249,25 @@ void NodeDialogNewDoc::init_controls()
if (btn_ok->on_click)
btn_ok->on_click(btn_ok);
};
#if defined(_WIN32) || defined(__OSX__)
static char path_buffer[256];
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*){
App::I->pick_dir([this](std::string path){
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
#ifdef _WIN32
GetFullPathNameA(path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
});
};
working_path = find<NodeText>("path");
#ifdef _WIN32
GetFullPathNameA(App::I->work_path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(App::I->work_path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
#endif
if (App::I->supports_working_directory_picker())
{
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*) {
App::I->pick_dir([this](std::string path) {
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
const auto display_path = App::I->format_working_directory_path(path);
working_path->set_text_format(
"Working dir: %s",
display_path.c_str());
});
};
working_path = find<NodeText>("path");
const auto display_path = App::I->format_working_directory_path(App::I->work_path);
working_path->set_text_format(
"Working dir: %s",
display_path.c_str());
}
}
void NodeDialogNewDoc::loaded()
{

View File

@@ -63,6 +63,8 @@ public:
virtual void pick_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
virtual void pick_save_file(std::vector<std::string> file_types, PickedPathCallback callback) = 0;
virtual void pick_directory(PickedPathCallback callback) = 0;
[[nodiscard]] virtual bool supports_working_directory_picker() = 0;
[[nodiscard]] virtual std::string format_working_directory_path(std::string_view path) = 0;
[[nodiscard]] virtual bool uses_prepared_file_writes() = 0;
[[nodiscard]] virtual bool uses_work_directory_document_export_collections() = 0;
[[nodiscard]] virtual bool disables_network_tls_verification() = 0;

View File

@@ -432,6 +432,25 @@ public:
#endif
}
[[nodiscard]] bool supports_working_directory_picker() override
{
#if defined(__OSX__)
return true;
#else
return false;
#endif
}
[[nodiscard]] std::string format_working_directory_path(std::string_view path) override
{
#if defined(__OSX__)
char path_buffer[4096] = {};
if (realpath(std::string(path).c_str(), path_buffer))
return path_buffer;
#endif
return std::string(path);
}
[[nodiscard]] bool uses_prepared_file_writes() override
{
#if __IOS__ || __WEB__

View File

@@ -449,6 +449,24 @@ public:
invoke_selected_path(path, callback);
}
[[nodiscard]] bool supports_working_directory_picker() override
{
return true;
}
[[nodiscard]] std::string format_working_directory_path(std::string_view path) override
{
char path_buffer[MAX_PATH] = {};
const auto length = GetFullPathNameA(
std::string(path).c_str(),
static_cast<DWORD>(sizeof(path_buffer)),
path_buffer,
nullptr);
if (length > 0 && length < sizeof(path_buffer))
return path_buffer;
return std::string(path);
}
[[nodiscard]] bool uses_prepared_file_writes() override
{
return false;