Sha256: 2389c166a131e7f96e6e05dd2f0ce54782a070a8d78f0fd290cba534fa2ae7f4

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# @provides MotionKit::ConstraintsTarget
module MotionKit
  class ConstraintsTarget
    attr :view

    def initialize(view)
      @view = view
      @constraints = []
    end

    def add_constraints(constraints)
      @constraints.concat(constraints)
    end

    def ==(value)
      if value.is_a?(ConstraintsTarget)
        super
      else
        @view == value
      end
    end

    def apply_all_constraints(layout, target)
      @constraints.map do |mk_constraint|
        mk_constraint.resolve_all(layout, target).map do |constraint|
          base_view_class = MotionKit.base_view_class

          if constraint.firstItem.is_a?(base_view_class) && constraint.secondItem.is_a?(base_view_class)
            common_ancestor = nil

            ancestors = [constraint.firstItem]
            parent_view = constraint.firstItem
            while parent_view = parent_view.superview
              ancestors << parent_view
            end

            current_view = constraint.secondItem
            while current_view
              if ancestors.include? current_view
                common_ancestor = current_view
                break
              end
              current_view = current_view.superview
            end

            unless common_ancestor
              raise NoCommonAncestorError.new("No common ancestors between #{constraint.firstItem} and #{constraint.secondItem}")
            end
          else
            common_ancestor = constraint.firstItem
          end

          common_ancestor.addConstraint(constraint)
          constraint
        end
      end.flatten
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
motion-kit-0.17.0 lib/motion-kit-cocoa/constraints/constraints_target.rb
motion-kit-0.16.0 lib/motion-kit-cocoa/constraints/constraints_target.rb
motion-kit-0.15.0 lib/motion-kit-cocoa/constraints/constraints_target.rb
motion-kit-0.14.2 lib/motion-kit-cocoa/constraints/constraints_target.rb
motion-kit-0.14.1 lib/motion-kit-cocoa/constraints/constraints_target.rb
motion-kit-0.14.0 lib/motion-kit-cocoa/constraints/constraints_target.rb