From d81df5f4e26d719b92a0e6fb404b890221fa429e Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Mon, 25 Apr 2022 16:48:17 -0500 Subject: [PATCH] Moved min_width/height, max_width/height and fill control from Container into Element#dimensional_size so any element can use them --- lib/cyberarm_engine/ui/element.rb | 21 +++++++++++++++++++- lib/cyberarm_engine/ui/elements/container.rb | 20 ------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/cyberarm_engine/ui/element.rb b/lib/cyberarm_engine/ui/element.rb index fd33713..e269851 100644 --- a/lib/cyberarm_engine/ui/element.rb +++ b/lib/cyberarm_engine/ui/element.rb @@ -429,11 +429,30 @@ module CyberarmEngine def dimensional_size(size, dimension) raise "dimension must be either :width or :height" unless %i[width height].include?(dimension) - if size.is_a?(Numeric) && size.between?(0.0, 1.0) + new_size = if size.is_a?(Numeric) && size.between?(0.0, 1.0) (@parent.send(:"content_#{dimension}") * size).round - send(:"noncontent_#{dimension}").round else size end + + if @parent && @style.fill # Handle fill behavior + fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing + + if dimension == :width && @parent.is_a?(Flow) + space_available_width = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings).round + return space_available_width - noncontent_width + + elsif dimension == :height && @parent.is_a?(Stack) + space_available_height = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings).round + return space_available_height - noncontent_height + end + + else # Handle min_width/height and max_width/height + return @style.send(:"min_#{dimension}") if @style.send(:"min_#{dimension}") && new_size < @style.send(:"min_#{dimension}") + return @style.send(:"max_#{dimension}") if @style.send(:"max_#{dimension}") && new_size > @style.send(:"max_#{dimension}") + end + + new_size end def background=(_background) diff --git a/lib/cyberarm_engine/ui/elements/container.rb b/lib/cyberarm_engine/ui/elements/container.rb index 6c0baff..098697d 100644 --- a/lib/cyberarm_engine/ui/elements/container.rb +++ b/lib/cyberarm_engine/ui/elements/container.rb @@ -124,26 +124,6 @@ module CyberarmEngine @width = _width || (@children.map { |c| c.x + c.outer_width }.max || 0).round @height = _height || (@children.map { |c| c.y + c.outer_height }.max || 0).round - - # min width/height - @width = @style.min_width if @style.min_width && @width < @style.min_width - @height = @style.min_height if @style.min_height && @height < @style.min_height - - # max width/height - @width = @style.max_width if @style.max_width && @width > @style.max_width - @height = @style.max_height if @style.max_height && @height > @style.max_height - - if @parent && @style.fill - fill_siblings = @parent.children.select { |c| c.style.fill }.count.to_f # include self since we're dividing - - space_available_width = ((@parent.content_width - (@parent.children.reject { |c| c.style.fill }).map(&:outer_width).sum) / fill_siblings).round - space_available_height = ((@parent.content_height - (@parent.children.reject { |c| c.style.fill }).map(&:outer_height).sum) / fill_siblings).round - - @width = space_available_width - noncontent_width if @parent.is_a?(Flow) - @height = space_available_height - noncontent_height if @parent.is_a?(Stack) - - # pp ["siblings: #{fill_siblings}", "parent is #{@parent.inspect}", [@parent.content_width, space_available_width, @width, outer_width], [@parent.content_height, space_available_height, @height, outer_height]] - end end # Move child to parent after positioning