Class: RMQViewStyler

Inherits:
Object show all
Defined in:
lib/project/ruby_motion_query/stylers/rmq_view_styler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, context) ⇒ RMQViewStyler

Returns a new instance of RMQViewStyler.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color.



4
5
6
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 4

def bg_color
  @bg_color
end

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 3

def context
  @context
end

#corner_radius(corner_radius) ⇒ Object

Returns the value of attribute corner_radius.



5
6
7
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 5

def corner_radius
  @corner_radius
end

#viewObject

Returns the value of attribute view.



2
3
4
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 2

def view
  @view
end

Instance Method Details

#background_color=(color) ⇒ Object



127
128
129
# 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) ⇒ Object



131
132
133
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 131

def background_resource=(bg)
  @view.backgroundResource = bg
end

#cleanupObject



15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object



245
246
247
248
# 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_gravity(gravity) ⇒ Object



250
251
252
253
254
255
256
257
# 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) ⇒ Object



267
268
269
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 267

def create_drawable(corner_radius)
  createDrawable(corner_radius)
end

#densityObject



263
264
265
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 263

def density
  @density ||= @context.getResources.getDisplayMetrics.density
end

#dp2px(dp_val) ⇒ Object



259
260
261
# 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

#finalizeObject

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



113
114
115
116
117
118
119
120
121
122
123
124
# 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

#gravity=(gravity) ⇒ Object



145
146
147
# 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) ⇒ Object Also known as: frame=



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#layout_align_parent_left=(left_in_parent) ⇒ Object



170
171
172
173
174
175
# 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) ⇒ Object



177
178
179
180
181
182
# 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) ⇒ Object



163
164
165
166
167
168
# 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) ⇒ Object



149
150
151
152
153
154
# 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) ⇒ Object



156
157
158
159
160
161
# 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) ⇒ Object



235
236
237
238
# 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) ⇒ Object



140
141
142
143
# 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_paramsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

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



230
231
232
233
# 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) ⇒ Object



135
136
137
138
# 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) ⇒ Object



224
225
226
227
# 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) ⇒ Object



219
220
221
222
# 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) ⇒ Object



204
205
206
207
# 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) ⇒ Object



214
215
216
217
# 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) ⇒ Object



209
210
211
212
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



199
200
201
202
# 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) ⇒ Object



184
185
186
187
# 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) ⇒ Object



194
195
196
197
# 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) ⇒ Object



189
190
191
192
# 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) ⇒ Object Also known as: visible=



271
272
273
274
275
276
277
278
279
280
# 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