Skip to content

Commit

Permalink
GH-304 Track recently accessed orchestrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed May 1, 2024
1 parent 7621afe commit 5f06fc6
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/editor/main_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ void OrchestratorMainView::_notification(int p_what)
{
if (p_what == NOTIFICATION_READY)
{
// Load the recent files
const Ref<ConfigFile> metadata = _plugin->get_metadata();
_recent_files = metadata->get_value("recent_files", "orchestrations", {});

set_anchors_preset(PRESET_FULL_RECT);
set_h_size_flags(SIZE_EXPAND_FILL);
set_v_size_flags(SIZE_EXPAND_FILL);
Expand Down Expand Up @@ -103,6 +107,15 @@ void OrchestratorMainView::_notification(int p_what)
_file_menu->get_popup()->clear();
_file_menu->get_popup()->add_item("New Orchestration...", AccelMenuIds::NEW, SKEY(KEY_MASK_CTRL, KEY_N));
_file_menu->get_popup()->add_item("Open...", AccelMenuIds::OPEN);

_recent_history = memnew(PopupMenu);
_recent_history->set_name("OrchestratorRecentHistory");
_recent_history->connect("index_pressed", callable_mp(this, &OrchestratorMainView::_on_recent_history_selected));
_file_menu->get_popup()->add_child(_recent_history);

_file_menu->get_popup()->add_submenu_item("Open Recent", _recent_history->get_name(), AccelMenuIds::OPEN_RECENT);
_update_recent_history();

_file_menu->get_popup()->add_separator();
_file_menu->get_popup()->add_item("Save", AccelMenuIds::SAVE, SKEY(KEY_MASK_CTRL | KEY_MASK_ALT, KEY_S));
_file_menu->get_popup()->add_item("Save As...", AccelMenuIds::SAVE_AS);
Expand Down Expand Up @@ -617,6 +630,30 @@ void OrchestratorMainView::_update_files_list()
}
}

void OrchestratorMainView::_update_recent_history()
{
_recent_history->clear();

for (const String& recent : _recent_files)
_recent_history->add_item(recent.replace("res://", ""));

_recent_history->add_separator();
_recent_history->add_item("Clear Recent Files");

if (_recent_files.is_empty())
_recent_history->set_item_disabled(1, true);
}

void OrchestratorMainView::_save_recent_history()
{
// Only save the last 10
_recent_files = _recent_files.slice(0, 10);

Ref<ConfigFile> metadata = _plugin->get_metadata();
metadata->set_value("recent_files", "orchestrations", _recent_files);
_plugin->save_metadata(metadata);
}

void OrchestratorMainView::_navigate_to_current_path()
{
if (_has_open_script())
Expand All @@ -637,6 +674,14 @@ void OrchestratorMainView::_show_script_editor_view(const String& p_file_name)

_current_index = _get_script_file_index_by_file_name(p_file_name);
_script_files[_current_index].editor->show();

if (_recent_files.has(p_file_name))
_recent_files.remove_at(_recent_files.find(p_file_name));

_recent_files.insert(0, p_file_name);

_save_recent_history();
_update_recent_history();
}

void OrchestratorMainView::_on_prepare_file_menu()
Expand Down Expand Up @@ -893,3 +938,20 @@ void OrchestratorMainView::_on_folder_removed(const String& p_folder_name)
i++;
}
}

