// // KMPDFEditToolbar.swift // PDF Master // // Created by tangchao on 2023/5/4. // import Cocoa enum KMPageEditType: Int { case pageRange = 1 case leftRotate case rightRotate case insert case extract case replace case split case reverse case delete case zoomOut case zoomIn static var insert_files: Int { get { return self.insert.rawValue + 1000 } } static var insert_blank_page: Int { get { return self.insert.rawValue + 1001 } } static var insert_custom_page: Int { get { return self.insert.rawValue + 1002 } } } class KMPDFEditToolbar: NSView { private var toolBar_ = KMToolbar() var toolBar: KMToolbar { get { return self.toolBar_ } } private var zoomOutButton_: NSButton? private var zoomInButton_: NSButton? open var zoomOutButton: NSButton? { get { return self.zoomOutButton_ } } open var zoomInButton: NSButton? { get { return self.zoomInButton_ } } private var pageRangeView_: KMDesignSelect? open var pageRangeView: KMDesignSelect? { get { return self.pageRangeView_ } } private var pageRangeViewItems = [KMTools.pageRangeTypeString(pageRange: .all), KMTools.pageRangeTypeString(pageRange: .odd), KMTools.pageRangeTypeString(pageRange: .even), KMTools.pageRangeTypeString(pageRange: .horizontal), KMTools.pageRangeTypeString(pageRange: .vertical), KMTools.pageRangeTypeString(pageRange: .custom)] var itemClick: KMItemClickBlock? var pageRangeValueDidChange: KMValueDidChangeBlock? override init(frame frameRect: NSRect) { super.init(frame: frameRect) self.initDefaultValue() self.initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) self.initDefaultValue() self.initSubViews() } public func initDefaultValue() { } public func initSubViews() { self.addSubview(self.toolBar) self.toolBar.frame = self.bounds self.toolBar.autoresizingMask = [.width, .height] self.toolBar.delegate = self let rightItemView = self.rightItemView self.addSubview(rightItemView) rightItemView.frame = NSMakeRect(NSWidth(self.frame)-80, 0, 80, NSHeight(self.frame)) rightItemView.autoresizingMask = [.minXMargin, .height] } // MARK: - Publick Methods public func setPageRangeString(_ string: String) { self.pageRangeView?.stringValue = string self.pageRangeView?.editable = true } // MARK: - Actions @objc private func zoomOutButtonAction() { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.zoomOut.rawValue) } @objc private func zoomInButtonAction() { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.zoomIn.rawValue) } // MARK: toolBar 菜单事件 @objc func pageRangeMenuAction(sender: KMToolbarClickButton) { } @objc func leftRotateMenuAction(sender: KMToolbarClickButton?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.leftRotate.rawValue) } @objc func rightRotateMenuAction(sender: KMToolbarClickButton?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.rightRotate.rawValue) } @objc func insertMenuAction(sender: KMToolbarClickButton) { let menu = NSMenu() let insertFileItem = NSMenuItem(title: NSLocalizedString("Insert File", comment: ""), action: #selector(item_insertFile), target: self) menu.addItem(insertFileItem!) let insertBlankPageItem = NSMenuItem(title: NSLocalizedString("Insert a blank page", comment: ""), action: #selector(item_insertBlankPage), target: self) menu.addItem(insertBlankPageItem!) let insertCustomPageItem = NSMenuItem(title: NSLocalizedString("Insert custom page", comment: ""), action: #selector(item_insertCustomPage), target: self) menu.addItem(insertCustomPageItem!) menu.popUp(positioning: nil, at: NSPoint(x: 0, y: 40), in: sender) } @objc func extractMenuAction(sender: KMToolbarClickButton?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.extract.rawValue) } @objc func replaceMenuAction(sender: KMToolbarClickButton?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.replace.rawValue) } @objc func splitMenuAction(sender: KMToolbarClickButton) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.split.rawValue) } @objc func reverseMenuAction(sender: KMToolbarClickButton) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.reverse.rawValue) } @objc func deleteMenuAction(sender: KMToolbarClickButton) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.delete.rawValue) } @objc func item_insertFile(sender: NSMenuItem?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.insert_files) } @objc func item_insertBlankPage(sender: NSMenuItem?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.insert_blank_page) } @objc func item_insertCustomPage(sender: NSMenuItem?) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.insert_custom_page) } private lazy var rightItemView: NSView = { let view = NSView() let size: CGFloat = 28 let y: CGFloat = 6 let zoomOutButton = NSButton() view.addSubview(zoomOutButton) zoomOutButton.frame = NSMakeRect(0, y, size, size) zoomOutButton.isBordered = false zoomOutButton.image = NSImage(named: "KMImageNamePageEditZoomOut") zoomOutButton.target = self zoomOutButton.action = #selector(zoomOutButtonAction) self.zoomOutButton_ = zoomOutButton let zoomInButton = NSButton() view.addSubview(zoomInButton) zoomInButton.frame = NSMakeRect(size+7, y, size, size) zoomInButton.isBordered = false zoomInButton.image = NSImage(named: "KMImageNamePageEditZoomIn") zoomInButton.target = self zoomInButton.action = #selector(zoomInButtonAction) self.zoomInButton_ = zoomInButton return view }() } // MARK: - KMNewToolbarDelegate extension KMPDFEditToolbar: KMNewToolbarDelegate { func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbar!) -> [String]! { return [KMToolbarPageEditPageRangeItemIdentifier, KMToolbarPageEditLeftRotateItemIdentifier, KMToolbarPageEditRightRotateItemIdentifier, // KMToolbarDividerItemIdentifier, // KMToolbarPageEditInsetItemIdentifier, // KMToolbarPageEditExtractItemIdentifier, // KMToolbarPageEditReplaceItemIdentifier, // KMToolbarPageEditSplitItemIdentifier, // KMToolbarPageEditReverseItemIdentifier, KMToolbarDividerItemIdentifier, KMToolbarPageEditDeleteItemIdentifier] } func toolbar(_ toolbar: KMToolbar!, itemForItemIdentifier identifier: String!) -> KMToolBoxItem! { let item = KMToolBoxItem(itemIdentifier: identifier) if (identifier == KMToolbarPageEditPageRangeItemIdentifier) { // item?.image = NSImage(named: "KMImageNameMainToolbarrRight") item?.alternateImage = NSImage(named: "KMImageNameMainToolbarrRight") item?.target = self item?.btnTag = KMPageEditType.pageRange.rawValue // item?.toolTip = NSLocalizedString("pageRange", comment: "") item?.titleName = NSLocalizedString("pageRange ", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(pageRangeMenuAction) let view = NSView() item?.addSubview(view) view.frame = item!.bounds view.autoresizingMask = [.width,.height] view.wantsLayer = true view.layer?.backgroundColor = NSColor(hex: "#F7F8FA").cgColor let comBoxView = KMDesignSelect(withType: .Combox) view.addSubview(comBoxView.view) comBoxView.view.frame = NSMakeRect(0, 8, NSWidth(view.frame), 24) comBoxView.view.autoresizingMask = [.width] comBoxView.removeAllItems() comBoxView.addItems(withObjectValues: self.pageRangeViewItems) comBoxView.selectItem(at: 0) comBoxView.placeholderString = NSLocalizedString("eg .1,3-5,10", comment: "") comBoxView.editable = false comBoxView.delete = self self.pageRangeView_ = comBoxView let menuItem = NSMenuItem.init(title: "pageRange", action: #selector(pageRangeMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditLeftRotateItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageLeftRotate") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageLeftRotate") item?.target = self item?.btnTag = KMPageEditType.leftRotate.rawValue item?.toolTip = NSLocalizedString("Rotate Left", comment: "") item?.titleName = NSLocalizedString("", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(leftRotateMenuAction) let menuItem = NSMenuItem.init(title: "", action: #selector(leftRotateMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditRightRotateItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageRightRotate") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageRightRotate") item?.target = self item?.btnTag = KMPageEditType.rightRotate.rawValue item?.toolTip = NSLocalizedString("Rotate Right", comment: "") item?.titleName = NSLocalizedString("", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(rightRotateMenuAction) let menuItem = NSMenuItem.init(title: "", action: #selector(rightRotateMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditInsetItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageInsert") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageInsert") item?.target = self item?.btnTag = KMPageEditType.insert.rawValue item?.toolTip = NSLocalizedString("Insert", comment: "") item?.titleName = NSLocalizedString("Insert", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(insertMenuAction) let menuItem = NSMenuItem.init(title: "Insert", action: #selector(insertMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditExtractItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageExtract") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageExtract") item?.target = self item?.btnTag = KMPageEditType.extract.rawValue item?.toolTip = NSLocalizedString("Extract", comment: "") item?.titleName = NSLocalizedString("Extract", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(extractMenuAction) let menuItem = NSMenuItem.init(title: "Extract", action: #selector(extractMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditReplaceItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageReplace") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageReplace") item?.target = self item?.btnTag = KMPageEditType.replace.rawValue item?.toolTip = NSLocalizedString("Replace", comment: "") item?.titleName = NSLocalizedString("Replace", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(replaceMenuAction) let menuItem = NSMenuItem.init(title: "Replace", action: #selector(replaceMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditSplitItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageSplit") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageSplit") item?.target = self item?.btnTag = KMPageEditType.split.rawValue item?.toolTip = NSLocalizedString("Split the PDF into multiple files", comment: "") item?.titleName = NSLocalizedString("Split", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(splitMenuAction) let menuItem = NSMenuItem.init(title: "Split", action: #selector(splitMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditReverseItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageReverse") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageReverse") item?.target = self item?.btnTag = KMPageEditType.reverse.rawValue item?.toolTip = NSLocalizedString("Reverse", comment: "") item?.titleName = NSLocalizedString("Reverse", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(reverseMenuAction) let menuItem = NSMenuItem.init(title: "Reverse", action: #selector(reverseMenuAction), target: self) item?.menuFormRepresentation = menuItem } else if (identifier == KMToolbarPageEditDeleteItemIdentifier) { item?.image = NSImage(named: "KMImageNameMainToolPageDelete") item?.alternateImage = NSImage(named: "KMImageNameMainToolPageDelete") item?.target = self item?.btnTag = KMPageEditType.delete.rawValue item?.toolTip = NSLocalizedString("Delete", comment: "") item?.titleName = NSLocalizedString("", comment: "") item?.boxImagePosition = KMToolBoxImagePosition.left item?.btnAction = #selector(deleteMenuAction) let menuItem = NSMenuItem.init(title: "Delete", action: #selector(deleteMenuAction), target: self) item?.menuFormRepresentation = menuItem } return item } } // MARK: - KMSelectPopButtonDelegate extension KMPDFEditToolbar: KMSelectPopButtonDelegate { func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) { guard let callback = self.itemClick else { return } callback(self, KMPageEditType.pageRange.rawValue) } func km_controlTextDidChange(_ obj: KMDesignSelect) { } func km_controlTextDidEndEditing(_ obj: KMDesignSelect) { guard let callback = self.pageRangeValueDidChange else { return } callback(self.pageRangeView?.stringValue, nil) } }