MainFlutterWindow.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Cocoa
  2. import FlutterMacOS
  3. private let kTrafficLightOffetTop = 22
  4. class MainFlutterWindow: NSWindow {
  5. func registerMethodChannel(flutterViewController: FlutterViewController) {
  6. let cocoaWindowChannel = FlutterMethodChannel(name: "flutter/cocoaWindow", binaryMessenger: flutterViewController.engine.binaryMessenger)
  7. cocoaWindowChannel.setMethodCallHandler({
  8. (call: FlutterMethodCall, result: FlutterResult) -> Void in
  9. if call.method == "setWindowPosition" {
  10. guard let position = call.arguments as? NSArray else {
  11. result(nil)
  12. return
  13. }
  14. let nX = position[0] as! NSNumber
  15. let nY = position[1] as! NSNumber
  16. let x = nX.doubleValue
  17. let y = nY.doubleValue
  18. self.setFrameOrigin(NSPoint(x: x, y: y))
  19. result(nil)
  20. return
  21. } else if call.method == "getWindowPosition" {
  22. let frame = self.frame
  23. result([frame.origin.x, frame.origin.y])
  24. return
  25. } else if call.method == "zoom" {
  26. self.zoom(self)
  27. result(nil)
  28. return
  29. }
  30. result(FlutterMethodNotImplemented)
  31. })
  32. }
  33. func layoutTrafficLightButton(titlebarView: NSView, button: NSButton, offsetTop: CGFloat, offsetLeft: CGFloat) {
  34. button.translatesAutoresizingMaskIntoConstraints = false;
  35. titlebarView.addConstraint(NSLayoutConstraint.init(
  36. item: button,
  37. attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: titlebarView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: offsetTop))
  38. titlebarView.addConstraint(NSLayoutConstraint.init(
  39. item: button,
  40. attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: titlebarView, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: offsetLeft))
  41. }
  42. func layoutTrafficLights() {
  43. let closeButton = self.standardWindowButton(ButtonType.closeButton)!
  44. let minButton = self.standardWindowButton(ButtonType.miniaturizeButton)!
  45. let zoomButton = self.standardWindowButton(ButtonType.zoomButton)!
  46. let titlebarView = closeButton.superview!
  47. self.layoutTrafficLightButton(titlebarView: titlebarView, button: closeButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 20)
  48. self.layoutTrafficLightButton(titlebarView: titlebarView, button: minButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 38)
  49. self.layoutTrafficLightButton(titlebarView: titlebarView, button: zoomButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 56)
  50. let customToolbar = NSTitlebarAccessoryViewController()
  51. let newView = NSView()
  52. newView.frame = NSRect(origin: CGPoint(), size: CGSize(width: 0, height: 40)) // only the height is cared
  53. customToolbar.view = newView
  54. self.addTitlebarAccessoryViewController(customToolbar)
  55. }
  56. override func awakeFromNib() {
  57. let flutterViewController = FlutterViewController.init()
  58. let windowFrame = self.frame
  59. self.contentViewController = flutterViewController
  60. self.registerMethodChannel(flutterViewController: flutterViewController)
  61. self.setFrame(windowFrame, display: true)
  62. self.titlebarAppearsTransparent = true
  63. self.titleVisibility = .hidden
  64. self.styleMask.insert(StyleMask.fullSizeContentView)
  65. self.isMovableByWindowBackground = true
  66. self.isMovable = false
  67. self.layoutTrafficLights()
  68. RegisterGeneratedPlugins(registry: flutterViewController)
  69. super.awakeFromNib()
  70. }
  71. }