Class: MotionKit::BaseLayout

Inherits:
Object
  • Object
show all
Extended by:
BaseLayoutClassMethods
Defined in:
lib/motion-kit/helpers/base_layout.rb,
lib/motion-kit-ios/helpers/layout_device.rb,
lib/motion-kit-tvos/helpers/layout_device.rb,
lib/motion-kit-cocoa/helpers/sugarcube_compat.rb,
lib/motion-kit-ios/helpers/layout_orientation.rb,
lib/motion-kit-tvos/helpers/layout_orientation.rb,
lib/motion-kit-cocoa/helpers/accessibility_compat.rb

Overview

Abstract base class, responsible for “registration” of layout classes with the class ‘targets` method.

Very few methods are defined on BaseLayout, and any unknown methods are delegated to the ‘apply’ method, which accepts a method name, arguments, and an optional block to set the new context.

The TreeLayout subclass defines methods that are appropriate for adding and removing views to a view hierarchy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseLayoutClassMethods

layout_for, memoize, target_klasses, targets

Constructor Details

#initialize(args = {}) ⇒ BaseLayout

Returns a new instance of BaseLayout.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/motion-kit/helpers/base_layout.rb', line 19

def initialize(args={})
  # @layout is the object we look in for style methods
  @layout = nil
  # the Layout object that implements custom style methods. Leave this as nil
  # in the initializer.
  @layout_delegate = nil
  @layout_state = :initial
  # You can set a root view by using .new(root: some_view)
  # Explicit roots will not have a strong reference from
  # MotionKit, so retain one yourself from your controller
  # or other view to prevent deallocation.
  @preset_root = args[:root]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Methods that style the view start out as missing methods. This just calls ‘apply’, which searches for the method in the delegate (‘@layout_delegate`) or using inspection (`respond_to?(:setFoo)`).

Examples:

def 
  frame [[0, 0], [100, 20]]
  title 'Login'
end


176
177
178
# File 'lib/motion-kit/helpers/base_layout.rb', line 176

def method_missing(method_name, *args, &block)
  self.apply(method_name, *args, &block)
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/motion-kit/helpers/base_layout.rb', line 17

def parent
  @parent
end

Class Method Details

.delegate_method_fix(method_name) ⇒ Object

Prevents infinite loops when methods that are defined on Object/Kernel are not properly delegated to the target.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/motion-kit/helpers/base_layout.rb', line 319

def delegate_method_fix(method_name)
  running_name = "motion_kit_is_calling_#{method_name}"
  define_method(method_name) do |*args, &block|
    if target.motion_kit_meta[running_name]
      if block
        apply_with_context(method_name, *args, &block)
      else
        apply_with_target(method_name, *args)
      end
    else
      target.motion_kit_meta[running_name] = true
      retval = apply(method_name, *args, &block)
      target.motion_kit_meta[running_name] = false
      return retval
    end
  end
end

Instance Method Details

#add_deferred_block(context, &block) ⇒ Object

Only intended for private use



137
138
139
140
141
142
143
144
145
146
# File 'lib/motion-kit/helpers/base_layout.rb', line 137

def add_deferred_block(context, &block)
  context ||= @context
  if context.nil? && @assign_root
    context ||= self.create_default_root_context
  end
  raise InvalidDeferredError.new('deferred must be run inside of a context') unless context
  raise ArgumentError.new('Block required') unless block

  self.deferred_blocks << [context, block]
end

#apply(method_name, *args, &block) ⇒ Object

