// // 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] var datas: [KMEditPDFToolbarModel] = [] for key in contentViewC?.itemKeys ?? [] { let model = KMEditPDFToolbarModel() model.itemKey = key datas.append(model) } contentViewC?.datas = datas width = 320 } else { // text if self.isMultiple { width = 478 contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment, .separator, .alignmentLeft, .alignmentTop] var datas: [KMEditPDFToolbarModel] = [] for key in contentViewC?.itemKeys ?? [] { let model = KMEditPDFToolbarModel() model.itemKey = key if key == .color { model.isEnabled = self.model.editAreasFontColorIsEqual() } else if key == .fontStyle { model.isEnabled = self.model.editAreasFontNameIsEqual() } else if key == .fontAdd { model.isEnabled = self.model.editAreasFontSizeIsEqual() } else if key == .fontReduce { model.isEnabled = self.model.editAreasFontSizeIsEqual() } else if key == .fontBold { model.isEnabled = self.model.editAreasFontBoldIsEqual() } else if key == .fontItalic { model.isEnabled = self.model.editAreasFontItalicIsEqual() } else if key == .textAlignment { model.isEnabled = self.model.editAreasTextAlignmentIsEqual() } datas.append(model) } contentViewC?.datas = datas } else { width = 392 contentViewC?.itemKeys = [.color, .fontStyle, .fontAdd, .fontReduce, .fontBold, .fontItalic, .textAlignment] var datas: [KMEditPDFToolbarModel] = [] for key in contentViewC?.itemKeys ?? [] { let model = KMEditPDFToolbarModel() model.itemKey = key if key == .color { model.isEnabled = self.model.editAreasFontColorIsEqual() } else if key == .fontStyle { model.isEnabled = self.model.editAreasFontNameIsEqual() } else if key == .fontAdd { model.isEnabled = self.model.editAreasFontSizeIsEqual() } else if key == .fontReduce { model.isEnabled = self.model.editAreasFontSizeIsEqual() } else if key == .fontBold { model.isEnabled = self.model.editAreasFontBoldIsEqual() } else if key == .fontItalic { model.isEnabled = self.model.editAreasFontItalicIsEqual() } else if key == .textAlignment { model.isEnabled = self.model.editAreasTextAlignmentIsEqual() } datas.append(model) } contentViewC?.datas = datas } } } 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] var datas: [KMEditPDFToolbarModel] = [] for key in contentViewC?.itemKeys ?? [] { let model = KMEditPDFToolbarModel() model.itemKey = key if key == .crop { model.isEnabled = !self.isMultiple } datas.append(model) } contentViewC?.datas = datas } else { width = 304 contentViewC?.itemKeys = [.leftRotate, .rightRotate, .separator, .reverseX, .reverseY, .separator, .crop, .replace, .export] var datas: [KMEditPDFToolbarModel] = [] for key in contentViewC?.itemKeys ?? [] { let model = KMEditPDFToolbarModel() model.itemKey = key if key == .crop { model.isEnabled = !self.isMultiple } datas.append(model) } contentViewC?.datas = datas } } else { // none } } contentViewC?.fontColor = self.model.fontColors.last ?? .black let winFrame = positioningView.window?.frame ?? .zero var position = positioningView.convert(positioningRect.origin, to: nil) // var position = NSEvent.mouseLocation position.x += winFrame.origin.x position.y += winFrame.origin.y position.y += positioningRect.size.height position.y += 26 // 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 } } extension KMEditPDFPopToolBarWindow: KMInterfaceThemeChangedProtocol { func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { self.appearance = .init(named: appearance) self.contentViewController?.interfaceThemeDidChanged(appearance) } }