Module: MotionPrime::Layout

Included in:
Screen
Defined in:
motion-prime/views/layout.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'motion-prime/views/layout.rb', line 45

def self.included base
  base.class_eval do
    [::UIActionSheet, ::UIActivityIndicatorView, ::UIButton, ::UIDatePicker, ::UIImageView, ::UILabel,
      ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
      ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UITableView, ::UITableViewCell, ::UITextField, ::UITextView,
      ::UIToolbar, ::UIWebView, ::UINavigationBar, ::MPCellWithSection, ::MBProgressHUD, ::MPSpinner].each do |klass|

      shorthand = "#{klass}"[2..-1].underscore.to_sym

      define_method(shorthand) do |options, &block|
        options[:screen] = self
        element = MotionPrime::BaseElement.factory(shorthand, options)
        element.render({}, &block)
        element
      end
    end
  end
end

Instance Method Details

#add_view(klass, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'motion-prime/views/layout.rb', line 6

def add_view(klass, options = {}, &block)
  options = options.clone
  render_target = options.delete(:render_target)
  parent_view = options.delete(:parent_view) || render_target

  bounds = if view_stack.empty?
    parent_view.try(:bounds) || CGRectZero
  else
    view_stack.last.bounds
  end
  builder = ViewBuilder.new(klass, options)
  options = builder.options.merge(calculate_frame: true, bounds: bounds)
  view = builder.view
  insert_index = options.delete(:at_index)

  if render_target
    options[:bounds] = render_target.bounds
    insert_index ? render_target.insertSubview(view, atIndex: insert_index) : render_target.addSubview(view)
  elsif view_stack.any?
    insert_index ? view_stack.last.insertSubview(view, atIndex: insert_index) : view_stack.last.addSubview(view)
  end

  setup(view, options, &block)
  view.on_added if view.respond_to?(:on_added)

  view
end

#setup(view, options = {}, &block) ⇒ Object



34
35
36
37
38
39
# File 'motion-prime/views/layout.rb', line 34

def setup(view, options = {}, &block)
  ViewStyler.new(view, options.delete(:bounds), options).apply
  view_stack.push(view)
  block.call(view) if block_given?
  view_stack.pop
end

#view_stackObject



41
42
43
# File 'motion-prime/views/layout.rb', line 41

def view_stack
  @view_stack ||= []
end