]> git.r.bdr.sh - rbdr/captura/blob - Captura/RecordingWindow.swift
cf572e18d61f77f093bc2451559835194f72e0cf
[rbdr/captura] / Captura / RecordingWindow.swift
1 import Cocoa
2
3 class RecordingWindow: NSWindow {
4
5 init() {
6
7 let screens = NSScreen.screens
8 var boundingBox = NSZeroRect
9 for screen in screens {
10 boundingBox = NSUnionRect(boundingBox, screen.frame)
11 }
12
13 super.init(
14 contentRect: boundingBox,
15 styleMask: [.borderless],
16 backing: .buffered,
17 defer: false)
18
19 self.center()
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
28 self.isOpaque = false
29 self.hasShadow = false
30 self.makeKeyAndOrderFront(nil)
31 self.makeFirstResponder(nil)
32 }
33
34 override func resetCursorRects() {
35 super.resetCursorRects()
36 let cursor = NSCursor.crosshair
37 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
38 }
39
40 override var canBecomeKey: Bool {
41 return true
42 }
43
44 override var canBecomeMain: Bool {
45 return true
46 }
47 }
48
49 class RecordingContentView: NSView {
50
51 var isDrawing = false
52 var isMoving = false
53 var isResizing = false
54 var box: NSRect? = nil
55 var mouseLocation: NSPoint = NSPoint()
56 var origin: NSPoint = NSPoint()
57 var boxOrigin: NSPoint = NSPoint()
58
59 var resizeBox: NSRect? {
60 if let box {
61 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
62 }
63 return nil
64 }
65
66 override func updateTrackingAreas() {
67 super.updateTrackingAreas()
68
69 for trackingArea in self.trackingAreas {
70 self.removeTrackingArea(trackingArea)
71 }
72
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)
76 }
77
78 override func mouseMoved(with event: NSEvent) {
79
80 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
81
82 if let box {
83 if resizeBox!.contains(mouseLocation) {
84 NSCursor.arrow.set()
85 } else {
86 if box.contains(mouseLocation) {
87 NSCursor.openHand.set()
88 } else {
89 NSCursor.crosshair.set()
90 }
91 }
92 } else {
93 NSCursor.crosshair.set()
94 }
95
96
97 self.setNeedsDisplay(self.bounds)
98 }
99
100 override func mouseDragged(with event: NSEvent) {
101 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
102 if isDrawing {
103 box = NSRect(
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))
108 )
109 }
110
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)
116 }
117 self.setNeedsDisplay(self.bounds)
118 }
119
120 override func cursorUpdate(with event: NSEvent) {
121 NSCursor.crosshair.set()
122 }
123
124 override func hitTest(_ point: NSPoint) -> NSView? {
125 return self
126 }
127
128 override var acceptsFirstResponder: Bool {
129 return true
130 }
131
132 override func mouseDown(with event: NSEvent) {
133 self.origin = self.convert(event.locationInWindow, from: nil)
134 if let box {
135 if box.contains(origin) {
136 isMoving = true
137 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
138 return
139 }
140 }
141
142 isDrawing = true
143 }
144
145 override func mouseUp(with event: NSEvent) {
146 isDrawing = false
147 isMoving = false
148 }
149
150 override func draw(_ dirtyRect: NSRect) {
151 NSColor(white: 1.0, alpha: 0.001).setFill()
152 dirtyRect.fill()
153
154 let dashLength: CGFloat = 5.0
155 let lineWidth = 0.5
156
157 if !isDrawing && box == nil {
158 let blackLine = NSBezierPath()
159 blackLine.lineWidth = lineWidth
160 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
161
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)))
165
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))
169
170 NSColor.black.setStroke()
171 blackLine.stroke()
172
173 let whiteLine = NSBezierPath()
174 whiteLine.lineWidth = lineWidth
175 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
176
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)))
180
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))
184
185 NSColor.white.setStroke()
186 whiteLine.stroke()
187 }
188
189 if let box {
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()
199 blackBox.stroke()
200
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()
210 whiteBox.stroke()
211
212 if let resizeBox {
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()
220 clearBox.fill()
221 }
222
223 if box.contains(mouseLocation) && !isResizing {
224 return;
225 }
226 }
227
228 // Draw text
229
230 let offset = NSPoint(x: 10, y: 10)
231 let padding = NSPoint(x: 5, y: 2)
232
233 let textAttributes = [
234 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
235 NSAttributedString.Key.foregroundColor: NSColor.white,
236 ]
237
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)
242
243 NSColor.black.set()
244 rect.fill()
245
246 string.draw(in: textRect, withAttributes: textAttributes)
247 }
248 }