Class: MotionKit::TreeLayout

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

Overview

A sensible parent class for any Tree-like layout class. Platform agnostic. Any platform-specific tasks are offloaded to child elements (add_child, remove_child). You could use a TreeLayout 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, #context, #deferred, #deferred_blocks, delegate_method_fix, #initialize, #ipad?, #iphone35?, #iphone4?, #iphone?, method_added, #method_missing, overridden_methods, override_start, override_stop, overrides, #retina?, #set_layout, #target, #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


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

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

Instantiates a view via create and adds the view to the current target. If no view exists on the stack, a default root view can be created if that has been enabled. The block is run in the context of the new view.



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

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)

  if block
    context(element, &block)
  end

  element
end

#all(element_id, in: root) ⇒ Object

Same as all, but with the root view specified.



214
215
216
217
218
219
# File 'lib/motion-kit/layouts/tree_layout.rb', line 214

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



119
120
121
122
123
124
125
126
127
# File 'lib/motion-kit/layouts/tree_layout.rb', line 119

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.



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

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



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/motion-kit/layouts/tree_layout.rb', line 257

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



190
# File 'lib/motion-kit/layouts/tree_layout.rb', line 190

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

#get(element_id, in: root) ⇒ Object

Same as get, but with the root view specified.



184
185
186
187
188
189
# File 'lib/motion-kit/layouts/tree_layout.rb', line 184

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)


151
152
153
154
155
156
157
158
# File 'lib/motion-kit/layouts/tree_layout.rb', line 151

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.



201
202
203
204
205
206
# File 'lib/motion-kit/layouts/tree_layout.rb', line 201

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.



227
228
229
230
231
232
# File 'lib/motion-kit/layouts/tree_layout.rb', line 227

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)


142
143
144
145
146
147
148
149
# File 'lib/motion-kit/layouts/tree_layout.rb', line 142

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/motion-kit/layouts/tree_layout.rb', line 130

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.



241
242
243
244
245
246
# File 'lib/motion-kit/layouts/tree_layout.rb', line 241

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.



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

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.



52
53
54
55
56
57
# File 'lib/motion-kit/layouts/tree_layout.rb', line 52

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