use the persistence class to store recently opened decks

This commit is contained in:
Lera Elvoé 2023-12-13 14:30:11 +03:00
parent 1ebcef1d0e
commit 68d9af52c6
No known key found for this signature in database
2 changed files with 43 additions and 2 deletions

View file

@ -9,6 +9,8 @@ class_name DeckHolderRenderer
const DECK_SCENE := preload("res://graph_node_renderer/deck_renderer_graph_edit.tscn") 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 DEBUG_DECKS_LIST := preload("res://graph_node_renderer/debug_decks_list.tscn")
const PERSISTENCE_NAMESPACE := "default"
## Reference to the main windows [TabContainerCustom] ## Reference to the main windows [TabContainerCustom]
@onready var tab_container: TabContainerCustom = %TabContainerCustom as TabContainerCustom @onready var tab_container: TabContainerCustom = %TabContainerCustom as TabContainerCustom
## Reference to the [FileDialog] used for File operations through the program. ## Reference to the [FileDialog] used for File operations through the program.
@ -27,9 +29,11 @@ enum FileMenuId {
SAVE = 3, SAVE = 3,
SAVE_AS, SAVE_AS,
CLOSE = 6, CLOSE = 6,
RECENTS,
} }
@onready var file_popup_menu: PopupMenu = %File as PopupMenu @onready var file_popup_menu: PopupMenu = %File as PopupMenu
var max_recents := 4
var recent_files := []
enum ConnectionsMenuId { enum ConnectionsMenuId {
OBS, OBS,
@ -48,6 +52,13 @@ var _deck_to_save: WeakRef
func _ready() -> void: func _ready() -> void:
tab_container.add_button_pressed.connect(add_empty_deck) 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( tab_container.tab_close_requested.connect(
func(tab: int): 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) tab_container.set_tab_metadata(tab_container.get_current_tab(), deck.id)
inst.initialize_from_deck() inst.initialize_from_deck()
inst.group_enter_requested.connect(_on_deck_renderer_group_enter_requested) inst.group_enter_requested.connect(_on_deck_renderer_group_enter_requested)
add_recent_file(path)
## Gets the currently active [Deck] from [member tab_container] ## Gets the currently active [Deck] from [member tab_container]
func get_active_deck() -> Deck: 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) tab_container.set_tab_metadata(tab_container.get_current_tab(), deck.id)
inst.initialize_from_deck() inst.initialize_from_deck()
inst.group_enter_requested.connect(_on_deck_renderer_group_enter_requested) 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)

View file

@ -93,7 +93,7 @@ layout_mode = 2
[node name="File" type="PopupMenu" parent="MarginContainer/VSplitContainer/VBoxContainer/MenuBar"] [node name="File" type="PopupMenu" parent="MarginContainer/VSplitContainer/VBoxContainer/MenuBar"]
unique_name_in_owner = true unique_name_in_owner = true
item_count = 7 item_count = 8
item_0/text = "New Deck" item_0/text = "New Deck"
item_0/id = 0 item_0/id = 0
item_1/text = "Open Deck" item_1/text = "Open Deck"
@ -110,6 +110,9 @@ item_5/id = 5
item_5/separator = true item_5/separator = true
item_6/text = "Close Deck" item_6/text = "Close Deck"
item_6/id = 6 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"] [node name="Edit" type="PopupMenu" parent="MarginContainer/VSplitContainer/VBoxContainer/MenuBar"]
unique_name_in_owner = true unique_name_in_owner = true