Skip to content

Commit

Permalink
tree: When changing children of tree node, don't bother resorting chi…
Browse files Browse the repository at this point in the history
…ldren if already sorted
  • Loading branch information
davidfstr committed Aug 31, 2023
1 parent 1f971d7 commit d9ab5b8
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/crystal/ui/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,32 @@ def set_children(self,
for child in children_to_add:
child._attach(NodeViewPeer(self.peer._tree, self.peer.AppendItem('')))

# Reorder children
for (index, child) in enumerate(new_children):
child._order_index = index # type: ignore[attr-defined]
self.peer.SortChildren()
# Calculate whether needs_reorder
last_order_index = -1
for child in new_children:
cur_order_index = getattr(child, '_order_index', None)
if cur_order_index is None:
if last_order_index != -1:
# New child without order-index mixed with
# old children with order-index
needs_reorder = True
break
else:
# New child without order-index mixed with
# only other new children without order-index
continue
if cur_order_index < last_order_index:
needs_reorder = True
break
last_order_index = cur_order_index
else:
needs_reorder = False

if needs_reorder:
# Reorder children
for (index, child) in enumerate(new_children):
child._order_index = index # type: ignore[attr-defined]
self.peer.SortChildren()
except WindowDeletedError:
pass

Expand Down

0 comments on commit d9ab5b8

Please sign in to comment.