Skip to content

Commit

Permalink
Fix "replace" when moving children in the collection (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
FeodorFitsner authored Dec 14, 2022
1 parent 249952c commit cc48859
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
43 changes: 18 additions & 25 deletions sdk/python/flet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)

n = 0
for tag, a1, a2, b1, b2 in sm.get_opcodes():
if tag == "delete":
if tag == "delete" or tag == "replace":
# deleted controls
ids = []
for h in previous_ints[a1:a2]:
Expand All @@ -321,6 +321,23 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)
ids.append(ctrl.__uid)
if len(ids) > 0:
commands.append(Command(0, "remove", ids))
if tag == "replace":
# add
for h in current_ints[b1:b2]:
ctrl = hashes[h]
innerCmds = ctrl._build_add_commands(
index=index, added_controls=added_controls
)
assert self.__uid is not None
commands.append(
Command(
indent=0,
name="add",
attrs={"to": self.__uid, "at": str(n)},
commands=innerCmds,
)
)
n += 1
elif tag == "equal":
# unchanged control
for h in previous_ints[a1:a2]:
Expand All @@ -329,30 +346,6 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)
index, added_controls, commands, isolated=ctrl._is_isolated()
)
n += 1
elif tag == "replace":
ids = []
for h in previous_ints[a1:a2]:
# delete
ctrl = hashes[h]
self._remove_control_recursively(index, ctrl)
ids.append(ctrl.__uid)
commands.append(Command(0, "remove", ids))
for h in current_ints[b1:b2]:
# add
ctrl = hashes[h]
innerCmds = ctrl._build_add_commands(
index=index, added_controls=added_controls
)
assert self.__uid is not None
commands.append(
Command(
indent=0,
name="add",
attrs={"to": self.__uid, "at": str(n)},
commands=innerCmds,
)
)
n += 1
elif tag == "insert":
# add
for h in current_ints[b1:b2]:
Expand Down
41 changes: 41 additions & 0 deletions sdk/python/tests/test_moving_children.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import random

import flet as ft


def test_moving_children():
c = ft.Stack()
c._Control__uid = "0"
for i in range(0, 10):
c.controls.append(ft.Container())
c.controls[i]._Control__uid = f"_{i}"

index = []
added_controls = []
commands = []
c.build_update_commands(index, added_controls, commands, False)

def replace_controls(c):
random.shuffle(c.controls)
commands.clear()

# print("=======")
r = set()
for ctrl in c.controls:
# print(ctrl._Control__uid)
r.add(ctrl._Control__uid)
c.build_update_commands(index, added_controls, commands, False)
for cmd in commands:
if cmd.name == "add":
for sub_cmd in cmd.commands:
# print("add", sub_cmd.attrs["id"], "at", cmd.attrs["at"])
r.add(sub_cmd.attrs["id"])
elif cmd.name == "remove":
for v in cmd.values:
# print("remove", v)
r.remove(v)
# print(r)
assert len(r) == len(c.controls)

for i in range(0, 20):
replace_controls(c)

0 comments on commit cc48859

Please sign in to comment.