MainFlutterWindow.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import Cocoa
  2. import AVFoundation
  3. import FlutterMacOS
  4. import desktop_multi_window
  5. // import bitsdojo_window_macos
  6. import desktop_drop
  7. import device_info_plus
  8. import flutter_custom_cursor
  9. import package_info_plus
  10. import path_provider_foundation
  11. import screen_retriever
  12. import sqflite
  13. // import tray_manager
  14. import uni_links_desktop
  15. import url_launcher_macos
  16. import wakelock_plus
  17. import window_manager
  18. import window_size
  19. import texture_rgba_renderer
  20. class MainFlutterWindow: NSWindow {
  21. override func awakeFromNib() {
  22. rustdesk_core_main();
  23. let flutterViewController = FlutterViewController.init()
  24. let windowFrame = self.frame
  25. self.contentViewController = flutterViewController
  26. self.setFrame(windowFrame, display: true)
  27. // register self method handler
  28. let registrar = flutterViewController.registrar(forPlugin: "RustDeskPlugin")
  29. setMethodHandler(registrar: registrar)
  30. RegisterGeneratedPlugins(registry: flutterViewController)
  31. FlutterMultiWindowPlugin.setOnWindowCreatedCallback { controller in
  32. // Register the plugin which you want access from other isolate.
  33. // DesktopLifecyclePlugin.register(with: controller.registrar(forPlugin: "DesktopLifecyclePlugin"))
  34. // Note: copy below from above RegisterGeneratedPlugins
  35. self.setMethodHandler(registrar: controller.registrar(forPlugin: "RustDeskPlugin"))
  36. DesktopDropPlugin.register(with: controller.registrar(forPlugin: "DesktopDropPlugin"))
  37. DeviceInfoPlusMacosPlugin.register(with: controller.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
  38. FlutterCustomCursorPlugin.register(with: controller.registrar(forPlugin: "FlutterCustomCursorPlugin"))
  39. FPPPackageInfoPlusPlugin.register(with: controller.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
  40. PathProviderPlugin.register(with: controller.registrar(forPlugin: "PathProviderPlugin"))
  41. SqflitePlugin.register(with: controller.registrar(forPlugin: "SqflitePlugin"))
  42. // TrayManagerPlugin.register(with: controller.registrar(forPlugin: "TrayManagerPlugin"))
  43. UniLinksDesktopPlugin.register(with: controller.registrar(forPlugin: "UniLinksDesktopPlugin"))
  44. UrlLauncherPlugin.register(with: controller.registrar(forPlugin: "UrlLauncherPlugin"))
  45. WakelockPlusMacosPlugin.register(with: controller.registrar(forPlugin: "WakelockPlusMacosPlugin"))
  46. WindowSizePlugin.register(with: controller.registrar(forPlugin: "WindowSizePlugin"))
  47. TextureRgbaRendererPlugin.register(with: controller.registrar(forPlugin: "TextureRgbaRendererPlugin"))
  48. }
  49. super.awakeFromNib()
  50. }
  51. override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
  52. super.order(place, relativeTo: otherWin)
  53. hiddenWindowAtLaunch()
  54. }
  55. /// Override window theme.
  56. public func setWindowInterfaceMode(window: NSWindow, themeName: String) {
  57. window.appearance = NSAppearance(named: themeName == "light" ? .aqua : .darkAqua)
  58. }
  59. public func setMethodHandler(registrar: FlutterPluginRegistrar) {
  60. let channel = FlutterMethodChannel(name: "org.rustdesk.rustdesk/macos", binaryMessenger: registrar.messenger)
  61. channel.setMethodCallHandler({
  62. (call, result) -> Void in
  63. switch call.method {
  64. case "setWindowTheme":
  65. let arg = call.arguments as! [String: Any]
  66. let themeName = arg["themeName"] as? String
  67. guard let window = registrar.view?.window else {
  68. result(nil)
  69. return
  70. }
  71. self.setWindowInterfaceMode(window: window,themeName: themeName ?? "light")
  72. result(nil)
  73. break;
  74. case "terminate":
  75. NSApplication.shared.terminate(self)
  76. result(nil)
  77. case "canRecordAudio":
  78. switch AVCaptureDevice.authorizationStatus(for: .audio) {
  79. case .authorized:
  80. result(1)
  81. break
  82. case .notDetermined:
  83. result(0)
  84. break
  85. default:
  86. result(-1)
  87. break
  88. }
  89. case "requestRecordAudio":
  90. AVCaptureDevice.requestAccess(for: .audio, completionHandler: { granted in
  91. result(granted)
  92. })
  93. break
  94. default:
  95. result(FlutterMethodNotImplemented)
  96. }
  97. })
  98. }
  99. }