class RMQViewStyler

Attributes

bg_color[RW]
context[RW]
corner_radius[RW]
view[RW]

Public Class Methods

new(view, context) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 7
def initialize(view, context)
  @needs_finalize = false
  @view = view
  @context = context
  @bg_color = nil
  @corner_radius = nil
end

Public Instance Methods

background_color=(color) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 127
def background_color=(color)
  @view.backgroundColor = @bg_color = convert_color(color)
end
background_resource=(bg) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 131
def background_resource=(bg)
  @view.backgroundResource = bg
end
cleanup() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 15
def cleanup
  @layout_params = nil
  @needs_finalize = nil
  @context = nil
  @bg_color = nil
  @corner_radius = nil
  @margin = nil
  @padding = nil
  @view = nil
end
convert_color(color) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 245
def convert_color(color)
  return ColorFactory.from_hex(color) if color.is_a?(String)
  color
end
convert_dimension_value(value) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 26
def convert_dimension_value(value)
  case value
  when :match_parent, :full
    Android::View::ViewGroup::LayoutParams::MATCH_PARENT
  when :wrap_content
    Android::View::ViewGroup::LayoutParams::WRAP_CONTENT
  else
    dp2px(value)
  end
end
convert_gravity(gravity) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 250
def convert_gravity(gravity)
  case gravity
  when :center then Android::View::Gravity::CENTER
  when :left then Android::View::Gravity::LEFT
  else
    gravity
  end
end
create_drawable(corner_radius) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 267
def create_drawable(corner_radius)
  createDrawable(corner_radius)
end
density() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 263
def density
  @density ||= @context.getResources.getDisplayMetrics.density
end
dp2px(dp_val) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 259
def dp2px(dp_val)
  (dp_val * density + 0.5).to_i
end
finalize() click to toggle source

use this if you need to do something after all style methods have been called (e.g. applying layout params)

# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 113
def finalize
  return unless @needs_finalize

  create_rounded_bg if corner_radius
  layout_params.setMargins(margin[:left],
    margin[:top],
    margin[:right],
    margin[:bottom]) if layout_params.respond_to?(:setMargins)
  @view.setLayoutParams(layout_params)

  @view.setPadding(padding[:left], padding[:top], padding[:right], padding[:bottom])
end
frame=(value)
Alias for: layout=
gravity=(gravity) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 145
def gravity=(gravity)
  layout_params.gravity = convert_gravity(gravity)
end
layout=(value) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 52
def layout=(value)
  return unless lp = layout_params

  if value == :full
    lp.width = convert_dimension_value(:full)
    lp.height = convert_dimension_value(:full)
    @view.setLayoutParams(lp)
  elsif value.is_a?(Hash)
    hash = value
    if w = (hash[:w] || hash[:width])
      lp.width = convert_dimension_value(w)
    end
    if h = (hash[:h] || hash[:height])
      lp.height = convert_dimension_value(h)
    end
    if l = (hash[:l] || hash[:left] || hash[:left_margin])
      lp.leftMargin = convert_dimension_value(l)
    end
    if t = (hash[:t] || hash[:top] || hash[:top_margin])
      lp.topMargin = convert_dimension_value(t)
    end
    if fr = (hash[:fr] || hash[:from_right] || hash[:right_margin])
      lp.rightMargin = convert_dimension_value(fr)
    end
    if fb = (hash[:fb] || hash[:from_bottom] || hash[:bottom_margin])
      lp.bottomMargin = convert_dimension_value(fb)
    end

    # TODO do center

    # TODO gravity

    # TODO do the relative bits

    @view.setLayoutParams(lp)

    if pad = hash[:padding]
      self.padding = pad
    end
  end

end
Also aliased as: frame=
layout_align_parent_left=(left_in_parent) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 170
def layout_align_parent_left=(left_in_parent)
  @needs_finalize = true
  left = left_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::ALIGN_PARENT_LEFT, left)
end
layout_align_parent_right=(right_in_parent) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 177
def layout_align_parent_right=(right_in_parent)
  @needs_finalize = true
  right = right_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::ALIGN_PARENT_RIGHT, right)
end
layout_center_horizontal=(center_horizontal) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 163
def layout_center_horizontal=(center_horizontal)
  @needs_finalize = true
  center = center_horizontal ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::CENTER_HORIZONTAL, center)
end
layout_center_in_parent=(center_in_parent) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 149
def layout_center_in_parent=(center_in_parent)
  @needs_finalize = true
  center = center_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::CENTER_IN_PARENT, center)
