// // KMLongerButton.swift // Cisdem PDFMaster // // Created by kdanmobile on 2023/10/26. // import Cocoa class KMLongerButton: NSButton,KMCustomButtonPopMenuViewControllerDelegate,KMCustomButtonPopMenuViewControllerDataSources,NSPopoverDelegate{ weak var containerViewController: KMBatchOperateLeftViewController? var isImageToPDF = false var popOver: NSPopover? var buttonLayer: CALayer? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) } override var intrinsicContentSize: NSSize { var size = super.intrinsicContentSize size.width += 20 return size } override func awakeFromNib() { super.awakeFromNib() self.newTrackingArea() self.wantsLayer = true self.buttonLayer = CALayer() self.layer?.addSublayer(self.buttonLayer!) self.buttonLayer?.frame = CGRect(x: 0, y: 0, width: CGRectGetWidth(self.bounds), height: CGRectGetHeight(self.bounds)) self.buttonLayer?.backgroundColor = KMAppearance.KM_000000_Color5DarkChange01().cgColor self.buttonLayer?.cornerRadius = 1 self.buttonLayer?.isHidden = true } func newTrackingArea() { let trackingArea = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways], owner: self, userInfo: nil) self.addTrackingArea(trackingArea) } func showPop(sender: NSView) { let menuViewController = KMCustomButtonPopMenuViewController() menuViewController.delegate = self menuViewController.dataSources = self self.popOver = NSPopover() self.popOver?.delegate = self self.popOver?.contentViewController = menuViewController self.popOver?.animates = false self.popOver?.behavior = .semitransient self.popOver?.contentSize = menuViewController.view.frame.size var sourcesRect = sender.bounds sourcesRect = sender.convert(sourcesRect, to: nil) sourcesRect.origin.y -= 20 sourcesRect.size.height += 20 self.window?.popover = self.popOver self.window?.sourcesRect = sourcesRect self.popOver?.show(relativeTo: self.bounds.insetBy(dx: 0, dy: 5), of: self, preferredEdge: .maxY) } func closePop() { popOver?.close() popOver = nil window?.popover = nil } override func mouseEntered(with event: NSEvent) { super.mouseEntered(with: event) self.showPop(sender: self) self.buttonLayer?.isHidden = false } override func mouseExited(with event: NSEvent) { super.mouseExited(with: event) self.buttonLayer?.isHidden = true } override func mouseDown(with event: NSEvent) { super.mouseDown(with: event) } override func mouseUp(with event: NSEvent) { super.mouseUp(with: event) } func customViewButtonPopDidSelectIndex(_ index: Int) { self.closePop() if isImageToPDF { if index == 0 { containerViewController?.chooseFile() } else if index == 1 { containerViewController?.chooseFileFromCamera() } else { containerViewController?.chooseFileFromScanner() } } else { if index == 0 { containerViewController?.chooseFile() } else { containerViewController?.addOpendFile() } } } func numberOfLine() -> Int { if (self.isImageToPDF) { return 3; } else { return 2; } } func stringForLine(at index: Int) -> String? { if self.isImageToPDF { let arr = [String.localizedStringWithFormat(NSLocalizedString("Import From %@", comment: ""), NSLocalizedString("My Mac", comment: "")), NSLocalizedString("Import From Camera", comment: ""), NSLocalizedString("Import From Scanner", comment: "")] return arr[index] } else { let arr = [NSLocalizedString("Select Files", comment: ""), NSLocalizedString("Add Recent Files", comment: "")] return arr[index] } } func needInsertSeperateLine(at index: Int) -> Bool { return false } func needHightLightLine(at index: Int) -> Bool { return false } func itemEnable(at index: Int) -> Bool { if self.isImageToPDF { return true } else { if index == 0 { return true } else { var hasOpenPDF = false let documentArray = NSDocumentController.shared.documents for i in 0..