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],
20 self.isMovableByWindowBackground = false
21 self.isMovable = false
22 self.titlebarAppearsTransparent = true
23 self.setFrame(boundingBox, display: true)
24 self.titleVisibility = .hidden
25 self.contentView = RecordingContentView()
26 self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
27 self.level = .screenSaver
29 self.hasShadow = false
30 self.makeKeyAndOrderFront(nil)
31 self.makeFirstResponder(nil)
34 override func resetCursorRects() {
35 super.resetCursorRects()
36 let cursor = NSCursor.crosshair
37 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
40 override var canBecomeKey: Bool {
44 override var canBecomeMain: Bool {
49 class RecordingContentView: NSView {
53 var isResizing = false
54 var box: NSRect? = nil
55 var mouseLocation: NSPoint = NSPoint()
56 var origin: NSPoint = NSPoint()
57 var boxOrigin: NSPoint = NSPoint()
59 var resizeBox: NSRect? {
61 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
66 override func updateTrackingAreas() {
67 super.updateTrackingAreas()
69 for trackingArea in self.trackingAreas {
70 self.removeTrackingArea(trackingArea)
73 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
74 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
75 self.addTrackingArea(trackingArea)
78 override func mouseMoved(with event: NSEvent) {
80 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
83 if resizeBox!.contains(mouseLocation) {
86 if box.contains(mouseLocation) {
87 NSCursor.openHand.set()
89 NSCursor.crosshair.set()
93 NSCursor.crosshair.set()
97 self.setNeedsDisplay(self.bounds)
100 override func mouseDragged(with event: NSEvent) {
101 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
104 x: round(min(origin.x, mouseLocation.x)),
105 y: round(min(origin.y, mouseLocation.y)),
106 width: round(abs(mouseLocation.x - origin.x)),
107 height: round(abs(mouseLocation.y - origin.y))
111 if isMoving && box != nil {
112 NSCursor.closedHand.set()
113 box!.origin = NSPoint(
114 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
115 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
117 self.setNeedsDisplay(self.bounds)
120 override func cursorUpdate(with event: NSEvent) {
121 NSCursor.crosshair.set()
124 override func hitTest(_ point: NSPoint) -> NSView? {
128 override var acceptsFirstResponder: Bool {
132 override func mouseDown(with event: NSEvent) {
133 self.origin = self.convert(event.locationInWindow, from: nil)
135 if box.contains(origin) {
137 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
145 override func mouseUp(with event: NSEvent) {
150 override func draw(_ dirtyRect: NSRect) {
151 NSColor(white: 1.0, alpha: 0.001).setFill()
154 let dashLength: CGFloat = 5.0
157 if !isDrawing && box == nil {
158 let blackLine = NSBezierPath()
159 blackLine.lineWidth = lineWidth
160 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
162 // Vertical line (Black)
163 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
164 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
166 // Horizontal line (Black)
167 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
168 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
170 NSColor.black.setStroke()
173 let whiteLine = NSBezierPath()
174 whiteLine.lineWidth = lineWidth
175 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
177 // Vertical line (White)
178 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
179 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
181 // Horizontal line (White)
182 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
183 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
185 NSColor.white.setStroke()
190 let blackBox = NSBezierPath()
191 blackBox.lineWidth = lineWidth
192 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
193 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
194 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
195 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
196 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
197 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
198 NSColor.black.setStroke()
201 let whiteBox = NSBezierPath()
202 whiteBox.lineWidth = lineWidth
203 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
204 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
205 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
206 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
207 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
208 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
209 NSColor.white.setStroke()
213 let clearBox = NSBezierPath()
214 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
215 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
216 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
217 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
218 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
219 NSColor(white: 0, alpha: 0.2).setFill()
223 if box.contains(mouseLocation) && !isResizing {
230 let offset = NSPoint(x: 10, y: 10)
231 let padding = NSPoint(x: 5, y: 2)
233 let textAttributes = [
234 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
235 NSAttributedString.Key.foregroundColor: NSColor.white,
238 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
239 let size = string.size(withAttributes: textAttributes)
240 let rect = NSRect(x: mouseLocation.x + offset.x, y: mouseLocation.y + offset.y, width: size.width + 2 * padding.x, height: size.height + 2 * padding.y)
241 let textRect = NSRect(x: mouseLocation.x + offset.x + padding.x, y: mouseLocation.y + offset.y + padding.y, width: size.width, height: size.height)
246 string.draw(in: textRect, withAttributes: textAttributes)