KMDeviceBrowserWindowController.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // KMDeviceBrowserWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/3/29.
  6. //
  7. import Cocoa
  8. import Quartz
  9. typealias ImportCameraFileCallback = (_ url: NSURL) -> ()
  10. typealias ImportScannerFileCallback = (_ url: NSURL) -> Void
  11. class KMDeviceBrowserWindowController: NSWindowController {
  12. @IBOutlet weak var splitView: NSSplitView!
  13. @IBOutlet weak var deviceBrowserView: IKDeviceBrowserView!
  14. @IBOutlet weak var cameraDeviceView: IKCameraDeviceView!
  15. @IBOutlet weak var scannerDeviceView: IKScannerDeviceView!
  16. static let shared = KMDeviceBrowserWindowController(windowNibName: "KMDeviceBrowserWindowController")
  17. var type: ICDeviceType = .camera
  18. var importCameraFileCallback: ImportCameraFileCallback?
  19. var importScannerFileCallback: ImportScannerFileCallback?
  20. deinit {
  21. KMPrint("KMDeviceBrowserWindowController 已释放.")
  22. self.deviceBrowserView.delegate = nil
  23. self.cameraDeviceView.delegate = nil
  24. self.scannerDeviceView.delegate = nil
  25. }
  26. override func windowDidLoad() {
  27. super.windowDidLoad()
  28. self.deviceBrowserView.displaysLocalCameras = false
  29. self.deviceBrowserView.displaysNetworkCameras = false
  30. self.splitView.setPosition(300.0, ofDividerAt: 0)
  31. }
  32. }
  33. extension KMDeviceBrowserWindowController: NSSplitViewDelegate {
  34. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  35. return 150.0
  36. }
  37. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  38. return 300.0
  39. }
  40. }
  41. extension KMDeviceBrowserWindowController: IKDeviceBrowserViewDelegate {
  42. func deviceBrowserView(_ deviceBrowserView: IKDeviceBrowserView!, selectionDidChange device: ICDevice!) {
  43. // if (device.type == .camera) {
  44. // cameraDeviceView.cameraDevice = (device as! ICCameraDevice)
  45. // cameraDeviceView.isHidden = false
  46. // cameraDeviceView.isHidden = true
  47. // } else if (device.type == .scanner) {
  48. // scannerDeviceView.scannerDevice = (device as! ICScannerDevice)
  49. // scannerDeviceView.isHidden = false
  50. // scannerDeviceView.isHidden = true
  51. // } else {
  52. // scannerDeviceView.isHidden = true
  53. // scannerDeviceView.isHidden = true
  54. // }
  55. if device != nil {
  56. scannerDeviceView.scannerDevice = (device as! ICScannerDevice)
  57. cameraDeviceView.isHidden = false
  58. cameraDeviceView.isHidden = true
  59. }
  60. }
  61. func deviceBrowserView(_ deviceBrowserView: IKDeviceBrowserView!, didEncounterError error: Error!) {
  62. KMPrint("111")
  63. }
  64. }
  65. extension KMDeviceBrowserWindowController: IKCameraDeviceViewDelegate {
  66. func cameraDeviceView(_ cameraDeviceView: IKCameraDeviceView!, didDownloadFile file: ICCameraFile!, location url: URL!, fileData data: Data!, error: Error!) {
  67. // if (!error &&
  68. // [file.UTI isEqualToString:(NSString *)kUTTypeImage]) {
  69. // if (self.importCameraFileCallback) {
  70. // self.importCameraFileCallback(url);
  71. // }
  72. // }
  73. KMPrint("获取图片完成")
  74. }
  75. }
  76. extension KMDeviceBrowserWindowController: IKScannerDeviceViewDelegate {
  77. func scannerDeviceView(_ scannerDeviceView: IKScannerDeviceView!, didScanTo url: URL!, fileData data: Data!, error: Error!) {
  78. if ((error == nil) && (url != nil)) {
  79. if let callback = importScannerFileCallback {
  80. callback(url! as NSURL)
  81. }
  82. }
  83. }
  84. }