From 3615b188d7b2aaa8668d15d65405265684302b2d Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 30 Jun 2025 16:43:34 +0200 Subject: [PATCH] Expose methods to access FileDialog's favorite/recent lists --- doc/classes/FileDialog.xml | 28 +++++++++++++++++ scene/gui/file_dialog.cpp | 63 ++++++++++++++++++++++++++++++++++++++ scene/gui/file_dialog.h | 6 ++++ 3 files changed, 97 insertions(+) diff --git a/doc/classes/FileDialog.xml b/doc/classes/FileDialog.xml index 8a1b38c5f19..bfa0c329490 100644 --- a/doc/classes/FileDialog.xml +++ b/doc/classes/FileDialog.xml @@ -47,6 +47,12 @@ Clear all currently selected items in the dialog. + + + + Returns the list of favorite directories, which is shared by all [FileDialog] nodes. Useful to store the list of favorites between project sessions. This method can be called only from the main thread. + + @@ -75,6 +81,12 @@ Returns an array of values of the [OptionButton] with index [param option]. + + + + Returns the list of recent directories, which is shared by all [FileDialog] nodes. Useful to store the list of recents between project sessions. This method can be called only from the main thread. + + @@ -111,6 +123,14 @@ Toggles the specified customization [param flag], allowing to customize features available in this [FileDialog]. See [enum Customization] for options. + + + + + Sets the list of favorite directories, which is shared by all [FileDialog] nodes. Useful to restore the list of favorites saved with [method get_favorite_list]. This method can be called only from the main thread. + [b]Note:[/b] [FileDialog] will update its internal [ItemList] of favorites when its visibility changes. Be sure to call this method earlier if you want your changes to have effect. + + @@ -135,6 +155,14 @@ Sets the option values of the [OptionButton] with index [param option]. + + + + + Sets the list of recent directories, which is shared by all [FileDialog] nodes. Useful to restore the list of recents saved with [method set_recent_list]. This method can be called only from the main thread. + [b]Note:[/b] [FileDialog] will update its internal [ItemList] of recent directories when its visibility changes. Be sure to call this method earlier if you want your changes to have effect. + + diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index 3d7a3891ece..52db20327ba 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -1335,6 +1335,64 @@ FileDialog::DisplayMode FileDialog::get_display_mode() const { return display_mode; } +void FileDialog::set_favorite_list(const PackedStringArray &p_favorites) { + ERR_FAIL_COND_MSG(Thread::get_caller_id() != Thread::get_main_id(), "Setting favorite list can only be done on the main thread."); + + global_favorites.clear(); + global_favorites.reserve(p_favorites.size()); + for (const String &fav : p_favorites) { + if (fav.ends_with("/")) { + global_favorites.push_back(fav); + } else { + global_favorites.push_back(fav + "/"); + } + } +} + +PackedStringArray FileDialog::get_favorite_list() { + PackedStringArray ret; + ERR_FAIL_COND_V_MSG(Thread::get_caller_id() != Thread::get_main_id(), ret, "Getting favorite list can only be done on the main thread."); + + ret.resize(global_favorites.size()); + + String *fav_write = ret.ptrw(); + int i = 0; + for (const String &fav : global_favorites) { + fav_write[i] = fav; + i++; + } + return ret; +} + +void FileDialog::set_recent_list(const PackedStringArray &p_recents) { + ERR_FAIL_COND_MSG(Thread::get_caller_id() != Thread::get_main_id(), "Setting recent list can only be done on the main thread."); + + global_recents.clear(); + global_recents.reserve(p_recents.size()); + for (const String &recent : p_recents) { + if (recent.ends_with("/")) { + global_recents.push_back(recent); + } else { + global_recents.push_back(recent + "/"); + } + } +} + +PackedStringArray FileDialog::get_recent_list() { + PackedStringArray ret; + ERR_FAIL_COND_V_MSG(Thread::get_caller_id() != Thread::get_main_id(), ret, "Getting recent list can only be done on the main thread."); + + ret.resize(global_recents.size()); + + String *recent_write = ret.ptrw(); + int i = 0; + for (const String &recent : global_recents) { + recent_write[i] = recent; + i++; + } + return ret; +} + void FileDialog::set_customization_flag_enabled(Customization p_flag, bool p_enabled) { ERR_FAIL_INDEX(p_flag, CUSTOMIZATION_MAX); if (customization_flags[p_flag] == p_enabled) { @@ -1921,6 +1979,11 @@ void FileDialog::_bind_methods() { ClassDB::bind_method(D_METHOD("is_customization_flag_enabled", "flag"), &FileDialog::is_customization_flag_enabled); ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all); + ClassDB::bind_static_method("FileDialog", D_METHOD("set_favorite_list", "favorites"), &FileDialog::set_favorite_list); + ClassDB::bind_static_method("FileDialog", D_METHOD("get_favorite_list"), &FileDialog::get_favorite_list); + ClassDB::bind_static_method("FileDialog", D_METHOD("set_recent_list", "recents"), &FileDialog::set_recent_list); + ClassDB::bind_static_method("FileDialog", D_METHOD("get_recent_list"), &FileDialog::get_recent_list); + ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title"); diff --git a/scene/gui/file_dialog.h b/scene/gui/file_dialog.h index f69356f019f..e8b94fba286 100644 --- a/scene/gui/file_dialog.h +++ b/scene/gui/file_dialog.h @@ -408,6 +408,12 @@ public: void set_display_mode(DisplayMode p_mode); DisplayMode get_display_mode() const; + static void set_favorite_list(const PackedStringArray &p_favorites); + static PackedStringArray get_favorite_list(); + + static void set_recent_list(const PackedStringArray &p_recents); + static PackedStringArray get_recent_list(); + void set_customization_flag_enabled(Customization p_flag, bool p_enabled); bool is_customization_flag_enabled(Customization p_flag) const;