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_missing, #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.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/motion-kit/layouts/tree_layout.rb', line 187

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

  element = initialize_element(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.



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

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

#build ⇒ Object

Builds the layout and then returns self for chaining.



60
61
62
63
# File 'lib/motion-kit/layouts/tree_layout.rb', line 60

def build
  view
  self
end

#built? ⇒ Boolean Also known as: build?

Checks if the layout has been built yet or not.

Returns:

  • (Boolean)


66
67
68
# File 'lib/motion-kit/layouts/tree_layout.rb', line 66

def built?
  !@view.nil?
end

#call_style_method(element, element_id) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/motion-kit/layouts/tree_layout.rb', line 135

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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/motion-kit/layouts/tree_layout.rb', line 118

def create(element, element_id=nil, &block)
  element = initialize_element(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



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/motion-kit/layouts/tree_layout.rb', line 281

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(preset_root || default_root)
  else
    raise NoContextError.new("No top level view specified (missing outer 'create' method?)")
  end
end

#first(element_id, in: root) ⇒ Object



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

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

#get(element_id, in: root) ⇒ Object

Same as get, but with the root view specified.



208
209
210
211
212
213
# File 'lib/motion-kit/layouts/tree_layout.rb', line 208

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)


175
176
177
178
179
180
181
182
# File 'lib/motion-kit/layouts/tree_layout.rb', line 175

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

  if initial?
    yield
  end
  return self
end

#initial? ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/motion-kit/layouts/tree_layout.rb', line 171

def initial?
  @layout_state == :initial
end

#last(element_id, in: root) ⇒ Object

Same as last, but with the root view specified.



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

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.



251
252
253
254
255
256
# File 'lib/motion-kit/layouts/tree_layout.rb', line 251

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)


162
163
164
165
166
167
168
169
# File 'lib/motion-kit/layouts/tree_layout.rb', line 162

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

  if reapply?
    yield
  end
  return self
end

#reapply!(root = nil) ⇒ Object

Calls the style method of all objects in the view hierarchy



146
147
148
149
150
151
152
153
154
155
# File 'lib/motion-kit/layouts/tree_layout.rb', line 146

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

#reapply? ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/motion-kit/layouts/tree_layout.rb', line 157

def reapply?
  @layout_state == :reapply
end

#remove(element_id, from: root) ⇒ Object

Same as remove, but with the root view specified.



265
266
267
268
269
270
# File 'lib/motion-kit/layouts/tree_layout.rb', line 265

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/motion-kit/layouts/tree_layout.rb', line 79

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 = preset_root || default_root
  elsif preset_root && preset_root != element
    # You're trying to make two roots, one at initialization
    # and one in your layout itself.
    raise ContextConflictError.new("Already created the root view")
  end

  @view = initialize_element(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 main 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