]> git.r.bdr.sh - rbdr/captura/blame - Captura/RecordingWindow.swift
Add resize, keyboard shortcuts, menu items
[rbdr/captura] / Captura / RecordingWindow.swift
CommitLineData
24220348
RBR
1import Cocoa
2
3class 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
e834022c 19 self.isReleasedWhenClosed = false
24220348
RBR
20 self.center()
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
29 self.isOpaque = false
30 self.hasShadow = false
31 self.makeKeyAndOrderFront(nil)
32 self.makeFirstResponder(nil)
33 }
34
35 override func resetCursorRects() {
36 super.resetCursorRects()
37 let cursor = NSCursor.crosshair
38 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
39 }
40
41 override var canBecomeKey: Bool {
42 return true
43 }
44
45 override var canBecomeMain: Bool {
46 return true
47 }
e834022c
RBR
48
49 override func resignMain() {
50 super.resignMain()
51 self.ignoresMouseEvents = false
52 }
53
54 override func becomeMain() {
55 super.becomeMain()
56 (self.contentView as? RecordingContentView)?.state = .idle
57 }
58}
59
60enum RecordingWindowState {
61 case passthrough, idle, drawing, moving, resizing;
24220348
RBR
62}
63
64class RecordingContentView: NSView {
65
e834022c 66 var state: RecordingWindowState = .idle
24220348
RBR
67 var box: NSRect? = nil
68 var mouseLocation: NSPoint = NSPoint()
69 var origin: NSPoint = NSPoint()
70 var boxOrigin: NSPoint = NSPoint()
71
72 var resizeBox: NSRect? {
73 if let box {
74 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
75 }
76 return nil
77 }
78
79 override func updateTrackingAreas() {
80 super.updateTrackingAreas()
81
82 for trackingArea in self.trackingAreas {
83 self.removeTrackingArea(trackingArea)
84 }
85
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)
89 }
90
91 override func mouseMoved(with event: NSEvent) {
92
93 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
94
95 if let box {
96 if resizeBox!.contains(mouseLocation) {
97 NSCursor.arrow.set()
98 } else {
99 if box.contains(mouseLocation) {
100 NSCursor.openHand.set()
101 } else {
102 NSCursor.crosshair.set()
103 }
104 }
105 } else {
106 NSCursor.crosshair.set()
107 }
108
109
110 self.setNeedsDisplay(self.bounds)
111 }
112
113 override func mouseDragged(with event: NSEvent) {
114 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
e834022c 115 if state == .drawing {
24220348
RBR
116 box = NSRect(
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))
121 )
122 }
123
e834022c
RBR
124 if box != nil {
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)
130 }
131
132 if state == .resizing {
133 box = NSRect(
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))
138 )
139 }
24220348
RBR
140 }
141 self.setNeedsDisplay(self.bounds)
142 }
143
144 override func cursorUpdate(with event: NSEvent) {
145 NSCursor.crosshair.set()
146 }
147
148 override func hitTest(_ point: NSPoint) -> NSView? {
e834022c 149 return state == .passthrough ? nil : self
24220348
RBR
150 }
151
152 override var acceptsFirstResponder: Bool {
153 return true
154 }
155
156 override func mouseDown(with event: NSEvent) {
157 self.origin = self.convert(event.locationInWindow, from: nil)
158 if let box {
e834022c
RBR
159 if resizeBox!.contains(origin) {
160 self.origin = NSPoint(x: box.minX, y: box.maxY)
161 state = .resizing
162 return
163 }
24220348 164 if box.contains(origin) {
e834022c 165 state = .moving
24220348
RBR
166 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
167 return
168 }
169 }
170
e834022c 171 state = .drawing
24220348
RBR
172 }
173
174 override func mouseUp(with event: NSEvent) {
e834022c
RBR
175 state = .idle
176 }
177
178 override func keyDown(with event: NSEvent) {
179 print("key down")
180 switch event.keyCode {
181 case 53: // Escape key
182 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
183 default:
184 super.keyDown(with: event)
185 }
186 }
187
188 override func flagsChanged(with event: NSEvent) {
189 if event.modifierFlags.contains(.shift) {
190 state = .passthrough
191 window?.ignoresMouseEvents = true
192 } else {
193 state = .idle
194 window?.ignoresMouseEvents = false
195 }
24220348
RBR
196 }
197
198 override func draw(_ dirtyRect: NSRect) {
e834022c
RBR
199 if state == .passthrough {
200 NSColor.clear.setFill()
201 } else {
202 NSColor(white: 1.0, alpha: 0.001).setFill()
203 }
24220348
RBR
204 dirtyRect.fill()
205
206 let dashLength: CGFloat = 5.0
207 let lineWidth = 0.5
208
e834022c 209 if state == .idle && box == nil {
24220348
RBR
210 let blackLine = NSBezierPath()
211 blackLine.lineWidth = lineWidth
212 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
213
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)))
217
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))
221
222 NSColor.black.setStroke()
223 blackLine.stroke()
224
225 let whiteLine = NSBezierPath()
226 whiteLine.lineWidth = lineWidth
227 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
228
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)))
232
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))
236
237 NSColor.white.setStroke()
238 whiteLine.stroke()
239 }
240
241 if let box {
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()
251 blackBox.stroke()
252
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()
262 whiteBox.stroke()
263
264 if let resizeBox {
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()
272 clearBox.fill()
273 }
274
e834022c
RBR
275 if state == .moving {
276 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
277 drawText(string, NSPoint(
278 x: box.minX,
279 y: box.maxY
280 ), true)
281 }
282
283 if state == .resizing {
284 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
285 drawText(string, mouseLocation)
286 }
287
288 if box.contains(mouseLocation) && state != .resizing {
24220348
RBR
289 return;
290 }
291 }
292
293 // Draw text
e834022c
RBR
294
295 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
296 drawText(string, mouseLocation)
297 }
298
299 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
24220348
RBR
300
301 let textAttributes = [
302 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
303 NSAttributedString.Key.foregroundColor: NSColor.white,
304 ]
e834022c
RBR
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)
310
311 if (isBottomRight) {
312 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
313 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
314 }
24220348
RBR
315
316 NSColor.black.set()
317 rect.fill()
318
e834022c 319 text.draw(in: textRect, withAttributes: textAttributes)
24220348
RBR
320 }
321}