From 1af276219a013bfa9dddb68f0f089e23e09babb2 Mon Sep 17 00:00:00 2001 From: Ian Harrigan Date: Sat, 4 May 2024 08:22:43 +0200 Subject: [PATCH] ability to set layout properties from css --- haxe/ui/containers/Box.hx | 3 ++ haxe/ui/layouts/Layout.hx | 4 ++ haxe/ui/layouts/VerticalGridLayout.hx | 8 ++++ haxe/ui/styles/Style.hx | 55 +++++++++++++++++++++------ 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/haxe/ui/containers/Box.hx b/haxe/ui/containers/Box.hx index cb9d45ed1..2e35a12b2 100644 --- a/haxe/ui/containers/Box.hx +++ b/haxe/ui/containers/Box.hx @@ -86,6 +86,9 @@ class Box extends Component implements IDataComponent { if (style.layout != null) { layoutName = style.layout; } + if (style.layoutProperties != null && this.layout != null) { + this.layout.applyProperties(style.layoutProperties); + } } } diff --git a/haxe/ui/layouts/Layout.hx b/haxe/ui/layouts/Layout.hx index 785cd3318..166adebbf 100644 --- a/haxe/ui/layouts/Layout.hx +++ b/haxe/ui/layouts/Layout.hx @@ -37,6 +37,10 @@ class Layout implements ILayout { return _component.findComponents(styleName, type, maxDepth); } + public function applyProperties(props:Map) { + + } + @:access(haxe.ui.core.Component) public function refresh() { if (_component != null && _component.isReady == true) { diff --git a/haxe/ui/layouts/VerticalGridLayout.hx b/haxe/ui/layouts/VerticalGridLayout.hx index c8cc1b9f3..8c23c8cf1 100644 --- a/haxe/ui/layouts/VerticalGridLayout.hx +++ b/haxe/ui/layouts/VerticalGridLayout.hx @@ -7,6 +7,14 @@ class VerticalGridLayout extends Layout { super(); } + public override function applyProperties(props:Map) { + if (props != null) { + if (props.exists("columns") && props.get("columns") != null) { + this.columns = props.get("columns"); + } + } + } + private var _columns:Int = 1; @:clonable public var columns(get, set):Int; private function get_columns():Int { diff --git a/haxe/ui/styles/Style.hx b/haxe/ui/styles/Style.hx index d13f12216..6519ad9e1 100644 --- a/haxe/ui/styles/Style.hx +++ b/haxe/ui/styles/Style.hx @@ -183,6 +183,8 @@ class Style { return 0; } + @:optional public var layoutProperties:Map = null; + @:optional public var customDirectives:Map = null; public function mergeDirectives(map:Map) { @@ -512,18 +514,26 @@ class Style { case "justify-content": justifyContent = ValueTools.string(v.value); case _: - var use = DirectiveHandler.hasDirectiveHandler(v.directive); - #if haxeui_custom_directives_relaxed // means we dont require a handler, so the backend can just do "anything" with them - use = true; - #end - if (use) { - if (customDirectives == null) { - customDirectives = new Map(); + if (StringTools.startsWith(key, "layout-")) { + var layoutPropName = key.substring("layout-".length); + if (layoutProperties == null) { + layoutProperties = new Map(); } - if (v.value == null || v.value == VNone) { - customDirectives.remove(v.directive); - } else { - customDirectives.set(v.directive, v); + layoutProperties.set(layoutPropName, ValueTools.any(v.value)); + } else { + var use = DirectiveHandler.hasDirectiveHandler(v.directive); + #if haxeui_custom_directives_relaxed // means we dont require a handler, so the backend can just do "anything" with them + use = true; + #end + if (use) { + if (customDirectives == null) { + customDirectives = new Map(); + } + if (v.value == null || v.value == VNone) { + customDirectives.remove(v.directive); + } else { + customDirectives.set(v.directive, v); + } } } } @@ -683,6 +693,16 @@ class Style { if (s.includeInLayout != null) includeInLayout = s.includeInLayout; if (s.justifyContent != null) justifyContent = s.justifyContent; + if (s.layoutProperties != null) { + if (this.layoutProperties == null) { + this.layoutProperties = new Map(); + } + for (layoutPropName in s.layoutProperties.keys()) { + var v = s.layoutProperties.get(layoutPropName); + this.layoutProperties.set(layoutPropName, v); + } + } + if (s.customDirectives != null) { if (this.customDirectives == null) { this.customDirectives = new Map(); @@ -829,6 +849,19 @@ class Style { if (s.includeInLayout != includeInLayout) return false; if (s.justifyContent != justifyContent) return false; + if (s.layoutProperties != null && s.layoutProperties != null) { + for (layoutPropName in s.layoutProperties) { + if (!this.layoutProperties.exists(layoutPropName)) { + return false; + } + } + for (layoutPropName in this.layoutProperties) { + if (!s.layoutProperties.exists(layoutPropName)) { + return false; + } + } + } + if (s.customDirectives != null && this.customDirectives != null) { for (directive in s.customDirectives.keys()) { if (!this.customDirectives.exists(directive)) {