Class: MacOS::Display
- Inherits:
-
Object
- Object
- MacOS::Display
- Defined in:
- lib/macos/display.rb
Overview
Simulates a macOS display.
Defined Under Namespace
Classes: Size
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
- #bounds ⇒ Size
- #high ⇒ Object
-
#initialize(id: 0) ⇒ Display
constructor
A new instance of Display.
- #screenshot {|file| ... } ⇒ Object
- #wide ⇒ Object
Constructor Details
#initialize(id: 0) ⇒ Display
Returns a new instance of Display.
20 21 22 |
# File 'lib/macos/display.rb', line 20 def initialize(id: 0) @id = id end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/macos/display.rb', line 12 def id @id end |
Class Method Details
.main ⇒ Object
14 15 16 17 |
# File 'lib/macos/display.rb', line 14 def self.main id = Library::CoreGraphics.CGMainDisplayID new(id:) end |
Instance Method Details
#bounds ⇒ Size
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 |
#high ⇒ Object
40 41 42 |
# File 'lib/macos/display.rb', line 40 def high Library::CoreGraphics.CGDisplayPixelsHigh(@id) end |
#screenshot {|file| ... } ⇒ Object
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 |
#wide ⇒ Object
35 36 37 |
# File 'lib/macos/display.rb', line 35 def wide Library::CoreGraphics.CGDisplayPixelsWide(@id) end |