25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

114 lines
2.6 KiB

  1. //
  2. // WebviewWindowController.swift
  3. // webview_window
  4. //
  5. // Created by Bin Yang on 2021/10/15.
  6. //
  7. import Cocoa
  8. import FlutterMacOS
  9. import WebKit
  10. class WebviewWindowController: NSWindowController {
  11. private let methodChannel: FlutterMethodChannel
  12. private let viewId: Int64
  13. private let width, height: Int
  14. private let titleBarHeight: Int
  15. private let titleBarTopPadding: Int
  16. private let title: String
  17. public weak var webviewPlugin: WebviewUniversalPlugin?
  18. init(viewId: Int64, methodChannel: FlutterMethodChannel,
  19. width: Int, height: Int,
  20. title: String, titleBarHeight: Int,
  21. titleBarTopPadding: Int) {
  22. self.viewId = viewId
  23. self.methodChannel = methodChannel
  24. self.width = width
  25. self.height = height
  26. self.titleBarHeight = titleBarHeight
  27. self.titleBarTopPadding = titleBarTopPadding
  28. self.title = title
  29. super.init(window: nil)
  30. }
  31. required init?(coder: NSCoder) {
  32. fatalError("init(coder:) has not been implemented")
  33. }
  34. public var webViewController: WebViewLayoutController {
  35. contentViewController as! WebViewLayoutController
  36. }
  37. override func windowDidLoad() {
  38. super.windowDidLoad()
  39. contentViewController = WebViewLayoutController(
  40. methodChannel: methodChannel,
  41. viewId: viewId, titleBarHeight: titleBarHeight,
  42. titleBarTopPadding: titleBarTopPadding)
  43. window?.setContentSize(NSSize(width: width, height: height))
  44. window?.center()
  45. window?.title = title
  46. window?.isReleasedWhenClosed = false
  47. window?.delegate = self
  48. }
  49. override func keyDown(with event: NSEvent) {
  50. if event.charactersIgnoringModifiers == "w" && event.modifierFlags.contains(.command) {
  51. close()
  52. }
  53. }
  54. func destroy() {
  55. window?.delegate = nil
  56. contentViewController = nil
  57. }
  58. func setAppearance(brightness: Int) {
  59. switch brightness {
  60. case 0:
  61. if #available(macOS 10.14, *) {
  62. window?.appearance = NSAppearance(named: .darkAqua)
  63. } else {
  64. // Fallback on earlier versions
  65. }
  66. break
  67. case 1:
  68. window?.appearance = NSAppearance(named: .aqua)
  69. break
  70. default:
  71. window?.appearance = nil
  72. break
  73. }
  74. }
  75. deinit {
  76. #if DEBUG
  77. print("\(self) deinited")
  78. #endif
  79. }
  80. override var windowNibName: NSNib.Name? {
  81. "WebviewWindowController"
  82. }
  83. }
  84. extension WebviewWindowController: NSWindowDelegate {
  85. func windowWillClose(_ notification: Notification) {
  86. webViewController.destroy()
  87. methodChannel.invokeMethod("onWindowClose", arguments: ["id": viewId])
  88. DispatchQueue.main.async {
  89. self.webviewPlugin?.onWebviewWindowClose(viewId: self.viewId, wc: self)
  90. }
  91. }
  92. }