Class: MotionKit::ViewLayout

Inherits:
BaseLayout show all
Defined in:
lib/motion-kit/layouts/view_layout.rb

Overview

A sensible parent class for any View-like layout class. Platform agnostic. Any platform-specific tasks are offloaded to child views (add_child, remove_child). Actually, "view like" is misleading, since technically it only assumes "tree like". You could use a ViewLayout subclass to construct a hierarchy representing a family tree, for instance. But that would be a silly use of MotionKit.

Direct Known Subclasses

CALayerLayout, Layout, MenuLayout, WindowLayout

Instance Attribute Summary

Attributes inherited from BaseLayout

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseLayout

#add_deferred_block, #apply, #apply_with_context, #apply_with_target, #center, #context, #deferred, #deferred_blocks, #down, #frame, #initialize, #ipad?, #iphone35?, #iphone4?, #iphone?, #left, method_added, #method_missing, #origin, overridden_methods, override_start, override_stop, overrides, #retina?, #right, #set_layout, #size, #target, #up, #v

Methods included from BaseLayoutClassMethods

#layout_for, #memoize, #new_child, #target_klasses, #targets

Constructor Details

This class inherits a constructor from MotionKit::BaseLayout

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MotionKit::BaseLayout

Class Method Details

.view(name) ⇒ Object

This is an attr_reader-like method that also calls build_view if the assigned to ivars in your layout method.

Examples:

class MyLayout < MK::Layout
  view :label
  view :login_button

  def layout
    # if element id and attr name match, no need to assign to ivar
    add UILabel, :label
    # if they don't match you must assign.  If you are using
    # Key-Value observation you should use the setter:
    self. = add UIButton, :button
  end

end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/motion-kit/layouts/view_layout.rb', line 33

def view(name)
  ivar_name = "@#{name}"
  define_method(name) do
    unless instance_variable_get(ivar_name)
      view = self.get(name)
      unless view
        build_view unless @view
        view = instance_variable_get(ivar_name) || self.get(name)
      end
      self.send("#{name}=", view)
      return view
    end
    return instance_variable_get(ivar_name)
  end
  # KVO compliance
  attr_writer name
end

Instance Method Details

#add(element, element_id = nil, &block) ⇒ Object

Delegates to create to instantiate a view and run a layout block, and adds the view to the current view on the view stack. If no view exists on the stack, a default root view can be created if that has been enabled.



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/motion-kit/layouts/view_layout.rb', line 165

def add(element, element_id=nil, &block)
  # make sure we have a target - raises NoContextError if none exists
  self.target

  element = initialize_view(element)
  unless @context
    create_default_root_context
  end
  self.apply(:add_child, element)
  create(element, element_id, &block)

  element
end

#all(element_id, in: root) ⇒ Object

Same as all, but with the root view specified.



212
213
214
215
216
217
# File 'lib/motion-kit/layouts/view_layout.rb', line 212

def all(element_id)
  if @layout != self
    return @layout.all(element_id)
  end
  self.all(element_id, in: self.view)
end

#call_style_method(element, element_id) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/motion-kit/layouts/view_layout.rb', line 121

def call_style_method(element, element_id)
  style_method = "#{element_id}_style"
  if @layout.respond_to?(style_method)
    @layout.context(element) do
      @layout.send(style_method)
    end
  end
  return element
end

#create(element, element_id = nil, &block) ⇒ Object

instantiates a view, possibly running a 'layout block' to add child views.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/motion-kit/layouts/view_layout.rb', line 104

def create(element, element_id=nil, &block)
  element = initialize_view(element)

  if element_id
    # We set the optional id and call the '_style' method, if it's been
    # defined.
    element.motion_kit_id = element_id
    self.call_style_method(element, element_id)
  end

  if block
    context(element, &block)
  end

  element
end

#create_default_root_context ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/motion-kit/layouts/view_layout.rb', line 255

def create_default_root_context
  if @assign_root
    # Originally I thought default_root should be `apply`ied like other
    # view-related methods, but actually this method *only* gets called
    # from within the `layout` method, and so should already be inside the
    # correct Layout subclass.
    @context = root(default_root)
  else
    raise NoContextError.new("No top level view specified (missing outer 'create' method?)")
  end