Tries to call the setter (‘foo ’value’‘ => `view.setFoo(’value’)‘), or assignment method (`foo ’value’‘ => `view.foo = ’value’‘), or if a block is given, then the object returned by ’method_name’ is assigned as the new context, and the block is executed.

You can call this method directly, but usually it is called via method_missing.

Raises:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/motion-kit/helpers/base_layout.rb', line 226

def apply(method_name, *args, &block)
  method_name = method_name.to_s
  raise ApplyError.new("Cannot apply #{method_name.inspect} to instance of #{target.class.name}") if method_name.length == 0

  # if there is no target, than we should raise the NoMethodError; someone
  # called a method on the layout directly.
  begin
    target = self.target
  rescue NoContextError => e
    raise NoMethodError.new("undefined method `#{method_name}' for #{self}:#{self.class}", method_name)
  end

  objc_method_name, objc_method_args = objc_version(method_name, args)
  ruby_method_name, ruby_method_args = ruby_version(method_name, args)

  @layout_delegate ||= Layout.layout_for(target.class)
  if @layout_delegate
    @layout_delegate.set_parent_layout(parent_layout)
    if objc_method_name && @layout_delegate.respond_to?(objc_method_name)
      return @layout_delegate.send(objc_method_name, *objc_method_args, &block)
    elsif @layout_delegate.respond_to?(ruby_method_name)
      return @layout_delegate.send(ruby_method_name, *ruby_method_args, &block)
    end
  end

  if block
    apply_with_context(method_name, *args, &block)
  else
    apply_with_target(method_name, *args, &block)
  end
end

#apply_with_context(method_name, *args, &block) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/motion-kit/helpers/base_layout.rb', line 258

def apply_with_context(method_name, *args, &block)
  objc_method_name, objc_method_args = objc_version(method_name, args)
  ruby_method_name, ruby_method_args = ruby_version(method_name, args)

  if objc_method_name && target.respond_to?(objc_method_name)
    new_context = target.send(objc_method_name, *objc_method_args)
    self.context(new_context, &block)
  elsif target.respond_to?(ruby_method_name)
    new_context = target.send(ruby_method_name, *ruby_method_args)
    self.context(new_context, &block)
  elsif ruby_method_name.include?('_')
    objc_name = MotionKit.objective_c_method_name(ruby_method_name)
    self.apply(objc_name, *args, &block)
  else
    raise ApplyError.new("Cannot apply #{ruby_method_name.inspect} to instance of #{target.class.name}")
  end
end

#apply_with_target(method_name, *args, &block) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/motion-kit/helpers/base_layout.rb', line 276

def apply_with_target(method_name, *args, &block)
  objc_method_name, objc_method_args = objc_version(method_name, args)
  ruby_method_name, ruby_method_args = ruby_version(method_name, args)

  objc_setter = objc_method_name && MotionKit.setter(objc_method_name)
  setter = MotionKit.setter(ruby_method_name)
  assign = "#{ruby_method_name}="

  # The order is important here.
  # - unchanged method name if no args are passed (e.g. `layer`)
  # - setter (`setLayer(val)`)
  # - assign (`layer=val`)
  # - unchanged method name *again*, because many Ruby classes provide a
  #   combined getter/setter (`layer(val)`)
  # - lastly, try again after converting to camelCase
  if objc_method_name && target.respond_to?(objc_method_name)
    target.send(objc_method_name, *objc_method_args, &block)
  elsif args.empty? && target.respond_to?(ruby_method_name)
    target.send(ruby_method_name, *ruby_method_args, &block)
  elsif objc_setter && target.respond_to?(objc_setter)
    target.send(objc_setter, *objc_method_args, &block)
  elsif target.respond_to?(setter)
    target.send(setter, *args, &block)
  elsif target.respond_to?(assign)
    target.send(assign, *args, &block)
  elsif target.respond_to?(ruby_method_name)
    target.send(ruby_method_name, *ruby_method_args, &block)
  # UIAppearance classes are a whole OTHER thing; they never return 'true'
  elsif target.is_a?(MotionKit.appearance_class)
    target.send(setter, *args, &block)
  # Finally, try again with camel case if there's an underscore.
  elsif method_name.include?('_')
    objc_name = MotionKit.objective_c_method_name(method_name)
    self.apply(objc_name, *args, &block)
  else
    target.send(setter, *args, &block)
  end
end

#context(new_target, &block) ⇒ Object

Runs a block of code with a new object as the ‘context’. Methods from the Layout classes are applied to this target object, and missing methods are delegated to a new Layout instance that is created based on the new context.

This method is part of the public API, you can pass in any object to have it become the ‘context’.

Example:

def table_view_style
  content = target.contentView
  if content
    context(content) do
      background_color UIColor.clearColor
    end
  end

  # usually you use 'context' automatically via method_missing, by
  # passing a block to a method that returns an object. That object becomes
  # the new context.
  layer do
    # target is now a CALayer, and methods are delegated to CALayerHelpers
    corner_radius 5
  end
end


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
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/motion-kit/helpers/base_layout.rb', line 91

def context(new_target, &block)
  return new_target unless block
  # this little line is incredibly important; the context is only set on
  # the top-level Layout object.

  # mp "MOTIONKIT CONTEXT is #{new_target} meta: #{new_target.motion_kit_meta}"
  return parent_layout.context(new_target, &block) unless is_parent_layout?

  if new_target.is_a?(Symbol)
    new_target = self.get_view(new_target)
  end

  context_was, parent_was, delegate_was = @context, @parent, @layout_delegate

  prev_should_run = @should_run_deferred
  if @should_run_deferred.nil?
    @should_run_deferred = true
  else
    @should_run_deferred = false
  end
  @parent = MK::Parent.new(context_was)
  @context = new_target
  @context.motion_kit_meta[:delegate] ||= Layout.layout_for(@context.class)
  @layout_delegate = @context.motion_kit_meta[:delegate]
  if @layout_delegate
    @layout_delegate.set_parent_layout(parent_layout)
  end
  yield
  @layout_delegate, @context, @parent = delegate_was, context_was, parent_was
  if @should_run_deferred
    run_deferred(new_target)
  end
  @should_run_deferred = prev_should_run

  new_target
end

#deferred(context = nil, &block) ⇒ Object

Blocks passed to ‘deferred` are run at the end of a “session”, usually after a call to Layout#layout.



130
131
132
133
134
# File 'lib/motion-kit/helpers/base_layout.rb', line 130

def deferred(context=nil, &block)
  context ||= @context
  parent_layout.add_deferred_block(context, &block)
  return self
end

#deferred_blocksObject

Only intended for private use



149
150
151
# File 'lib/motion-kit/helpers/base_layout.rb', line 149

def deferred_blocks
  @deferred_blocks ||= []
end

#has_context?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# File 'lib/motion-kit/helpers/base_layout.rb', line 45

def has_context?
  if is_parent_layout?
    !!@context
  else
    parent_layout.has_context?
  end
end

#ipad?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 25

def ipad?
  UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
end

#iphone35?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 21

def iphone35?
  iphone? && UIScreen.mainScreen.bounds.size.width == 320 && UIScreen.mainScreen.bounds.size.height == 480
end

#iphone47?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 13

def iphone47?
  iphone? && UIScreen.mainScreen.bounds.size.width == 375 && UIScreen.mainScreen.bounds.size.height == 667
end

#iphone4?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 17

def iphone4?
  iphone? && UIScreen.mainScreen.bounds.size.width == 320 && UIScreen.mainScreen.bounds.size.height == 568
end

#iphone55?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 9

def iphone55?
  iphone? && UIScreen.mainScreen.bounds.size.width == 414 && UIScreen.mainScreen.bounds.size.height == 736
end

#iphone?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 5

def iphone?
  UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone
end

#is_parent_layout?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/motion-kit/helpers/base_layout.rb', line 41

def is_parent_layout?
  @layout.nil? || @layout == self
end

#objc_version(method_name, args) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/motion-kit/helpers/base_layout.rb', line 180

def objc_version(method_name, args)
  if method_name.count(':') > 1
    objc_method_name = method_name
  elsif args.length == 2 && args[1].is_a?(Hash) && !args[1].empty?
    objc_method_name = "#{method_name}:#{args[1].keys.join(':')}:"
  else
    return nil, nil
  end

  if args[1].is_a?(Hash)
    objc_method_args = [args[0]].concat args[1].values
  else
    objc_method_args = args
  end

  return objc_method_name, objc_method_args
end

#orientation?(value) ⇒ Boolean

This method is used to check the orientation. On an ipad, this method returns true for :portrait if the device is “upside down”, but it returns false in the same situation on an iphone.

Returns:

  • (Boolean)


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
33
# File 'lib/motion-kit-ios/helpers/layout_orientation.rb', line 8

def orientation?(value)
  if target.is_a?(UIView) && target.nextResponder && target.nextResponder.is_a?(UIViewController)
    interface_orientation = target.nextResponder.interfaceOrientation
  else
    interface_orientation = UIApplication.sharedApplication.statusBarOrientation
  end

  return case value
  when :portrait
    if ipad?
      interface_orientation == UIInterfaceOrientationPortrait || interface_orientation == UIInterfaceOrientationPortraitUpsideDown
    else
      interface_orientation == UIInterfaceOrientationPortrait
    end
  when :upright, UIInterfaceOrientationPortrait
    interface_orientation == UIInterfaceOrientationPortrait
  when :landscape
    interface_orientation == UIInterfaceOrientationLandscapeLeft || interface_orientation == UIInterfaceOrientationLandscapeRight
  when :landscape_left, UIInterfaceOrientationLandscapeLeft
    interface_orientation == UIInterfaceOrientationLandscapeLeft
  when :landscape_right, UIInterfaceOrientationLandscapeRight
    interface_orientation == UIInterfaceOrientationLandscapeRight
  when :upside_down, UIInterfaceOrientationPortraitUpsideDown
    interface_orientation == UIInterfaceOrientationPortraitUpsideDown
  end
end

#orientation_block(orientation, block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/motion-kit-ios/helpers/layout_orientation.rb', line 44

def orientation_block(orientation, block)
  block = block.weak!
  always do
    if orientation?(orientation)
      block.call
    end
  end
end

#parent_layoutObject



37
38
39
# File 'lib/motion-kit/helpers/base_layout.rb', line 37

def parent_layout
  @layout || self
end

#retina?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/motion-kit-ios/helpers/layout_device.rb', line 29

def retina?
  UIScreen.mainScreen.respond_to?(:scale) && UIScreen.mainScreen.scale == 2
end

#ruby_version(method_name, args) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/motion-kit/helpers/base_layout.rb', line 198

def ruby_version(method_name, args)
  if method_name.count(':') > 1
    parts = method_name.split(':')
    ruby_method_name = parts.first
    if args[1].is_a?(Hash)
      ruby_method_args = args
    else
      keys = parts[1..-1].map(&:to_sym)
      ruby_method_args = [args[0]] << Hash[keys.zip args[1..-1]]
    end
  elsif method_name.count(':') == 1
    ruby_method_name = method_name.split(':').first
    ruby_method_args = args
  else
    ruby_method_name = method_name
    ruby_method_args = args
  end

  return ruby_method_name, ruby_method_args
end

#run_deferred(top_level_context) ⇒ Object

Only intended for private use



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/motion-kit/helpers/base_layout.rb', line 154

def run_deferred(top_level_context)
  deferred_blocks = self.deferred_blocks
  @deferred_blocks = nil

  deferred_blocks.each do |target, block|
    context(target, &block)
  end

  if @deferred_blocks
    run_deferred(top_level_context)
  end
end

#set_parent_layout(layout) ⇒ Object



33
34
35
# File 'lib/motion-kit/helpers/base_layout.rb', line 33

def set_parent_layout(layout)
  @layout = WeakRef.new(layout)
end

#targetObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/motion-kit/helpers/base_layout.rb', line 53

def target
  if is_parent_layout?
    # only the "root layout" instance is allowed to change the context.
    # if there isn't a context set, try and create a root instance; this
    # will fail if we're not in a state that allows the root to be created
    @context ||= create_default_root_context
  else
    # child layouts get the context from the root layout
    parent_layout.target
  end
end

#tv?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/motion-kit-tvos/helpers/layout_device.rb', line 5

def tv?
  UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomTV
end

#vObject



64
# File 'lib/motion-kit/helpers/base_layout.rb', line 64

def v ; target ; end