Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski KevinGutowski/minimap.js
Last active Oct 6, 2019

Embed
What would you like to do?
framework("QuartzCore")
var panelWidth = 300
var panelHeight = 200
let panel = NSPanel.alloc().init()
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true)
panel.setStyleMask(NSWindowStyleMaskHUDWindow
| NSTitledWindowMask | NSClosableWindowMask)
panel.title = "Testing"
panel.center()
panel.makeKeyAndOrderFront(null)
panel.setLevel(NSFloatingWindowLevel)
let contentView = panel.contentView()
contentView.setFlipped(true)
contentView.setWantsLayer(true)
contentView.layer().backgroundColor = NSColor.grayColor().CGColor()
let inset = 8
let windowWidth = panel.contentView().frame().size.width
let windowHeight = panel.contentView().frame().size.height
let windowframe = CGRectMake(inset,inset, windowWidth - inset * 2, windowHeight - inset * 2)
let map = CALayer.layer()
map.anchorPoint = CGPointMake(0,0)
let frames = []
let artboards = context.document.currentPage().artboards()
artboards.forEach(ab=> {
let artboard = CAShapeLayer.layer()
let artboardFrame = ab.absoluteRect().rect()
frames.push(artboardFrame)
artboard.anchorPoint = CGPointMake(0,0)
artboard.frame = artboardFrame
artboard.backgroundColor = NSColor.whiteColor().CGColor()
map.addSublayer(artboard)
})
let superFrame = getSuperFrameFromFrames(frames)
map.bounds = superFrame
//map.backgroundColor = NSColor.redColor().CGColor()
let drawView = context.document.contentDrawView()
let drawViewFrame = drawView.frame()
let drawViewPosition = drawView.scrollOrigin()
let drawViewZoom = drawView.zoomValue()
let viewPort = CAShapeLayer.layer()
viewPort.anchorPoint = CGPointMake(0,0)
viewPort.frame = NSMakeRect(-drawViewPosition.x/drawViewZoom, -drawViewPosition.y/drawViewZoom, drawViewFrame.size.width/drawViewZoom, drawViewFrame.size.height/drawViewZoom)
let accentColor = NSColor.controlAccentColor()
viewPort.backgroundColor = accentColor.colorWithAlphaComponent(0.2).CGColor()
viewPort.borderColor = accentColor.CGColor()
map.addSublayer(viewPort)
contentView.layer().addSublayer(map)
resizeMapToFitWindowframe(map,windowframe,viewPort)
centerMapToFrame(map,panel.contentView().frame())
function getSuperFrameFromFrames(frames) {
while (frames.length > 1) {
frames[0] = CGRectUnion(frames[0], frames[1])
frames.splice(1,1)
}
return frames[0]
}
function resizeMapToFitWindowframe(map,windowframe,viewPort) {
let mapWidth = map.frame().size.width
let mapHeight = map.frame().size.height
let windowFrameWidth = windowframe.size.width
let windowFrameHeight = windowframe.size.height
let scaling
let mapAspectRatio = mapWidth / mapHeight
let windowFrameAspectRatio = windowFrameWidth / windowFrameHeight
if (mapAspectRatio > windowFrameAspectRatio) {
scaling = windowframe.size.width / mapWidth
} else {
scaling = windowframe.size.height / mapHeight
}
map.setTransform(CATransform3DMakeScale(scaling, scaling,1))
viewPort.borderWidth = 2/scaling
}
function centerMapToFrame(map,frame) {
map.anchorPoint = CGPointMake(0.5,0.5)
map.position = CGPointMake(frame.size.width / 2,frame.size.height / 2)
}
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.