end

#first(element_id, in: root) ⇒ Object



188
# File 'lib/motion-kit/layouts/view_layout.rb', line 188

def first(element_id) ; get(element_id) ; end

#get(element_id, in: root) ⇒ Object

Same as get, but with the root view specified.



182
183
184
185
186
187
# File 'lib/motion-kit/layouts/view_layout.rb', line 182

def get(element_id)
  if @layout != self
    return @layout.get(element_id)
  end
  self.get(element_id, in: self.view)
end

#initial(&block) ⇒ Object

Raises:

  • (ArgumentError)


153
154
155
156
157
158
159
160
# File 'lib/motion-kit/layouts/view_layout.rb', line 153

def initial(&block)
  raise ArgumentError.new('Block required') unless block

  if @layout_state == :initial
    yield
  end
  return self
end

#last(element_id, in: root) ⇒ Object

Same as last, but with the root view specified.



199
200
201
202
203
204
# File 'lib/motion-kit/layouts/view_layout.rb', line 199

def last(element_id)
  if @layout != self
    return @layout.last(element_id)
  end
  self.last(element_id, in: self.view)
end

#nth(element_id, at: index, in: root) ⇒ Object

Same as nth, but with the root view specified.



225
226
227
228
229
230
# File 'lib/motion-kit/layouts/view_layout.rb', line 225

def nth(element_id, index)
  if @layout != self
    return @layout.nth(element_id, index)
  end
  self.all(element_id, in: self.view)[index]
end

#reapply(&block) ⇒ Object

Calls the style method of all objects in the view hierarchy

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
# File 'lib/motion-kit/layouts/view_layout.rb', line 144

def reapply(&block)
  raise ArgumentError.new('Block required') unless block

  if @layout_state == :reapply
    yield
  end
  return self
end

#reapply!(root = nil) ⇒ Object

Calls the style method of all objects in the view hierarchy



132
133
134
135
136
137
138
139
140
141
# File 'lib/motion-kit/layouts/view_layout.rb', line 132

def reapply!(root=nil)
  root ||= self.view
  @layout_state = :reapply
  MotionKit.find_all_views(root) do |view|
    call_style_method(view, view.motion_kit_id) if view.motion_kit_id
  end
  @layout_state = :initial

  return self
end

#remove(element_id, from: root) ⇒ Object

Same as remove, but with the root view specified.



239
240
241
242
243
244
# File 'lib/motion-kit/layouts/view_layout.rb', line 239

def remove(element_id)
  if @layout != self
    return @layout.remove(element_id)
  end
  self.remove(element_id, from: self.view)
end

#root(element, element_id = nil, &block) ⇒ Object

Assign a view to act as the 'root' view for this layout. This method can only be called once, and must be called before add is called for the first time (otherwise add will create a default root view). This method must be called from inside layout, otherwise you should just use create.

You can also call this method with just an element_id, and the default root view will be created.



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
94
95
96
97
98
99
100
101
# File 'lib/motion-kit/layouts/view_layout.rb', line 69

def root(element, element_id=nil, &block)
  if @view
    raise ContextConflictError.new("Already created the root view")
  end
  unless @assign_root
    raise InvalidRootError.new("You should only create a 'root' view from inside the 'layout' method (use 'create' elsewhere)")
  end
  @assign_root = false

  # this method can be called with just a symbol, to assign the root element_id
  if element.is_a?(Symbol)
    element_id = element
    # See note below about why we don't need to `apply(:default_root)`
    element = default_root
  end

  @view = initialize_view(element)
  if block
    if @context
      raise ContextConflictError.new("Already in a context")
    end

    context(@view) do
      # We're just using the `create` method for its side effects: calling the
      # style method and calling the block.
      create(@view, element_id, &block)
    end
  elsif element_id
    create(@view, element_id)
  end

  return @view
end

#view ⇒ Object

The parent view. This method builds the layout and returns the root view.



54
55
56
57
58
59
# File 'lib/motion-kit/layouts/view_layout.rb', line 54

def view
  if @layout != self
    return @layout.view
  end
  @view ||= build_view
end