123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- //
- // KMPDFEditToolbar.swift
- // PDF Master
- //
- // Created by tangchao on 2023/5/4.
- //
- import Cocoa
- enum KMPageEditType: Int {
- case none = 0
- case pageRange = 1
- case leftRotate
- case rightRotate
- case insert
- case extract
- case replace
- case split
- case reverse
- case delete
-
- case zoomOut
- case zoomIn
-
- case paste = 100
-
- // static let none: KMPageEditType = KMPageEditType(rawValue: 0)!
- // static let paste: KMPageEditType = KMPageEditType(rawValue: 100)!
-
- 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<KMPDFEditToolbar>?
- var pageRangeValueDidChange: KMValueDidChangeBlock?
-
- lazy var insertItemStrings: [String] = {
- return [NSLocalizedString("Insert a Blank Page", comment: ""),
- NSLocalizedString("Insert Custom Page", comment: ""),
- NSLocalizedString("Insert File", comment: "")]
- }()
-
- private weak var popover: NSPopover?
-
- 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
- }
-
- public func getItem(for itemId: String) -> KMToolBoxItem? {
- return self.toolBar.item(forItemIdentifier: itemId)
- }
-
- public func selectItem(for itemId: String) {
- self.getItem(for: itemId)?.isSelected = true
- }
-
- public func unSelectItem(for itemId: String) {
- self.getItem(for: itemId)?.isSelected = false
- }
-
- // 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) {
- guard let callback = self.itemClick else {
- return
- }
- callback(self, KMPageEditType.insert.rawValue)
-
- if let _popover = self.popover, _popover.isShown {
- self.popover?.close()
- self.popover = nil
- return
- }
-
- let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(self.insertItemStrings)
- let popover: NSPopover = NSPopover.init()
- self.popover = popover
- popover.contentViewController = vc
- popover.animates = true
- popover.behavior = .semitransient
- popover.setValue(true, forKey: "shouldHideAnchor")
- popover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
- vc.downCallback = { [weak self] downEntered, count in
- self?.popover?.close()
- self?.popover = nil
-
- if (!downEntered) {
- return
- }
- guard let index = self?.insertItemStrings.firstIndex(of: count) else {
- return
- }
- if (index == 0) {
- self?.item_insertBlankPage(sender: nil)
- } else if (index == 1) {
- self?.item_insertCustomPage(sender: nil)
- } else if (index == 2) {
- self?.item_insertFile(sender: nil)
- }
- }
- }
-
- @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 zoomInButton = NSButton()
- view.addSubview(zoomInButton)
- zoomInButton.frame = NSMakeRect(0, y, size, size)
- zoomInButton.isBordered = false
- zoomInButton.image = NSImage(named: "KMImageNamePageEditZoomIn")
- zoomInButton.target = self
- zoomInButton.action = #selector(zoomInButtonAction)
- self.zoomInButton_ = zoomInButton
-
- let zoomOutButton = NSButton()
- view.addSubview(zoomOutButton)
- zoomOutButton.frame = NSMakeRect(size+7, y, size, size)
- zoomOutButton.isBordered = false
- zoomOutButton.image = NSImage(named: "KMImageNamePageEditZoomOut")
- zoomOutButton.target = self
- zoomOutButton.action = #selector(zoomOutButtonAction)
- self.zoomOutButton_ = zoomOutButton
-
- 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.km_init(hex: "#F7F8FA").cgColor
-
- let comBoxView = KMDesignSelect(withType: .Combox)
- view.addSubview(comBoxView.view)
- comBoxView.view.frame = NSMakeRect(0, 0.5, NSWidth(view.frame), 21)
- 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.expandLeft
- 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)
- }
- }
|