Last active
September 26, 2020 15:44
-
-
Save KevinGutowski/80f81a094420d0189ba54b89e1842890 to your computer and use it in GitHub Desktop.
Replicating Sketch's Preview window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let previewWindow = NSApplication.sharedApplication().windows().find(window => window.title() == 'Preview') | |
let webview = previewWindow.contentView().subviews()[1] // need to launch sketch preview in order to find the proper webview and to start up the sketch server | |
let threadDictionary = NSThread.mainThread().threadDictionary() | |
let panelID = "com.testPanel" | |
if (threadDictionary[panelID]) { | |
let panel = threadDictionary[panelID] | |
panel.close() | |
threadDictionary.removeObjectForKey(panelID) | |
} | |
var panelWidth = 312 | |
var panelHeight = 210 | |
let panel = NSPanel.alloc().init() | |
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true) | |
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask) | |
panel.title = "Testing" | |
panel.center() | |
panel.makeKeyAndOrderFront(null) | |
panel.setLevel(NSFloatingWindowLevel) | |
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true) | |
panel.standardWindowButton(NSWindowZoomButton).setHidden(true) | |
threadDictionary[panelID] = panel | |
//Setup | |
let frame = NSMakeRect(0,0,panelWidth,panelHeight) | |
let webviewConfig = webview.configuration().copy() | |
let newWebview = MSMirrorWebView.alloc().initWithFrame_configuration(frame,webviewConfig) | |
let url = webview.URL() | |
let request = NSURLRequest.alloc().initWithURL(url) | |
newWebview.loadRequest(request) | |
panel.contentView().addSubview(newWebview) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment