Sha256: c054af2e89eef14e5c2be32a71f330a27de48f758d975904c4687feb3356f258

Contents?: true

Size: 1.96 KB

Versions: 4

Compression:

Stored size: 1.96 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, :width).equals(width),
        Teacup::Constraint.new(:self, :height).equals(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).minus(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

4 entries across 4 versions & 1 rubygems

Version Path
teacup-1.2.4 lib/teacup/stylesheet_extensions/constraints.rb
teacup-1.2.3 lib/teacup/stylesheet_extensions/constraints.rb
teacup-1.2.2 lib/teacup/stylesheet_extensions/constraints.rb
teacup-1.0.4 lib/teacup/stylesheet_extensions/constraints.rb