void OrchestratorMainView::_on_recent_history_selected(int p_index)
{
if (p_index == _recent_history->get_item_count() - 1)
{
// Clear history recent
_recent_files.clear();

_save_recent_history();
_update_recent_history();
}
else
{
_show_script_editor_view(_recent_files[p_index]);
_update_files_list();
}
}
14 changes: 14 additions & 0 deletions src/editor/main_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace godot
class ItemList;
class LineEdit;
class MenuButton;
class PopupMenu;
class Script;
class ScriptCreateDialog;
}
Expand All @@ -54,6 +55,7 @@ class OrchestratorMainView : public Control
{
NEW,
OPEN,
OPEN_RECENT,
SAVE,
SAVE_AS,
SAVE_ALL,
Expand Down Expand Up @@ -95,12 +97,14 @@ class OrchestratorMainView : public Control
Control* _updater{ nullptr }; //! Updater widget
ConfirmationDialog* _close_confirm{ nullptr }; //! Confirmation dialog
ConfirmationDialog* _goto_dialog{ nullptr }; //! Goto node dialog
PopupMenu* _recent_history{ nullptr }; //! Recent history
List<ScriptFile> _script_close_queue; //! Queue of scripts to be removed
OrchestratorPlugin* _plugin{ nullptr }; //! Orchestrator plugin
bool _floating{ false }; //! Whether this window is floating
Control* _select_separator{ nullptr }; //! Screen selection separator
OrchestratorScreenSelect* _select{ nullptr }; //! Screen selection
OrchestratorWindowWrapper* _wrapper{ nullptr }; //! Window wrapper
PackedStringArray _recent_files; //! Recent files list

static void _bind_methods();
OrchestratorMainView() = default;
Expand Down Expand Up @@ -179,6 +183,12 @@ class OrchestratorMainView : public Control
/// Dispatches an update to the files list
void _update_files_list();

/// Updates the reecnt history
void _update_recent_history();

/// Saves the recent history to disk
void _save_recent_history();

/// Navigate to the current file in the FileSystemDock
void _navigate_to_current_path();

Expand Down Expand Up @@ -252,6 +262,10 @@ class OrchestratorMainView : public Control
/// Dispatched when a folder is removed from the project
/// @param p_folder_name the folder name
void _on_folder_removed(const String& p_folder_name);

/// When a recent history item is selected
/// @param p_index the selected index
void _on_recent_history_selected(int p_index);
};

#endif // ORCHESTRATOR_MAIN_VIEW_H
22 changes: 21 additions & 1 deletion src/plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include <godot_cpp/classes/control.hpp>
#include <godot_cpp/classes/display_server.hpp>
#include <godot_cpp/classes/editor_interface.hpp>
#include <godot_cpp/classes/editor_paths.hpp>
#include <godot_cpp/classes/editor_settings.hpp>
#include <godot_cpp/classes/label.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/theme_db.hpp>
#include <godot_cpp/classes/theme.hpp>
#include <godot_cpp/classes/theme_db.hpp>
#include <godot_cpp/classes/v_box_container.hpp>
#include <godot_cpp/core/class_db.hpp>

Expand Down Expand Up @@ -179,6 +180,25 @@ Ref<Texture2D> OrchestratorPlugin::get_plugin_icon_hires() const
return ResourceLoader::get_singleton()->load("res://addons/orchestrator/icons/Orchestrator_Logo.svg");
}

Ref<ConfigFile> OrchestratorPlugin::get_metadata()
{
const String file = get_editor_interface()->get_editor_paths()->get_project_settings_dir()
.path_join("orchestrator_metadata.cfg");

Ref<ConfigFile> metadata(memnew(ConfigFile));
metadata->load(file);

return metadata;
}

void OrchestratorPlugin::save_metadata(const Ref<ConfigFile>& p_metadata)
{
const String file = get_editor_interface()->get_editor_paths()->get_project_settings_dir()
.path_join("orchestrator_metadata.cfg");

p_metadata->save(file);
}

void OrchestratorPlugin::request_editor_restart()
{
AcceptDialog* request = memnew(AcceptDialog);
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class OrchestratorPlugin : public EditorPlugin
/// @return high-res texture
Ref<Texture2D> get_plugin_icon_hires() const;

/// Get the plugin's editor metadata configuration
/// @return the metadata configuration file
Ref<ConfigFile> get_metadata();

/// Saves the metadata
/// @param p_metadata the metadata to save
void save_metadata(const Ref<ConfigFile>& p_metadata);

//~ Begin EditorPlugin interface
String get_plugin_version() const;
void _edit(Object* p_object) override;
Expand Down

0 comments on commit 5f06fc6

Please sign in to comment.