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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'motion-prime/views/layout.rb', line 51

def self.included base
  base.class_eval do
    [::UIActionSheet, ::UIActivityIndicatorView, ::MPButton, ::UIDatePicker, ::UIImageView, ::MPLabel,
      ::UIPageControl, ::UIPickerView, ::UIProgressView, ::UIScrollView, ::UISearchBar, ::UISegmentedControl,
      ::UISlider, ::UIStepper, ::UISwitch, ::UITabBar, ::UICollectionView, ::UITableView, ::UITableViewCell,
      ::MPTextField, ::MPTextView, ::UIToolbar, ::UIWebView, ::UINavigationBar, ::UIPageViewController,
      ::MPTableCellWithSection, ::MPCollectionCellWithSection, ::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



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 11

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

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

  set_options_for(view, options, &block)
  if superview = render_target || view_stack.last
    insert_index ? superview.insertSubview(view, atIndex: insert_index) : superview.addSubview(view)
  end
  view.on_added if view.respond_to?(:on_added)
  view
end

#set_options_for(view, options = {}, &block) ⇒ Object Also known as: update_options_for



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

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

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



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

def setup(view, options = {}, &block)
  puts "DEPRECATION: screen#setup is deprecated, please use screen#set_options_for instead"
  set_options_for(view, options, &block)
end

#view_stackObject



47
48
49
# File 'motion-prime/views/layout.rb', line 47

def view_stack
  @view_stack ||= []
end