AppFlowy/frontend/appflowy_flutter/macos/Runner/MainFlutterWindow.swift

86 lines
3.5 KiB
Swift
Raw Normal View History

2021-06-19 15:41:19 +00:00
import Cocoa
import FlutterMacOS
2022-05-27 02:34:12 +00:00
private let kTrafficLightOffetTop = 22
2021-06-19 15:41:19 +00:00
class MainFlutterWindow: NSWindow {
2022-05-27 02:34:12 +00:00
func registerMethodChannel(flutterViewController: FlutterViewController) {
let cocoaWindowChannel = FlutterMethodChannel(name: "flutter/cocoaWindow", binaryMessenger: flutterViewController.engine.binaryMessenger)
cocoaWindowChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: FlutterResult) -> Void in
if call.method == "setWindowPosition" {
guard let position = call.arguments as? NSArray else {
result(nil)
return
}
let nX = position[0] as! NSNumber
let nY = position[1] as! NSNumber
let x = nX.doubleValue
let y = nY.doubleValue
self.setFrameOrigin(NSPoint(x: x, y: y))
result(nil)
return
} else if call.method == "getWindowPosition" {
let frame = self.frame
result([frame.origin.x, frame.origin.y])
return
} else if call.method == "zoom" {
self.zoom(self)
result(nil)
return
}
result(FlutterMethodNotImplemented)
})
}
func layoutTrafficLightButton(titlebarView: NSView, button: NSButton, offsetTop: CGFloat, offsetLeft: CGFloat) {
button.translatesAutoresizingMaskIntoConstraints = false;
titlebarView.addConstraint(NSLayoutConstraint.init(
item: button,
attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: titlebarView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: offsetTop))
titlebarView.addConstraint(NSLayoutConstraint.init(
item: button,
attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: titlebarView, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: offsetLeft))
}
func layoutTrafficLights() {
let closeButton = self.standardWindowButton(ButtonType.closeButton)!
let minButton = self.standardWindowButton(ButtonType.miniaturizeButton)!
let zoomButton = self.standardWindowButton(ButtonType.zoomButton)!
let titlebarView = closeButton.superview!
self.layoutTrafficLightButton(titlebarView: titlebarView, button: closeButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 20)
self.layoutTrafficLightButton(titlebarView: titlebarView, button: minButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 38)
self.layoutTrafficLightButton(titlebarView: titlebarView, button: zoomButton, offsetTop: CGFloat(kTrafficLightOffetTop), offsetLeft: 56)
let customToolbar = NSTitlebarAccessoryViewController()
let newView = NSView()
newView.frame = NSRect(origin: CGPoint(), size: CGSize(width: 0, height: 40)) // only the height is cared
customToolbar.view = newView
self.addTitlebarAccessoryViewController(customToolbar)
}
2021-06-19 15:41:19 +00:00
override func awakeFromNib() {
let flutterViewController = FlutterViewController.init()
let windowFrame = self.frame
self.contentViewController = flutterViewController
2022-05-27 02:34:12 +00:00
self.registerMethodChannel(flutterViewController: flutterViewController)
2021-06-19 15:41:19 +00:00
self.setFrame(windowFrame, display: true)
2022-05-27 02:34:12 +00:00
self.titlebarAppearsTransparent = true
self.titleVisibility = .hidden
self.styleMask.insert(StyleMask.fullSizeContentView)
self.isMovableByWindowBackground = true
self.isMovable = false
self.layoutTrafficLights()
2021-06-19 15:41:19 +00:00
RegisterGeneratedPlugins(registry: flutterViewController)
super.awakeFromNib()
}
}