Fix live tree selection lost after wiresheet DOM rebuild

After open_wiresheet rebuilds the DOM and tree, re-find the target
element by its comp id (not the old tree item ID which no longer
exists) and re-select it with the restoring flag to prevent recursion.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 00:24:10 +03:00
co-authored by Claude Opus 4.6
parent 1fd6e72b8c
commit 67c403339c
2 changed files with 14 additions and 4 deletions
+13 -3
View File
@@ -578,7 +578,7 @@ def on_canvas_wheel_zoom(event):
def on_tree_select(event):
"""Triggered when a branch node is clicked in the Navigation view."""
global _pending_link_selection
global _pending_link_selection, _restoring_selection
# Read and cleared first thing, so a link a jump asked for is only ever picked out
# by the very selection that jump made. A selection that turns out not to paint a
@@ -621,8 +621,18 @@ def on_tree_select(event):
ds.build_links_elements(sax_root)
app_state.xml_root_element = sax_root
sax_file.refresh_treeview_from_dom(sax_root)
# re-find the element after DOM rebuild
element = app_state.tree_element_map.get(selected_id)
# re-find element by comp id after DOM rebuild (tree item IDs changed)
element = None
for tid, elem in app_state.tree_element_map.items():
if elem.get("id") == comp_id_str:
element = elem
selected_id = tid
break
if element is not None:
_restoring_selection = True
tree.selection_set(selected_id)
tree.see(selected_id)
tree.after_idle(_clear_restoring_flag)
app_state.current_sheet_parent = element
except Exception:
pass