// // KMEditPDFPopToolBarWindow.swift // PDF Reader Pro // // Created by tangchao on 2024/6/25. // import Cocoa @objcMembers class KMEditPDFPopToolBarWindow: NSWindow { static let shared = KMEditPDFPopToolBarWindow() var style: KMEditPDFToolbarStyle = .text var isMultiple: Bool = false let model = KMEditPDFModel() var itemClick: ((KMEditPDFToolbarItemKey, Any?)->Void)? convenience init() { let rect = NSRect(x: 0, y: 0, width: 400, height: 44) let styleMask: NSWindow.StyleMask = [.fullSizeContentView] self.init(contentRect: rect, styleMask: styleMask, backing: .buffered, defer: false) } override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) { super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag) let contentViewC = KMEditPDFPopToolBarController() self.contentViewController = contentViewC self.titlebarAppearsTransparent = true self.titleVisibility = .hidden // self.level = .popUpMenu // self.isMovableByWindowBackground = false self.isMovable = false self.contentView?.wantsLayer = true self.contentView?.layer?.cornerRadius = 4 self.contentView?.layer?.masksToBounds = true self.backgroundColor = .clear contentViewC.itemClick = { [weak self] itemKey, obj in self?.itemClick?(itemKey, obj) } } func show(relativeTo positioningRect: NSRect, of positioningView: NSView, preferredEdge: NSRectEdge) { let contentViewC = (self.contentViewController as? KMEditPDFPopToolBarController) var width: CGFloat = 392 if self.style.contains(.text) { if self.style.contains(.image) { // text + image contentViewC?.itemKeys = [.alignmentLeft, .alignmentCenterX, .alignmentRight, .alignmentjustifiedX, .alignmentTop, .alignmentCenterY, .alignmentBottom, .alignmentjustifiedY] width = 320 } else { // text if self.isMultiple { width = 478 contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment, .separator, .alignmentLeft, .alignmentTop] } else { width = 392 contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment] } } } else { if self.style.contains(.image) { // image if self.isMultiple { width = 396 contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export, .separator, .alignmentLeft, .alignmentTop] } else { width = 304 contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export] } } else { // none } } contentViewC?.fontColor = self.model.fontColors.last ?? .black // var position = positioningView.convert(positioningRect.origin, to: nil) var position = NSEvent.mouseLocation position.y += 20 position.x -= 60 // self.setFrameOrigin(position) let frame = NSMakeRect(position.x, position.y, width, 44) self.setFrame(frame, display: true) self.contentViewController?.view.frame = NSMakeRect(0, 0, width, 44) self.orderFront(nil) // self.makeKeyAndOrderFront(nil) } override var isMainWindow: Bool { return true } override var isKeyWindow: Bool { return true } }