Skip to content

Instantly share code, notes, and snippets.

@gerrit
Forked from matthewtodd/gist:728014
Created October 13, 2011 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerrit/1283690 to your computer and use it in GitHub Desktop.
Save gerrit/1283690 to your computer and use it in GitHub Desktop.
Resize an OSX display with RubyCocoa.
require 'osx/cocoa'
class Screen
class << self
def resize(width, height, &block)
new.resize(width, height, &block)
end
end
def initialize(screen = OSX::CGMainDisplayID())
@screen = screen
@mode = OSX::CGDisplayCopyDisplayMode(@screen)
end
def resize(width, height, &block)
configure(best_mode(width, height))
block.call
ensure
configure(@mode)
end
private
def best_mode(width, height)
# TODO Try to figure out color depth from CGDisplayModeCopyPixelEncoding?
OSX::CGDisplayCopyAllDisplayModes(@screen, nil).detect do |mode|
OSX::CGDisplayModeGetWidth(mode) == width &&
OSX::CGDisplayModeGetHeight(mode) == height &&
OSX::CGDisplayModeIsUsableForDesktopGUI(mode)
end
end
def configure(mode)
result, config = OSX::CGBeginDisplayConfiguration()
if result == OSX::KCGErrorSuccess
OSX::CGConfigureDisplayWithDisplayMode(config, @screen, mode, nil)
OSX::CGCompleteDisplayConfiguration(config, OSX::KCGConfigureForSession)
else
raise "I have no clue what just happened: #{result}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment