Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Resize an OSX display with RubyCocoa.
require 'osx/cocoa'
include OSX
class Screen
class << self
def resize(width, height, &block)
new.resize(width, height, &block)
end
end
def initialize(screen = CGMainDisplayID())
@screen = screen
@mode = 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?
CGDisplayCopyAllDisplayModes(@screen, nil).detect do |mode|
CGDisplayModeGetWidth(mode) == width &&
CGDisplayModeGetHeight(mode) == height &&
CGDisplayModeIsUsableForDesktopGUI(mode)
end
end
def configure(mode)
result, config = CGBeginDisplayConfiguration()
if result == KCGErrorSuccess
CGConfigureDisplayWithDisplayMode(config, @screen, mode, nil)
CGCompleteDisplayConfiguration(config, 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
You can’t perform that action at this time.