Skip to content

Commit 360a8d3

Browse files
authored
Add logic for tilde symbol expansion (#1233)
* Add logic for tilde symbol expansion * Apply suggestions
1 parent ed42d73 commit 360a8d3

7 files changed

Lines changed: 48 additions & 6 deletions

File tree

core/os/os.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
349349
return ".";
350350
}
351351

352+
String OS::expand_path(const String &p_path) const {
353+
return p_path;
354+
}
355+
352356
void OS::create_lock_file() {
353357
if (Engine::get_singleton()->is_recovery_mode_hint()) {
354358
return;

core/os/os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ class OS {
314314

315315
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
316316

317+
virtual String expand_path(const String &p_path) const;
318+
317319
virtual Error move_to_trash(const String &p_path) { return FAILED; }
318320

319321
void create_lock_file();

drivers/unix/os_unix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,19 @@ String OS_Unix::get_executable_path() const {
11911191
#endif
11921192
}
11931193

1194+
String OS_Unix::expand_path(const String &p_path) const {
1195+
String path = p_path;
1196+
1197+
if (path.begins_with("~/") || path == "~") {
1198+
String home = get_environment("HOME");
1199+
if (!home.is_empty()) {
1200+
path = home + path.substr(1);
1201+
}
1202+
}
1203+
1204+
return path;
1205+
}
1206+
11941207
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<Ref<ScriptBacktrace>> &p_script_backtraces) {
11951208
if (!should_log(true)) {
11961209
return;

drivers/unix/os_unix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class OS_Unix : public OS {
142142

143143
virtual String get_executable_path() const override;
144144
virtual String get_user_data_dir(const String &p_user_dir) const override;
145+
146+
virtual String expand_path(const String &p_path) const override;
145147
};
146148

147149
class UnixTerminalLogger : public StdLogger {

platform/windows/os_windows.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,6 +2500,19 @@ String OS_Windows::get_user_data_dir(const String &p_user_dir) const {
25002500
return get_data_path().path_join(p_user_dir).replace_char('\\', '/');
25012501
}
25022502

2503+
String OS_Windows::expand_path(const String &p_path) const {
2504+
String path = p_path.replace_char('\\', '/');
2505+
2506+
if (path.begins_with("~/") || path == "~") {
2507+
String home = get_environment("USERPROFILE").replace_char('\\', '/').rstrip("/");
2508+
if (!home.is_empty()) {
2509+
path = home + path.substr(1);
2510+
}
2511+
}
2512+
2513+
return path;
2514+
}
2515+
25032516
String OS_Windows::get_unique_id() const {
25042517
HW_PROFILE_INFOA HwProfInfo;
25052518
ERR_FAIL_COND_V(!GetCurrentHwProfileA(&HwProfInfo), "");

platform/windows/os_windows.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class OS_Windows : public OS {
246246
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const override;
247247
virtual String get_user_data_dir(const String &p_user_dir) const override;
248248

249+
virtual String expand_path(const String &p_path) const override;
250+
249251
virtual String get_unique_id() const override;
250252

251253
virtual Error shell_open(const String &p_uri) override;

scene/gui/file_dialog.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void FileDialog::update_dir() {
393393
}
394394

395395
void FileDialog::_dir_submitted(String p_dir) {
396-
String new_dir = p_dir;
396+
String new_dir = OS::get_singleton()->expand_path(p_dir);
397397
#ifdef WINDOWS_ENABLED
398398
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) {
399399
// Non network path without X:/ prefix on Windows, add drive letter.
@@ -466,6 +466,9 @@ void FileDialog::_action_pressed() {
466466
}
467467

468468
String file_text = filename_edit->get_text();
469+
470+
file_text = OS::get_singleton()->expand_path(file_text);
471+
469472
String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);
470473

471474
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 {
12031206
}
12041207

12051208
void FileDialog::set_current_dir(const String &p_dir) {
1206-
_change_dir(p_dir);
1209+
_change_dir(OS::get_singleton()->expand_path(p_dir));
12071210

12081211
_push_history();
12091212
}
@@ -1222,12 +1225,15 @@ void FileDialog::set_current_path(const String &p_path) {
12221225
if (!p_path.size()) {
12231226
return;
12241227
}
1225-
int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\'));
1228+
1229+
String path = OS::get_singleton()->expand_path(p_path);
1230+
1231+
int pos = MAX(path.rfind_char('/'), path.rfind_char('\\'));
12261232
if (pos == -1) {
1227-
set_current_file(p_path);
1233+
set_current_file(path);
12281234
} else {
1229-
String path_dir = p_path.substr(0, pos);
1230-
String path_file = p_path.substr(pos + 1);
1235+
String path_dir = path.substr(0, pos);
1236+
String path_file = path.substr(pos + 1);
12311237
set_current_dir(path_dir);
12321238
set_current_file(path_file);
12331239
}

0 commit comments

Comments
 (0)