From e038dcbfd8386cc2b90dccb9f28ad3d214b70149 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Tue, 2 Aug 2022 13:31:26 -0700 Subject: [PATCH] Allow partial theme and other JSON prop updates Fix #77 --- sdk/python/flet/alert_dialog.py | 25 +++++++++++++-------- sdk/python/flet/banner.py | 17 +++++++++------ sdk/python/flet/card.py | 9 +++++--- sdk/python/flet/container.py | 31 +++++++++++++++++---------- sdk/python/flet/control.py | 16 +++++++++++--- sdk/python/flet/form_field_control.py | 17 +++++++++------ sdk/python/flet/grid_view.py | 9 +++++--- sdk/python/flet/image.py | 10 ++++++--- sdk/python/flet/list_tile.py | 10 ++++++--- sdk/python/flet/list_view.py | 9 +++++--- sdk/python/flet/navigation_rail.py | 9 +++++--- sdk/python/flet/page.py | 21 ++++++++++++------ sdk/python/flet/view.py | 9 +++++--- sdk/python/tests/test_image.py | 2 +- 14 files changed, 130 insertions(+), 64 deletions(-) diff --git a/sdk/python/flet/alert_dialog.py b/sdk/python/flet/alert_dialog.py index 81772891b..566944cf1 100644 --- a/sdk/python/flet/alert_dialog.py +++ b/sdk/python/flet/alert_dialog.py @@ -55,6 +55,22 @@ def __init__( def _get_control_name(self): return "alertdialog" + def _before_build_command(self): + value = self.__actions_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("actionsPadding", value) + + value = self.__content_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("contentPadding", value) + + value = self.__title_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("titlePadding", value) + def _get_children(self): children = [] if self.__title: @@ -106,9 +122,6 @@ def title_padding(self): @beartype def title_padding(self, value: PaddingValue): self.__title_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("titlePadding", value) # content @property @@ -128,9 +141,6 @@ def content_padding(self): @beartype def content_padding(self, value: PaddingValue): self.__content_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("contentPadding", value) # actions @property @@ -150,9 +160,6 @@ def actions_padding(self): @beartype def actions_padding(self, value: PaddingValue): self.__actions_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("actionsPadding", value) # actions_alignment @property diff --git a/sdk/python/flet/banner.py b/sdk/python/flet/banner.py index d9b887453..1aff14a39 100644 --- a/sdk/python/flet/banner.py +++ b/sdk/python/flet/banner.py @@ -51,6 +51,17 @@ def __init__( def _get_control_name(self): return "banner" + def _before_build_command(self): + value = self.__content_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("contentPadding", value) + + value = self.__leading_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("leadingPadding", value) + def _get_children(self): children = [] if self.__leading: @@ -102,9 +113,6 @@ def leading_padding(self): @beartype def leading_padding(self, value: PaddingValue): self.__leading_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("leadingPadding", value) # content @property @@ -124,9 +132,6 @@ def content_padding(self): @beartype def content_padding(self, value: PaddingValue): self.__content_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("contentPadding", value) # actions @property diff --git a/sdk/python/flet/card.py b/sdk/python/flet/card.py index 505bcc2be..fc6193263 100644 --- a/sdk/python/flet/card.py +++ b/sdk/python/flet/card.py @@ -55,6 +55,12 @@ def __init__( def _get_control_name(self): return "card" + def _before_build_command(self): + value = self.__margin + if value != None and isinstance(value, (int, float)): + value = margin.all(value) + self._set_attr_json("margin", value) + def _get_children(self): children = [] if self.__content != None: @@ -71,9 +77,6 @@ def margin(self): @beartype def margin(self, value: MarginValue): self.__margin = value - if value != None and isinstance(value, (int, float)): - value = margin.all(value) - self._set_attr_json("margin", value) # elevation @property diff --git a/sdk/python/flet/container.py b/sdk/python/flet/container.py index de343d6bb..0c8757e65 100644 --- a/sdk/python/flet/container.py +++ b/sdk/python/flet/container.py @@ -81,6 +81,26 @@ def __init__( def _get_control_name(self): return "container" + def _before_build_command(self): + value = self.__border_radius + if value and isinstance(value, (int, float)): + value = border_radius.all(value) + self._set_attr_json("borderRadius", value) + + self._set_attr_json("border", self.__border) + + value = self.__margin + if value != None and isinstance(value, (int, float)): + value = margin.all(value) + self._set_attr_json("margin", value) + + value = self.__padding + if value != None and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("padding", value) + + self._set_attr_json("alignment", self.__alignment) + def _get_children(self): children = [] if self.__content != None: @@ -97,7 +117,6 @@ def alignment(self): @beartype def alignment(self, value: Optional[Alignment]): self.__alignment = value - self._set_attr_json("alignment", value) # padding @property @@ -108,9 +127,6 @@ def padding(self): @beartype def padding(self, value: PaddingValue): self.__padding = value - if value != None and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("padding", value) # margin @property @@ -121,9 +137,6 @@ def margin(self): @beartype def margin(self, value: MarginValue): self.__margin = value - if value != None and isinstance(value, (int, float)): - value = margin.all(value) - self._set_attr_json("margin", value) # bgcolor @property @@ -143,7 +156,6 @@ def border(self): @beartype def border(self, value: Optional[Border]): self.__border = value - self._set_attr_json("border", value) # border_radius @property @@ -154,9 +166,6 @@ def border_radius(self): @beartype def border_radius(self, value: BorderRadiusValue): self.__border_radius = value - if value and isinstance(value, (int, float)): - value = border_radius.all(value) - self._set_attr_json("borderRadius", value) # content @property diff --git a/sdk/python/flet/control.py b/sdk/python/flet/control.py index 9532e6374..cad162c60 100644 --- a/sdk/python/flet/control.py +++ b/sdk/python/flet/control.py @@ -92,6 +92,9 @@ def _is_isolated(self): def _build(self): pass + def _before_build_command(self): + pass + def did_mount(self): pass @@ -160,11 +163,16 @@ def _set_attr_internal(self, name, value, dirty=True): self.__attrs[name] = (value, dirty) def _set_attr_json(self, name, value): - self._set_attr( - name, + ov = self._get_attr(name) + nv = self._convert_attr_json(value) + if ov != nv: + self._set_attr(name, nv) + + def _convert_attr_json(self, value): + return ( json.dumps(value, cls=EmbedJsonEncoder, separators=(",", ":")) if value - else None, + else None ) # event_handlers @@ -414,6 +422,8 @@ def _build_command(self, update=False): if update and not self.__uid: return command + self._before_build_command() + for attrName in sorted(self.__attrs): attrName = attrName.lower() dirty = self.__attrs[attrName][1] diff --git a/sdk/python/flet/form_field_control.py b/sdk/python/flet/form_field_control.py index d294405b2..6bb83d97f 100644 --- a/sdk/python/flet/form_field_control.py +++ b/sdk/python/flet/form_field_control.py @@ -105,6 +105,17 @@ def __init__( self.suffix_icon = suffix_icon self.suffix_text = suffix_text + def _before_build_command(self): + value = self.__border_radius + if value and isinstance(value, (int, float)): + value = border_radius.all(value) + self._set_attr_json("borderRadius", value) + + value = self.__content_padding + if value and isinstance(value, (int, float)): + value = padding.all(value) + self._set_attr_json("contentPadding", value) + def _get_children(self): children = [] if self.__prefix: @@ -180,9 +191,6 @@ def border_radius(self): @beartype def border_radius(self, value: BorderRadiusValue): self.__border_radius = value - if value and isinstance(value, (int, float)): - value = border_radius.all(value) - self._set_attr_json("borderRadius", value) # border_width @property @@ -249,9 +257,6 @@ def content_padding(self): @beartype def content_padding(self, value: PaddingValue): self.__content_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("contentPadding", value) # filled @property diff --git a/sdk/python/flet/grid_view.py b/sdk/python/flet/grid_view.py index 4d41a1f67..40bd9de52 100644 --- a/sdk/python/flet/grid_view.py +++ b/sdk/python/flet/grid_view.py @@ -64,6 +64,12 @@ def __init__( def _get_control_name(self): return "gridview" + def _before_build_command(self): + v = self.__padding + if v != None and isinstance(v, (int, float)): + v = padding.all(v) + self._set_attr_json("padding", v) + def _get_children(self): return self.__controls @@ -140,9 +146,6 @@ def padding(self): @beartype def padding(self, value: PaddingValue): self.__padding = value - if value != None and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("padding", value) # controls @property diff --git a/sdk/python/flet/image.py b/sdk/python/flet/image.py index a43ff24bb..1c371b173 100644 --- a/sdk/python/flet/image.py +++ b/sdk/python/flet/image.py @@ -1,3 +1,4 @@ +from sys import version from typing import Optional, Union from beartype import beartype @@ -62,6 +63,12 @@ def __init__( def _get_control_name(self): return "image" + def _before_build_command(self): + v = self.__border_radius + if v and isinstance(v, (int, float)): + v = border_radius.all(v) + self._set_attr_json("borderRadius", v) + # src @property def src(self): @@ -120,6 +127,3 @@ def border_radius(self): @beartype def border_radius(self, value: BorderRadiusValue): self.__border_radius = value - if value and isinstance(value, (int, float)): - value = border_radius.all(value) - self._set_attr_json("borderRadius", value) diff --git a/sdk/python/flet/list_tile.py b/sdk/python/flet/list_tile.py index a2a7e8116..df5f198a6 100644 --- a/sdk/python/flet/list_tile.py +++ b/sdk/python/flet/list_tile.py @@ -1,3 +1,4 @@ +from marshal import version from typing import Optional, Union from beartype import beartype @@ -70,6 +71,12 @@ def __init__( def _get_control_name(self): return "listtile" + def _before_build_command(self): + v = self.__content_padding + if v and isinstance(v, (int, float)): + v = padding.all(v) + self._set_attr_json("contentPadding", v) + def _get_children(self): children = [] if self.__leading: @@ -95,9 +102,6 @@ def content_padding(self): @beartype def content_padding(self, value: PaddingValue): self.__content_padding = value - if value and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("contentPadding", value) # leading @property diff --git a/sdk/python/flet/list_view.py b/sdk/python/flet/list_view.py index f4bdd0759..dbd3b69e0 100644 --- a/sdk/python/flet/list_view.py +++ b/sdk/python/flet/list_view.py @@ -64,6 +64,12 @@ def __init__( def _get_control_name(self): return "listview" + def _before_build_command(self): + v = self.__padding + if v != None and isinstance(v, (int, float)): + v = padding.all(v) + self._set_attr_json("padding", v) + def _get_children(self): return self.__controls @@ -130,9 +136,6 @@ def padding(self): @beartype def padding(self, value: PaddingValue): self.__padding = value - if value != None and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("padding", value) # controls @property diff --git a/sdk/python/flet/navigation_rail.py b/sdk/python/flet/navigation_rail.py index a362b8c9d..10a7f7c3e 100644 --- a/sdk/python/flet/navigation_rail.py +++ b/sdk/python/flet/navigation_rail.py @@ -43,6 +43,12 @@ def __init__( def _get_control_name(self): return "navigationraildestination" + def _before_build_command(self): + v = self.__padding + if v != None and isinstance(v, (int, float)): + v = padding.all(v) + self._set_attr_json("padding", v) + def _get_children(self): children = [] if self.__label_content: @@ -124,9 +130,6 @@ def padding(self): @beartype def padding(self, value: PaddingValue): self.__padding = value - if value != None and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("padding", value) class NavigationRail(ConstrainedControl): diff --git a/sdk/python/flet/page.py b/sdk/python/flet/page.py index 5640e707b..8e9e35bf2 100644 --- a/sdk/python/flet/page.py +++ b/sdk/python/flet/page.py @@ -87,6 +87,20 @@ def __exit__(self, type, value, traceback): def get_control(self, id): return self._index.get(id) + def _before_build_command(self): + # fonts + self._set_attr_json("fonts", self.__fonts) + + # light theme + if self.__theme: + self.__theme.brightness = "light" + self._set_attr_json("theme", self.__theme) + + # dark theme + if self.__dark_theme: + self.__dark_theme.brightness = "dark" + self._set_attr_json("darkTheme", self.__dark_theme) + def _get_children(self): children = [] children.extend(self.__views) @@ -350,7 +364,6 @@ def fonts(self): @beartype def fonts(self, value: Optional[Dict[str, str]]): self.__fonts = value - self._set_attr_json("fonts", value) # views @property @@ -515,9 +528,6 @@ def theme(self): @beartype def theme(self, value: Optional[Theme]): self.__theme = value - if self.__theme: - self.__theme.brightness = "light" - self._set_attr_json("theme", value) # dark_theme @property @@ -528,9 +538,6 @@ def dark_theme(self): @beartype def dark_theme(self, value: Optional[Theme]): self.__dark_theme = value - if self.__dark_theme: - self.__dark_theme.brightness = "dark" - self._set_attr_json("darkTheme", value) # rtl @property diff --git a/sdk/python/flet/view.py b/sdk/python/flet/view.py index 635bb723e..02333a1c4 100644 --- a/sdk/python/flet/view.py +++ b/sdk/python/flet/view.py @@ -45,6 +45,12 @@ def __init__( def _get_control_name(self): return "view" + def _before_build_command(self): + v = self.__padding + if v != None and isinstance(v, (int, float)): + v = padding.all(v) + self._set_attr_json("padding", v) + def _get_children(self): children = [] if self.__appbar: @@ -133,9 +139,6 @@ def padding(self): @beartype def padding(self, value: PaddingValue): self.__padding = value - if value != None and isinstance(value, (int, float)): - value = padding.all(value) - self._set_attr_json("padding", value) # bgcolor @property diff --git a/sdk/python/tests/test_image.py b/sdk/python/tests/test_image.py index 4bead3f15..6862b27db 100644 --- a/sdk/python/tests/test_image.py +++ b/sdk/python/tests/test_image.py @@ -9,7 +9,7 @@ def test_image_add(): ) assert isinstance(i, flet.Control) assert isinstance(i, flet.Image) - assert i._build_add_command() == [ + assert i._build_add_commands() == [ Command( indent=0, name=None,