Skip to content

Commit

Permalink
Allow partial theme and other JSON prop updates
Browse files Browse the repository at this point in the history
Fix #77
  • Loading branch information
FeodorFitsner committed Aug 2, 2022
1 parent c3273c1 commit e038dcb
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 64 deletions.
25 changes: 16 additions & 9 deletions sdk/python/flet/alert_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions sdk/python/flet/banner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions sdk/python/flet/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
31 changes: 20 additions & 11 deletions sdk/python/flet/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions sdk/python/flet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def _is_isolated(self):
def _build(self):
pass

def _before_build_command(self):
pass

def did_mount(self):
pass

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 11 additions & 6 deletions sdk/python/flet/form_field_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions sdk/python/flet/grid_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions sdk/python/flet/image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sys import version
from typing import Optional, Union

from beartype import beartype
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
10 changes: 7 additions & 3 deletions sdk/python/flet/list_tile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from marshal import version
from typing import Optional, Union

from beartype import beartype
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions sdk/python/flet/list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions sdk/python/flet/navigation_rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
Loading

0 comments on commit e038dcb

Please sign in to comment.