end
layout_center_vertical=(center_vertical) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 156
def layout_center_vertical=(center_vertical)
  @needs_finalize = true
  center = center_vertical ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::CENTER_VERTICAL, center)
end
layout_gravity=(gravity) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 235
def layout_gravity=(gravity)
  @needs_finalize = true
  layout_params.gravity = convert_gravity(gravity)
end
layout_height=(layout_height) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 140
def layout_height=(layout_height)
  @needs_finalize = true
  layout_params.height = convert_dimension_value(layout_height)
end
layout_params() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 37
def layout_params
  @layout_params ||= begin
    #@view.setMargins(0, 0, 0, 0)
    #mp @view.LayoutParams
    if lp = @view.getLayoutParams
      lp
    else
      #mp 1
      #mp @view
      Android::View::ViewGroup::LayoutParams.new(0,0)
      #Android::Widget::LinearLayout::LayoutParams.new(0,0)
    end
  end
end
layout_weight=(weight) click to toggle source

This can only be used on a widget that’s within a LinearLayout

# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 230
def layout_weight=(weight)
  @needs_finalize = true
  layout_params.weight = weight
end
layout_width=(layout_width) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 135
def layout_width=(layout_width)
  @needs_finalize = true
  layout_params.width = convert_dimension_value(layout_width)
end
margin=(m) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 224
def margin=(m)
  @needs_finalize = true
  margin[:left] = margin[:top] = margin[:right] = margin[:bottom] = dp2px(m)
end
margin_bottom=(m_bottom) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 219
def margin_bottom=(m_bottom)
  @needs_finalize = true
  margin[:bottom] = dp2px(m_bottom)
end
margin_left=(m_left) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 204
def margin_left=(m_left)
  @needs_finalize = true
  margin[:left] = dp2px(m_left)
end
margin_right=(m_right) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 214
def margin_right=(m_right)
  @needs_finalize = true
  margin[:right] = dp2px(m_right)
end
margin_top=(m_top) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 209
def margin_top=(m_top)
  @needs_finalize = true
  margin[:top] = dp2px(m_top)
end
padding=(pad) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 96
def padding=(pad)
  if pad.is_a?(Potion::Integer)
    pad = convert_dimension_value(pad)
    @view.setPadding(pad, pad, pad, pad)
  elsif pad.is_a?(Hash)
    @view.setPadding(
      convert_dimension_value(pad[:l] || pad[:left] || 0),
      convert_dimension_value(pad[:t] || pad[:top] || 0),
      convert_dimension_value(pad[:r] || pad[:right] || 0),
      convert_dimension_value(pad[:b] || pad[:bottom] || 0))
  else
    mp pad.class.name
  end
end
padding_bottom=(pad_bottom) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 199
def padding_bottom=(pad_bottom)
  @needs_finalize = true
  padding[:bottom] = dp2px(pad_bottom)
end
padding_left=(pad_left) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 184
def padding_left=(pad_left)
  @needs_finalize = true
  padding[:left] = dp2px(pad_left)
end
padding_right=(pad_right) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 194
def padding_right=(pad_right)
  @needs_finalize = true
  padding[:right] = dp2px(pad_right)
end
padding_top=(pad_top) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 189
def padding_top=(pad_top)
  @needs_finalize = true
  padding[:top] = dp2px(pad_top)
end
visibility=(value) click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 271
def visibility=(value)
  case value
  when :visible, true
    view.setVisibility(Potion::View::VISIBLE)
  when :invisible, false
    view.setVisibility(Potion::View::INVISIBLE)
  when :gone
    view.setVisibility(Potion::View::GONE)
  end
end
Also aliased as: visible=
visible=(value)
Alias for: visibility=

Private Instance Methods

create_rounded_bg() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 304
def create_rounded_bg
  # creating this shape in Ruby raises an ART error, but it works in Java
  #radii = [corner_radius, corner_radius, corner_radius, corner_radius,
  #         corner_radius, corner_radius, corner_radius, corner_radius]
  #shape = Android::Graphics::Drawable::Shapes::RoundRectShape.new(radii, nil, nil)

  # StyleHelper contains a Java extension
  shape = StylerHelper.shared.createRoundRect(dp2px(corner_radius))

  drawable = Android::Graphics::Drawable::ShapeDrawable.new(shape)
  drawable.paint.color = bg_color
  @view.setBackgroundDrawable(drawable)
end
margin() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 289
def margin
  @margin ||= { left: 0, top: 0, right: 0, bottom: 0 }
end
padding() click to toggle source
# File lib/project/ruby_motion_query/stylers/rmq_view_styler.rb, line 285
def padding
  @padding ||= { left: 0, top: 0, right: 0, bottom: 0 }
end