Sha256: 9bccbbad87fd1ea00dd6e33a0832abf660b9b80d6089ffea91310fe6e6471280

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

module MotionKit
  module_function

  def siblings(view)
    case view
    when CALayer
      view.superlayer ? view.superlayer.sublayers : []
    else
      view.superview ? view.superview.subviews : []
    end
  end

  def children(view)
    case view
    when CALayer
      view.sublayers
    else
      view.subviews
    end
  end

  def parent(view)
    case view
    when CALayer
      view.superlayer
    else
      view.superview
    end
  end

  # - check view
  # - check subviews (unless 'skip' is provided)
  # - check siblings (skipping 'view')
  # - go up to parent and repeat, skipping children
  def nearest(view, skip=nil, &test)
    return nil if view.nil?

    if test.call(view)
      return view
    end

    children = MotionKit.children(view)
    siblings = MotionKit.siblings(view)
    parent = MotionKit.parent(view)

    found = nil

    # only check the children starting at the "root", e.g. nearest hasn't been
    # called recursively.
    if !skip || skip == parent
      children.each do |child|
        found = nearest(child, &test)
        break if found
      end
      return found if found
    end

    # siblings are closer than parents
    # passing 'parent' means only check self and children
    unless skip == parent
      siblings.each do |sibling|
        found = (sibling != view && nearest(sibling, parent, &test))
        break if found
      end
    end

    if found
      found
    elsif skip != parent
      nearest(parent, true, &test)
    end
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
motion-kit-1.1.1 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-1.1.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-1.0.3 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-1.0.2 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-1.0.1 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-1.0.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.18.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.17.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.16.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.15.0 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.14.2 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.14.1 lib/motion-kit-cocoa/cocoa_util.rb
motion-kit-0.14.0 lib/motion-kit-cocoa/cocoa_util.rb