Class: CGRect

Inherits:
Struct show all
Defined in:
lib/accessibility/bridge/mri.rb,
lib/accessibility/bridge/mri.rb,
lib/accessibility/bridge/common.rb,
lib/accessibility/extras/common.rb

Overview

Complete definition of a rectangle in a 2D coordinate system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin = CGPoint.new, size = CGSize.new) ⇒ CGRect

Returns a new instance of CGRect.

Parameters:

  • origin (CGPoint, #to_point) (defaults to: CGPoint.new)
  • size (CGSize, #to_size) (defaults to: CGSize.new)


72
73
74
# File 'lib/accessibility/bridge/mri.rb', line 72

def initialize origin = CGPoint.new, size = CGSize.new
  super(origin.to_point, size.to_size)
end

Instance Attribute Details

#originCGPoint, #to_point

The origin point

Returns:



67
68
69
# File 'lib/accessibility/bridge/mri.rb', line 67

def origin
  @origin
end

#sizeCGSize, #to_size

The size of the rectangle

Returns:



67
68
69
# File 'lib/accessibility/bridge/mri.rb', line 67

def size
  @size
end

Instance Method Details

#contains?(inner) ⇒ Boolean

Whether or not the receiver completely encloses the inner rect

On MacRuby this is equivalent to calling NSContainsRect().

Parameters:

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/accessibility/bridge/common.rb', line 47

def contains? inner
  ox = origin.x; oy = origin.y; ow = size.width; oh = size.height

  inner = inner.to_rect
  ix    = inner.origin.x;   iy = inner.origin.y
  iw    = inner.size.width; ih = inner.size.height

  if iw.zero? || ih.zero?
    false
  else
    (ox <= ix)               && (oy <= iy)               &&
    ((ox + ow) >= (ix + iw)) && ((oy + oh) >= (iy + ih))
  end
end

#flip!CGRect

Treats the rect as belonging to one co-ordinate system and then converts it to the other system

This is useful because accessibility API's expect to work with the flipped co-ordinate system (origin in top left), but AppKit prefers to use the cartesian co-ordinate system (origin in bottom left).

Returns:



12
13
14
15
16
17
18
# File 'lib/accessibility/extras/common.rb', line 12

def flip!
  frame         = ::NSScreen.screens.first.frame
  screen_height = frame.origin.y + frame.size.height
  self_max_y    = self.origin.y + self.size.height
  origin.y      = screen_height - self_max_y
  self
end

#inspectString

Return a nice string representation of the rectangle

Overrides Object#inspect to more closely mimic MacRuby Boxed#inspect.

Returns:



90
91
92
# File 'lib/accessibility/bridge/mri.rb', line 90

def inspect
  "#<CGRect origin=#{self.origin.inspect} size=#{self.size.inspect}>"
end

#to_pointCGPoint

Returns the center point for the rectangle as a CGPoint

Returns:



34
35
36
37
38
# File 'lib/accessibility/bridge/common.rb', line 34

def to_point
  o = origin
  s = size
  CGPoint.new((o.x + (s.width / 2)), (o.y + (s.height / 2)))
end

#to_rectCGRect

Returns the receiver, since the receiver is already a CGRect

Returns:



26
27
28
# File 'lib/accessibility/bridge/common.rb', line 26

def to_rect
  self
end