Skip to content

Instantly share code, notes, and snippets.

@metacritical
Forked from maccman/widget.rb
Created December 8, 2011 22:54
Show Gist options
  • Save metacritical/1449084 to your computer and use it in GitHub Desktop.
Save metacritical/1449084 to your computer and use it in GitHub Desktop.
MacRuby Webkit Interface
framework "WebKit"
class Widget
class RPC
class << self
public :include
end
def initialize(widget)
@widget = widget
end
def call(method, *args)
return unless respond_to?(method)
send(method, *args)
end
def show
@widget.window.show
end
def setSize(width, height)
@widget.window.setContentSize(NSSize.new(width, height))
end
def setPosition(x, y)
@widget.window.setFrameOrigin(NSPoint.new(x, y))
end
def log(str)
puts str
end
def test
true
end
end
class CustomWindow < NSWindow
attr_reader :widget
def init(widget, frame)
@widget = widget
initWithContentRect(frame,
styleMask: NSBorderlessWindowMask,
backing: NSBackingStoreBuffered,
defer: true)
setBackgroundColor(NSColor.clearColor)
setLevel(NSNormalWindowLevel)
setAlphaValue(1.0)
setOpaque(false)
setHasShadow(false)
end
def <<(control)
contentView.addSubview control
end
def view
contentView
end
def view=(view)
setContentView(view)
end
def canBecomeKeyWindow
true
end
def canBecomeMainWindow
true
end
def show
display
makeKeyAndOrderFront(nil)
orderFrontRegardless
end
end
class CustomWebView < WebView
attr_reader :widget
def init(widget, frame)
@widget = widget
initWithFrame(frame)
setDrawsBackground(false)
setAutoresizingMask(NSViewHeightSizable|NSViewWidthSizable)
mainFrame.frameView.setAllowsScrolling(false)
setFrameLoadDelegate(FrameLoadDelegate.new(@widget))
setUIDelegate(UIDelegate.new(@widget))
@preferences = WebPreferences.standardPreferences
setPreferences(@preferences)
end
def file=(value)
self.url = "file://#{File.expand_path(value)}"
end
def url=(url)
url = url.kind_of?(String) ? NSURL.alloc.initWithString(url) : url
mainFrame.loadRequest(NSURLRequest.requestWithURL(url))
end
def evalJS(str)
windowScriptObject.evaluateWebScript(str)
end
def preferences=(values)
values.each {|key, value| @preferences.send(key, value) }
setPreferences(@preferences)
end
class FrameLoadDelegate
attr_reader :widget
def initialize(widget)
@widget = widget
end
def webView(webView, didClearWindowObject:windowScriptObject, forFrame:frame)
windowScriptObject.setValue(ScriptDelegate.new(widget), forKey: "widget")
end
end
class ScriptDelegate
def initialize(widget)
@rpc = RPC.new(widget)
end
def invokeDefaultMethodWithArguments(args)
@rpc.call(*args)
end
end
class UIDelegate
attr_reader :widget
def initialize(widget)
@widget = widget
end
def webView(webView, contextMenuItemsForElement: ctx, defaultMenuItems: default)
widget.developer? ? default : []
end
end
end
attr_reader :window, :webView, :options
def initialize(options = {})
@options = options
setup
file = options.delete(:file)
url = options.delete(:url)
self.file = file if file
self.url = url if url
self.webView.preferences = {
setDeveloperExtrasEnabled: !!options[:developer]
}
end
def file=(value)
@webView.file = value
end
def url=(value)
@webView.url = value
end
def setup
windowFrame = options.delete(:windowFrame) || [0, 0, 500, 500]
webViewFrame = options.delete(:webViewFrame) || windowFrame
@window = CustomWindow.alloc.init(self, windowFrame)
@webView = CustomWebView.alloc.init(self, webViewFrame)
@window.view = @webView
end
def developer?
options[:developer]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment