Sha256: c20acdcbf8c3ac8892ac23823f9fa30927ca205b3dec29f39a7b5d6ace834247

Contents?: true

Size: 1.81 KB

Versions: 7

Compression:

Stored size: 1.81 KB

Contents

# @requires MotionKit::Calculator
# @requires module:MotionKit::OriginCalculator
# @requires module:MotionKit::SizeCalculator
# @requires module:MotionKit::FrameCalculator
# @provides MotionKit::ViewCalculator
module MotionKit
  class ViewCalculator
    extend MotionKit::OriginCalculator
    extend MotionKit::SizeCalculator
    extend MotionKit::FrameCalculator

    def self.calculate(view, dimension, amount, full_view=nil)
      if amount.is_a? Proc
        return view.instance_exec(&amount)
      elsif dimension == :origin || dimension == :center
        return calculate_origin(view, dimension, amount, nil, full_view)
      elsif dimension == :size
        return calculate_size(view, amount, full_view)
      elsif dimension == :frame
        return calculate_frame(view, amount, full_view)
      elsif amount == :full
        return calculate(view, dimension, '100%', full_view)
      elsif amount == :auto
        return intrinsic_size(view).send(dimension) # :left or :right
      elsif amount.is_a?(String) && amount.include?('%')
        full_view ||= view.superview || begin
          raise NoSuperviewError.new("Cannot calculate #{amount}% of #{dimension.inspect} because view #{view} has no superview.")
        end
        calc = Calculator.scan(amount)

        raise "Unknown dimension #{dimension}" unless [:width, :height].include?(dimension)
        return (full_view.frame.size.send(dimension) * calc.factor + calc.constant).round
      else
        return amount
      end
    end

    def self.intrinsic_size(view)
      size_that_fits = view.intrinsicContentSize
      if size_that_fits.width == MotionKit.no_intrinsic_metric
        size_that_fits.width = 0
      end
      if size_that_fits.height == MotionKit.no_intrinsic_metric
        size_that_fits.height = 0
      end
      return size_that_fits
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
motion-kit-0.14.1 lib/motion-kit-cocoa/calculator/view_calculator.rb
motion-kit-0.14.0 lib/motion-kit-cocoa/calculator/view_calculator.rb
motion-kit-0.13.0 lib/motion-kit-cocoa/calculator/view_calculator.rb
motion-kit-0.12.0 lib/motion-kit/calculator/view_calculator.rb
motion-kit-0.11.2 lib/motion-kit/calculator/view_calculator.rb
motion-kit-0.11.1 lib/motion-kit/calculator/view_calculator.rb
motion-kit-0.11.0 lib/motion-kit/calculator/view_calculator.rb