From 4b58a77ec2e52379625ad60af74c5e26da08545b Mon Sep 17 00:00:00 2001 From: Face <69168154+face-hh@users.noreply.github.com> Date: Wed, 30 Jul 2025 14:39:33 +0300 Subject: [PATCH] % width/height, body background color, margin/padding --- README.md | 2 +- Scenes/main.tscn | 1 + Scripts/AutoSizingFlexContainer.gd | 109 ++++++++++--------- Scripts/B9/CSSParser.gd | 52 +++++++++ Scripts/Constants.gd | 79 +++++++++++++- Scripts/StyleManager.gd | 163 ++++++++++++++++++++++++++--- Scripts/main.gd | 5 + 7 files changed, 347 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index e810707..497e305 100644 --- a/README.md +++ b/README.md @@ -59,4 +59,4 @@ Supported styles: - `flex-shrink-{n}` (flex-shrink) - `basis-{size}` (flex-basis) - `self-auto`, `self-start`, `self-end`, `self-center`, `self-stretch`, `self-baseline` (align-self) -- `order-{n}` (order) \ No newline at end of file +- `order-{n}` (order) diff --git a/Scenes/main.tscn b/Scenes/main.tscn index 689f383..8808591 100644 --- a/Scenes/main.tscn +++ b/Scenes/main.tscn @@ -266,6 +266,7 @@ layout_mode = 2 mouse_filter = 1 [node name="WebsiteBackground" type="Panel" parent="."] +unique_name_in_owner = true z_index = -1 layout_mode = 1 anchors_preset = 15 diff --git a/Scripts/AutoSizingFlexContainer.gd b/Scripts/AutoSizingFlexContainer.gd index 91a93ad..0b8d9e0 100644 --- a/Scripts/AutoSizingFlexContainer.gd +++ b/Scripts/AutoSizingFlexContainer.gd @@ -85,38 +85,17 @@ func _resort() -> void: _root.mark_dirty_and_propogate() - var auto_size_width = not has_meta("custom_css_width") and not has_meta("should_fill_horizontal") - var auto_size_height = not has_meta("custom_css_height") and not has_meta("should_fill_vertical") + var auto_size_width = not has_meta("custom_css_width") and not has_meta("should_fill_horizontal") and not has_meta("custom_css_width_percentage") + var auto_size_height = not has_meta("custom_css_height") and not has_meta("should_fill_vertical") and not has_meta("custom_css_height_percentage") var available_width = NAN var available_height = NAN if not auto_size_width: - if has_meta("should_fill_horizontal"): - # For w-full, use the full parent size if available - var parent_container = get_parent() - if parent_container and parent_container.size.x > 0: - available_width = parent_container.size.x - elif size.x > 0: - available_width = size.x - else: - # Fallback: try to get from custom_minimum_size - available_width = custom_minimum_size.x if custom_minimum_size.x > 0 else NAN - else: - available_width = size.x + available_width = calculate_available_dimension(true) if not auto_size_height: - if has_meta("should_fill_vertical"): - # For h-full, use the full parent size if available - var parent_container = get_parent() - if parent_container and parent_container.size.y > 0: - available_height = parent_container.size.y - elif size.y > 0: - available_height = size.y - else: - available_height = custom_minimum_size.y if custom_minimum_size.y > 0 else NAN - else: - available_height = size.y + available_height = calculate_available_dimension(false) _root.calculate_layout(available_width, available_height, 1) # 1 = LTR direction @@ -127,31 +106,8 @@ func _resort() -> void: ) # Respect any explicit width/height set via metadata - var custom_w = 0.0 - if has_meta("custom_css_width"): - custom_w = float(get_meta("custom_css_width")) - elif has_meta("should_fill_horizontal"): - # Use the same logic as available_width calculation - var parent_container = get_parent() - if parent_container and parent_container.size.x > 0: - custom_w = parent_container.size.x - elif size.x > 0: - custom_w = size.x - else: - custom_w = custom_minimum_size.x if custom_minimum_size.x > 0 else 0.0 - - var custom_h = 0.0 - if has_meta("custom_css_height"): - custom_h = float(get_meta("custom_css_height")) - elif has_meta("should_fill_vertical"): - # Use the same logic as available_height calculation - var parent_container = get_parent() - if parent_container and parent_container.size.y > 0: - custom_h = parent_container.size.y - elif size.y > 0: - custom_h = size.y - else: - custom_h = custom_minimum_size.y if custom_minimum_size.y > 0 else 0.0 + var custom_w = calculate_custom_dimension(true) + var custom_h = calculate_custom_dimension(false) var needed_size = Vector2( max(custom_w, computed_size.x), @@ -200,3 +156,56 @@ func _resort() -> void: queue_redraw() emit_signal("flex_resized") + +func calculate_available_dimension(is_width: bool) -> float: + var dimension_key = "custom_css_width" if is_width else "custom_css_height" + var percentage_key = "custom_css_width_percentage" if is_width else "custom_css_height_percentage" + var fill_key = "should_fill_horizontal" if is_width else "should_fill_vertical" + + if has_meta(fill_key): + return get_parent_or_fallback_size(is_width) + elif has_meta(percentage_key): + var percentage_str = get_meta(percentage_key) + var percentage = float(percentage_str.replace("%", "")) / 100.0 + var parent_size = get_parent_size(is_width) + return parent_size * percentage if parent_size > 0 else (custom_minimum_size.x if is_width else custom_minimum_size.y) + else: + return size.x if is_width else size.y + +func calculate_custom_dimension(is_width: bool) -> float: + var dimension_key = "custom_css_width" if is_width else "custom_css_height" + var percentage_key = "custom_css_width_percentage" if is_width else "custom_css_height_percentage" + var fill_key = "should_fill_horizontal" if is_width else "should_fill_vertical" + + if has_meta(dimension_key): + return float(get_meta(dimension_key)) + elif has_meta(percentage_key): + var percentage_str = get_meta(percentage_key) + var percentage = float(percentage_str.replace("%", "")) / 100.0 + var parent_size = get_parent_size(is_width) + if parent_size > 0: + return parent_size * percentage + elif (size.x if is_width else size.y) > 0: + return (size.x if is_width else size.y) * percentage + else: + return 0.0 + elif has_meta(fill_key): + return get_parent_or_fallback_size(is_width) + else: + return 0.0 + +func get_parent_size(is_width: bool) -> float: + var parent_container = get_parent() + if parent_container: + return parent_container.size.x if is_width else parent_container.size.y + return 0.0 + +func get_parent_or_fallback_size(is_width: bool) -> float: + var parent_container = get_parent() + if parent_container and (parent_container.size.x if is_width else parent_container.size.y) > 0: + return parent_container.size.x if is_width else parent_container.size.y + elif (size.x if is_width else size.y) > 0: + return size.x if is_width else size.y + else: + var fallback = custom_minimum_size.x if is_width else custom_minimum_size.y + return fallback if fallback > 0 else NAN diff --git a/Scripts/B9/CSSParser.gd b/Scripts/B9/CSSParser.gd index 46d5c73..0abeb94 100644 --- a/Scripts/B9/CSSParser.gd +++ b/Scripts/B9/CSSParser.gd @@ -249,11 +249,15 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) -> # Width if utility_name.begins_with("w-"): var val = utility_name.substr(2) + if val.begins_with("[") and val.ends_with("]"): + val = val.substr(1, val.length() - 2) rule.properties["width"] = parse_size(val) return # Height if utility_name.begins_with("h-"): var val = utility_name.substr(2) + if val.begins_with("[") and val.ends_with("]"): + val = val.substr(1, val.length() - 2) rule.properties["height"] = parse_size(val) return # Min width @@ -367,6 +371,54 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) -> rule.properties["order"] = val.to_int() return + # Handle border-radius classes like rounded-8, rounded-lg, etc. + if utility_name.begins_with("rounded-"): + var val = utility_name.substr(8) + rule.properties["border-radius"] = parse_size(val) + return + if utility_name == "rounded": + rule.properties["border-radius"] = "4px" # Default rounded + return + + # Handle padding classes like p-8, px-4, py-2, etc. + if utility_name.begins_with("p-"): + var val = utility_name.substr(2) + var padding_value = parse_size(val) + rule.properties["padding"] = padding_value + return + if utility_name.begins_with("px-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-left"] = padding_value + rule.properties["padding-right"] = padding_value + return + if utility_name.begins_with("py-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-top"] = padding_value + rule.properties["padding-bottom"] = padding_value + return + if utility_name.begins_with("pt-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-top"] = padding_value + return + if utility_name.begins_with("pr-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-right"] = padding_value + return + if utility_name.begins_with("pb-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-bottom"] = padding_value + return + if utility_name.begins_with("pl-"): + var val = utility_name.substr(3) + var padding_value = parse_size(val) + rule.properties["padding-left"] = padding_value + return + # Handle border radius classes like rounded, rounded-lg, rounded-[12px] if utility_name == "rounded": rule.properties["border-radius"] = "4px" diff --git a/Scripts/Constants.gd b/Scripts/Constants.gd index 29dfa11..156c25b 100644 --- a/Scripts/Constants.gd +++ b/Scripts/Constants.gd @@ -6,6 +6,7 @@ const SECONDARY_COLOR = Color(43/255.0, 43/255.0, 43/255.0, 1) const HOVER_COLOR = Color(0, 0, 0, 1) const DEFAULT_CSS = """ +body { text-base text-[#000000] text-left } h1 { text-5xl font-bold } h2 { text-4xl font-bold } h3 { text-3xl font-bold } @@ -23,6 +24,82 @@ pre { text-xl font-mono } button { bg-[#1b1b1b] rounded-md text-white hover:bg-[#2a2a2a] active:bg-[#101010] } """ +var HTML_CONTENT = """
+1,240
+$9,842
+3,590
+Name: Jane Doe
+Email: jane@example.com
+Status: Active
+Plan: Pro
+Projects: 8
+Tasks: 42
+