module UnderOs::UI::Style::Margins

Public Instance Methods

display() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 7
def display
  @display || :block
end
display=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 11
def display=(value)
  @display = %w[none block inline].include?(value.to_s) ? value.to_sym : :block
  @view.hidden = @display == :none
  set_offsets if @display == :inline
end
margin() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 17
def margin
  [marginTop, marginRight, marginBottom, marginLeft]
end
margin=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 21
def margin=(value)
  @margin_top, @margin_right, @margin_botom, @margin_left = to_4dim_array(value)
  set_offsets
end
marginBottom() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 53
def marginBottom
  @margin_botom || 0
end
marginBottom=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 57
def marginBottom=(value)
  @margin_botom = value
  set_offsets
end
marginLeft() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 35
def marginLeft
  @margin_left || 0
end
marginLeft=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 39
def marginLeft=(value)
  @margin_left = value
  set_offsets
end
marginRight() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 44
def marginRight
  @margin_right || 0
end
marginRight=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 48
def marginRight=(value)
  @margin_right = value
  set_offsets
end
marginTop() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 26
def marginTop
  @margin_top || 0
end
marginTop=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 30
def marginTop=(value)
  @margin_top = value
  set_offsets
end
padding() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 62
def padding
  [paddingTop, paddingRight, paddingBottom, paddingLeft]
end
padding=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 66
def padding=(value)
  @padding_top, @padding_right, @padding_botom, @padding_left = to_4dim_array(value)
  set_offsets
end
paddingBottom() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 98
def paddingBottom
  @padding_botom || 0
end
paddingBottom=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 102
def paddingBottom=(value)
  @padding_botom = value
  set_paddings
end
paddingLeft() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 80
def paddingLeft
  @padding_left || 0
end
paddingLeft=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 84
def paddingLeft=(value)
  @padding_left = value
  set_paddings
end
paddingRight() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 89
def paddingRight
  @padding_right || 0
end
paddingRight=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 93
def paddingRight=(value)
  @padding_right = value
  set_paddings
end
paddingTop() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 71
def paddingTop
  @padding_top || 0
end
paddingTop=(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 75
def paddingTop=(value)
  @padding_top = value
  set_paddings
end

Private Instance Methods

parent_offset_x() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 136
def parent_offset_x
  offset       = 0
  parent_frame = parent_view_frame

  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

  offset
end
parent_view_frame() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 151
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
set_offsets() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 119
def set_offsets
  return nil if display != :inline

  position_x = marginLeft + parent_offset_x
  position_y = marginTop

  @view.frame = [[position_x, position_y], [
    @view.frame.size.width, @view.frame.size.height
  ]]
end
set_paddings() click to toggle source
# File lib/under_os/ui/style/margins.rb, line 130
def set_paddings
  if @view.respond_to?(:contentEdgeInsets)
    @view.contentEdgeInsets = UIEdgeInsetsMake(paddingTop, paddingLeft, paddingBottom, paddingRight)
  end
end
to_4dim_array(value) click to toggle source
# File lib/under_os/ui/style/margins.rb, line 109
def to_4dim_array(value)
  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
  end
end