123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- //
- // KMLongerButton.swift
- // PDF Reader Pro
- //
- // 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: NSWidth(self.bounds), height: NSHeight(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..<documentArray.count {
- let document = documentArray[i]
- let path = document.fileURL?.path
- if path == nil {
- return false
- }
- let UTTypeString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, path!.customPathExtension as CFString, nil)?.takeRetainedValue() as String?
- let TypeCFString = UTTypeString as CFString?
- if UTTypeConformsTo(TypeCFString!, kUTTypePDF) {
- hasOpenPDF = true
- break
- }
- }
-
- return hasOpenPDF
- }
- }
- }
-
- }
|