From 96af008bef67a4df05558b9d26086989f959a5f0 Mon Sep 17 00:00:00 2001 From: AR Date: Mon, 16 Mar 2026 13:51:35 +0500 Subject: [PATCH 1/2] Add logic for tilde symbol expansion --- core/os/os.cpp | 4 ++++ core/os/os.h | 2 ++ drivers/unix/os_unix.cpp | 13 +++++++++++++ drivers/unix/os_unix.h | 2 ++ platform/windows/os_windows.cpp | 13 +++++++++++++ platform/windows/os_windows.h | 2 ++ scene/gui/file_dialog.cpp | 18 ++++++++++++------ 7 files changed, 48 insertions(+), 6 deletions(-) diff --git a/core/os/os.cpp b/core/os/os.cpp index 79d015359d0..2e298f5637b 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -349,6 +349,10 @@ String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const { return "."; } +String OS::expand_path(const String &p_path) const { + return p_path; +} + void OS::create_lock_file() { if (Engine::get_singleton()->is_recovery_mode_hint()) { return; diff --git a/core/os/os.h b/core/os/os.h index a1118b58cea..8a4d74a5dc4 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -314,6 +314,8 @@ class OS { virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const; + virtual String expand_path(const String &p_path) const; + virtual Error move_to_trash(const String &p_path) { return FAILED; } void create_lock_file(); diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 0c29246f0f9..de48cd944f0 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -1191,6 +1191,19 @@ String OS_Unix::get_executable_path() const { #endif } +String OS_Unix::expand_path(const String &p_path) const { + String path = p_path; + + if (path.begins_with("~/") || path == "~") { + String home = get_environment("HOME"); + if (!home.is_empty()) { + path = home + path.substr(1); + } + } + + return path; +} + void UnixTerminalLogger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector> &p_script_backtraces) { if (!should_log(true)) { return; diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 7a2a115ec05..6bdb55caf1d 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -142,6 +142,8 @@ class OS_Unix : public OS { virtual String get_executable_path() const override; virtual String get_user_data_dir(const String &p_user_dir) const override; + + virtual String expand_path(const String &p_path) const override; }; class UnixTerminalLogger : public StdLogger { diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index b0083d0bc11..bea618c9dc9 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2500,6 +2500,19 @@ String OS_Windows::get_user_data_dir(const String &p_user_dir) const { return get_data_path().path_join(p_user_dir).replace_char('\\', '/'); } +String OS_Windows::expand_path(const String &p_path) const { + String path = p_path; + + if (path.begins_with("~/") || path.begins_with("~\\") || path == "~") { + String home = get_environment("USERPROFILE"); + if (!home.is_empty()) { + path = home + path.substr(1); + } + } + + return path; +} + String OS_Windows::get_unique_id() const { HW_PROFILE_INFOA HwProfInfo; ERR_FAIL_COND_V(!GetCurrentHwProfileA(&HwProfInfo), ""); diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index f81fc2dad5f..81668bc0eed 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -246,6 +246,8 @@ class OS_Windows : public OS { virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override; virtual String get_user_data_dir(const String &p_user_dir) const override; + virtual String expand_path(const String &p_path) const override; + virtual String get_unique_id() const override; virtual Error shell_open(const String &p_uri) override; diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index a77a4b71bb1..52b3430b0e8 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -393,7 +393,7 @@ void FileDialog::update_dir() { } void FileDialog::_dir_submitted(String p_dir) { - String new_dir = p_dir; + String new_dir = OS::get_singleton()->expand_path(p_dir); #ifdef WINDOWS_ENABLED if (root_prefix.is_empty() && drives->is_visible() && !new_dir.is_network_share_path() && new_dir.is_absolute_path() && new_dir.find(":/") == -1 && new_dir.find(":\\") == -1) { // Non network path without X:/ prefix on Windows, add drive letter. @@ -466,6 +466,9 @@ void FileDialog::_action_pressed() { } String file_text = filename_edit->get_text(); + + file_text = OS::get_singleton()->expand_path(file_text); + String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text); if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && (dir_access->file_exists(f) || dir_access->is_bundle(f))) { @@ -1203,7 +1206,7 @@ String FileDialog::get_current_path() const { } void FileDialog::set_current_dir(const String &p_dir) { - _change_dir(p_dir); + _change_dir(OS::get_singleton()->expand_path(p_dir)); _push_history(); } @@ -1222,12 +1225,15 @@ void FileDialog::set_current_path(const String &p_path) { if (!p_path.size()) { return; } - int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\')); + + String path = OS::get_singleton()->expand_path(p_path); + + int pos = MAX(path.rfind_char('/'), path.rfind_char('\\')); if (pos == -1) { - set_current_file(p_path); + set_current_file(path); } else { - String path_dir = p_path.substr(0, pos); - String path_file = p_path.substr(pos + 1); + String path_dir = path.substr(0, pos); + String path_file = path.substr(pos + 1); set_current_dir(path_dir); set_current_file(path_file); } From f7c9e4e654653c8de9ae7cf667f291f33b9e6b87 Mon Sep 17 00:00:00 2001 From: AbdulRehman Date: Mon, 16 Mar 2026 14:44:38 +0500 Subject: [PATCH 2/2] Apply suggestions Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- platform/windows/os_windows.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index bea618c9dc9..54e7807fb48 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2501,10 +2501,10 @@ String OS_Windows::get_user_data_dir(const String &p_user_dir) const { } String OS_Windows::expand_path(const String &p_path) const { - String path = p_path; + String path = p_path.replace_char('\\', '/'); - if (path.begins_with("~/") || path.begins_with("~\\") || path == "~") { - String home = get_environment("USERPROFILE"); + if (path.begins_with("~/") || path == "~") { + String home = get_environment("USERPROFILE").replace_char('\\', '/').rstrip("/"); if (!home.is_empty()) { path = home + path.substr(1); }