KMSnapshotWindow.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // KMSnapshotWindow.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/13.
  6. //
  7. import Cocoa
  8. class KMSnapshotWindow: NSWindow {
  9. var disableConstrainToScreen = false
  10. private let MIN_WINDOW_COORDINATE: CGFloat = -16000
  11. var windowImage: NSImage? {
  12. get {
  13. let frame = self.frame
  14. let visible = self.isVisible
  15. if visible == false {
  16. self.disableConstrainToScreen = true
  17. self.setFrameOrigin(NSMakePoint(MIN_WINDOW_COORDINATE, MIN_WINDOW_COORDINATE))
  18. self.orderBack(nil)
  19. self.displayIfNeeded()
  20. self.disableConstrainToScreen = false
  21. }
  22. let cgImage = CGWindowListCreateImage(CGRect.null, .optionIncludingWindow, CGWindowID(self.windowNumber), .boundsIgnoreFraming)
  23. if visible == false {
  24. self.orderOut(nil)
  25. self.setFrameOrigin(frame.origin)
  26. }
  27. if let data = cgImage {
  28. let image = NSImage(cgImage: data, size: frame.size)
  29. return image
  30. }
  31. return nil
  32. }
  33. }
  34. override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
  35. super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
  36. self.standardWindowButton(.miniaturizeButton)?.isEnabled = true
  37. }
  38. override func miniaturize(_ sender: Any?) {
  39. (self.windowController as? KMSnapshotWindowController)?.miniaturize()
  40. }
  41. override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect {
  42. if self.disableConstrainToScreen {
  43. return frameRect
  44. }
  45. return super.constrainFrameRect(frameRect, to: screen)
  46. }
  47. // - (id)_updateButtonsForModeChanged {
  48. // id rv = [super _updateButtonsForModeChanged];
  49. // [[self standardWindowButton:NSWindowMiniaturizeButton] setEnabled:YES];
  50. // return rv;
  51. // }
  52. }