From 35ea4a01c7f14755550a0c6b4a28e40d8f273223 Mon Sep 17 00:00:00 2001 From: stu Date: Fri, 29 Sep 2023 19:44:50 +0800 Subject: [PATCH 1/5] Switch: make it work with undocumented designer api --- client_code/Switch/__init__.py | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/client_code/Switch/__init__.py b/client_code/Switch/__init__.py index a09e7dae..9f6a4985 100644 --- a/client_code/Switch/__init__.py +++ b/client_code/Switch/__init__.py @@ -107,6 +107,32 @@ """ _html_injector.css(css) +_remove_props = ["allow_indeterminate", "text", "underline"] + +_include_props = [ + { + "name": "checked_color", + "type": "color", + "default_value": None, + "group": "appearance", + "important": False, + }, + { + "name": "text_pre", + "type": "string", + "default_value": "", + "group": "text", + "important": True, + }, + { + "name": "text_post", + "type": "string", + "default_value": "", + "group": "text", + "important": True, + }, +] + class Switch(CheckBox): def __init__(self, checked_color=primary, text_pre="", text_post="", **properties): @@ -157,3 +183,17 @@ def text_post(self, value): self._textnode_post.textContent = value text = text_post # override the CheckBox property + + def _anvil_get_design_info_(self, *args, **kws): + design_info = super()._anvil_get_design_info_(*args, **kws) + props = design_info.get("propertyDescriptions", []) + props = [p for p in props if p.get("name") not in _remove_props] + props = _include_props + props + design_info["propertyDescriptions"] = props + return design_info + + def _anvil_set_property_values_(self, updates): + # we probably won't need this + for attr, val in updates.items(): + setattr(self, attr, val) + return {attr: getattr(self, attr) for attr in updates} From 31c0989a829895b4af318d758bb023307258948a Mon Sep 17 00:00:00 2001 From: stu Date: Tue, 3 Oct 2023 15:23:49 +0800 Subject: [PATCH 2/5] No longer need _anvil_set_property_values_ --- client_code/Switch/__init__.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/client_code/Switch/__init__.py b/client_code/Switch/__init__.py index 9f6a4985..47d4c84c 100644 --- a/client_code/Switch/__init__.py +++ b/client_code/Switch/__init__.py @@ -191,9 +191,3 @@ def _anvil_get_design_info_(self, *args, **kws): props = _include_props + props design_info["propertyDescriptions"] = props return design_info - - def _anvil_set_property_values_(self, updates): - # we probably won't need this - for attr, val in updates.items(): - setattr(self, attr, val) - return {attr: getattr(self, attr) for attr in updates} From 1bd7fd7cb3c434a58f2ca5e26542e8c91a333159 Mon Sep 17 00:00:00 2001 From: stu Date: Tue, 3 Oct 2023 16:29:32 +0800 Subject: [PATCH 3/5] future proof against possible internal name change --- client_code/Switch/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/client_code/Switch/__init__.py b/client_code/Switch/__init__.py index 47d4c84c..83986388 100644 --- a/client_code/Switch/__init__.py +++ b/client_code/Switch/__init__.py @@ -186,8 +186,13 @@ def text_post(self, value): def _anvil_get_design_info_(self, *args, **kws): design_info = super()._anvil_get_design_info_(*args, **kws) - props = design_info.get("propertyDescriptions", []) + prop_key = ( + "propertyDescriptions" + if "propertyDescriptions" in design_info + else "properties" + ) + props = design_info.get(prop_key, []) props = [p for p in props if p.get("name") not in _remove_props] props = _include_props + props - design_info["propertyDescriptions"] = props + design_info[prop_key] = props return design_info From 4c8c4306c6f803856ba3c23b483fa0988e166823 Mon Sep 17 00:00:00 2001 From: stu Date: Tue, 3 Oct 2023 16:32:49 +0800 Subject: [PATCH 4/5] Update change log --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13568d0a..db5a6f62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,8 @@ ## Minor Changes * wait_for_writeback is now written in pure python https://github.com/anvilistas/anvil-extras/pull/431 +* Switch - improve designer behaviour + https://github.com/anvilistas/anvil-extras/pull/470 # v2.4.0 14-Jun-2023 From 4fb38a33a00a0c9eefbf3a71eaba4cd6e34e63f7 Mon Sep 17 00:00:00 2001 From: stu Date: Tue, 3 Oct 2023 16:53:15 +0800 Subject: [PATCH 5/5] future proof with designer api alt --- client_code/Switch/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/client_code/Switch/__init__.py b/client_code/Switch/__init__.py index 83986388..6f0c2c67 100644 --- a/client_code/Switch/__init__.py +++ b/client_code/Switch/__init__.py @@ -134,6 +134,14 @@ ] +def _clean_props(props): + props = [p for p in props if p.get("name") not in _remove_props] + return _include_props + props + + +_prop_descriptions = _clean_props(getattr(CheckBox, "_anvil_properties_", [])) + + class Switch(CheckBox): def __init__(self, checked_color=primary, text_pre="", text_post="", **properties): dom_node = self._dom_node = _get_dom_node(self) @@ -184,6 +192,8 @@ def text_post(self, value): text = text_post # override the CheckBox property + _anvil_properties_ = _prop_descriptions + def _anvil_get_design_info_(self, *args, **kws): design_info = super()._anvil_get_design_info_(*args, **kws) prop_key = ( @@ -192,7 +202,5 @@ def _anvil_get_design_info_(self, *args, **kws): else "properties" ) props = design_info.get(prop_key, []) - props = [p for p in props if p.get("name") not in _remove_props] - props = _include_props + props - design_info[prop_key] = props + design_info[prop_key] = _clean_props(props) return design_info