Sha256: e5c065590d18532867f4fc874cbe40255739ca688ca2ef669c9b39554b9e6aa4

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module Teacup
  class Stylesheet

    def constrain(target, attribute=nil)
      if attribute.nil?
        attribute = target
        target = :self
      end
      Teacup::Constraint.new(target, attribute)
    end

    ##|
    def constrain_xy(x, y)
      [
        Teacup::Constraint.new(:self, :left).equals(:superview, :left).plus(x),
        Teacup::Constraint.new(:self, :top).equals(:superview, :top).plus(y),
      ]
    end

    def constrain_left(x)
      Teacup::Constraint.new(:self, :left).equals(:superview, :left).plus(x)
    end

    def constrain_right(x)
      Teacup::Constraint.new(:self, :right).equals(:superview, :right).plus(x)
    end

    def constrain_top(y)
      Teacup::Constraint.new(:self, :top).equals(:superview, :top).plus(y)
    end

    def constrain_bottom(y)
      Teacup::Constraint.new(:self, :bottom).equals(:superview, :bottom).plus(y)
    end

    def constrain_width(width)
      Teacup::Constraint.new(:self, :width).equals(width)
    end

    def constrain_height(height)
      Teacup::Constraint.new(:self, :height).equals(height)
    end

    def constrain_size(width, height)
      [
        Teacup::Constraint.new(:self, :right).equals(:self, :left).plus(width),
        Teacup::Constraint.new(:self, :bottom).equals(:self, :top).plus(height),
      ]
    end

    ##|
    def constrain_below(relative_to, margin=0)
      margin = 8 if margin == :auto
      Teacup::Constraint.new(:self, :top).equals(relative_to, :bottom).plus(margin)
    end

    def constrain_above(relative_to, margin=0)
      margin = 8 if margin == :auto
      Teacup::Constraint.new(:self, :bottom).equals(relative_to, :top).plus(margin)
    end

    def constrain_to_right(relative_to, margin=0)
      margin = 20 if margin == :auto
      Teacup::Constraint.new(:self, :left).equals(relative_to, :right).plus(margin)
    end

    def constrain_to_left(relative_to, margin=0)
      margin = 20 if margin == :auto
      Teacup::Constraint.new(:self, :right).equals(relative_to, :left).minus(margin)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
teacup-1.0.3 lib/teacup/stylesheet_extensions/constraints.rb
teacup-1.0.2 lib/teacup/stylesheet_extensions/constraints.rb