Last active
September 27, 2020 14:08
-
-
Save KevinGutowski/318aa47e88ccb844c176b60a02bf5d4f to your computer and use it in GitHub Desktop.
Creating a Sketch NSPanel
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
export function runPanel() { | |
let threadDictionary = NSThread.mainThread().threadDictionary() | |
let identifier = "co.yourIdentifier" | |
// If there is already a panel, prevent the plugin from running again | |
if (threadDictionary[identifier]) return | |
threadDictionary.panelOpen = true | |
setupPanel(threadDictionary, identifier) | |
} | |
function setupPanel(threadDictionary, identifier) { | |
var panelWidth = 500 | |
var panelHeight = 300 | |
let panel = NSPanel.alloc().init() | |
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true) | |
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask | NSFullSizeContentViewWindowMask | NSWindowStyleMaskResizable) | |
panel.setBackgroundColor(NSColor.whiteColor()); | |
panel.title = "SVGO UI" | |
panel.center() | |
panel.makeKeyAndOrderFront(null) | |
panel.setLevel(NSFloatingWindowLevel) | |
panel.minSize = NSMakeSize(panelWidth, panelHeight) | |
COScript.currentCOScript().setShouldKeepAround_(true) | |
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true) | |
panel.standardWindowButton(NSWindowZoomButton).setHidden(true) | |
var vibrancy = NSVisualEffectView.alloc().initWithFrame(NSMakeRect(0, 0, panelWidth, panelHeight)) | |
vibrancy.setAppearance(NSAppearance.appearanceNamed(NSAppearanceNameVibrantLight)) | |
vibrancy.setBlendingMode(NSVisualEffectBlendingModeBehindWindow) | |
panel.contentView().addSubview(vibrancy) | |
fitSubviewToView(vibrancy,panel.contentView(), [0.0,0.0,0.0,0.0]) | |
threadDictionary[identifier] = panel; | |
var closeButton = panel.standardWindowButton(NSWindowCloseButton) | |
closeButton.setCOSJSTargetFunction(function(sender) { | |
panel.close() | |
//Remove the reference to the panel | |
threadDictionary.removeObjectForKey(identifier) | |
threadDictionary.panelOpen = false | |
//Stop this script | |
COScript.currentCOScript().setShouldKeepAround_(false) | |
}) | |
} | |
function fitSubviewToView(subview, view, constants) { | |
subview.setTranslatesAutoresizingMaskIntoConstraints(false) | |
addEdgeConstraint(NSLayoutAttributeLeft, subview, view, constants[0]) | |
addEdgeConstraint(NSLayoutAttributeTop, subview, view, constants[1]) | |
addEdgeConstraint(NSLayoutAttributeRight, subview, view, constants[2]) | |
addEdgeConstraint(NSLayoutAttributeBottom, subview, view, constants[3]) | |
} | |
function addEdgeConstraint(layoutAttribute, subview, view, constant) { | |
view.addConstraint( | |
NSLayoutConstraint.constraintWithItem_attribute_relatedBy_toItem_attribute_multiplier_constant( | |
subview, | |
layoutAttribute, | |
NSLayoutRelationEqual, | |
view, | |
layoutAttribute, | |
1.0, | |
constant | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment