Sha256: e72f13c8f062fc6f6cdfa2a20c7b8bbbfedca724a18fb9b51242e3f6fdf3429d

Contents?: true

Size: 1.93 KB

Versions: 24

Compression:

Stored size: 1.93 KB

Contents

# @provides module:MotionKit::SizeCalculator
module MotionKit

  module SizeCalculator

    def calculate_size(view, amount, full_view)
      if amount.is_a?(Array) || amount.is_a?(Hash)
        calculate_size_from_multiple(view, amount, full_view)
      elsif amount == :full
        w = calculate(view, :width, '100%', full_view)
        h = calculate(view, :height, '100%', full_view)
        return CGSize.new(w, h)
      elsif amount == :auto
        return intrinsic_size(view)
      elsif amount.is_a?(Symbol)
        raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_size"
      else
        return amount
      end
    end

    def calculate_size_from_multiple(view, amount, full_view)
      w, h = resolve_width_and_height(view, amount)

      if w == :scale || h == :scale
        w, h = calculate_scaled_width_height(w, h, view, full_view)
      else
        w = calculate(view, :width, w, full_view)
        h = calculate(view, :height, h, full_view)
      end

      return CGSize.new(w, h)
    end

    def calculate_scaled_width_height(w, h, view, full_view)
      # scaling is handled a little differently, because it is dependent on
      # the other amount and the intrinsic size.
      if w == :scale && h == :scale
        raise "Either width or height can be :scale, but not both"
      elsif w == :scale
        h = calculate(view, :height, h, full_view)
        size = intrinsic_size(view)
        w = h * size.width / size.height
      elsif h == :scale
        w = calculate(view, :width, w, full_view)
        size = intrinsic_size(view)
        h = w * size.height / size.width
      end
      return w, h
    end

    def resolve_width_and_height(view, amount)
      if amount.is_a?(Hash)
        w = amount[:w] || amount[:width]  || view.frame.size.width
        h = amount[:h] || amount[:height] || view.frame.size.height
      else
        w = amount[0]
        h = amount[1]
      end
      return w, h
    end

  end

end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
motion-kit-1.1.1 lib/motion-kit/calculator/size_calculator.rb
motion-kit-1.1.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-1.0.3 lib/motion-kit/calculator/size_calculator.rb
motion-kit-1.0.2 lib/motion-kit/calculator/size_calculator.rb
motion-kit-1.0.1 lib/motion-kit/calculator/size_calculator.rb
motion-kit-1.0.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.18.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.17.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.16.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.15.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.14.2 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.14.1 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.14.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.13.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.12.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.11.2 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.11.1 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.11.0 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.10.11 lib/motion-kit/calculator/size_calculator.rb
motion-kit-0.10.10 lib/motion-kit/calculator/size_calculator.rb