]> git.r.bdr.sh - rbdr/captura/blobdiff - Captura/Presentation/Windows/RecordingWindow.swift
Save WIP -> Multimonitor change working
[rbdr/captura] / Captura / Presentation / Windows / RecordingWindow.swift
index f5941dc82bdbde22970439157d5088a2572eef23..b5c44139cb6a26cac5b0d86c720379764a4c85af 100644 (file)
@@ -13,11 +13,7 @@ class RecordingWindow: NSWindow {
   
   init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) {
     
   
   init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) {
     
-    let screens = NSScreen.screens
-    var boundingBox = NSZeroRect
-    for screen in screens {
-        boundingBox = NSUnionRect(boundingBox, screen.frame)
-    }
+    let boundingBox = NSScreen.screenWithMouse?.frame ?? NSZeroRect
     
     super.init(
       contentRect: boundingBox,
     
     super.init(
       contentRect: boundingBox,
@@ -25,6 +21,7 @@ class RecordingWindow: NSWindow {
       backing: .buffered,
       defer: false)
 
       backing: .buffered,
       defer: false)
 
+
     self.isReleasedWhenClosed = false
     self.collectionBehavior = [.canJoinAllSpaces]
     self.isMovableByWindowBackground = false
     self.isReleasedWhenClosed = false
     self.collectionBehavior = [.canJoinAllSpaces]
     self.isMovableByWindowBackground = false
@@ -33,14 +30,19 @@ class RecordingWindow: NSWindow {
     self.titlebarAppearsTransparent = true
     self.setFrame(boundingBox, display: true)
     self.titleVisibility = .hidden
     self.titlebarAppearsTransparent = true
     self.setFrame(boundingBox, display: true)
     self.titleVisibility = .hidden
-    let recordingView = RecordingContentView(configuration, frame: boundingBox)
+    let recordingView = RecordingContentView(configuration, frame: boundingBox, button: button ?? NSZeroRect)
     recordingView.frame = boundingBox
     recordingView.frame = boundingBox
-    recordingView.button = button
     self.contentView = recordingView
     self.contentView = recordingView
-    self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
+    //self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
+    self.backgroundColor = NSColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.5)
     self.level = .screenSaver
     self.isOpaque = false
     self.hasShadow = false
     self.level = .screenSaver
     self.isOpaque = false
     self.hasShadow = false
+    
+    print("AAAAH INIT CHANGE")
+    print("AAAAH FRAME X: \(recordingView.frame.minX) \(recordingView.frame.maxX) // Y: \(recordingView.frame.minY) \(recordingView.frame.maxY)")
+    print("AAAAH BOUNDS X: \(recordingView.bounds.minX) \(recordingView.bounds.maxX) // Y: \(recordingView.bounds.minY) \(recordingView.bounds.maxY)")
+    print("AAAAH WIN F X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
   }
   
   // MARK: - Window Behavior Overrides
   }
   
   // MARK: - Window Behavior Overrides
@@ -80,11 +82,33 @@ enum RecordingWindowState {
 
 class RecordingContentView: NSView {
   
 
 class RecordingContentView: NSView {
   
-  init(_ configuration: CaptureSessionConfiguration, frame: NSRect) {
+  init(_ configuration: CaptureSessionConfiguration, frame: NSRect, button: NSRect) {
     super.init(frame: frame)
     preventResize = configuration.preventResize
     preventMove = configuration.preventMove
     autoStart = configuration.autoStart
     super.init(frame: frame)
     preventResize = configuration.preventResize
     preventMove = configuration.preventMove
     autoStart = configuration.autoStart
+    self.button = button
+    
+    for screen in NSScreen.screens {
+      print(screen.frame)
+      // BEFORE YOU WENT TO BED:
+      // You were checking which screen contains the button, so you can calculate the offset which should give you
+      // the location of the button, which you can then use to draw the button at the same offset whenever you need
+      // to change screen! This would keep the behavior of pressing the record button.
+      // If this does work, remember to then test it with a Hi DPI display, because we might need to adjust for pixel
+      // density.
+      // Finally, if it does work, make sure the alternate monitor still responds, by adjusting the behavior of the
+      // real button
+      if screen.frame.intersects(button) {
+        print("CONTAINS! ->")
+        let relativeX = screen.frame.width - button.maxX
+        let relativeY = screen.frame.height - button.maxY
+        print("The rect is at (\(relativeX), \(relativeY)) relative to the top right of the screen frame.")
+      } else {
+        print("NO CONTAINS ->")
+      }
+      print(button)
+    }
     
     if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
       box = NSRect(
     
     if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
       box = NSRect(
@@ -106,6 +130,7 @@ class RecordingContentView: NSView {
     fatalError("init(coder:) has not been implemented")
   }
   
     fatalError("init(coder:) has not been implemented")
   }
   
+  private var buttons: [NSRect] = []
   public var button: NSRect? = nil
   @Published public var box: NSRect? = nil
   public var state: RecordingWindowState = .idle
   public var button: NSRect? = nil
   @Published public var box: NSRect? = nil
   public var state: RecordingWindowState = .idle
@@ -167,6 +192,12 @@ class RecordingContentView: NSView {
     self.addTrackingArea(trackingArea)
   }
   
     self.addTrackingArea(trackingArea)
   }
   
+  override func mouseExited(with event: NSEvent) {
+    if state == .idle && box == nil {
+      self.moveWindow()
+    }
+  }
+  
   override func mouseMoved(with event: NSEvent) {
     
     self.mouseLocation = self.convert(event.locationInWindow, from: nil)
   override func mouseMoved(with event: NSEvent) {
     
     self.mouseLocation = self.convert(event.locationInWindow, from: nil)
@@ -305,6 +336,17 @@ class RecordingContentView: NSView {
     
     let dashLength: CGFloat = 5.0
     let lineWidth = 0.5
     
     let dashLength: CGFloat = 5.0
     let lineWidth = 0.5
+    
+    if let button {
+      let buttonPath = NSBezierPath()
+      buttonPath.move(to: NSPoint(x: button.minX, y: button.minY))
+      buttonPath.line(to: NSPoint(x: button.maxX, y: button.minY))
+      buttonPath.line(to: NSPoint(x: button.maxX, y: button.maxY))
+      buttonPath.line(to: NSPoint(x: button.minX, y: button.maxY))
+      buttonPath.line(to: NSPoint(x: button.minX, y: button.minY))
+      NSColor(red: 1, green: 0, blue: 1, alpha: 1).setFill()
+      buttonPath.fill()
+    }
 
     if state == .idle && box == nil {
       let blackLine = NSBezierPath()
 
     if state == .idle && box == nil {
       let blackLine = NSBezierPath()
@@ -424,4 +466,31 @@ class RecordingContentView: NSView {
 
     text.draw(in: textRect, withAttributes: textAttributes)
   }
 
     text.draw(in: textRect, withAttributes: textAttributes)
   }
+  
+  private func moveWindow() {
+    print("AAAAH BEFORE WE CHANGE")
+    print("AAAAH FRAME X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
+    print("AAAAH BOUNDS X: \(self.bounds.minX) \(self.bounds.maxX) // Y: \(self.bounds.minY) \(self.bounds.maxY)")
+    print("AAAAH WIN F X: \(self.window?.frame.minX) \(self.window?.frame.maxX) // Y: \(self.window?.frame.minY) \(self.window?.frame.maxY)")
+    let screen = NSScreen.screenWithMouse
+    if let currentScreen = self.window?.screen {
+      if currentScreen != screen {
+        let frame = screen?.frame ?? NSZeroRect
+        self.frame = CGRect(origin: NSZeroPoint, size: frame.size)
+        self.bounds = CGRect(origin: NSZeroPoint, size: frame.size)
+        self.updateTrackingAreas()
+        
+        if let window = self.window {
+          self.bounds = frame
+          window.setFrame(frame, display: true, animate: false)
+          window.makeKeyAndOrderFront(nil)
+          window.orderFrontRegardless()
+          print("AAAAH AFTER CHANGE")
+          print("AAAAH FRAME X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
+          print("AAAAH BOUNDS X: \(self.bounds.minX) \(self.bounds.maxX) // Y: \(self.bounds.minY) \(self.bounds.maxY)")
+          print("AAAAH WIN F X: \(self.window?.frame.minX) \(self.window?.frame.maxX) // Y: \(self.window?.frame.minY) \(self.window?.frame.maxY)")
+        }
+      }
+    }
+  }
 }
 }