Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest
import pytest_asyncio

import flet as ft
import flet.testing as ftt


# Create a new flet_app instance for each test method
@pytest_asyncio.fixture(scope="function", autouse=True)
def flet_app(flet_app_function):
return flet_app_function


@pytest.mark.asyncio(loop_scope="function")
async def test_color_scheme(flet_app: ftt.FletTestApp):
flet_app.page.theme = ft.Theme(
color_scheme=ft.ColorScheme(
primary=ft.Colors.GREEN,
on_primary=ft.Colors.YELLOW,
primary_container=ft.Colors.GREEN_900,
on_primary_container=ft.Colors.YELLOW,
secondary=ft.Colors.BLUE,
on_secondary=ft.Colors.WHITE,
secondary_container=ft.Colors.BLUE_900,
on_secondary_container=ft.Colors.WHITE,
tertiary=ft.Colors.RED,
on_tertiary=ft.Colors.WHITE,
tertiary_container=ft.Colors.RED_900,
on_tertiary_container=ft.Colors.WHITE,
error=ft.Colors.RED,
on_error=ft.Colors.WHITE,
error_container=ft.Colors.RED_900,
on_error_container=ft.Colors.WHITE,
surface=ft.Colors.ORANGE_400,
on_surface=ft.Colors.BLACK,
on_surface_variant=ft.Colors.RED,
outline=ft.Colors.BLUE_200,
outline_variant=ft.Colors.BLUE_400,
shadow=ft.Colors.BLACK,
scrim=ft.Colors.BLACK,
inverse_surface=ft.Colors.BLACK,
on_inverse_surface=ft.Colors.WHITE,
inverse_primary=ft.Colors.GREEN_900,
surface_tint=ft.Colors.GREEN,
on_primary_fixed=ft.Colors.WHITE,
on_secondary_fixed=ft.Colors.WHITE,
on_tertiary_fixed=ft.Colors.WHITE,
on_primary_fixed_variant=ft.Colors.WHITE,
on_secondary_fixed_variant=ft.Colors.WHITE,
on_tertiary_fixed_variant=ft.Colors.WHITE,
)
)

flet_app.page.enable_screenshots = True
flet_app.page.window.width = 400
flet_app.page.window.height = 600
flet_app.page.add(
ft.FilledButton("Primary button"),
ft.FilledTonalButton("Secondary button"),
ft.Container(
ft.Text("Container surface", color=ft.Colors.ON_SURFACE_VARIANT, size=20),
width=200,
height=100,
bgcolor=ft.Colors.SURFACE,
),
)
await flet_app.tester.pump_and_settle()

flet_app.assert_screenshot(
"cupertino_action_sheet_basic",
await flet_app.page.take_screenshot(
pixel_ratio=flet_app.screenshots_pixel_ratio
),
)
22 changes: 13 additions & 9 deletions sdk/python/packages/flet/src/flet/controls/object_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,13 @@ def _compare_dataclasses(self, parent, path, src, dst, frozen):
old = change[0]
new = change[1]

if field_name.startswith("on_") and (
"event" not in fields[field_name].metadata
or fields[field_name].metadata["event"]
):
old = old is not None
new = new is not None

logger.debug("\n\n_compare_values:changes %s %s", old, new)

self._compare_values(dst, path, field_name, old, new, frozen)
Expand Down Expand Up @@ -963,7 +970,9 @@ def _compare_dataclasses(self, parent, path, src, dst, frozen):
if "skip" not in field.metadata:
old = getattr(src, field.name)
new = getattr(dst, field.name)
if field.name.startswith("on_"):
if field.name.startswith("on_") and (
"event" not in field.metadata or field.metadata["event"]
):
old = old is not None
new = new is not None
self._compare_values(dst, path, field.name, old, new, frozen)
Expand Down Expand Up @@ -1126,20 +1135,15 @@ def control_setattr(obj, name, value):

if hasattr(obj, "__changes"):
old_value = getattr(obj, name, None)
if name.startswith("on_"):
old_value = old_value is not None
new_value = (
value if not name.startswith("on_") else value is not None
)
if old_value != new_value:
if old_value != value:
# logger.debug(
# f"\n\nset_attr: {obj.__class__.__name__}.{name} = "
# f"{new_value}, old: {old_value}"
# )
changes = getattr(obj, "__changes")
changes[name] = (old_value, new_value)
changes[name] = (old_value, value)
if hasattr(obj, "_notify"):
obj._notify(name, new_value)
obj._notify(name, value)
object.__setattr__(obj, name, value)

item.__class__.__setattr__ = control_setattr # type: ignore
Expand Down
58 changes: 41 additions & 17 deletions sdk/python/packages/flet/src/flet/controls/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ColorScheme:
The color displayed most frequently across your app's screens and components.
"""

on_primary: Optional[ColorValue] = None
on_primary: Optional[ColorValue] = field(default=None, metadata={"event": False})
"""
A color that's clearly legible when drawn on `primary`.
"""
Expand All @@ -85,7 +85,9 @@ class ColorScheme:
A color used for elements needing less emphasis than `primary`.
"""

on_primary_container: Optional[ColorValue] = None
on_primary_container: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `primary_container`.
"""
Expand All @@ -96,7 +98,7 @@ class ColorScheme:
while expanding the opportunity for color expression.
"""

