Optimize adding or removing many tabs at once
SaveData has methods for adding or removing a tab. They operate on a single tab and also change the active tab appropriately. Adding a new tab makes it active, and removing the active tab forces it to change to a different one.
However, when multiple tabs are added at once (multi file import) or removed at once (closing multiple tabs), changing the active tab multiple times is unnecessary, and it's also something that currently always triggers a cascade of expensive operations.
It's very hard to rework this without changing the behavior, because tab removal can be paused at any point if a tab isn't saved. Also, adding tabs can be paused by missing files, import warnings, or a dialogue mentioning that the tab is already being edited inside the app. Tab additions and removals are currently processed one by one, because it's very hard to make safe batches.
For tab adding, this is particularly important, because this means multi file import is as slow as loading everything combined. For tab removing, it's less important, since this slow case doesn't always get triggered.
# Adds new tabs with the given paths. The last added tab is activated.
# If the last path already exists, the tab using it is set as the active tab instead.
func add_tabs_with_paths(new_file_paths: PackedStringArray) -> void:
var last_tab_index = -1
for new_file_path in new_file_paths:
var existing_tab_index = -1
for idx in _tabs.size():
if _tabs[idx].svg_file_path == new_file_path:
existing_tab_index = idx
break
if existing_tab_index != -1:
last_tab_index = existing_tab_index
else:
_add_new_tab()
_tabs[-1].svg_file_path = new_file_path
last_tab_index = _tabs.size() - 1
# Activate the last processed tab and emit signals.
if last_tab_index != -1:
emit_changed()
Configs.tabs_changed.emit()
set_active_tab_index(last_tab_index)
Code I wrote for an addition method for SaveData that accepts many paths. Not tested.