Skip to content

Commit cc48859

Browse files
Fix "replace" when moving children in the collection (#711)
1 parent 249952c commit cc48859

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

sdk/python/flet/control.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)
297297

298298
n = 0
299299
for tag, a1, a2, b1, b2 in sm.get_opcodes():
300-
if tag == "delete":
300+
if tag == "delete" or tag == "replace":
301301
# deleted controls
302302
ids = []
303303
for h in previous_ints[a1:a2]:
@@ -321,6 +321,23 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)
321321
ids.append(ctrl.__uid)
322322
if len(ids) > 0:
323323
commands.append(Command(0, "remove", ids))
324+
if tag == "replace":
325+
# add
326+
for h in current_ints[b1:b2]:
327+
ctrl = hashes[h]
328+
innerCmds = ctrl._build_add_commands(
329+
index=index, added_controls=added_controls
330+
)
331+
assert self.__uid is not None
332+
commands.append(
333+
Command(
334+
indent=0,
335+
name="add",
336+
attrs={"to": self.__uid, "at": str(n)},
337+
commands=innerCmds,
338+
)
339+
)
340+
n += 1
324341
elif tag == "equal":
325342
# unchanged control
326343
for h in previous_ints[a1:a2]:
@@ -329,30 +346,6 @@ def build_update_commands(self, index, added_controls, commands, isolated=False)
329346
index, added_controls, commands, isolated=ctrl._is_isolated()
330347
)
331348
n += 1
332-
elif tag == "replace":
333-
ids = []
334-
for h in previous_ints[a1:a2]:
335-
# delete
336-
ctrl = hashes[h]
337-
self._remove_control_recursively(index, ctrl)
338-
ids.append(ctrl.__uid)
339-
commands.append(Command(0, "remove", ids))
340-
for h in current_ints[b1:b2]:
341-
# add
342-
ctrl = hashes[h]
343-
innerCmds = ctrl._build_add_commands(
344-
index=index, added_controls=added_controls
345-
)
346-
assert self.__uid is not None
347-
commands.append(
348-
Command(
349-
indent=0,
350-
name="add",
351-
attrs={"to": self.__uid, "at": str(n)},
352-
commands=innerCmds,
353-
)
354-
)
355-
n += 1
356349
elif tag == "insert":
357350
# add
358351
for h in current_ints[b1:b2]:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import random
2+
3+
import flet as ft
4+
5+
6+
def test_moving_children():
7+
c = ft.Stack()
8+
c._Control__uid = "0"
9+
for i in range(0, 10):
10+
c.controls.append(ft.Container())
11+
c.controls[i]._Control__uid = f"_{i}"
12+
13+
index = []
14+
added_controls = []
15+
commands = []
16+
c.build_update_commands(index, added_controls, commands, False)
17+
18+
def replace_controls(c):
19+
random.shuffle(c.controls)
20+
commands.clear()
21+
22+
# print("=======")
23+
r = set()
24+
for ctrl in c.controls:
25+
# print(ctrl._Control__uid)
26+
r.add(ctrl._Control__uid)
27+
c.build_update_commands(index, added_controls, commands, False)
28+
for cmd in commands:
29+
if cmd.name == "add":
30+
for sub_cmd in cmd.commands:
31+
# print("add", sub_cmd.attrs["id"], "at", cmd.attrs["at"])
32+
r.add(sub_cmd.attrs["id"])
33+
elif cmd.name == "remove":
34+
for v in cmd.values:
35+
# print("remove", v)
36+
r.remove(v)
37+
# print(r)
38+
assert len(r) == len(c.controls)
39+
40+
for i in range(0, 20):
41+
replace_controls(c)

0 commit comments

Comments
 (0)