3 class RecordingWindow: NSWindow {
7 let screens = NSScreen.screens
8 var boundingBox = NSZeroRect
9 for screen in screens {
10 boundingBox = NSUnionRect(boundingBox, screen.frame)
14 contentRect: boundingBox,
15 styleMask: [.borderless],
19 self.isReleasedWhenClosed = false
21 self.isMovableByWindowBackground = false
22 self.isMovable = false
23 self.titlebarAppearsTransparent = true
24 self.setFrame(boundingBox, display: true)
25 self.titleVisibility = .hidden
26 self.contentView = RecordingContentView()
27 self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
28 self.level = .screenSaver
30 self.hasShadow = false
31 self.makeKeyAndOrderFront(nil)
32 self.makeFirstResponder(nil)
35 override func resetCursorRects() {
36 super.resetCursorRects()
37 let cursor = NSCursor.crosshair
38 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
41 override var canBecomeKey: Bool {
45 override var canBecomeMain: Bool {
49 override func resignMain() {
51 self.ignoresMouseEvents = false
54 override func becomeMain() {
56 (self.contentView as? RecordingContentView)?.state = .idle
60 enum RecordingWindowState {
61 case passthrough, idle, drawing, moving, resizing;
64 class RecordingContentView: NSView {
66 var state: RecordingWindowState = .idle
67 var box: NSRect? = nil
68 var mouseLocation: NSPoint = NSPoint()
69 var origin: NSPoint = NSPoint()
70 var boxOrigin: NSPoint = NSPoint()
72 var resizeBox: NSRect? {
74 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
79 override func updateTrackingAreas() {
80 super.updateTrackingAreas()
82 for trackingArea in self.trackingAreas {
83 self.removeTrackingArea(trackingArea)
86 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
87 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
88 self.addTrackingArea(trackingArea)
91 override func mouseMoved(with event: NSEvent) {
93 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
96 if resizeBox!.contains(mouseLocation) {
99 if box.contains(mouseLocation) {
100 NSCursor.openHand.set()
102 NSCursor.crosshair.set()
106 NSCursor.crosshair.set()
110 self.setNeedsDisplay(self.bounds)
113 override func mouseDragged(with event: NSEvent) {
114 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
115 if state == .drawing {
117 x: round(min(origin.x, mouseLocation.x)),
118 y: round(min(origin.y, mouseLocation.y)),
119 width: round(abs(mouseLocation.x - origin.x)),
120 height: round(abs(mouseLocation.y - origin.y))
125 if state == .moving {
126 NSCursor.closedHand.set()
127 box!.origin = NSPoint(
128 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
129 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
132 if state == .resizing {
134 x: round(min(origin.x, mouseLocation.x)),
135 y: round(min(origin.y, mouseLocation.y)),
136 width: round(abs(mouseLocation.x - origin.x)),
137 height: round(abs(mouseLocation.y - origin.y))
141 self.setNeedsDisplay(self.bounds)
144 override func cursorUpdate(with event: NSEvent) {
145 NSCursor.crosshair.set()
148 override func hitTest(_ point: NSPoint) -> NSView? {
149 return state == .passthrough ? nil : self
152 override var acceptsFirstResponder: Bool {
156 override func mouseDown(with event: NSEvent) {
157 self.origin = self.convert(event.locationInWindow, from: nil)
159 if resizeBox!.contains(origin) {
160 self.origin = NSPoint(x: box.minX, y: box.maxY)
164 if box.contains(origin) {
166 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
174 override func mouseUp(with event: NSEvent) {
178 override func keyDown(with event: NSEvent) {
180 switch event.keyCode {
181 case 53: // Escape key
182 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
184 super.keyDown(with: event)
188 override func flagsChanged(with event: NSEvent) {
189 if event.modifierFlags.contains(.shift) {
191 window?.ignoresMouseEvents = true
194 window?.ignoresMouseEvents = false
198 override func draw(_ dirtyRect: NSRect) {
199 if state == .passthrough {
200 NSColor.clear.setFill()
202 NSColor(white: 1.0, alpha: 0.001).setFill()
206 let dashLength: CGFloat = 5.0
209 if state == .idle && box == nil {
210 let blackLine = NSBezierPath()
211 blackLine.lineWidth = lineWidth
212 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
214 // Vertical line (Black)
215 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
216 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
218 // Horizontal line (Black)
219 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
220 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
222 NSColor.black.setStroke()
225 let whiteLine = NSBezierPath()
226 whiteLine.lineWidth = lineWidth
227 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
229 // Vertical line (White)
230 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
231 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
233 // Horizontal line (White)
234 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
235 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
237 NSColor.white.setStroke()
242 let blackBox = NSBezierPath()
243 blackBox.lineWidth = lineWidth
244 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
245 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
246 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
247 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
248 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
249 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
250 NSColor.black.setStroke()
253 let whiteBox = NSBezierPath()
254 whiteBox.lineWidth = lineWidth
255 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
256 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
257 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
258 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
259 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
260 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
261 NSColor.white.setStroke()
265 let clearBox = NSBezierPath()
266 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
267 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
268 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
269 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
270 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
271 NSColor(white: 0, alpha: 0.2).setFill()
275 if state == .moving {
276 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
277 drawText(string, NSPoint(
283 if state == .resizing {
284 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
285 drawText(string, mouseLocation)
288 if box.contains(mouseLocation) && state != .resizing {
295 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
296 drawText(string, mouseLocation)
299 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
301 let textAttributes = [
302 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
303 NSAttributedString.Key.foregroundColor: NSColor.white,
305 let offset = NSPoint(x: 10, y: 10)
306 let padding = NSPoint(x: 5, y: 2)
307 let size = text.size(withAttributes: textAttributes)
308 var rect = NSRect(x: location.x + offset.x, y: location.y + offset.y, width: size.width + 2 * padding.x, height: size.height + 2 * padding.y)
309 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
312 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
313 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
319 text.draw(in: textRect, withAttributes: textAttributes)