lib/under_os/ui/style/margins.rb in under-os-1.0.0 vs lib/under_os/ui/style/margins.rb in under-os-1.1.0
- old
+ new
@@ -17,15 +17,12 @@
def margin
[marginTop, marginRight, marginBottom, marginLeft]
end
def margin=(value)
- value = to_4dim_array(value)
- self.marginTop = value[0]
- self.marginLeft = value[3]
- self.marginRight = value[1]
- self.marginBottom = value[2]
+ @margin_top, @margin_right, @margin_botom, @margin_left = to_4dim_array(value)
+ set_offsets
end
def marginTop
@margin_top || 0
end
@@ -65,15 +62,12 @@
def padding
[paddingTop, paddingRight, paddingBottom, paddingLeft]
end
def padding=(value)
- value = to_4dim_array(value)
- self.paddingTop = value[0]
- self.paddingLeft = value[3]
- self.paddingRight = value[1]
- self.paddingBottom = value[2]
+ @padding_top, @padding_right, @padding_botom, @padding_left = to_4dim_array(value)
+ set_offsets
end
def paddingTop
@padding_top || 0
end
@@ -111,14 +105,11 @@
end
private
def to_4dim_array(value)
- if value.is_a?(String)
- value = value.gsub('px', '').strip.split().reject(&:blank?).map(&:to_f)
- end
-
+ value = value.gsub('px', '').split.map(&:to_f) if value.is_a?(String)
value = Array(value)
case value.size
when 1 then value * 4
when 2 then [value[0], value[1], value[0], value[1]]
else value
@@ -126,35 +117,48 @@
end
def set_offsets
return nil if display != :inline
- position_x = marginLeft
- position_y = marginTop
+ position_x = marginLeft + parent_offset_x
+ position_y = marginTop
- parent_view = view.superview
- previous_view = parent_view.subviews[parent_view.subviews.index(view)-1] unless parent_view.subviews[0] === view
+ @view.frame = [[position_x, position_y], [
+ @view.frame.size.width, @view.frame.size.height
+ ]]
+ end
- if previous_view
- pos = previous_view.frame.origin
- size = previous_view.frame.size
+ def set_paddings
+ if @view.respond_to?(:contentEdgeInsets)
+ @view.contentEdgeInsets = UIEdgeInsetsMake(paddingTop, paddingLeft, paddingBottom, paddingRight)
+ end
+ end
- position_x += pos.x + size.width
+ def parent_offset_x
+ offset = 0
+ parent_frame = parent_view_frame
- if wrap = UnderOs::UI::View.wrap_for(view)
- position_x += wrap.style.marginRight
+ if parent_frame
+ offset += parent_frame.origin.x + parent_frame.size.width
+
+ if wrap = UnderOs::UI::View.wrap_for(@view)
+ offset += wrap.style.marginRight
end
end
- view.frame = [[position_x, position_y],[
- view.frame.size.width, view.frame.size.height
- ]]
+ offset
end
- def set_paddings
- if @view.respond_to?(:contentEdgeInsets)
- @view.contentEdgeInsets = UIEdgeInsetsMake(paddingTop, paddingLeft, paddingBottom, paddingRight)
+ def parent_view_frame
+ parent_view = @view.superview
+ parent_frame = nil
+
+ unless parent_view.subviews[0] === @view
+ previous_view = parent_view.subviews[parent_view.subviews.index(@view)-1]
+ parent_frame = previous_view && previous_view.frame
end
+
+ parent_frame
end
end
end
end