Class: MacOS::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/macos/display.rb

Overview

Simulates a macOS display.

Defined Under Namespace

Classes: Size

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: 0) ⇒ Display

Returns a new instance of Display.

Parameters:

  • id (Integer) (defaults to: 0)

    d [Integer



20
21
22
# File 'lib/macos/display.rb', line 20

def initialize(id: 0)
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/macos/display.rb', line 12

def id
  @id
end

Class Method Details

.mainObject



14
15
16
17
# File 'lib/macos/display.rb', line 14

def self.main
  id = Library::CoreGraphics.CGMainDisplayID
  new(id:)
end

Instance Method Details

#boundsSize

Returns:



25
26
27
28
29
30
31
32
# File 'lib/macos/display.rb', line 25

def bounds
  bounds = Library::CoreGraphics.CGDisplayBounds(@id)

  width = Integer(bounds[:size][:width])
  height = Integer(bounds[:size][:height])

  Size.new(width, height)
end

#highObject



40
41
42
# File 'lib/macos/display.rb', line 40

def high
  Library::CoreGraphics.CGDisplayPixelsHigh(@id)
end

#screenshot {|file| ... } ⇒ Object

Yields:

  • (file)

Yield Parameters:

  • file (File)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/macos/display.rb', line 46

def screenshot(&)
  tempfile = Tempfile.new(["screenshot", ".png"])

  image = Library::CoreGraphics.CGDisplayCreateImage(@id)

  encoding = Library::CoreFoundation::ENCODING_UTF8
  path = Library::CoreFoundation.CFStringCreateWithCString(nil, tempfile.path, encoding)
  url = Library::CoreFoundation.CFURLCreateWithFileSystemPath(nil, path, 0, false)
  uti = Library::CoreFoundation.CFStringCreateWithCString(nil, "public.png", encoding)

  dest = Library::ImageIO.CGImageDestinationCreateWithURL(url, uti, 1, nil)
  Library::ImageIO.CGImageDestinationAddImage(dest, image, nil)
  Library::ImageIO.CGImageDestinationFinalize(dest)

  Library::CoreGraphics.CFRelease(image)
  Library::CoreFoundation.CFRelease(path)
  Library::CoreFoundation.CFRelease(url)
  Library::CoreFoundation.CFRelease(uti)
  Library::CoreFoundation.CFRelease(dest)

  File.open(tempfile.path, "rb", &)
ensure
  tempfile.close
  tempfile.unlink
end

#wideObject



35
36
37
# File 'lib/macos/display.rb', line 35

def wide
  Library::CoreGraphics.CGDisplayPixelsWide(@id)
end