KMDeviceBrowserWindowController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // KMDeviceBrowserWindowController.swift
  3. // PDF Reader Pro
  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. var type: ICDeviceType = .camera
  17. var importCameraFileCallback: ImportCameraFileCallback?
  18. var importScannerFileCallback: ImportScannerFileCallback?
  19. static let shared = KMDeviceBrowserWindowController(windowNibName: "KMDeviceBrowserWindowController")
  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. if (self.type == .camera) {
  29. self.deviceBrowserView.displaysLocalScanners = false;
  30. self.deviceBrowserView.displaysNetworkScanners = false;
  31. } else if (self.type == .scanner) {
  32. self.deviceBrowserView.displaysLocalCameras = false;
  33. self.deviceBrowserView.displaysNetworkCameras = false;
  34. }
  35. self.cameraDeviceView.downloadSelectedControlLabel = NSLocalizedString("Import", comment: "")
  36. self.cameraDeviceView.downloadAllControlLabel = NSLocalizedString("Import All", comment: "")
  37. self.window!.title = NSLocalizedString("Import", comment: "")
  38. }
  39. }
  40. extension KMDeviceBrowserWindowController: NSSplitViewDelegate {
  41. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  42. return 150.0
  43. }
  44. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  45. return 300.0
  46. }
  47. }
  48. extension KMDeviceBrowserWindowController: IKDeviceBrowserViewDelegate {
  49. func deviceBrowserView(_ deviceBrowserView: IKDeviceBrowserView!, selectionDidChange device: ICDevice!) {
  50. if device != nil {
  51. if (device!.className == "ICCameraDevice") {
  52. cameraDeviceView.cameraDevice = (device as! ICCameraDevice)
  53. cameraDeviceView.isHidden = false
  54. scannerDeviceView.isHidden = true
  55. } else if (device.className == "ICScannerDevice") {
  56. scannerDeviceView.scannerDevice = (device as! ICScannerDevice)
  57. scannerDeviceView.isHidden = false
  58. cameraDeviceView.isHidden = true
  59. } else {
  60. cameraDeviceView.isHidden = true
  61. scannerDeviceView.isHidden = true
  62. }
  63. }
  64. }
  65. func deviceBrowserView(_ deviceBrowserView: IKDeviceBrowserView!, didEncounterError error: Error!) {
  66. KMPrint("111")
  67. }
  68. }
  69. extension KMDeviceBrowserWindowController: IKCameraDeviceViewDelegate {
  70. func cameraDeviceView(_ cameraDeviceView: IKCameraDeviceView, didDownloadFile file: ICCameraFile, location url: URL, fileData data: Data?, error: Error?) {
  71. file.imageRepresentationType()
  72. if error == nil {
  73. importCameraFileCallback?(url as NSURL)
  74. }
  75. }
  76. }
  77. extension KMDeviceBrowserWindowController: IKScannerDeviceViewDelegate {
  78. func scannerDeviceView(_ scannerDeviceView: IKScannerDeviceView!, didScanTo url: URL!, fileData data: Data!, error: Error!) {
  79. if ((error == nil) && (url != nil)) {
  80. if let callback = importScannerFileCallback {
  81. callback(url! as NSURL)
  82. }
  83. }
  84. }
  85. }