From 68d9af52c69607f2b965ac2402d50199b3bd5973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 13 Dec 2023 14:30:11 +0300 Subject: [PATCH] use the persistence class to store recently opened decks --- graph_node_renderer/deck_holder_renderer.gd | 40 ++++++++++++++++++- graph_node_renderer/deck_holder_renderer.tscn | 5 ++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/graph_node_renderer/deck_holder_renderer.gd b/graph_node_renderer/deck_holder_renderer.gd index 15008b7..1b3d7cb 100644 --- a/graph_node_renderer/deck_holder_renderer.gd +++ b/graph_node_renderer/deck_holder_renderer.gd @@ -9,6 +9,8 @@ class_name DeckHolderRenderer const DECK_SCENE := preload("res://graph_node_renderer/deck_renderer_graph_edit.tscn") const DEBUG_DECKS_LIST := preload("res://graph_node_renderer/debug_decks_list.tscn") +const PERSISTENCE_NAMESPACE := "default" + ## Reference to the main windows [TabContainerCustom] @onready var tab_container: TabContainerCustom = %TabContainerCustom as TabContainerCustom ## Reference to the [FileDialog] used for File operations through the program. @@ -27,9 +29,11 @@ enum FileMenuId { SAVE = 3, SAVE_AS, CLOSE = 6, + RECENTS, } @onready var file_popup_menu: PopupMenu = %File as PopupMenu - +var max_recents := 4 +var recent_files := [] enum ConnectionsMenuId { OBS, @@ -48,6 +52,13 @@ var _deck_to_save: WeakRef func _ready() -> void: tab_container.add_button_pressed.connect(add_empty_deck) + RendererPersistence.init_namespace(PERSISTENCE_NAMESPACE) + + recent_files = RendererPersistence.get_or_create( + PERSISTENCE_NAMESPACE, "config", + "recent_files", [] + ) + add_recents_to_menu() tab_container.tab_close_requested.connect( func(tab: int): @@ -139,6 +150,7 @@ func _on_file_dialog_open_files(paths: PackedStringArray) -> void: tab_container.set_tab_metadata(tab_container.get_current_tab(), deck.id) inst.initialize_from_deck() inst.group_enter_requested.connect(_on_deck_renderer_group_enter_requested) + add_recent_file(path) ## Gets the currently active [Deck] from [member tab_container] func get_active_deck() -> Deck: @@ -227,3 +239,29 @@ func _on_debug_decks_viewer_item_pressed(deck_id: String, instance_id: String) - tab_container.set_tab_metadata(tab_container.get_current_tab(), deck.id) inst.initialize_from_deck() inst.group_enter_requested.connect(_on_deck_renderer_group_enter_requested) + + +func add_recent_file(path: String) -> void: + var item := recent_files.find(path) + if item == -1: + recent_files.push_front(path) + else: + recent_files.push_front(recent_files.pop_at(item)) + recent_files = recent_files.slice(0, max_recents) + + add_recents_to_menu() + + +func add_recents_to_menu() -> void: + if recent_files.is_empty(): + return + + if file_popup_menu.get_item_count() > FileMenuId.RECENTS + 1: + for i in range(FileMenuId.RECENTS, file_popup_menu.get_item_count() - FileMenuId.RECENTS + 1): + file_popup_menu.remove_item(i) + + for i in recent_files: + file_popup_menu.add_item(i) + + RendererPersistence.set_value(PERSISTENCE_NAMESPACE, "config", "recent_files", recent_files) + RendererPersistence.commit(PERSISTENCE_NAMESPACE) diff --git a/graph_node_renderer/deck_holder_renderer.tscn b/graph_node_renderer/deck_holder_renderer.tscn index 19cc642..91f250e 100644 --- a/graph_node_renderer/deck_holder_renderer.tscn +++ b/graph_node_renderer/deck_holder_renderer.tscn @@ -93,7 +93,7 @@ layout_mode = 2 [node name="File" type="PopupMenu" parent="MarginContainer/VSplitContainer/VBoxContainer/MenuBar"] unique_name_in_owner = true -item_count = 7 +item_count = 8 item_0/text = "New Deck" item_0/id = 0 item_1/text = "Open Deck" @@ -110,6 +110,9 @@ item_5/id = 5 item_5/separator = true item_6/text = "Close Deck" item_6/id = 6 +item_7/text = "Recent Decks" +item_7/id = 7 +item_7/separator = true [node name="Edit" type="PopupMenu" parent="MarginContainer/VSplitContainer/VBoxContainer/MenuBar"] unique_name_in_owner = true