From adf4dd1bdcfb2d550516a96b6fba4f3fdedfd80f Mon Sep 17 00:00:00 2001 From: thetableman <78121198+thetableman@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:06:43 +1030 Subject: [PATCH 1/2] added name_element --- nicegui/elements/icon.py | 5 +- nicegui/elements/mixins/name_element.py | 84 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 nicegui/elements/mixins/name_element.py diff --git a/nicegui/elements/icon.py b/nicegui/elements/icon.py index c1c341c3a..c0c85fb37 100644 --- a/nicegui/elements/icon.py +++ b/nicegui/elements/icon.py @@ -1,9 +1,10 @@ from typing import Optional +from .mixins.name_element import NameElement from .mixins.color_elements import TextColorElement -class Icon(TextColorElement): +class Icon(NameElement, TextColorElement): def __init__(self, name: str, @@ -21,7 +22,7 @@ def __init__(self, :param size: size in CSS units, including unit name or standard size name (xs|sm|md|lg|xl), examples: 16px, 2rem :param color: icon color (either a Quasar, Tailwind, or CSS color or `None`, default: `None`) """ - super().__init__(tag='q-icon', text_color=color) + super().__init__(tag='q-icon', name=name, text_color=color) self._props['name'] = name if size: diff --git a/nicegui/elements/mixins/name_element.py b/nicegui/elements/mixins/name_element.py new file mode 100644 index 000000000..f8929f870 --- /dev/null +++ b/nicegui/elements/mixins/name_element.py @@ -0,0 +1,84 @@ +from typing import Any, Callable + +from typing_extensions import Self + +from ...binding import BindableProperty, bind, bind_from, bind_to +from ...element import Element + + +class NameElement(Element): + name = BindableProperty(on_change=lambda sender, name: sender.on_name_change(name)) + + def __init__(self, *, name: str, **kwargs: Any) -> None: + super().__init__(**kwargs) + self._props['name'] = name + self._name_to_model_name(name) + + def bind_name_to(self, + target_object: Any, + target_name: str = 'name', + forward: Callable[..., Any] = lambda x: x, + ) -> Self: + """Bind the name of this element to the target object's target_name property. + + The binding works one way only, from this element to the target. + + :param target_object: The object to bind to. + :param target_name: The name of the property to bind to. + :param forward: A function to apply to the value before applying it to the target. + """ + bind_to(self, 'name', target_object, target_name, forward) + return self + + def bind_name_from(self, + target_object: Any, + target_name: str = 'name', + backward: Callable[..., Any] = lambda x: x, + ) -> Self: + """Bind the name of this element from the target object's target_name property. + + The binding works one way only, from the target to this element. + + :param target_object: The object to bind from. + :param target_name: The name of the property to bind from. + :param backward: A function to apply to the value before applying it to this element. + """ + bind_from(self, 'name', target_object, target_name, backward) + return self + + def bind_name(self, + target_object: Any, + target_name: str = 'name', *, + forward: Callable[..., Any] = lambda x: x, + backward: Callable[..., Any] = lambda x: x, + ) -> Self: + """Bind the name of this element to the target object's target_name property. + + The binding works both ways, from this element to the target and from the target to this element. + + :param target_object: The object to bind to. + :param target_name: The name of the property to bind to. + :param forward: A function to apply to the value before applying it to the target. + :param backward: A function to apply to the value before applying it to this element. + """ + bind(self, 'name', target_object, target_name, forward=forward, backward=backward) + return self + + def set_name(self, name: str) -> None: + """Set the name of this element. + + :param name: The new name. + """ + self._props['name'] = name + self.update() + + def on_name_change(self, name: str) -> None: + """Called when the name of this element changes. + + :param name: The new name. + """ + self._name_to_model_name(name) + self.update() + + def _name_to_model_name(self, name: str) -> None: + self._props['name'] = name From 2d753eb1bd61c9169c2bcd87a70c9265062cbbaa Mon Sep 17 00:00:00 2001 From: Falko Schindler Date: Mon, 2 Oct 2023 14:03:08 +0200 Subject: [PATCH 2/2] code review --- nicegui/elements/icon.py | 3 +-- nicegui/elements/mixins/name_element.py | 10 +++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/nicegui/elements/icon.py b/nicegui/elements/icon.py index c0c85fb37..0c699540d 100644 --- a/nicegui/elements/icon.py +++ b/nicegui/elements/icon.py @@ -1,7 +1,7 @@ from typing import Optional -from .mixins.name_element import NameElement from .mixins.color_elements import TextColorElement +from .mixins.name_element import NameElement class Icon(NameElement, TextColorElement): @@ -23,7 +23,6 @@ def __init__(self, :param color: icon color (either a Quasar, Tailwind, or CSS color or `None`, default: `None`) """ super().__init__(tag='q-icon', name=name, text_color=color) - self._props['name'] = name if size: self._props['size'] = size diff --git a/nicegui/elements/mixins/name_element.py b/nicegui/elements/mixins/name_element.py index f8929f870..175d0c985 100644 --- a/nicegui/elements/mixins/name_element.py +++ b/nicegui/elements/mixins/name_element.py @@ -11,8 +11,8 @@ class NameElement(Element): def __init__(self, *, name: str, **kwargs: Any) -> None: super().__init__(**kwargs) + self.name = name self._props['name'] = name - self._name_to_model_name(name) def bind_name_to(self, target_object: Any, @@ -69,16 +69,12 @@ def set_name(self, name: str) -> None: :param name: The new name. """ - self._props['name'] = name - self.update() + self.name = name def on_name_change(self, name: str) -> None: """Called when the name of this element changes. :param name: The new name. """ - self._name_to_model_name(name) - self.update() - - def _name_to_model_name(self, name: str) -> None: self._props['name'] = name + self.update()