on_secondary: Optional[ColorValue] = None
on_secondary: Optional[ColorValue] = field(default=None, metadata={"event": False})
"""
A color that's clearly legible when drawn on `secondary`.
"""
Expand All @@ -106,7 +108,9 @@ class ColorScheme:
A color used for elements needing less emphasis than `secondary`.
"""

on_secondary_container: Optional[ColorValue] = None
on_secondary_container: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `secondary_container`.
"""
Expand All @@ -117,7 +121,7 @@ class ColorScheme:
colors or bring heightened attention to an element, such as an input field.
"""

on_tertiary: Optional[ColorValue] = None
on_tertiary: Optional[ColorValue] = field(default=None, metadata={"event": False})
"""
A color that's clearly legible when drawn on `tertiary`.
"""
Expand All @@ -127,7 +131,9 @@ class ColorScheme:
A color used for elements needing less emphasis than `tertiary`.
"""

on_tertiary_container: Optional[ColorValue] = None
on_tertiary_container: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `tertiary_container`.
"""
Expand All @@ -137,7 +143,7 @@ class ColorScheme:
The color to use for input validation errors, e.g. for `TextField.error_text`.
"""

on_error: Optional[ColorValue] = None
on_error: Optional[ColorValue] = field(default=None, metadata={"event": False})
"""
A color that's clearly legible when drawn on `error`.
"""
Expand All @@ -147,7 +153,9 @@ class ColorScheme:
A color used for error elements needing less emphasis than `error`.
"""

on_error_container: Optional[ColorValue] = None
on_error_container: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `error_container`.
"""
Expand All @@ -157,12 +165,14 @@ class ColorScheme:
The background color for widgets like `Card`.
"""

on_surface: Optional[ColorValue] = None
on_surface: Optional[ColorValue] = field(default=None, metadata={"event": False})
"""
A color that's clearly legible when drawn on `surface`.
"""

on_surface_variant: Optional[ColorValue] = None
on_surface_variant: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `surface_variant`.
"""
Expand Down Expand Up @@ -194,7 +204,9 @@ class ColorScheme:
UI, for example in a `SnackBar` to bring attention to an alert.
"""

on_inverse_surface: Optional[ColorValue] = None
on_inverse_surface: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that's clearly legible when drawn on `inverse_surface`.
"""
Expand All @@ -210,37 +222,49 @@ class ColorScheme:
A color used as an overlay on a surface color to indicate a component's elevation.
"""

on_primary_fixed: Optional[ColorValue] = None
on_primary_fixed: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that is used for text and icons that exist on top of elements having
`primary_fixed` color.
"""

on_secondary_fixed: Optional[ColorValue] = None
on_secondary_fixed: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that is used for text and icons that exist on top of elements having
`secondary_fixed` color.
"""

on_tertiary_fixed: Optional[ColorValue] = None
on_tertiary_fixed: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that is used for text and icons that exist on top of elements having
`tertiary_fixed` color.
"""

on_primary_fixed_variant: Optional[ColorValue] = None
on_primary_fixed_variant: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that provides a lower-emphasis option for text and icons than
`on_primary_fixed`.
"""

on_secondary_fixed_variant: Optional[ColorValue] = None
on_secondary_fixed_variant: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that provides a lower-emphasis option for text and icons than
`on_secondary_fixed`.
"""

on_tertiary_fixed_variant: Optional[ColorValue] = None
on_tertiary_fixed_variant: Optional[ColorValue] = field(
default=None, metadata={"event": False}
)
"""
A color that provides a lower-emphasis option for text and icons than
`on_tertiary_fixed`.
Expand Down
4 changes: 3 additions & 1 deletion sdk/python/packages/flet/src/flet/messaging/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def encode_object_for_msgpack(obj):
if len(v) > 0:
r[field.name] = v
prev_dicts[field.name] = v
elif field.name.startswith("on_"):
elif field.name.startswith("on_") and (
"event" not in field.metadata or field.metadata["event"]
):
v = v is not None
if v:
r[field.name] = v
Expand Down
2 changes: 2 additions & 0 deletions sdk/python/packages/flet/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@ft.control("MyText")
class MyText(ft.BaseControl):
value: str
color_scheme: Optional[ft.ColorScheme] = None
on_select: Optional[ft.EventHandler] = None

def __str__(self):
return f"{self._c}({self.value}, key={self.key} - {id(self)})"
Expand Down
50 changes: 50 additions & 0 deletions sdk/python/packages/flet/tests/test_object_diff_frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
LineChartData,
LineChartDataPoint,
MyText,
b_unpack,
cmp_ops,
make_diff,
make_msg,
Expand Down Expand Up @@ -1253,3 +1254,52 @@ def test_list_move_11():
},
],
)


def test_fields_start_with_on():
t1 = MyText("Text 1")
t2 = MyText(
"Text 2",
color_scheme=ft.ColorScheme(on_surface_variant=ft.Colors.RED),
on_select=lambda e: print("Selected"),
)
t1._frozen = True

msg, _, _, _, _ = make_msg(t2, t1, show_details=False)
u_msg = b_unpack(msg)

expected = [
[0],
[0, 0, "value", "Text 2"],
[0, 0, "color_scheme", {"on_surface_variant": "red"}],
[0, 0, "on_select", True],
]

assert isinstance(u_msg, list)
assert u_msg == expected


def test_fields_start_with_on_update():
t1 = MyText(
"Text 1",
color_scheme=ft.ColorScheme(on_surface_variant=ft.Colors.RED),
on_select=lambda e: print("Selected"),
)
t2 = MyText(
"Text 2",
color_scheme=ft.ColorScheme(on_surface_variant=ft.Colors.BLUE),
)
t1._frozen = True

msg, _, _, _, _ = make_msg(t2, t1, show_details=False)
u_msg = b_unpack(msg)

expected = [
[0, {"color_scheme": [1]}],
[0, 0, "value", "Text 2"],
[0, 1, "on_surface_variant", "blue"],
[0, 0, "on_select", False],
]

assert isinstance(u_msg, list)
assert u_msg == expected
Loading
Loading