Skip to content

Commit 1b0716e

Browse files
committed
Prioritize visible widgets during tree projection
1 parent 0fbdd91 commit 1b0716e

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/pyqt_reactive/services/widget_tree_projection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,11 @@ def _project_children(
839839
options=Qt.FindChildOption.FindDirectChildrenOnly,
840840
)
841841
descriptors: list[WidgetDescriptor] = []
842-
for child_index, child_widget in enumerate(child_widgets):
842+
indexed_child_widgets = sorted(
843+
enumerate(child_widgets),
844+
key=lambda indexed_widget: not indexed_widget[1].isVisible(),
845+
)
846+
for child_index, child_widget in indexed_child_widgets:
843847
child_path = (*path, child_index)
844848
if not state.consume_node(child_path):
845849
break

tests/test_widget_tree_projection.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ def test_widget_tree_node_bound_stops_live_projection(qapp) -> None:
268268
assert [label.projection_reads for label in labels] == [1, 1, 0, 0, 0, 0]
269269

270270

271+
def test_bounded_widget_tree_projects_visible_siblings_before_hidden_siblings(
272+
qapp,
273+
) -> None:
274+
root = QWidget()
275+
hidden = _ProjectionCountingLabel("hidden", root)
276+
visible = _ProjectionCountingLabel("visible", root)
277+
root.show()
278+
hidden.hide()
279+
qapp.processEvents()
280+
281+
projection = WidgetTreeProjectionService.project(
282+
root,
283+
policy=WidgetTreeProjectionPolicy(maximum_nodes=2),
284+
)
285+
286+
assert projection.root.children[0].text == "visible"
287+
assert projection.root.children[0].path == (1,)
288+
assert visible.projection_reads == 1
289+
assert hidden.projection_reads == 0
290+
291+
271292
def test_widget_tree_depth_bound_stops_live_projection(qapp) -> None:
272293
root = QWidget()
273294
branch = QWidget(root)

0 commit comments

Comments
 (0)