Fix tree expand in live mode: use focused item and run IO in background

_on_tree_open used tree.selection() which may differ from the opened
node; switched to tree.focus(). Network calls in _live_tree_expand now
run in a daemon thread so the UI stays responsive during lazy loading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 09:11:14 +03:00
co-authored by Claude Opus 4.6
parent 67c403339c
commit d32285abe8
2 changed files with 35 additions and 13 deletions
+34 -12
View File
@@ -1352,13 +1352,13 @@ def attach_tree_scrollbars(container, tree):
def _on_tree_open(event):
tree.after_idle(fit_tree_column, tree)
if app_state.live_mode:
sel = tree.selection()
if sel:
elem = app_state.tree_element_map.get(sel[0])
opened = tree.focus()
if opened:
elem = app_state.tree_element_map.get(opened)
if elem is not None:
cid = elem.get("id")
if cid is not None:
_live_tree_expand(int(cid))
_live_tree_expand(int(cid), opened)
tree.bind("<<TreeviewOpen>>", _on_tree_open, add="+")
tree.bind("<<TreeviewClose>>", lambda event: tree.after_idle(fit_tree_column, tree), add="+")
@@ -2041,20 +2041,42 @@ def _start_cov_polling():
_cov_poll_id = app_state.window.after(100, poll)
def _live_tree_expand(comp_id):
def _live_tree_expand(comp_id, tree_item_id):
"""Called when user expands a tree node in live mode to lazy-load children."""
ds = app_state.device_session
if ds is None or not ds.is_connected:
return
children = ds.load_children(comp_id)
if not children:
return
app_state.set_status("Loading children...")
sax_root = ds.build_dom_tree(ds.sox_client.cache_get(0))
ds.build_links_elements(sax_root)
app_state.xml_root_element = sax_root
sax_file.refresh_treeview_from_dom(sax_root)
def _bg():
try:
children = ds.load_children(comp_id)
if not children:
app_state.window.after(0, lambda: app_state.set_status("No children"))
return
sax_root = ds.build_dom_tree(ds.sox_client.cache_get(0))
ds.build_links_elements(sax_root)
def _ui():
app_state.xml_root_element = sax_root
sax_file.refresh_treeview_from_dom(sax_root)
# Re-open and select the node the user clicked
for iid, elem in app_state.tree_element_map.items():
if elem.get("id") == str(comp_id):
tree.item(iid, open=True)
tree.selection_set(iid)
tree.see(iid)
break
app_state.set_status(f"Loaded {len(children)} children")
app_state.window.after(0, _ui)
except Exception as exc:
app_state.window.after(
0, lambda: app_state.set_status(f"Load error: {exc}", warn=True)
)
threading.Thread(target=_bg, daemon=True).start()
# --- UI Setup ---