Last active
June 10, 2021 19:09
-
-
Save chrishoage/4336f75d6eba184044d6 to your computer and use it in GitHub Desktop.
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
import Foundation | |
import AppKit | |
func parseFrame(args: Array<String>) -> NSRect { | |
var frameHash = [String: Int]() | |
frameHash["w"] = 0 | |
frameHash["h"] = 0 | |
frameHash["x"] = 0 | |
frameHash["y"] = 0 | |
let option = CGWindowListOption(arrayLiteral: CGWindowListOption.OptionAll) | |
let relativeToWindow = CGWindowID(0) | |
let windowListInfo = CGWindowListCopyWindowInfo(option, relativeToWindow) | |
let infoList = windowListInfo as NSArray? as? [[String: AnyObject]] | |
let windowId = Int(args[0]) | |
let scrn: NSScreen = NSScreen.mainScreen()! | |
let scrnHeight: Int = Int(scrn.frame.size.height) | |
for i in infoList! { | |
let windowNumber = i["kCGWindowNumber"]! as! Int | |
if (windowNumber == windowId) { | |
let windowBounds = i["kCGWindowBounds"]! as! [String : Int] | |
frameHash["w"] = windowBounds["Width"] | |
frameHash["h"] = windowBounds["Height"]! | |
frameHash["x"] = windowBounds["X"] | |
frameHash["y"] = scrnHeight - (windowBounds["Y"]! + windowBounds["Height"]!) | |
break | |
} | |
} | |
for arg in args { | |
if ((arg.characters.indexOf(":")) != nil) { | |
let pair: Array = arg.componentsSeparatedByString(":") | |
let key: String = pair[0] | |
let val: String = pair[1] | |
switch key { | |
case "x": | |
frameHash["x"] = Int(val) | |
break | |
case "y": | |
frameHash["y"] = Int(val) | |
break | |
case "w", "width": | |
frameHash["w"] = Int(val) | |
break | |
case "h", "height": | |
frameHash["h"] = Int(val) | |
break | |
default: | |
break | |
} | |
} | |
} | |
return NSRect(x: frameHash["x"]!, y: frameHash["y"]!, width: frameHash["w"]!, height: frameHash["h"]!) | |
} | |
func parseColor(args: Array<String>) -> NSColor { | |
var colorHash = [String: CGFloat]() | |
colorHash["r"] = 1 | |
colorHash["g"] = 0 | |
colorHash["b"] = 0 | |
colorHash["a"] = 1 | |
for arg in args { | |
if ((arg.characters.indexOf(":")) != nil) { | |
let pair: Array = arg.componentsSeparatedByString(":") | |
let key: String = pair[0] | |
let val: String = pair[1] | |
switch key { | |
case "r", "red": | |
colorHash["r"] = CGFloat(Float(val)!) | |
break | |
case "b", "blue": | |
colorHash["b"] = CGFloat(Float(val)!) | |
break | |
case "g", "green": | |
colorHash["g"] = CGFloat(Float(val)!) | |
break | |
case "a", "alpha": | |
colorHash["a"] = CGFloat(Float(val)!) | |
break | |
default: | |
break | |
} | |
} | |
} | |
return NSColor(red: colorHash["r"]!, green: colorHash["g"]!, blue: colorHash["b"]!, alpha: colorHash["a"]!) | |
} | |
func parseWidth(args: Array<String>) -> CGFloat { | |
var widthVal: CGFloat = 4 | |
for arg in args { | |
if ((arg.characters.indexOf(":")) != nil) { | |
let pair: Array = arg.componentsSeparatedByString(":") | |
let key: String = pair[0] | |
let val: String = pair[1] | |
switch key { | |
case "s", "stroke": | |
widthVal = CGFloat(Float(val)!) | |
break | |
default: | |
break | |
} | |
} | |
} | |
return widthVal | |
} | |
class OverlayView: NSView | |
{ | |
let borderColor: NSColor | |
var lineWidth: CGFloat | |
init(frame: NSRect, color: NSColor, width: CGFloat) | |
{ | |
borderColor = color | |
lineWidth = width | |
super.init(frame: frame) | |
} | |
required init?(coder: NSCoder) | |
{ | |
fatalError("init(coder:) has not been implemented") | |
} | |
var colorclear = NSColor.clearColor() | |
override func drawRect(rect: NSRect) | |
{ | |
colorclear.setFill() | |
let bpath:NSBezierPath = NSBezierPath(rect: rect) | |
borderColor.set() | |
bpath.lineWidth = lineWidth | |
bpath.stroke() | |
NSLog("drawRect has updated the view") | |
} | |
} | |
class OverlayController: NSObject, NSApplicationDelegate | |
{ | |
let window = NSWindow() | |
func showOverlayView(args: Array<String>) { | |
let overlayFrame = parseFrame(args) | |
let overlayColor = parseColor(args) | |
let overlayWidth = parseWidth(args) | |
let overlayView = OverlayView(frame: overlayFrame, color: overlayColor, width: overlayWidth) | |
window.opaque = false | |
window.backgroundColor = overlayView.colorclear | |
window.titleVisibility = NSWindowTitleVisibility.Hidden | |
window.titlebarAppearsTransparent = true | |
window.ignoresMouseEvents = true | |
window.styleMask |= NSFullSizeContentViewWindowMask | |
window.level = Int(CGWindowLevelForKey(.FloatingWindowLevelKey)) | |
window.hasShadow = false | |
window.makeKeyAndOrderFront(self) | |
window.contentView = overlayView | |
window.setFrame(NSRectToCGRect(overlayFrame), display: true) | |
} | |
func applicationDidFinishLaunching(aNotification: NSNotification) | |
{ | |
let outHandle = NSFileHandle.fileHandleWithStandardInput() | |
outHandle.waitForDataInBackgroundAndNotify() | |
var obs1 : NSObjectProtocol! | |
obs1 = NSNotificationCenter.defaultCenter().addObserverForName(NSFileHandleDataAvailableNotification, object: outHandle, queue: nil) { | |
notification -> Void in | |
let data = outHandle.availableData | |
if (data.length > 0) { | |
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as String? { | |
let trimmedString = str.stringByTrimmingCharactersInSet( | |
NSCharacterSet.whitespaceAndNewlineCharacterSet() | |
) | |
let args = trimmedString.componentsSeparatedByString(" ") | |
self.showOverlayView(args) | |
outHandle.waitForDataInBackgroundAndNotify() | |
} else { | |
NSNotificationCenter.defaultCenter().removeObserver(obs1) | |
} | |
} | |
} | |
} | |
} | |
let app = NSApplication.sharedApplication() | |
let overlayController = OverlayController() | |
app.delegate = overlayController | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment