KMToolbarViewController.swift 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. //
  2. // KMToolbarViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/12/2.
  6. //
  7. import Cocoa
  8. @objc enum KMToolbarViewType : Int {
  9. case None
  10. case Main
  11. case Annatiton
  12. case Page
  13. case Conversion
  14. case ScanOCR
  15. case editPDF
  16. case Form
  17. case FillSign
  18. case Tool
  19. case redact
  20. case Move
  21. case Magnify
  22. case Select
  23. case SelectZoom
  24. case LeftPanel
  25. case RightPanel
  26. case viewSetting
  27. case AITools
  28. func isToolMode() -> Bool {
  29. return (self == .Move || self == .Magnify || self == .Select || self == .SelectZoom)
  30. }
  31. func itemIdentifier() -> String {
  32. if (self == .Annatiton) {
  33. return KMDocumentAnnotationToolbarItemIdentifier
  34. } else if (self == .Page) {
  35. return KMDocumentPageToolbarItemIdentifier
  36. } else if (self == .Conversion) {
  37. return KMDocumentConversonToolbarItemIdentifier
  38. } else if (self == .ScanOCR) {
  39. return KMDocumentScanOCRToolbarItemIdentifier
  40. } else if (self == .editPDF) {
  41. return KMDocumentEditToolbarItemIdentifier
  42. } else if (self == .Form) {
  43. return KMDocumentFormToolbarItemIdentifier
  44. } else if (self == .FillSign) {
  45. return KMDocumentFillSginToolbarItemIdentifier
  46. } else if (self == .Tool) {
  47. return KMDocumentToolToolbarItemIdentifier
  48. } else if (self == .Move) {
  49. return KMToolbarMoveToolModeItemIdentifier
  50. } else if (self == .Magnify) {
  51. return KMToolbarMagnifyToolModeItemIdentifier
  52. } else if (self == .Select) {
  53. return KMToolbarSelectToolModeItemIdentifier
  54. } else if (self == .SelectZoom) {
  55. return KMToolbarZoomToSelectionItemIdentifier
  56. } else if (self == .LeftPanel) {
  57. return KMLeftControlToolbarItemIdentifier
  58. } else if (self == .RightPanel) {
  59. return KMRightControlToolbarItemIdentifier
  60. } else if (self == .AITools) {
  61. return KMDocumentAIToolsToolbarItemIdentifier
  62. }
  63. return ""
  64. }
  65. }
  66. @objc enum KMToolbarType: Int {
  67. /// conversion
  68. case word = 3000
  69. case excel = 3001
  70. case ppt = 3002
  71. case rtf = 3003
  72. case csv = 3004
  73. case html = 3005
  74. case conversion_text = 3006
  75. case conversion_image = 3007
  76. /// tool
  77. case compress = 8000
  78. case merge = 8001
  79. case fileCompare = 8002
  80. case secure = 8003
  81. case redact = 8004
  82. case watermark = 8005
  83. case background = 8006
  84. case headerAndFooter = 8007
  85. case bates = 8008
  86. case crop = 8009
  87. case comparison = 8010
  88. case aiTools = 8011
  89. }
  90. @objc protocol KMToolbarViewControllerDelegate {
  91. @objc optional func changeAnnotationModeAction(item:KMToolbarClickButton)
  92. @objc optional func showPDFLayoutModeAction(show:Bool)
  93. @objc optional func changePDFViewZoomInAction()
  94. @objc optional func changePDFViewZoomOutAction()
  95. @objc optional func changePDFViewGotoNextPageAction()
  96. @objc optional func changePDFViewGoToPreviousPageAction()
  97. @objc optional func changePDFViewGotoBackAction()
  98. @objc optional func changePDFViewGoToForwardAction()
  99. @objc optional func aiTranslationPDFFileAction()
  100. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomSting : String)
  101. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, zoomModel selectedTag:Int)
  102. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareAction toolbarItem: KMToolbarItemView)
  103. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareDocument item:NSMenuItem)
  104. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareFlatten item:NSMenuItem)
  105. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, shareOriginalPDF item:NSMenuItem)
  106. @objc optional func toolbarViewController(_ viewController: KMToolbarViewController, clickMode toolMode: KMToolbarViewType, toolbar toolbarItem: KMToolbarItemView, _ pages: [Int])
  107. @objc optional func toolbarViewController(_ viewController: KMToolbarViewController, clickChaildToolType: KMToolbarType, toolbar toolbarItem: KMToolbarItemView)
  108. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, scanOCRModel selectedTag:Int)
  109. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, itemDidClick toolbarItem: KMToolbarItemView)
  110. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, searchAction searchString: String, forward: Bool)
  111. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, findSearchAction searchString: String, forward: Bool)
  112. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, menuItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?)
  113. @objc optional func toolbarViewController(_ viewController:KMToolbarViewController, viewItemDidClick toolbarItem: KMToolbarItemView, index: Int, info: Any?)
  114. }
  115. class KMToolbarViewController: NSViewController, NSTextFieldDelegate {
  116. var toolbar : KMToolbarView?
  117. var toolbarType : KMToolbarViewType = KMToolbarViewType.None
  118. var toolbarItems : [String : Any] = [:]
  119. @IBOutlet weak var mainWindowController : MainWindowController!
  120. open weak var delegate: KMToolbarViewControllerDelegate?
  121. @IBOutlet var zoomButton: NSPopUpButton!
  122. @IBOutlet var zoomView: NSView!
  123. @IBOutlet weak var zoomTextField: NSTextField!
  124. var searchField: NSSearchField = NSSearchField()
  125. weak var pdfView: CPDFListView?
  126. var shareButton = KMToolbarClickButton()
  127. private var bottomLine = NSView()
  128. // 是否显示所有注释
  129. var isShowAllAnnotations = true
  130. var ignoreCurrentAnnotationTypeChange = false
  131. private var _currentItemView: KMToolbarItemView?
  132. override func viewDidLoad() {
  133. super.viewDidLoad()
  134. self.zoomTextField.delegate = self
  135. for item in self.zoomButton.itemArray {
  136. let string = item.title
  137. item.title = NSLocalizedString(string, comment: "")
  138. }
  139. NotificationCenter.default.addObserver(self, selector: #selector(listViewAnnotationTypeChangeNotification), name: Notification.Name("CPDFListViewAnnotationTypeChangeNotification"), object: nil)
  140. NotificationCenter.default.addObserver(self, selector: #selector(listViewAnnotationTypeChangeNotification), name: Notification.Name("CPDFListViewToolModeChangeNotification"), object: nil)
  141. }
  142. override func loadView() {
  143. super.loadView()
  144. self.toolbar = KMToolbarView.init(identifier: "KMToolbar_New")
  145. self.toolbar?.frame = self.view.bounds
  146. self.toolbar?.autoresizingMask = [.width,.height]
  147. self.toolbar?.delegate = self
  148. self.view.addSubview(self.toolbar!)
  149. self.view.addSubview(self.bottomLine)
  150. self.bottomLine.frame = NSMakeRect(0, 0, NSWidth(self.view.bounds), 1)
  151. self.bottomLine.autoresizingMask = [.width]
  152. self.bottomLine.wantsLayer = true
  153. self.bottomLine.layer?.backgroundColor = NSColor.km_init(hex: "#0000001A").cgColor
  154. }
  155. func updateType(newType:KMToolbarViewType) {
  156. if newType != toolbarType {
  157. toolbarType = newType
  158. if self.toolbar != nil && self.toolbar?.superview != nil {
  159. self.toolbar?.removeFromSuperview()
  160. }
  161. self.toolbar = KMToolbarView.init(identifier: "KMToolbar_New")
  162. self.toolbar?.frame = self.view.bounds
  163. self.toolbar?.autoresizingMask = [.width,.height]
  164. self.toolbar?.delegate = self
  165. self.view.addSubview(self.toolbar!)
  166. }
  167. }
  168. func reloadateToolbar() -> Void {
  169. self.toolbar?.reloadData()
  170. }
  171. func resetToolbar() -> Void {
  172. for item in self.toolbar?.items ?? [] {
  173. item.isSelected = false
  174. }
  175. }
  176. // MARK: Action
  177. @IBAction func changeAnnotationMode(item: KMToolbarClickButton) {
  178. self.delegate?.changeAnnotationModeAction?(item: item)
  179. if item.isKind(of: KMToolbarClickButton.self) {
  180. self._itemSetPromptFlag(item: item.clickObject as? KMToolbarItemView)
  181. } else {
  182. }
  183. }
  184. @IBAction func leftControllButtonAction(item: KMToolbarClickButton) {
  185. let tag = KMToolbarViewType(rawValue: item.tag) ?? .None
  186. if tag == .editPDF {
  187. if IAPProductsManager.default().isAvailableAllFunction() == false {
  188. KMPreference.shared.openDocumentType = .newWindow
  189. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  190. return
  191. }
  192. let allowsPrinting = self.pdfView?.document?.allowsPrinting ?? false
  193. let allowsCopying = self.pdfView?.document?.allowsCopying ?? false
  194. if !allowsPrinting || !allowsCopying {
  195. let alert = NSAlert()
  196. alert.alertStyle = .critical
  197. alert.messageText = NSLocalizedString("This is a secured document. Editing is not permitted.", comment: "")
  198. alert.runModal()
  199. return
  200. }
  201. Task { @MainActor in
  202. self.delegate?.toolbarViewController?(self, clickMode: tag, toolbar: item.clickObject as! KMToolbarItemView, [])
  203. }
  204. } else {
  205. self.delegate?.toolbarViewController?(self, clickMode: tag, toolbar: item.clickObject as! KMToolbarItemView, [])
  206. }
  207. self._itemSetPromptFlag(item: item.clickObject as? KMToolbarItemView)
  208. }
  209. @objc private func _itemAction(_ item: KMToolbarClickButton) {
  210. if item.clickObject?.itemIdentifier == KMAnnotationImageToolbarItemIdentifier || item.clickObject?.itemIdentifier == KMAnnotationTableToolbarItemIdentifier {
  211. self.pdfView?.toolMode = .textToolMode
  212. self.pdfView?.annotationType = .unkown
  213. }
  214. if let data = item.clickObject as? KMToolbarItemView {
  215. if data.itemIdentifier == KMToolbarComparisonItemIdentifier {
  216. self._currentItemView = data
  217. if let subMenu = data.menuFormRepresentation?.submenu {
  218. subMenu.popUp(positioning: nil, at: item.frame.origin, in: item.superview)
  219. }
  220. return
  221. }
  222. }
  223. if let itemView = item.clickObject as? KMToolbarItemView {
  224. if itemView.itemIdentifier == KMDocumentRedactToolbarItemIdentifier {
  225. self.delegate?.toolbarViewController?(self, clickMode: .redact, toolbar: itemView, [])
  226. } else {
  227. self.delegate?.toolbarViewController?(self, itemDidClick: itemView)
  228. }
  229. self._itemSetPromptFlag(item: itemView)
  230. }
  231. }
  232. @objc private func OCRbtnAction1(_ item: KMToolbarClickButton) {
  233. FMTrackEventManager.defaultManager.trackEvent(event: "Tbr", withProperties: ["Tbr_Btn": "Btn_Tbr_OCR"])
  234. self._itemAction(item)
  235. }
  236. @objc private func OCRbtnAction2(_ item: KMToolbarClickButton) {
  237. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_Converter", withProperties: ["SubTbr_Btn": "Btn_SubTbr_Converter_OCR"])
  238. self._itemAction(item)
  239. }
  240. @objc private func _menuItemAction(_ item: NSMenuItem) {
  241. guard let itemId = item.representedObject as? String else {
  242. NSSound.beep()
  243. return
  244. }
  245. if itemId == KMDocumentConversonToolbarItemIdentifier {
  246. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  247. self.leftControllButtonAction(item: item_.clickButton)
  248. self._itemSetPromptFlag(item: item_)
  249. } else if itemId == KMDocumentRedactToolbarItemIdentifier {
  250. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  251. self._itemAction(item_.clickButton)
  252. self._itemSetPromptFlag(item: item_)
  253. } else if itemId == KMDocumentAnnotationToolbarItemIdentifier {
  254. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  255. self.leftControllButtonAction(item: item_.clickButton)
  256. self._itemSetPromptFlag(item: item_)
  257. } else if itemId == KMDocumentPageToolbarItemIdentifier {
  258. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  259. self.leftControllButtonAction(item: item_.clickButton)
  260. self._itemSetPromptFlag(item: item_)
  261. } else if itemId == KMDocumentToolToolbarItemIdentifier {
  262. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  263. self.leftControllButtonAction(item: item_.clickButton)
  264. self._itemSetPromptFlag(item: item_)
  265. } else if itemId == KMDocumentPresentationToolbarItemIdentifier {
  266. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  267. self._itemAction(item_.clickButton)
  268. self._itemSetPromptFlag(item: item_)
  269. } else {
  270. let item_ = self.toolbarItemFindItemIdentifiers(value: itemId)
  271. self.delegate?.toolbarViewController?(self, menuItemDidClick: item_, index: 0, info: nil)
  272. self._itemSetPromptFlag(item: item_)
  273. }
  274. }
  275. @objc private func _comparisonAction(sender: NSMenuItem) {
  276. if let data = self._currentItemView {
  277. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  278. } else {
  279. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarComparisonItemIdentifier), index: sender.tag, info: nil)
  280. }
  281. self._itemSetPromptFlag(itemIdentifier: KMToolbarComparisonItemIdentifier)
  282. }
  283. @IBAction func showPDFLayoutMode(sender: KMToolbarClickButton) {
  284. if let item = sender.clickObject as? KMToolbarItemView {
  285. item.isSelected = !item.isSelected
  286. self.delegate?.showPDFLayoutModeAction?(show: true)
  287. self._itemSetPromptFlag(item: item)
  288. }
  289. }
  290. @objc func cropMenuAction(item: KMToolbarItemView) {
  291. self.delegate?.toolbarViewController?(self, clickChaildToolType: .crop, toolbar: item)
  292. }
  293. @objc private func _cropAction(_ sender: NSMenuItem) {
  294. if let data = self._currentItemView {
  295. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  296. } else {
  297. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarCropIdentifier), index: sender.tag, info: nil)
  298. }
  299. self._itemSetPromptFlag(itemIdentifier: KMToolbarCropIdentifier)
  300. }
  301. @objc func batesMenuAction(sender: KMToolbarClickButton) {
  302. self._currentItemView = sender.clickObject as? KMToolbarItemView
  303. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  304. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  305. }
  306. }
  307. @objc private func _batesAction(sender: NSMenuItem) {
  308. if let data = self._currentItemView {
  309. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  310. } else {
  311. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolBatesItemIdentifier), index: sender.tag, info: nil)
  312. }
  313. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  314. }
  315. @objc private func _AIAction(sender: NSMenuItem) {
  316. if let data = self._currentItemView {
  317. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  318. } else {
  319. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarAIItemIdentifier), index: sender.tag, info: nil)
  320. }
  321. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolBatesItemIdentifier)
  322. }
  323. @objc func headerfooterMenuAction(sender: KMToolbarClickButton) {
  324. self._currentItemView = sender.clickObject as? KMToolbarItemView
  325. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  326. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  327. }
  328. }
  329. @objc private func _headerfooterAction(sender: NSMenuItem) {
  330. if let data = self._currentItemView {
  331. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  332. } else {
  333. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolHeaderFooterItemIdentifier), index: sender.tag, info: nil)
  334. }
  335. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolHeaderFooterItemIdentifier)
  336. }
  337. @objc func backgroundMenuAction(sender: KMToolbarClickButton) {
  338. self._currentItemView = sender.clickObject as? KMToolbarItemView
  339. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  340. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  341. }
  342. }
  343. @objc private func _backgroundAction(sender: NSMenuItem) {
  344. if let data = self._currentItemView {
  345. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  346. } else {
  347. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolBackgroundItemIdentifier), index: sender.tag, info: nil)
  348. }
  349. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolBackgroundItemIdentifier)
  350. }
  351. @objc func watermarkMenuAction(sender: KMToolbarClickButton) {
  352. self._currentItemView = sender.clickObject as? KMToolbarItemView
  353. if let subMenu = (sender.clickObject as? KMToolbarItemView)?.menuFormRepresentation?.submenu {
  354. subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  355. }
  356. }
  357. @objc private func _watermarkAction(sender: NSMenuItem) {
  358. if let data = self._currentItemView {
  359. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  360. } else {
  361. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolWatermarkItemIdentifier), index: sender.tag, info: nil)
  362. }
  363. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolWatermarkItemIdentifier)
  364. }
  365. @objc func redactMenuAction(sender: KMToolbarClickButton) {
  366. self.delegate?.toolbarViewController?(self, clickChaildToolType: .redact, toolbar: sender.clickObject as! KMToolbarItemView)
  367. self._itemSetPromptFlag(item: sender.clickObject as? KMToolbarItemView)
  368. }
  369. @objc func compressMenuAction(sender: KMToolbarClickButton) {
  370. self.delegate?.toolbarViewController?(self, clickChaildToolType: .compress, toolbar: sender.clickObject as! KMToolbarItemView)
  371. self._itemSetPromptFlag(item: sender.clickObject as? KMToolbarItemView)
  372. }
  373. @objc func secureMenuAction(sender: KMToolbarClickButton) {
  374. self.delegate?.toolbarViewController?(self, clickChaildToolType: .secure, toolbar: sender.clickObject as! KMToolbarItemView)
  375. self._itemSetPromptFlag(item: sender.clickObject as? KMToolbarItemView)
  376. }
  377. @objc private func _secureAction(sender: NSMenuItem) {
  378. if let data = self._currentItemView {
  379. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  380. } else {
  381. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolSecureItemIdentifier), index: sender.tag, info: nil)
  382. }
  383. self._itemSetPromptFlag(itemIdentifier: KMToolbarToolSecureItemIdentifier)
  384. }
  385. @objc func conversionMenuItemAction(sender: KMToolbarClickButton) {
  386. if let item: KMToolbarItemView = sender.clickObject as? KMToolbarItemView {
  387. var type: KMToolbarType = .word
  388. if (item.btnTag == KMToolbarType.excel.rawValue) {
  389. type = .excel
  390. } else if (item.btnTag == KMToolbarType.ppt.rawValue) {
  391. type = .ppt
  392. } else if (item.btnTag == KMToolbarType.rtf.rawValue) {
  393. type = .rtf
  394. } else if (item.btnTag == KMToolbarType.csv.rawValue) {
  395. type = .csv
  396. } else if (item.btnTag == KMToolbarType.html.rawValue) {
  397. type = .html
  398. } else if (item.btnTag == KMToolbarType.conversion_text.rawValue) {
  399. type = .conversion_text
  400. } else if (item.btnTag == KMToolbarType.conversion_image.rawValue) {
  401. type = .conversion_image
  402. }
  403. self.delegate?.toolbarViewController?(self, clickChaildToolType: type, toolbar: item)
  404. self._itemSetPromptFlag(item: item)
  405. }
  406. }
  407. @objc private func _pdfToImageAction(sender: NSMenuItem) {
  408. if let data = self._currentItemView {
  409. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  410. } else {
  411. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarConversionImageItemIdentifier), index: sender.tag, info: nil)
  412. }
  413. self._itemSetPromptFlag(itemIdentifier: KMToolbarConversionImageItemIdentifier)
  414. }
  415. @objc private func _exportImageAction(sender: NSMenuItem) {
  416. if let data = self._currentItemView {
  417. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  418. } else {
  419. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarConversionExportImageItemIdentifier), index: sender.tag, info: nil)
  420. }
  421. self._itemSetPromptFlag(itemIdentifier: KMToolbarConversionExportImageItemIdentifier)
  422. }
  423. @objc private func _newPDFAction(sender: NSMenuItem) {
  424. if let data = self._currentItemView {
  425. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  426. } else {
  427. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarConversionCreatePDFItemIdentifier), index: sender.tag, info: nil)
  428. }
  429. self._itemSetPromptFlag(itemIdentifier: KMToolbarConversionCreatePDFItemIdentifier)
  430. }
  431. @IBAction func zoomModelSelectButtonAction(_ sender: NSPopUpButton) {
  432. self.delegate?.toolbarViewController?(self, zoomModel: sender.selectedTag())
  433. for item in sender.itemArray {
  434. item.state = .off
  435. }
  436. sender.selectedItem?.state = .on
  437. }
  438. @IBAction func zoomPDFViewButtonAction(sender:KMToolbarClickButton) {
  439. if sender.tag == 0 {
  440. self.delegate?.changePDFViewZoomOutAction?()
  441. } else if sender.tag == 1 {
  442. self.delegate?.changePDFViewZoomInAction?()
  443. } else if sender.tag == 2 {
  444. self.delegate?.changePDFViewGotoBackAction?()
  445. } else if sender.tag == 3 {
  446. self.delegate?.changePDFViewGoToForwardAction?()
  447. }
  448. }
  449. @IBAction func aiTranslationButtonAction(sender:KMToolbarClickButton) {
  450. self.delegate?.aiTranslationPDFFileAction?()
  451. }
  452. @IBAction func shareButtonAction(sender:KMToolbarClickButton) {
  453. self.shareButton = sender
  454. let menu = NSMenu()
  455. menu.addItem(withTitle: NSLocalizedString("Document", comment: ""), action: #selector(shareViaWithDocument), target: self)
  456. menu.addItem(withTitle: NSLocalizedString("Flattened Copy", comment: ""), action: #selector(shareViaWithFlatten), target: self)
  457. menu.addItem(withTitle: NSLocalizedString("Original PDF", comment: ""), action: #selector(shareViaWithOriginalPDF), target: self)
  458. menu.popUp(positioning: nil, at: NSPoint(x: sender.frame.origin.x, y: sender.frame.origin.y-10), in: sender.superview)
  459. self.delegate?.toolbarViewController?(self, shareAction: sender.clickObject as! KMToolbarItemView)
  460. }
  461. @IBAction func shareViaWithDocument(sender:NSMenuItem) {
  462. self.delegate?.toolbarViewController?(self, shareDocument: sender)
  463. }
  464. @IBAction func shareViaWithFlatten(sender:NSMenuItem) {
  465. self.delegate?.toolbarViewController?(self, shareFlatten: sender)
  466. }
  467. @IBAction func shareViaWithOriginalPDF(sender:NSMenuItem) {
  468. self.delegate?.toolbarViewController?(self, shareOriginalPDF: sender)
  469. }
  470. @objc func mergeMenuItemAction(sender: KMToolbarClickButton) {
  471. self.delegate?.toolbarViewController?(self, clickChaildToolType: .merge, toolbar: sender.clickObject as! KMToolbarItemView)
  472. }
  473. @IBAction func scanOCRModelSelect(sender:KMToolbarClickButton) {
  474. self.delegate?.toolbarViewController?(self, scanOCRModel: sender.tag)
  475. }
  476. // Form 对齐
  477. @IBAction func formsAlignmentAction(sender:KMToolbarClickButton) {
  478. if sender.tag == 0 {
  479. self.pdfView?.formsAnnotationAlign(.left)
  480. } else if sender.tag == 1 {
  481. self.pdfView?.formsAnnotationAlign(.vertical)
  482. } else if sender.tag == 2 {
  483. self.pdfView?.formsAnnotationAlign(.right)
  484. } else if sender.tag == 3 {
  485. self.pdfView?.formsAnnotationAlign(.top)
  486. } else if sender.tag == 4 {
  487. self.pdfView?.formsAnnotationAlign(.horizontally)
  488. } else if sender.tag == 5 {
  489. self.pdfView?.formsAnnotationAlign(.bottom)
  490. } else if sender.tag == 6 {
  491. self.pdfView?.formsAnnotationAlign(.disHorizontally)
  492. } else if sender.tag == 7 {
  493. self.pdfView?.formsAnnotationAlign(.disVertical)
  494. }
  495. }
  496. @objc private func _alignmentAction(sender: NSMenuItem) {
  497. if let data = self._currentItemView {
  498. self.delegate?.toolbarViewController?(self, menuItemDidClick: data, index: sender.tag, info: nil)
  499. } else {
  500. self.delegate?.toolbarViewController?(self, menuItemDidClick: self.toolbarItemFindItemIdentifiers(value: KMToolbarToolFormAlignIdentifier), index: sender.tag, info: nil)
  501. }
  502. }
  503. // Form 更多操作
  504. @IBAction func formsMoreOperationAction(sender:KMToolbarClickButton) {
  505. }
  506. func toolbarItemFindItemIdentifiers(value : String) -> KMToolbarItemView{
  507. for item in self.toolbarItems {
  508. if item.key == value {
  509. return item.value as! KMToolbarItemView
  510. }
  511. }
  512. return KMToolbarItemView()
  513. }
  514. func controlTextDidEndEditing(_ obj: Notification) {
  515. let textField = obj.object as? NSTextField
  516. if textField == self.zoomTextField {
  517. self.delegate?.toolbarViewController?(self, zoomSting: self.zoomTextField.stringValue)
  518. }
  519. }
  520. func controlTextDidChange(_ obj: Notification) {
  521. }
  522. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  523. switch commandSelector {
  524. case #selector(NSResponder.insertNewline(_:)):
  525. if let inputView = control as? NSTextField {
  526. // //当当前TextField按下enter
  527. if inputView == zoomTextField {
  528. KMPrint("按下 enter")
  529. self.delegate?.toolbarViewController?(self, zoomSting: self.zoomTextField.stringValue)
  530. } else if let _ = control as? NSSearchField {
  531. self.searchPDFKey(control.stringValue)
  532. }
  533. }
  534. return true
  535. default:
  536. return false
  537. }
  538. }
  539. // MARK: - Private Methods
  540. private func _itemSetPromptFlag(item: KMToolbarItemView?) {
  541. if let item_ = item {
  542. if let identifier = item_.promptIdentifier {
  543. item?.isShowPrompt = false
  544. KMDataManager.ud_set(true, forKey: identifier)
  545. }
  546. }
  547. }
  548. private func _itemSetPromptFlag(itemIdentifier: String?) {
  549. let item = self.toolbarItemFindItemIdentifiers(value: itemIdentifier ?? "")
  550. self._itemSetPromptFlag(item: item)
  551. }
  552. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  553. super.interfaceThemeDidChanged(appearance)
  554. self.toolbar?.appearance = .init(named: appearance)
  555. self.bottomLine.wantsLayer = true
  556. if KMAppearance.isDarkMode() {
  557. self.bottomLine.layer?.backgroundColor = NSColor.km_init(hex: "#393B3E", alpha: 1).cgColor
  558. } else {
  559. self.bottomLine.layer?.backgroundColor = NSColor.km_init(hex: "#0000001A").cgColor
  560. }
  561. }
  562. }
  563. // MARK: - Public Methods
  564. extension KMToolbarViewController {
  565. func itemAction(_ item: KMToolbarItemView) {
  566. self._itemAction(item.clickButton)
  567. }
  568. }
  569. extension KMToolbarViewController : KMToolbarViewDelegate,NSToolbarItemValidation {
  570. func toolbarLeftDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] {
  571. if toolbarType == .Main {
  572. if let model = KMDataManager.toolbar_getData() {
  573. return model.leftCellIdentifiers ?? []
  574. }
  575. return [KMLeftControlToolbarItemIdentifier,
  576. KMNewToolbarSpaceItemIdentifier, KMDocumentZoomViewToolbarItemIdentifier,
  577. KMDocumentPreviousPageToolbarItemIdentifier, KMDocumentHomeToolbarItemIdentifier]
  578. }
  579. return []
  580. }
  581. func toolbarRightDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] {
  582. if toolbarType == .Main {
  583. if let model = KMDataManager.toolbar_getData() {
  584. return model.rightCellIdentifiers ?? []
  585. }
  586. return [KMDocumentViewDisplayToolbarItemIdentifier, KMDocumentShareToolbarItemIdentifier,
  587. KMDocumentSearchToolbarItemIdentifier, KMRightControlToolbarItemIdentifier]
  588. } else if [.Annatiton,.Magnify,.Move,.Select,.SelectZoom].contains(toolbarType) {
  589. return [KMToolbarAnnotationSettingItemIdentifier]
  590. }
  591. return []
  592. }
  593. func toolbarAllowedItemIdentifiers(_ toolbar: KMToolbarView) -> [String] {
  594. switch toolbarType {
  595. case .None:
  596. return []
  597. case .Main:
  598. var defaultCellIdentifiers = mainToolIdentifiers
  599. if defaultCellIdentifiers.contains(KMDocumentHomeToolbarItemIdentifier) {
  600. defaultCellIdentifiers.removeObject(KMDocumentHomeToolbarItemIdentifier)
  601. }
  602. return defaultCellIdentifiers
  603. case .Annatiton,.Magnify,.Move,.Select,.SelectZoom:
  604. // return annotationToolIdentifiers
  605. return [KMNewToolbarSpaceItemIdentifier,
  606. KMToolbarPageEditInsetItemIdentifier, KMToolbarToolMergeItemIdentifier, KMToolbarPageEditSplitItemIdentifier,
  607. KMToolbarToolCompressItemIdentifier, KMToolbarToolSecureItemIdentifier, KMToolbarToolWatermarkItemIdentifier,
  608. KMToolbarToolBackgroundItemIdentifier, KMToolbarToolHeaderFooterItemIdentifier, KMToolbarToolBatesItemIdentifier,
  609. KMToolbarAIItemIdentifier,
  610. KMToolbarCropIdentifier,
  611. KMToolbarConversionWordItemIdentifier, KMToolbarConversionExcelItemIdentifier, KMToolbarConversionPPTItemIdentifier,
  612. KMToolbarConversionRTFItemIdentifier, KMToolbarConversionCSVItemIdentifier, KMToolbarConversionHTMLItemIdentifier,
  613. KMToolbarConversionTextItemIdentifier, KMToolbarConversionImageItemIdentifier,
  614. KMToolbarMoveToolModeItemIdentifier,
  615. KMToolbarMagnifyToolModeItemIdentifier,
  616. KMToolbarSelectToolModeItemIdentifier,
  617. KMToolbarZoomToSelectionItemIdentifier,
  618. KMNewToolbarSpaceItemIdentifier,
  619. KMToolbarDividerItemIdentifier,
  620. KMNewToolbarSpaceItemIdentifier,
  621. KMToolbarHighlightAnnotationItemIdentifier,
  622. KMToolbarUnderlineAnnotationItemIdentifier,
  623. KMToolbarStrikeOutAnnotationItemIdentifier,
  624. KMToolbarInkAnnotationItemIdentifier,
  625. KMToolbarFreeTextAnnotationItemIdentifier,
  626. KMToolbarAnchoredAnnotationItemIdentifier,
  627. KMToolbarSquareAnnotationItemIdentifier, KMToolbarCircleAnnotationItemIdentifier, KMToolbarArrowAnnotationItemIdentifier, KMToolbarLineAnnotationItemIdentifier,
  628. KMToolbarLinkAnnotationItemIdentifier,
  629. KMAnnotationStampToolbarItemIdentifier,
  630. KMToolbarSignSignatureAnnotationItemIdentifier,
  631. // KMToolbarDividerItemIdentifier,
  632. // KMToolbarShowToolbarItemIdentifier,
  633. KMToolbarToolRadioButtonItemIdentifier]
  634. case .editPDF:
  635. return editPDFItemIdentifiers
  636. case .Page:
  637. return pageEditToolIdentifiers
  638. case .Tool:
  639. return toolToolIdentifiers
  640. case .Form:
  641. return formToolIdentifiers
  642. case .FillSign:
  643. return fillSignToolIdentifiers
  644. case .Conversion:
  645. return conversionToolIdentifiers
  646. case .viewSetting:
  647. return [KMToolbarViewSettingIdentifier, KMToolbarCropIdentifier,
  648. KMToolbarSnapshotIdentifier, KMToolbarTTSIdentifier]
  649. default:
  650. break
  651. }
  652. return []
  653. }
  654. func toolbarDefaultItemIdentifiers(_ toolbar: KMToolbarView) -> [String] {
  655. switch toolbarType {
  656. case .None:
  657. return []
  658. case .Main:
  659. if let model = KMDataManager.toolbar_getData() {
  660. return model.centerCellIdentifiers ?? []
  661. }
  662. return [KMDocumentAnnotationToolbarItemIdentifier,
  663. KMDocumentPageToolbarItemIdentifier,
  664. KMDocumentToolToolbarItemIdentifier,
  665. KMDocumentConversonToolbarItemIdentifier,
  666. KMDocumentFormToolbarItemIdentifier,
  667. KMDocumentFillSginToolbarItemIdentifier,
  668. KMDocumentEditToolbarItemIdentifier,
  669. KMDocumentDigitalSignToolbarItemIdentifier,
  670. KMDocumentRedactToolbarItemIdentifier,
  671. KMDocumentScanOCRToolbarItemIdentifier,
  672. KMDocumentAIToolsToolbarItemIdentifier]
  673. case .Annatiton,.Magnify,.Move,.Select,.SelectZoom:
  674. let itemIdentifiers: [String]? = self.toolbar?.toolbarIdentifier != nil ? (UserDefaults.standard.object(forKey: self.toolbar?.toolbarIdentifier ?? "") as? [String]) : []
  675. if itemIdentifiers == nil || itemIdentifiers!.count <= 0 {
  676. return [KMToolbarMoveToolModeItemIdentifier,
  677. KMToolbarMagnifyToolModeItemIdentifier,
  678. KMToolbarSelectToolModeItemIdentifier,
  679. KMToolbarZoomToSelectionItemIdentifier,
  680. KMNewToolbarSpaceItemIdentifier,
  681. KMNewToolbarSpaceItemIdentifier,
  682. KMToolbarHighlightAnnotationItemIdentifier,
  683. KMToolbarUnderlineAnnotationItemIdentifier,
  684. KMToolbarStrikeOutAnnotationItemIdentifier,
  685. KMToolbarInkAnnotationItemIdentifier,
  686. KMToolbarFreeTextAnnotationItemIdentifier,
  687. KMToolbarAnchoredAnnotationItemIdentifier,
  688. KMToolbarSquareAnnotationItemIdentifier, KMToolbarCircleAnnotationItemIdentifier, KMToolbarArrowAnnotationItemIdentifier, KMToolbarLineAnnotationItemIdentifier,
  689. KMToolbarLinkAnnotationItemIdentifier,
  690. KMNewToolbarSpaceItemIdentifier, KMNewToolbarSpaceItemIdentifier,
  691. KMAnnotationStampToolbarItemIdentifier, KMAnnotationImageToolbarItemIdentifier,
  692. KMToolbarSignSignatureAnnotationItemIdentifier, KMAnnotationTableToolbarItemIdentifier]
  693. }
  694. return itemIdentifiers!
  695. case .editPDF:
  696. return [KMToolbarAddTextEditPDFItemIdentifier,
  697. KMToolbarAddImageEditPDFItemIdentifier]
  698. case .Page:
  699. return []
  700. case .Tool:
  701. return [KMToolbarToolCompressItemIdentifier, KMToolbarToolMergeItemIdentifier,
  702. KMToolbarToolSecureItemIdentifier, KMToolbarComparisonItemIdentifier,
  703. KMToolbarToolWatermarkItemIdentifier, KMToolbarToolBackgroundItemIdentifier,
  704. KMToolbarToolHeaderFooterItemIdentifier, KMToolbarToolBatesItemIdentifier]
  705. case .Form:
  706. return [KMToolbarToolTextFieldItemIdentifier,
  707. KMToolbarToolCheckBoxItemIdentifier,
  708. KMToolbarToolRadioButtonItemIdentifier,
  709. KMToolbarToolListBoxItemIdentifier,
  710. KMToolbarToolPullDownmenuItemIdentifier,
  711. KMToolbarToolButtonIdentifier,
  712. KMToolbarToolSignBoxIdentifier,
  713. KMToolbarDividerItemIdentifier,
  714. KMToolbarToolFormAlignIdentifier]
  715. case .FillSign:
  716. return [
  717. // KMToolbarToolTextIdentifier,
  718. KMToolbarToolHookItemIdentifier,
  719. KMToolbarToolForkItemIdentifier,
  720. KMToolbarToolRectangleIdentifier,
  721. KMToolbarToolLineItemIdentifier,
  722. KMToolbarToolDotItemIdentifier,
  723. KMToolbarToolDateIdentifier,
  724. KMToolbarToolProfileIdentifier,
  725. KMToolbarFillSignSignatureIdentifier]
  726. case .Conversion:
  727. return [KMToolbarConversionCreatePDFItemIdentifier,
  728. KMToolbarConversionWordItemIdentifier,
  729. KMToolbarConversionExcelItemIdentifier,
  730. KMToolbarConversionPPTItemIdentifier,
  731. KMToolbarConversionRTFItemIdentifier,
  732. KMToolbarConversionCSVItemIdentifier,
  733. KMToolbarConversionHTMLItemIdentifier,
  734. KMToolbarConversionTextItemIdentifier,
  735. KMToolbarConversionImageItemIdentifier,
  736. KMToolbarDividerItemIdentifier, KMToolbarConversionOCRIdentifier, KMToolbarConversionExportImageItemIdentifier]
  737. case .ScanOCR:
  738. return [KMToolbarToolEnhancedScanIdentifier,
  739. KMToolbarToolOCRTextIdentifier]
  740. case .viewSetting:
  741. return [KMToolbarViewSettingIdentifier, KMToolbarCropIdentifier,
  742. KMToolbarSnapshotIdentifier, KMToolbarTTSIdentifier]
  743. default:
  744. break
  745. }
  746. return []
  747. }
  748. func toolbar(_ toolbar: KMToolbarView, itemFor identifier: String) -> KMToolbarItemView {
  749. var item: KMToolbarItemView? = self.toolbarItems[identifier] as? KMToolbarItemView
  750. var create = false
  751. if let data = item {
  752. let pId = parentIdentifier(data.itemIdentifier ?? "")
  753. if [.Annatiton,.Magnify,.Move,.Select,.SelectZoom].contains(toolbarType) && (pId != KMDocumentAnnotationToolbarItemIdentifier || data.itemIdentifier! == KMToolbarAnnotationSettingItemIdentifier) {
  754. create = true
  755. } else {
  756. if let toolbarId = self.toolbar?.toolbarIdentifier {
  757. let itemIdentifiers = (UserDefaults.standard.object(forKey: toolbarId) as? [String]) ?? []
  758. if itemIdentifiers.contains(identifier) && annotationToolIdentifiers.contains(identifier) == false {
  759. create = true
  760. }
  761. }
  762. }
  763. }
  764. if create == false {
  765. if KMDataManager.default.toolbarConfigDataUpdated {
  766. create = true
  767. }
  768. }
  769. if item == nil || create {
  770. if isMainToolItem(identifier) {
  771. item = KMToolbarMainItemView(itemIdentifier: identifier)
  772. } else if annotationToolIdentifiers.contains(identifier) {
  773. item = KMToolbarCustomItemView.init(itemIdentifier: identifier)
  774. } else {
  775. item = KMToolbarCustomItemView.init(itemIdentifier: identifier)
  776. }
  777. self.toolbarItems[identifier] = item
  778. if isMainToolItem(identifier) {
  779. self._setupMainItem(item)
  780. } else {
  781. if (annotationToolIdentifiers.contains(identifier)) {
  782. self._setupAnnotationItem(item)
  783. } else if editPDFItemIdentifiers.contains(identifier) {
  784. self._setupEditItem(item)
  785. } else if conversionToolIdentifiers.contains(identifier) {
  786. self._setupConvertItem(item)
  787. } else if fillSignToolIdentifiers.contains(identifier) {
  788. self._setupFillSignItem(item)
  789. } else if formToolIdentifiers.contains(identifier) {
  790. self._setupFormItem(item)
  791. } else if toolToolIdentifiers.contains(identifier) {
  792. self._setupToolItem(item)
  793. } else if KMViewDisplayIdentifiers.contains(identifier) {
  794. self._setupViewDisplayItem(item)
  795. } else if pageEditToolIdentifiers.contains(identifier) {
  796. if (identifier == KMToolbarPageEditPageRangeItemIdentifier) {
  797. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageRange)
  798. item?.target = self
  799. item?.btnTag = KMPageEditType.pageRange.rawValue
  800. // item?.toolTip = NSLocalizedString("pageRange", comment: "")
  801. item?.titleName = NSLocalizedString("Page Range", comment: "")
  802. item?.boxImagePosition = .imageExpandLeft
  803. // item?.btnAction = #selector(pageRangeMenuAction)
  804. item?.needExpandAction = true
  805. } else if (identifier == KMToolbarPageEditRotateItemIdentifier) {
  806. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageRotate)
  807. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageRotate)
  808. item?.target = self
  809. item?.btnTag = KMPageEditType.rightRotate.rawValue
  810. item?.toolTip = NSLocalizedString("Rotate", comment: "")
  811. item?.titleName = NSLocalizedString("Rotate", comment: "")
  812. item?.boxImagePosition = .imageLeft
  813. } else if (identifier == KMToolbarPageEditLeftRotateItemIdentifier) {
  814. item?.image = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  815. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageLeftRotate")
  816. item?.target = self
  817. item?.btnTag = KMPageEditType.leftRotate.rawValue
  818. item?.toolTip = NSLocalizedString("Rotate Left", comment: "")
  819. item?.titleName = NSLocalizedString("", comment: "")
  820. item?.boxImagePosition = .imageLeft
  821. } else if (identifier == KMToolbarPageEditRightRotateItemIdentifier) {
  822. item?.image = NSImage(named: "KMImageNameMainToolPageRightRotate")
  823. item?.alternateImage = NSImage(named: "KMImageNameMainToolPageRightRotate")
  824. item?.target = self
  825. item?.btnTag = KMPageEditType.rightRotate.rawValue
  826. item?.toolTip = NSLocalizedString("Rotate Right", comment: "")
  827. item?.titleName = NSLocalizedString("", comment: "")
  828. item?.boxImagePosition = .imageLeft
  829. } else if (identifier == KMToolbarPageEditInsetItemIdentifier) {
  830. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageInsert)
  831. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageInsert)
  832. item?.target = self
  833. item?.btnTag = KMPageEditType.insert.rawValue
  834. item?.isShowCustomToolTip = true
  835. item?.toolTip = NSLocalizedString("Inserting pages into an existing PDF", comment: "")
  836. item?.isPopToolTip = true
  837. item?.titleName = NSLocalizedString("Insert", comment: "")
  838. item?.boxImagePosition = .imageOnly
  839. item?.btnAction = #selector(_itemAction)
  840. } else if (identifier == KMToolbarPageEditExtractItemIdentifier) {
  841. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageExtract)
  842. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageExtract)
  843. item?.target = self
  844. item?.btnTag = KMPageEditType.extract.rawValue
  845. // item?.toolTip = NSLocalizedString("Exctract selected pages", comment: "")
  846. item?.titleName = NSLocalizedString("Extract", comment: "")
  847. item?.boxImagePosition = .imageExpandLeft
  848. // item?.btnAction = #selector(extractMenuAction)
  849. item?.needExpandAction = true
  850. } else if (identifier == KMToolbarPageEditReplaceItemIdentifier) {
  851. item?.image = NSImage(named: KMImageNameToolbarReplace)
  852. item?.alternateImage = NSImage(named: KMImageNameToolbarReplace)
  853. item?.target = self
  854. item?.btnTag = KMPageEditType.replace.rawValue
  855. item?.toolTip = NSLocalizedString("Replace", comment: "")
  856. item?.titleName = NSLocalizedString("Replace", comment: "")
  857. item?.boxImagePosition = .imageLeft
  858. } else if (identifier == KMToolbarPageEditSplitItemIdentifier) {
  859. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageSplit)
  860. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageSplit)
  861. item?.target = self
  862. item?.btnTag = KMPageEditType.split.rawValue
  863. item?.isShowCustomToolTip = true
  864. item?.toolTip = NSLocalizedString("Split and save selected PDF pages as a new separate document", comment: "")
  865. item?.isPopToolTip = true
  866. item?.titleName = NSLocalizedString("Split", comment: "")
  867. item?.boxImagePosition = .imageOnly
  868. item?.btnAction = #selector(_itemAction)
  869. } else if (identifier == KMToolbarPageEditReverseItemIdentifier) {
  870. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageReverse)
  871. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageReverse)
  872. item?.target = self
  873. item?.btnTag = KMPageEditType.reverse.rawValue
  874. item?.toolTip = NSLocalizedString("Reverse", comment: "")
  875. item?.titleName = NSLocalizedString("Reverse", comment: "")
  876. item?.boxImagePosition = .imageLeft
  877. } else if (identifier == KMToolbarPageEditCopyItemIdentifier) {
  878. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageCopy)
  879. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageCopy)
  880. item?.target = self
  881. item?.btnTag = KMPageEditType.delete.rawValue
  882. item?.toolTip = NSLocalizedString("Copy", comment: "")
  883. item?.titleName = NSLocalizedString("Copy", comment: "")
  884. item?.boxImagePosition = .imageLeft
  885. } else if (identifier == KMToolbarPageEditPasteItemIdentifier) {
  886. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPagePaste)
  887. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPagePaste)
  888. item?.target = self
  889. item?.btnTag = KMPageEditType.delete.rawValue
  890. item?.toolTip = NSLocalizedString("Paste", comment: "")
  891. item?.titleName = NSLocalizedString("Paste", comment: "")
  892. item?.boxImagePosition = .imageLeft
  893. } else if (identifier == KMToolbarPageEditDeleteItemIdentifier) {
  894. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarPageDelete)
  895. item?.alternateImage = NSImage(named: KMImageNameUXIconSubtoolbarPageDelete)
  896. item?.target = self
  897. item?.btnTag = KMPageEditType.delete.rawValue
  898. item?.toolTip = NSLocalizedString("Delete", comment: "")
  899. item?.titleName = NSLocalizedString("Delete", comment: "")
  900. item?.boxImagePosition = .imageLeft
  901. }
  902. return item!
  903. }
  904. }
  905. }
  906. return item!
  907. }
  908. func validateToolbarItem(_ item: NSToolbarItem) -> Bool {
  909. return true
  910. }
  911. @objc func listViewAnnotationTypeChangeNotification(sender: Notification?) {
  912. // if let notiName = sender?.name, notiName == NSNotification.Name.CPDFListViewAnnotationTypeChange, self.ignoreCurrentAnnotationTypeChange {
  913. // }
  914. if (self.ignoreCurrentAnnotationTypeChange) {
  915. // 忽略后重置 这个属性是基于单次添加,所以使用后会重置
  916. // self.ignoreCurrentAnnotationTypeChange = false
  917. return
  918. }
  919. let annotationType : CAnnotationType = pdfView?.annotationType ?? .unkown
  920. if annotationType == .square || annotationType == .circle || annotationType == .arrow || annotationType == .line{
  921. UserDefaults.standard.set(annotationType.rawValue, forKey: KMToolBarToolPDFShapeStyle)
  922. UserDefaults.standard.synchronize()
  923. }
  924. let toolMode = pdfView?.toolMode ?? .none
  925. for item in self.toolbar?.items ?? [] {
  926. if self.toolbarType == .Annatiton || self.toolbarType == .Move || self.toolbarType == .Magnify ||
  927. self.toolbarType == .Select || self.toolbarType == .SelectZoom {
  928. item.isSelected = false
  929. if item.itemIdentifier == KMDocumentAnnotationToolbarItemIdentifier {
  930. item.isSelected = true
  931. } else if item.itemIdentifier == KMToolbarHighlightAnnotationItemIdentifier {
  932. if annotationType == .highlight {
  933. item.isSelected = true
  934. }
  935. } else if item.itemIdentifier == KMToolbarUnderlineAnnotationItemIdentifier {
  936. if annotationType == .underline {
  937. item.isSelected = true
  938. }
  939. } else if item.itemIdentifier == KMToolbarStrikeOutAnnotationItemIdentifier {
  940. if annotationType == .strikeOut {
  941. item.isSelected = true
  942. }
  943. } else if item.itemIdentifier == KMToolbarInkAnnotationItemIdentifier {
  944. if annotationType == .ink || annotationType == .eraser {
  945. item.isSelected = true
  946. }
  947. } else if item.itemIdentifier == KMToolbarFreeTextAnnotationItemIdentifier {
  948. if annotationType == .freeText {
  949. item.isSelected = true
  950. }
  951. } else if item.itemIdentifier == KMToolbarAnchoredAnnotationItemIdentifier {
  952. if annotationType == .anchored {
  953. item.isSelected = true
  954. }
  955. } else if item.itemIdentifier == KMToolbarSquareAnnotationItemIdentifier {
  956. if annotationType == .square {
  957. item.isSelected = true
  958. }
  959. } else if item.itemIdentifier == KMToolbarCircleAnnotationItemIdentifier {
  960. if annotationType == .circle {
  961. item.isSelected = true
  962. }
  963. } else if item.itemIdentifier == KMToolbarArrowAnnotationItemIdentifier {
  964. if annotationType == .arrow {
  965. item.isSelected = true
  966. }
  967. } else if item.itemIdentifier == KMToolbarLineAnnotationItemIdentifier {
  968. if annotationType == .line {
  969. item.isSelected = true
  970. }
  971. } else if item.itemIdentifier == KMToolbarLinkAnnotationItemIdentifier {
  972. if annotationType == .link {
  973. item.isSelected = true
  974. }
  975. } else if item.itemIdentifier == KMAnnotationStampToolbarItemIdentifier {
  976. if annotationType == .stamp {
  977. item.isSelected = true
  978. }
  979. } else if item.itemIdentifier == KMToolbarSignSignatureAnnotationItemIdentifier {
  980. if annotationType == .signSignature {
  981. item.isSelected = true
  982. }
  983. } else if toolMode == .moveToolMode {
  984. if item.itemIdentifier == KMToolbarMoveToolModeItemIdentifier {
  985. item.isSelected = true
  986. }
  987. } else if toolMode == .magnifyToolMode {
  988. if item.itemIdentifier == KMToolbarMagnifyToolModeItemIdentifier {
  989. item.isSelected = true
  990. }
  991. } else if toolMode == .selectToolMode {
  992. if item.itemIdentifier == KMToolbarSelectToolModeItemIdentifier {
  993. item.isSelected = true
  994. }
  995. } else if toolMode == .selectZoomToolMode {
  996. if item.itemIdentifier == KMToolbarZoomToSelectionItemIdentifier {
  997. item.isSelected = true
  998. }
  999. }
  1000. if (item.itemIdentifier == KMToolbarShowToolbarItemIdentifier) {
  1001. item.isSelected = !self.isShowAllAnnotations
  1002. }
  1003. } else if self.toolbarType == .FillSign {
  1004. item.isSelected = false
  1005. if toolMode == .selfSignMode {
  1006. if item.itemIdentifier == KMToolbarToolHookItemIdentifier {
  1007. if annotationType == .signTure {
  1008. item.isSelected = true
  1009. }
  1010. } else if item.itemIdentifier == KMToolbarToolForkItemIdentifier {
  1011. if annotationType == .signFalse {
  1012. item.isSelected = true
  1013. }
  1014. } else if item.itemIdentifier == KMToolbarToolLineItemIdentifier {
  1015. if annotationType == .signLine {
  1016. item.isSelected = true
  1017. }
  1018. } else if item.itemIdentifier == KMToolbarToolDotItemIdentifier {
  1019. if annotationType == .signDot {
  1020. item.isSelected = true
  1021. }
  1022. } else if item.itemIdentifier == KMToolbarToolRectangleIdentifier {
  1023. if annotationType == .signCircle {
  1024. item.isSelected = true
  1025. }
  1026. } else if item.itemIdentifier == KMToolbarToolDateIdentifier {
  1027. if annotationType == .signDate {
  1028. item.isSelected = true
  1029. }
  1030. } else if item.itemIdentifier == KMToolbarToolTextIdentifier {
  1031. if annotationType == .signText {
  1032. item.isSelected = true
  1033. }
  1034. } else if (item.itemIdentifier == KMToolbarFillSignSignatureIdentifier) {
  1035. if (annotationType == .signSignature) {
  1036. item.isSelected = true
  1037. }
  1038. }
  1039. }
  1040. } else if self.toolbarType == .Form {
  1041. item.isSelected = false
  1042. if toolMode == .formToolMode {
  1043. if item.itemIdentifier == KMToolbarToolTextFieldItemIdentifier {
  1044. if annotationType == .textField {
  1045. item.isSelected = true
  1046. }
  1047. } else if item.itemIdentifier == KMToolbarToolCheckBoxItemIdentifier {
  1048. if annotationType == .checkBox {
  1049. item.isSelected = true
  1050. }
  1051. } else if item.itemIdentifier == KMToolbarToolRadioButtonItemIdentifier {
  1052. if annotationType == .radioButton {
  1053. item.isSelected = true
  1054. }
  1055. } else if item.itemIdentifier == KMToolbarToolListBoxItemIdentifier {
  1056. if annotationType == .listMenu {
  1057. item.isSelected = true
  1058. }
  1059. } else if item.itemIdentifier == KMToolbarToolPullDownmenuItemIdentifier {
  1060. if annotationType == .comboBox {
  1061. item.isSelected = true
  1062. }
  1063. } else if item.itemIdentifier == KMToolbarToolButtonIdentifier {
  1064. if annotationType == .actionButton {
  1065. item.isSelected = true
  1066. }
  1067. } else if item.itemIdentifier == KMToolbarToolSignBoxIdentifier {
  1068. if annotationType == .signature {
  1069. item.isSelected = true
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. extension KMToolbarViewController: NSSearchFieldDelegate {
  1078. }
  1079. extension KMToolbarViewController {
  1080. private func _setupMainItem(_ item: KMToolbarItemView?) {
  1081. let identifier = item?.itemIdentifier
  1082. if identifier == KMLeftControlToolbarItemIdentifier {
  1083. item?.image = NSImage(named: "KMImageNameUXIconBtnTriLeftNor")
  1084. item?.titleName = NSLocalizedString("Panel", comment: "")
  1085. item?.target = self
  1086. item?.toolTip = NSLocalizedString("View Settings", comment: "")
  1087. item?.boxImagePosition = .imageAbove
  1088. item?.selectBackgroundType = .imageBox
  1089. item?.btnAction = #selector(_itemAction)
  1090. } else if identifier == KMDocumentZoomViewToolbarItemIdentifier{
  1091. item?.titleName = NSLocalizedString("Zoom", comment: "")
  1092. item?.target = self
  1093. let view = KMToolbarZoomItemView(zoomView: self.zoomView)
  1094. view.callback = { [weak self] idx, _ in
  1095. if idx == 2 {
  1096. self?.delegate?.changePDFViewZoomInAction?()
  1097. } else if idx == 1 {
  1098. self?.delegate?.changePDFViewZoomOutAction?()
  1099. }
  1100. }
  1101. item?.customizeView = view
  1102. } else if identifier == KMDocumentPreviousPageToolbarItemIdentifier {
  1103. item?.titleName = NSLocalizedString("Zoom", comment: "")
  1104. item?.target = self
  1105. let view = KMToolbarPreviousNextItemView()
  1106. view.callback = { [weak self] idx, _ in
  1107. if idx == 1 {
  1108. self?.delegate?.changePDFViewGoToPreviousPageAction?()
  1109. } else if idx == 2 {
  1110. self?.delegate?.changePDFViewGotoNextPageAction?()
  1111. }
  1112. }
  1113. item?.customizeView = view
  1114. } else if identifier == KMDocumentHomeToolbarItemIdentifier {
  1115. item?.image = NSImage(named: "KMImageNameToolbarHomeNor")
  1116. item?.titleName = NSLocalizedString("Home", comment: "")
  1117. item?.target = self
  1118. item?.toolTip = NSLocalizedString("A Welcome Gift from Us", comment: "")
  1119. item?.boxImagePosition = .imageAbove
  1120. item?.selectBackgroundType = .imageBox
  1121. item?.btnAction = #selector(_itemAction)
  1122. } else if identifier == KMDocumentAnnotationToolbarItemIdentifier {
  1123. item?.titleName = NSLocalizedString("Tools", comment: "")
  1124. item?.image = NSImage(named: "KMImageNameUXIconToolbarMytoolsNor")
  1125. item?.target = self
  1126. item?.toolTip = String(format: "%@: %@, %@, %@, %@", KMLocalizedString("Tool Mode", nil),KMLocalizedString("Annotate", nil),KMLocalizedString("Scroll", nil),KMLocalizedString("Magnify", nil),KMLocalizedString("Select", nil))
  1127. item?.btnTag = KMToolbarViewType.Annatiton.rawValue
  1128. item?.btnAction = #selector(leftControllButtonAction)
  1129. item?.boxImagePosition = .imageAbove
  1130. item?.selectBackgroundType = .imageBox
  1131. let menuItem = NSMenuItem(title: item?.titleName ?? "", action: #selector(_menuItemAction), keyEquivalent: "")
  1132. menuItem.target = self
  1133. menuItem.representedObject = identifier
  1134. item?.menuFormRepresentation = menuItem
  1135. } else if identifier == KMDocumentPageToolbarItemIdentifier {
  1136. item?.titleName = NSLocalizedString("Page Edit", comment: "")
  1137. item?.target = self
  1138. item?.image = NSImage(named: "KMImageNameUXIconToolbarPageeditNor")
  1139. item?.toolTip = NSLocalizedString("PDF page editor: insert, delete, extract, rotate, reposition, and replace pages in a PDF", comment: "")
  1140. item?.btnTag = KMToolbarViewType.Page.rawValue
  1141. item?.btnAction = #selector(leftControllButtonAction)
  1142. item?.boxImagePosition = .imageAbove
  1143. item?.selectBackgroundType = .imageBox
  1144. let menuItem = NSMenuItem(title: item?.titleName ?? "", action: #selector(_menuItemAction), keyEquivalent: "")
  1145. menuItem.target = self
  1146. menuItem.representedObject = identifier
  1147. item?.menuFormRepresentation = menuItem
  1148. } else if identifier == KMDocumentConversonToolbarItemIdentifier {
  1149. item?.titleName = NSLocalizedString("Converter", comment: "")
  1150. item?.target = self
  1151. item?.image = NSImage(named: "KMImageNameUXIconToolbarConvertNor")
  1152. item?.toolTip = NSLocalizedString("Convert PDFs to Microsoft Word, PowerPoint, Excel, RTF, Text, Image, CSV, and more Offline", comment: "")
  1153. item?.btnTag = KMToolbarViewType.Conversion.rawValue
  1154. item?.btnAction = #selector(leftControllButtonAction)
  1155. item?.boxImagePosition = .imageAbove
  1156. item?.selectBackgroundType = .imageBox
  1157. item?.promptIdentifier = identifier
  1158. let menuItem = NSMenuItem(title: NSLocalizedString("Converter", comment: ""), action: #selector(_menuItemAction), keyEquivalent: "")
  1159. menuItem.target = self
  1160. menuItem.representedObject = identifier
  1161. item?.menuFormRepresentation = menuItem
  1162. } else if identifier == KMDocumentScanOCRToolbarItemIdentifier {
  1163. item?.titleName = NSLocalizedString("OCR", comment: "")
  1164. item?.target = self
  1165. item?.image = NSImage(named: "KMImageNameToolbarOCRNor")
  1166. item?.boxImagePosition = .imageAbove
  1167. item?.toolTip = NSLocalizedString("Recognize text from Image-based or Scanned PDF with OCR", comment: "")
  1168. item?.selectBackgroundType = .imageBox
  1169. item?.btnAction = #selector(OCRbtnAction1)
  1170. item?.promptIdentifier = identifier
  1171. let menuItem = NSMenuItem.init(title: NSLocalizedString("OCR", comment: ""), action: #selector(_menuItemAction), target: self)
  1172. menuItem.representedObject = identifier
  1173. item?.menuFormRepresentation = menuItem
  1174. } else if identifier == KMToolbarToolEnhancedScanIdentifier {
  1175. item?.image = NSImage(named: "KMImageNameMainToolEnhancedScan")
  1176. item?.target = self
  1177. item?.btnTag = 0
  1178. item?.toolTip = NSLocalizedString("Enhanced Scan", comment: "")
  1179. item?.titleName = NSLocalizedString("Enhanced Scan", comment: "")
  1180. item?.boxImagePosition = .imageLeft
  1181. item?.selectBackgroundType = .imageBox
  1182. item?.btnAction = #selector(scanOCRModelSelect)
  1183. } else if identifier == KMToolbarToolOCRTextIdentifier {
  1184. item?.image = NSImage(named: "KMImageNameMainToolOCRText")
  1185. item?.target = self
  1186. item?.toolTip = NSLocalizedString("OCR Text Recognition", comment: "")
  1187. item?.titleName = NSLocalizedString("OCR Text Recognition", comment: "")
  1188. item?.boxImagePosition = .imageLeft
  1189. item?.selectBackgroundType = .imageBox
  1190. item?.btnAction = #selector(scanOCRModelSelect)
  1191. item?.promptIdentifier = identifier
  1192. let menuItem = NSMenuItem.init(title: NSLocalizedString("OCR Text Recognition", comment: ""), action: #selector(scanOCRModelSelect), target: self)
  1193. menuItem.representedObject = identifier
  1194. item?.menuFormRepresentation = menuItem
  1195. } else if identifier == KMDocumentEditToolbarItemIdentifier {
  1196. item?.titleName = NSLocalizedString("Edit PDF", comment: "")
  1197. item?.target = self
  1198. item?.image = NSImage(named: "KMImageNameUXIconToolbarEditNor")
  1199. item?.boxImagePosition = .imageAbove
  1200. item?.btnTag = KMToolbarViewType.editPDF.rawValue
  1201. item?.toolTip = NSLocalizedString("Edit text and image in PDF ", comment: "")
  1202. item?.selectBackgroundType = .imageBox
  1203. item?.btnAction = #selector(leftControllButtonAction)
  1204. item?.promptIdentifier = identifier
  1205. let menuItem = NSMenuItem.init(title: NSLocalizedString("Edit PDF", comment: ""), action: #selector(_menuItemAction), target: self)
  1206. menuItem.tag = item!.btnTag
  1207. menuItem.representedObject = identifier
  1208. item?.menuFormRepresentation = menuItem
  1209. } else if identifier == KMDocumentFormToolbarItemIdentifier {
  1210. item?.titleName = NSLocalizedString("Forms", comment: "")
  1211. item?.target = self
  1212. item?.image = NSImage(named: "KMImageNameUXIconToolbarFormNor")
  1213. item?.boxImagePosition = .imageAbove
  1214. item?.btnTag = KMToolbarViewType.Form.rawValue
  1215. item?.toolTip = NSLocalizedString("Edit PDF Form", comment: "")
  1216. item?.selectBackgroundType = .imageBox
  1217. item?.btnAction = #selector(leftControllButtonAction)
  1218. item?.promptIdentifier = identifier
  1219. let menuItem = NSMenuItem.init(title: NSLocalizedString("Forms", comment: ""), action: #selector(_menuItemAction), target: self)
  1220. menuItem.tag = item!.btnTag
  1221. menuItem.representedObject = identifier
  1222. item?.menuFormRepresentation = menuItem
  1223. } else if identifier == KMDocumentFillSginToolbarItemIdentifier {
  1224. item?.titleName = NSLocalizedString("Fill & Sign", comment: "")
  1225. item?.target = self
  1226. item?.image = NSImage(named: "KMImageNameUXIconToolbarFillsignNor")
  1227. item?.boxImagePosition = .imageAbove
  1228. item?.btnTag = KMToolbarViewType.FillSign.rawValue
  1229. item?.toolTip = NSLocalizedString("Fill and sign forms", comment: "")
  1230. item?.selectBackgroundType = .imageBox
  1231. item?.btnAction = #selector(leftControllButtonAction)
  1232. item?.promptIdentifier = identifier
  1233. let menuItem = NSMenuItem.init(title: NSLocalizedString("Fill & Sign", comment: ""), action: #selector(_menuItemAction), target: self)
  1234. menuItem.tag = item!.btnTag
  1235. menuItem.representedObject = identifier
  1236. item?.menuFormRepresentation = menuItem
  1237. } else if identifier == KMDocumentToolToolbarItemIdentifier {
  1238. item?.titleName = NSLocalizedString("Editor", comment: "")
  1239. item?.target = self
  1240. item?.image = NSImage(named: "KMImageNameUXIconToolbarEdittoolNor")
  1241. item?.boxImagePosition = .imageAbove
  1242. item?.btnTag = KMToolbarViewType.Tool.rawValue
  1243. item?.toolTip = NSLocalizedString("Edit, delete, cut, copy, paste, and insert text in PDFs", comment: "")
  1244. item?.selectBackgroundType = .imageBox
  1245. item?.btnAction = #selector(leftControllButtonAction)
  1246. item?.promptIdentifier = identifier
  1247. let menuItem = NSMenuItem(title: item?.titleName ?? "", action: #selector(_menuItemAction), keyEquivalent: "")
  1248. menuItem.target = self
  1249. menuItem.representedObject = identifier
  1250. item?.menuFormRepresentation = menuItem
  1251. } else if identifier == KMDocumentRedactToolbarItemIdentifier {
  1252. item?.titleName = NSLocalizedString("Redact Text", comment: "")
  1253. item?.target = self
  1254. item?.image = NSImage(named: "KMImageNameUXIconToolbarRedactNor")
  1255. item?.boxImagePosition = .imageAbove
  1256. item?.toolTip = NSLocalizedString("Mark for redaction", comment: "")
  1257. item?.selectBackgroundType = .imageBox
  1258. item?.btnAction = #selector(_itemAction)
  1259. item?.promptIdentifier = identifier
  1260. let menuItem = NSMenuItem(title: item?.titleName ?? "", action: #selector(_menuItemAction), keyEquivalent: "")
  1261. menuItem.target = self
  1262. menuItem.representedObject = identifier
  1263. item?.menuFormRepresentation = menuItem
  1264. } else if identifier == KMDocumentAITranslationToolbarItemIdentifier {
  1265. item?.image = NSImage(named: "ic_function_other_AITranslation")
  1266. item?.titleName = "AI Translation"
  1267. item?.target = self
  1268. item?.toolTip = NSLocalizedString("AI Translation", comment: "")
  1269. item?.boxImagePosition = .imageOnly
  1270. item?.btnAction = #selector(aiTranslationButtonAction)
  1271. item?.promptIdentifier = identifier
  1272. } else if identifier == KMDocumentPrintToolbarItemIdentifier {
  1273. item?.image = NSImage(named: "KMImageNameToolbarPrintNor")
  1274. item?.titleName = "Print"
  1275. item?.target = self
  1276. item?.toolTip = NSLocalizedString("Print", comment: "")
  1277. item?.boxImagePosition = .imageOnly
  1278. item?.btnAction = #selector(_itemAction)
  1279. } else if identifier == KMDocumentViewDisplayToolbarItemIdentifier {
  1280. item?.image = NSImage(named: "KMImageNameUXIconToolbarPageviewNor")
  1281. item?.titleName = NSLocalizedString("Page Display", comment: "")
  1282. item?.target = self
  1283. item?.toolTip = NSLocalizedString("Page Display", comment: "")
  1284. item?.boxImagePosition = .imageAbove
  1285. item?.selectBackgroundType = .imageBox
  1286. item?.btnAction = #selector(_itemAction)
  1287. item?.promptIdentifier = identifier
  1288. let menuItem = NSMenuItem.init(title: NSLocalizedString("Page Display", comment: ""), action: #selector(_menuItemAction), target: self)
  1289. menuItem.representedObject = identifier
  1290. item?.menuFormRepresentation = menuItem
  1291. } else if identifier == KMDocumentAIToolsToolbarItemIdentifier {
  1292. item?.image = NSImage(named: "KMImageNameUXIconAINor")
  1293. item?.titleName = NSLocalizedString("AI Tools", comment: "")
  1294. item?.target = self
  1295. item?.toolTip = NSLocalizedString("AI Tools", comment: "")
  1296. item?.boxImagePosition = .imageAbove
  1297. item?.selectBackgroundType = .imageBox
  1298. item?.btnAction = #selector(_itemAction)
  1299. item?.promptIdentifier = identifier
  1300. let menuItem = NSMenuItem.init(title: NSLocalizedString("AI Tools", comment: ""), action: #selector(_menuItemAction), target: self)
  1301. menuItem.representedObject = identifier
  1302. item?.menuFormRepresentation = menuItem
  1303. } else if identifier == KMDocumentShareToolbarItemIdentifier {
  1304. item?.image = NSImage(named: "KMImageNameUXIconToolbarShareNor")
  1305. item?.titleName = NSLocalizedString("Share", comment: "")
  1306. item?.target = self
  1307. // item?.toolTip = NSLocalizedString("Share the file with others", comment: "")
  1308. item?.boxImagePosition = .imageAbove
  1309. item?.selectBackgroundType = .imageBox
  1310. item?.btnAction = #selector(shareButtonAction)
  1311. let menuItem = NSMenuItem.init(title: item?.titleName ?? "", action: nil, target: self)
  1312. let menu = NSMenu()
  1313. _ = menu.addItem(withTitle: NSLocalizedString("Document", comment: ""), action: #selector(shareViaWithDocument), target: self)
  1314. _ = menu.addItem(withTitle: NSLocalizedString("Flattened Copy", comment: ""), action: #selector(shareViaWithFlatten), target: self)
  1315. _ = menu.addItem(withTitle: NSLocalizedString("Original PDF", comment: ""), action: #selector(shareViaWithOriginalPDF), target: self)
  1316. menuItem.submenu = menu
  1317. item?.menuFormRepresentation = menuItem
  1318. } else if identifier == KMDocumentSearchToolbarItemIdentifier {
  1319. item?.titleName = NSLocalizedString("Search", comment: "")
  1320. item?.target = self
  1321. let view = NSView()
  1322. view.frame = NSMakeRect(0, 0, 150, 40)
  1323. let boxView = NSView()
  1324. boxView.frame = NSMakeRect(0, 16, 150, 22)
  1325. view.addSubview(boxView)
  1326. let searchView = NSSearchField()
  1327. searchView.frame = NSMakeRect(0, 0, 150, 22)
  1328. searchView.placeholderString = NSLocalizedString("Search", comment: "")
  1329. searchView.sendsWholeSearchString = true
  1330. searchView.sendsSearchStringImmediately = true
  1331. searchView.drawsBackground = false
  1332. searchView.delegate = self
  1333. self.searchField = searchView
  1334. self.refreshSearchBarMenu()
  1335. boxView.addSubview(searchView)
  1336. let titleLabel = NSTextField(labelWithString: NSLocalizedString("Search", comment: ""))
  1337. view.addSubview(titleLabel)
  1338. titleLabel.frame = NSMakeRect(0, 0, 130, 14)
  1339. titleLabel.alignment = .center
  1340. titleLabel.textColor = KMAppearance.subtitleColor()
  1341. titleLabel.font = KMToolbarMainItemView.textFont
  1342. item?.customizeView = view
  1343. let menuItem = NSMenuItem.init(title: NSLocalizedString("Search", comment: ""), action: #selector(_menuItemAction), target: self)
  1344. menuItem.representedObject = identifier
  1345. item?.menuFormRepresentation = menuItem
  1346. } else if identifier == KMRightControlToolbarItemIdentifier {
  1347. item?.image = NSImage(named: "KMImageNameUXIconBtnTriRightNor")
  1348. item?.titleName = NSLocalizedString("Properties", comment: "")
  1349. item?.target = self
  1350. item?.toolTip = NSLocalizedString("Show/Hide Annotation Properties Panel", comment: "")
  1351. item?.boxImagePosition = .imageAbove
  1352. item?.selectBackgroundType = .imageBox
  1353. item?.btnAction = #selector(_itemAction)
  1354. let menuItem = NSMenuItem.init(title: NSLocalizedString("Properties", comment: ""), action: #selector(_menuItemAction), target: self)
  1355. menuItem.representedObject = identifier
  1356. item?.menuFormRepresentation = menuItem
  1357. } else if identifier == KMToolbarToolRedactItemIdentifier {
  1358. item?.image = NSImage(named: "KMImageNameMainToolsRedact")
  1359. item?.target = self
  1360. item?.btnTag = KMToolbarType.redact.rawValue
  1361. item?.toolTip = NSLocalizedString("Redact", comment: "")
  1362. item?.titleName = NSLocalizedString("Redact", comment: "")
  1363. item?.selectBackgroundType = .imageBox
  1364. item?.btnAction = #selector(redactMenuAction)
  1365. item?.promptIdentifier = identifier
  1366. let menuItem = NSMenuItem.init(title: NSLocalizedString("Redact", comment: ""), action: #selector(redactMenuAction), target: self)
  1367. menuItem.tag = item!.btnTag
  1368. menuItem.representedObject = identifier
  1369. item?.menuFormRepresentation = menuItem
  1370. } else if identifier == KMDocumentDigitalSignToolbarItemIdentifier {
  1371. item?.image = NSImage(named: "DigitalSign_icon")
  1372. item?.target = self
  1373. item?.toolTip = NSLocalizedString("Digital signature ensures the authenticity and integrity of digital files. Click and drag the cursor to create a signature field on the page.", comment: "")
  1374. item?.titleName = NSLocalizedString("Digital Sign", comment: "")
  1375. item?.selectBackgroundType = .imageBox
  1376. item?.boxImagePosition = .imageAbove
  1377. item?.btnAction = #selector(_itemAction)
  1378. item?.promptIdentifier = identifier
  1379. let menuItem = NSMenuItem.init(title: NSLocalizedString("Digital Sign", comment: ""), action: #selector(_menuItemAction), target: self)
  1380. menuItem.tag = item!.btnTag
  1381. menuItem.representedObject = identifier
  1382. item?.menuFormRepresentation = menuItem
  1383. } else if identifier == KMDocumentPreviousBackToolbarItemIdentifier {
  1384. let view = KMToolbarPreviousBackItemView()
  1385. item?.customizeView = view
  1386. view.callback = { [weak self] idx, _ in
  1387. if idx == 1 {
  1388. self?.delegate?.changePDFViewGoToForwardAction?()
  1389. } else if idx == 2 {
  1390. self?.delegate?.changePDFViewGotoBackAction?()
  1391. }
  1392. }
  1393. } else if identifier == KMDocumentFirstLastToolbarItemIdentifier {
  1394. let view = KMToolbarFirstLastItemView()
  1395. item?.customizeView = view
  1396. view.callback = { [weak self] idx, _ in
  1397. if let item = self?.toolbarItemFindItemIdentifiers(value: identifier!) {
  1398. self?.delegate?.toolbarViewController?(self!, viewItemDidClick: item, index: idx, info: nil)
  1399. }
  1400. }
  1401. } else if identifier == KMDocumentPageIndicatorToolbarItemIdentifier {
  1402. // let view = KMToolbarPageIndicatorItemView(zoomView: nil)
  1403. // item?.customizeView = view
  1404. } else if identifier == KMDocumentPresentationToolbarItemIdentifier {
  1405. item?.image = NSImage(named: "KMImageNameToolbarSlideshowNor")
  1406. item?.titleName = NSLocalizedString("Presentation", comment: "")
  1407. item?.selectBackgroundType = .imageBox
  1408. item?.boxImagePosition = .imageAbove
  1409. item?.target = self
  1410. item?.btnTag = KMToolbarType.conversion_image.rawValue
  1411. // item?.toolTip = NSLocalizedString("Convert PDF to JPEG, PNG, TIFF, BMP, GIF or TGA files", comment: "")
  1412. item?.btnAction = #selector(_itemAction)
  1413. item?.promptIdentifier = identifier
  1414. let menuItem = NSMenuItem(title: item?.titleName ?? "", action: #selector(_menuItemAction), keyEquivalent: "")
  1415. menuItem.target = self
  1416. menuItem.representedObject = identifier
  1417. item?.menuFormRepresentation = menuItem
  1418. } else if identifier == KMToolbarFixedSpaceItemIdentifier {
  1419. let view = NSView()
  1420. view.frame = NSMakeRect(0, 0, 36, 36)
  1421. item?.customizeView = view
  1422. }
  1423. }
  1424. private func _setupAnnotationItem(_ item: KMToolbarItemView?) {
  1425. let identifier = item?.itemIdentifier
  1426. if identifier == KMToolbarMoveToolModeItemIdentifier {
  1427. item?.titleName = NSLocalizedString("Scroll Tool", comment: "")
  1428. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsPantool)
  1429. item?.target = self
  1430. item?.btnTag = KMToolbarViewType.Move.rawValue
  1431. item?.isShowCustomToolTip = true
  1432. item?.toolTip = NSLocalizedString("Scroll Tool", comment: "")
  1433. item?.isPopToolTip = true
  1434. item?.boxImagePosition = .imageOnly
  1435. item?.btnAction = #selector(leftControllButtonAction)
  1436. } else if identifier == KMToolbarMagnifyToolModeItemIdentifier {
  1437. item?.titleName = NSLocalizedString("Magnify Tool", comment: "")
  1438. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsMagnifier)
  1439. item?.target = self
  1440. item?.btnTag = KMToolbarViewType.Magnify.rawValue
  1441. item?.isShowCustomToolTip = true
  1442. item?.toolTip = NSLocalizedString("Magnify Tool", comment: "")
  1443. item?.isPopToolTip = true
  1444. item?.boxImagePosition = .imageOnly
  1445. item?.btnAction = #selector(leftControllButtonAction)
  1446. } else if identifier == KMToolbarSelectToolModeItemIdentifier {
  1447. item?.titleName = NSLocalizedString("Content Selection Tool", comment: "")
  1448. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsContentsel)
  1449. item?.target = self
  1450. item?.btnTag = KMToolbarViewType.Select.rawValue
  1451. item?.isShowCustomToolTip = true
  1452. item?.toolTip = NSLocalizedString("Content Selection Tool", comment: "")
  1453. item?.isPopToolTip = true
  1454. item?.boxImagePosition = .imageOnly
  1455. item?.btnAction = #selector(leftControllButtonAction)
  1456. } else if identifier == KMToolbarZoomToSelectionItemIdentifier {
  1457. item?.titleName = NSLocalizedString("Zoom to selected area", comment: "")
  1458. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsAreazoom)
  1459. item?.target = self
  1460. item?.btnTag = KMToolbarViewType.SelectZoom.rawValue
  1461. item?.isShowCustomToolTip = true
  1462. item?.toolTip = NSLocalizedString("Zoom to selected area", comment: "")
  1463. item?.isPopToolTip = true
  1464. item?.boxImagePosition = .imageOnly
  1465. item?.btnAction = #selector(leftControllButtonAction)
  1466. } else if identifier == KMToolbarHighlightAnnotationItemIdentifier {
  1467. item?.titleName = NSLocalizedString("Highlight", comment: "")
  1468. // item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsUnderline)
  1469. let imageV = KMNoteTypeImageView()
  1470. let color = UserDefaults.standard.PDFListViewColor(forKey: CHighlightNoteColorKey) ?? .red
  1471. item?.image = imageV.noteTypeImage(withType: SKNHighlightString, color: color)
  1472. item?.target = self
  1473. item?.btnTag = CAnnotationType.highlight.rawValue
  1474. item?.isShowCustomToolTip = true
  1475. item?.toolTip = String(format: "%@ (⌃⌘H)", NSLocalizedString("Highlight", comment: ""))
  1476. item?.isPopToolTip = true
  1477. item?.boxImagePosition = .imageOnly
  1478. item?.btnAction = #selector(changeAnnotationMode)
  1479. } else if identifier == KMToolbarUnderlineAnnotationItemIdentifier {
  1480. item?.titleName = NSLocalizedString("Underline", comment: "")
  1481. // item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsUnderline)
  1482. let imageV = KMNoteTypeImageView()
  1483. let color = UserDefaults.standard.PDFListViewColor(forKey: CUnderlineNoteColorKey) ?? .red
  1484. item?.image = imageV.noteTypeImage(withType: SKNUnderlineString, color: color)
  1485. item?.target = self
  1486. item?.btnTag = CAnnotationType.underline.rawValue
  1487. item?.isShowCustomToolTip = true
  1488. item?.toolTip = String(format: "%@ (⇧⌘M)", NSLocalizedString("Underline", comment: ""))
  1489. item?.isPopToolTip = true
  1490. item?.boxImagePosition = .imageOnly
  1491. item?.btnAction = #selector(changeAnnotationMode)
  1492. } else if identifier == KMToolbarStrikeOutAnnotationItemIdentifier {
  1493. item?.titleName = NSLocalizedString("Strikethrough", comment: "")
  1494. // item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsStrikeout)
  1495. let imageV = KMNoteTypeImageView()
  1496. let color = UserDefaults.standard.PDFListViewColor(forKey: CStrikeOutNoteColorKey) ?? .red
  1497. item?.image = imageV.noteTypeImage(withType: SKNStrikeOutString, color: color)
  1498. item?.target = self
  1499. item?.btnTag = CAnnotationType.strikeOut.rawValue
  1500. item?.isShowCustomToolTip = true
  1501. item?.toolTip = String(format: "%@ (⌃⌘M)", NSLocalizedString("Strikethrough", comment: ""))
  1502. item?.isPopToolTip = true
  1503. item?.boxImagePosition = .imageOnly
  1504. item?.btnAction = #selector(changeAnnotationMode)
  1505. } else if identifier == KMToolbarInkAnnotationItemIdentifier {
  1506. item?.titleName = NSLocalizedString("Freehand", comment: "")
  1507. // item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsPen)
  1508. let imageV = KMNoteTypeImageView()
  1509. let color = UserDefaults.standard.PDFListViewColor(forKey: CInkNoteColorKey) ?? .red
  1510. item?.image = imageV.noteTypeImage(withType: SKNInkString, color: color)
  1511. item?.target = self
  1512. item?.btnTag = CAnnotationType.ink.rawValue
  1513. item?.isShowCustomToolTip = true
  1514. item?.toolTip = NSLocalizedString("Add freehand drawings by hand writing just like you do with a pen.", comment: "")
  1515. item?.isPopToolTip = true
  1516. item?.boxImagePosition = .imageOnly
  1517. item?.btnAction = #selector(changeAnnotationMode)
  1518. } else if identifier == KMToolbarFreeTextAnnotationItemIdentifier {
  1519. item?.titleName = NSLocalizedString("Text Note", comment: "")
  1520. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsText)
  1521. item?.target = self
  1522. item?.btnTag = CAnnotationType.freeText.rawValue
  1523. item?.isShowCustomToolTip = true
  1524. item?.toolTip = String(format: "%@ (⌘N)", NSLocalizedString("Text Note", comment: ""))
  1525. item?.isPopToolTip = true
  1526. item?.boxImagePosition = .imageOnly
  1527. item?.btnAction = #selector(changeAnnotationMode)
  1528. } else if identifier == KMToolbarAnchoredAnnotationItemIdentifier {
  1529. item?.titleName = NSLocalizedString("Anchored Note", comment: "")
  1530. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsNote)
  1531. item?.target = self
  1532. item?.btnTag = CAnnotationType.anchored.rawValue
  1533. item?.isShowCustomToolTip = true
  1534. item?.toolTip = String(format: "%@ (⌥⌘N)", NSLocalizedString("Anchored Note", comment: ""))
  1535. item?.isPopToolTip = true
  1536. item?.boxImagePosition = .imageOnly
  1537. item?.btnAction = #selector(changeAnnotationMode)
  1538. } else if identifier == KMToolbarSquareAnnotationItemIdentifier {
  1539. item?.titleName = NSLocalizedString("Rectangle", comment: "")
  1540. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsRec)
  1541. item?.target = self
  1542. item?.btnTag = CAnnotationType.square.rawValue
  1543. item?.isShowCustomToolTip = true
  1544. item?.toolTip = NSLocalizedString("Draw rectangle; draw square by holding Shift key", comment: "")
  1545. item?.isPopToolTip = true
  1546. item?.boxImagePosition = .imageOnly
  1547. item?.btnAction = #selector(changeAnnotationMode)
  1548. }else if identifier == KMToolbarCircleAnnotationItemIdentifier {
  1549. item?.titleName = NSLocalizedString("Circle", comment: "")
  1550. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsOval)
  1551. item?.target = self
  1552. item?.btnTag = CAnnotationType.circle.rawValue
  1553. item?.isShowCustomToolTip = true
  1554. item?.toolTip = NSLocalizedString("Draw oval; draw circle by holding Shift key", comment: "")
  1555. item?.isPopToolTip = true
  1556. item?.boxImagePosition = .imageOnly
  1557. item?.btnAction = #selector(changeAnnotationMode)
  1558. } else if identifier == KMToolbarArrowAnnotationItemIdentifier {
  1559. item?.titleName = NSLocalizedString("Arrow", comment: "")
  1560. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsArrow)
  1561. item?.target = self
  1562. item?.btnTag = CAnnotationType.arrow.rawValue
  1563. item?.isShowCustomToolTip = true
  1564. item?.toolTip = NSLocalizedString("Draw arrow; draw straight arrow by holding Shift key", comment: "")
  1565. item?.isPopToolTip = true
  1566. item?.boxImagePosition = .imageOnly
  1567. item?.btnAction = #selector(changeAnnotationMode)
  1568. } else if identifier == KMToolbarLineAnnotationItemIdentifier {
  1569. item?.titleName = NSLocalizedString("Line", comment: "")
  1570. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsLine)
  1571. item?.target = self
  1572. item?.btnTag = CAnnotationType.line.rawValue
  1573. item?.isShowCustomToolTip = true
  1574. item?.toolTip = NSLocalizedString("Draw line; draw straight line by holding Shift key", comment: "")
  1575. item?.isPopToolTip = true
  1576. item?.boxImagePosition = .imageOnly
  1577. item?.btnAction = #selector(changeAnnotationMode)
  1578. } else if identifier == KMToolbarLinkAnnotationItemIdentifier {
  1579. item?.titleName = NSLocalizedString("Link", comment: "")
  1580. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsLink)
  1581. item?.target = self
  1582. item?.btnTag = CAnnotationType.link.rawValue
  1583. item?.isShowCustomToolTip = true
  1584. item?.toolTip = NSLocalizedString("Select an area or text on a page for a link to be inserted.", comment: "")
  1585. item?.isPopToolTip = true
  1586. item?.boxImagePosition = .imageOnly
  1587. item?.btnAction = #selector(changeAnnotationMode)
  1588. } else if identifier == KMToolbarSignSignatureAnnotationItemIdentifier {
  1589. item?.titleName = NSLocalizedString("Signature", comment: "")
  1590. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsSign)
  1591. item?.target = self
  1592. item?.btnTag = CAnnotationType.signSignature.rawValue
  1593. item?.isShowCustomToolTip = true
  1594. item?.toolTip = NSLocalizedString("Add beautiful handwriting or use your trackpad to create your own signature.", comment: "")
  1595. item?.isPopToolTip = true
  1596. item?.boxImagePosition = .imageOnly
  1597. item?.btnAction = #selector(changeAnnotationMode)
  1598. } else if identifier == KMAnnotationStampToolbarItemIdentifier {
  1599. item?.titleName = NSLocalizedString("Stamp", comment: "")
  1600. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsStamp)
  1601. item?.target = self
  1602. item?.btnTag = CAnnotationType.stamp.rawValue
  1603. item?.isShowCustomToolTip = true
  1604. item?.toolTip = NSLocalizedString("Add New Stamp", comment: "")
  1605. item?.isPopToolTip = true
  1606. item?.boxImagePosition = .imageOnly
  1607. item?.btnAction = #selector(changeAnnotationMode)
  1608. } else if identifier == KMToolbarShowToolbarItemIdentifier {
  1609. item?.image = NSImage(named: "KMImageNameMainToolShow")
  1610. item?.selectedImage = NSImage(named: "KMImageNameMainToolHide")
  1611. item?.alternateImage = NSImage(named: "KMImageNameMainToolShow")
  1612. item?.selectedBackgroundColor = NSColor.clear
  1613. item?.target = self
  1614. item?.isShowCustomToolTip = true
  1615. item?.toolTip = NSLocalizedString("Hide", comment: "")
  1616. item?.isPopToolTip = true
  1617. item?.titleName = NSLocalizedString("Hide", comment: "")
  1618. item?.boxImagePosition = .imageOnly
  1619. item?.btnAction = #selector(showPDFLayoutMode)
  1620. } else if identifier == KMToolbarAnnotationSettingItemIdentifier {
  1621. item?.image = NSImage(named: "KMImageNameUXIconBtnSetNor")
  1622. item?.target = self
  1623. item?.isShowCustomToolTip = true
  1624. item?.toolTip = String(format: "%@\n%@", NSLocalizedString("Customize Toolbar", comment: ""),NSLocalizedString("Drag-and-drop tools to change their order", comment: ""))
  1625. item?.isPopToolTip = true
  1626. item?.titleName = NSLocalizedString("Setting", comment: "")
  1627. item?.boxImagePosition = .imageOnly
  1628. item?.btnAction = #selector(_itemAction)
  1629. item?.selectBackgroundType = .imageBox
  1630. } else if identifier == KMAnnotationImageToolbarItemIdentifier {
  1631. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsImage)
  1632. item?.target = self
  1633. item?.titleName = NSLocalizedString("Image", comment: "")
  1634. item?.isShowCustomToolTip = true
  1635. item?.toolTip = NSLocalizedString("Choose an image to insert in a page.", comment: "")
  1636. item?.isPopToolTip = true
  1637. item?.boxImagePosition = .imageOnly
  1638. item?.btnAction = #selector(_itemAction)
  1639. } else if identifier == KMAnnotationTableToolbarItemIdentifier {
  1640. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarMytoolsTable)
  1641. item?.target = self
  1642. item?.btnTag = CAnnotationType.table.rawValue
  1643. item?.titleName = NSLocalizedString("Table", comment: "")
  1644. item?.isShowCustomToolTip = true
  1645. item?.toolTip = NSLocalizedString("Table", comment: "")
  1646. item?.isPopToolTip = true
  1647. item?.boxImagePosition = .imageOnly
  1648. item?.btnAction = #selector(_itemAction)
  1649. }
  1650. }
  1651. private func _setupEditItem(_ item: KMToolbarItemView?) {
  1652. let identifier = item?.itemIdentifier
  1653. if identifier == KMToolbarAddTextEditPDFItemIdentifier {
  1654. item?.image = NSImage(named: "KMImageNameButtonEidtPDFText")
  1655. item?.target = self
  1656. item?.btnTag = CAnnotationType.addText.rawValue
  1657. item?.toolTip = NSLocalizedString("Add Text", comment: "")
  1658. item?.titleName = NSLocalizedString("Add Text", comment: "")
  1659. item?.boxImagePosition = .imageLeft
  1660. item?.btnAction = #selector(changeAnnotationMode)
  1661. } else if identifier == KMToolbarAddImageEditPDFItemIdentifier {
  1662. item?.image = NSImage(named: "KMImageNameButtonEidtPDFImage")
  1663. item?.target = self
  1664. item?.btnTag = CAnnotationType.addImage.rawValue
  1665. item?.toolTip = NSLocalizedString("Add Image", comment: "")
  1666. item?.titleName = NSLocalizedString("Add Image", comment: "")
  1667. item?.boxImagePosition = .imageLeft
  1668. item?.btnAction = #selector(changeAnnotationMode)
  1669. }
  1670. }
  1671. private func _setupConvertItem(_ item: KMToolbarItemView?) {
  1672. let identifier = item?.itemIdentifier
  1673. if identifier == KMToolbarConversionWordItemIdentifier {
  1674. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertWord)
  1675. item?.target = self
  1676. item?.btnTag = KMToolbarType.word.rawValue
  1677. item?.toolTip = NSLocalizedString("Convert all the great work stored in your PDF to a Word document that’s easy to update. Keep the fonts and layouts intact — including bullets and tables.", comment: "")
  1678. item?.titleName = NSLocalizedString("To Word", comment: "")
  1679. item?.boxImagePosition = .imageLeft
  1680. item?.btnAction = #selector(conversionMenuItemAction)
  1681. item?.promptIdentifier = identifier
  1682. } else if identifier == KMToolbarConversionExcelItemIdentifier {
  1683. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertExcel)
  1684. item?.target = self
  1685. item?.btnTag = KMToolbarType.excel.rawValue
  1686. item?.toolTip = NSLocalizedString("Skip the data entry steps and tedious reformatting tasks, too. When you convert to Excel, your data is preserved along with your columns, layouts, and formatting.", comment: "")
  1687. item?.titleName = NSLocalizedString("To Excel", comment: "")
  1688. item?.boxImagePosition = .imageLeft
  1689. item?.btnAction = #selector(conversionMenuItemAction)
  1690. item?.promptIdentifier = identifier
  1691. } else if identifier == KMToolbarConversionPPTItemIdentifier {
  1692. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertPPT)
  1693. item?.target = self
  1694. item?.btnTag = KMToolbarType.ppt.rawValue
  1695. item?.toolTip = NSLocalizedString("When you use PDF Reader Pro to convert PDFs to PowerPoint, you know your formats will be saved, too. So you don’t have to bother redoing bullets, tables, objects, or master layouts.", comment: "")
  1696. item?.titleName = NSLocalizedString("To PPT", comment: "")
  1697. item?.boxImagePosition = .imageLeft
  1698. item?.btnAction = #selector(conversionMenuItemAction)
  1699. item?.promptIdentifier = identifier
  1700. } else if identifier == KMToolbarConversionRTFItemIdentifier {
  1701. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertRTF)
  1702. item?.target = self
  1703. item?.btnTag = KMToolbarType.rtf.rawValue
  1704. item?.toolTip = NSLocalizedString("Turn PDF documents into Rich Text Format (RTF) files that can be opened in virtually any word processor.", comment: "")
  1705. item?.titleName = NSLocalizedString("To RTF", comment: "")
  1706. item?.boxImagePosition = .imageLeft
  1707. item?.btnAction = #selector(conversionMenuItemAction)
  1708. item?.promptIdentifier = identifier
  1709. } else if identifier == KMToolbarConversionCSVItemIdentifier {
  1710. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertCSV)
  1711. item?.target = self
  1712. item?.btnTag = KMToolbarType.csv.rawValue
  1713. item?.toolTip = NSLocalizedString("PDF To CSV", comment: "")
  1714. item?.titleName = NSLocalizedString("To CSV", comment: "")
  1715. item?.boxImagePosition = .imageLeft
  1716. item?.btnAction = #selector(conversionMenuItemAction)
  1717. item?.promptIdentifier = identifier
  1718. } else if identifier == KMToolbarConversionHTMLItemIdentifier {
  1719. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertHtml)
  1720. item?.target = self
  1721. item?.btnTag = KMToolbarType.html.rawValue
  1722. item?.toolTip = NSLocalizedString("PDF To HTML", comment: "")
  1723. item?.titleName = NSLocalizedString("To HTML", comment: "")
  1724. item?.boxImagePosition = .imageLeft
  1725. item?.btnAction = #selector(conversionMenuItemAction)
  1726. } else if identifier == KMToolbarConversionTextItemIdentifier {
  1727. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertText)
  1728. item?.target = self
  1729. item?.btnTag = KMToolbarType.conversion_text.rawValue
  1730. item?.toolTip = NSLocalizedString("PDF To Text", comment: "")
  1731. item?.titleName = NSLocalizedString("To Text", comment: "")
  1732. item?.boxImagePosition = .imageLeft
  1733. item?.btnAction = #selector(conversionMenuItemAction)
  1734. } else if identifier == KMToolbarConversionImageItemIdentifier {
  1735. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertPDFToIMG)
  1736. item?.target = self
  1737. item?.btnTag = KMToolbarType.conversion_image.rawValue
  1738. // item?.toolTip = NSLocalizedString("Convert PDF to JPEG, PNG, TIFF, BMP, GIF or TGA files", comment: "")
  1739. item?.titleName = NSLocalizedString("To Image", comment: "")
  1740. item?.boxImagePosition = .imageExpandLeft
  1741. item?.needExpandAction = true
  1742. item?.btnAction = #selector(convertToImage)
  1743. item?.promptIdentifier = identifier
  1744. let menuItem = NSMenuItem.init(title: item?.titleName ?? "", action: nil, target: self)
  1745. let subMenu = NSMenu()
  1746. subMenu.addItem(title: NSLocalizedString("PDF To JPEG", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.jpeg.rawValue)
  1747. subMenu.addItem(title: NSLocalizedString("PDF To JPG", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.jpg.rawValue)
  1748. subMenu.addItem(title: NSLocalizedString("PDF To PNG", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.png.rawValue)
  1749. subMenu.addItem(title: NSLocalizedString("PDF To GIF", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.gif.rawValue)
  1750. subMenu.addItem(title: NSLocalizedString("PDF To TIFF", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.tiff.rawValue)
  1751. subMenu.addItem(title: NSLocalizedString("PDF To TGA", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.tga.rawValue)
  1752. subMenu.addItem(title: NSLocalizedString("PDF To BMP", comment: ""), action: #selector(_pdfToImageAction), target: self, tag: KMPDFConvertType.bmp.rawValue)
  1753. menuItem.submenu = subMenu
  1754. item?.menuFormRepresentation = menuItem
  1755. } else if item?.itemIdentifier == KMToolbarConversionExportImageItemIdentifier {
  1756. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertExportImage)
  1757. item?.target = self
  1758. // item?.toolTip = NSLocalizedString("The range you can extract images includes current page, odd page, even page, custom page, and all pages.", comment: "")
  1759. item?.titleName = NSLocalizedString("Extract Images", comment: "")
  1760. item?.boxImagePosition = .imageExpandLeft
  1761. item?.needExpandAction = true
  1762. item?.promptIdentifier = identifier
  1763. let menuItem = NSMenuItem.init(title: item?.titleName ?? "", action: nil, target: self)
  1764. let subMenu = NSMenu()
  1765. subMenu.addItem(title: NSLocalizedString("Extract All Images", comment: ""), action: #selector(_exportImageAction), target: self, tag: 1)
  1766. subMenu.addItem(title: NSLocalizedString("Extract Images", comment: ""), action: #selector(_exportImageAction), target: self, tag: 2)
  1767. menuItem.submenu = subMenu
  1768. item?.menuFormRepresentation = menuItem
  1769. } else if item?.itemIdentifier == KMToolbarConversionOCRIdentifier {
  1770. item?.titleName = NSLocalizedString("OCR", comment: "")
  1771. item?.target = self
  1772. item?.image = NSImage(named: "KMImageNameToolbarOCRNor")
  1773. item?.boxImagePosition = .imageLeft
  1774. item?.toolTip = NSLocalizedString("Recognize text from Image-based or Scanned PDF with OCR", comment: "")
  1775. item?.btnAction = #selector(OCRbtnAction2)
  1776. let menuItem = NSMenuItem.init(title: NSLocalizedString("OCR", comment: ""), action: #selector(_menuItemAction), target: self)
  1777. menuItem.representedObject = KMToolbarConversionOCRIdentifier
  1778. item?.menuFormRepresentation = menuItem
  1779. } else if item?.itemIdentifier == KMToolbarConversionCreatePDFItemIdentifier {
  1780. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarConvertCreatPDF)
  1781. item?.target = self
  1782. // item?.toolTip = NSLocalizedString("Convert JPEG, JPG, PNG, TIFF, BMP or PSD files to PDFs", comment: "")
  1783. item?.titleName = NSLocalizedString("New PDF Document", comment: "")
  1784. item?.boxImagePosition = .imageExpandLeft
  1785. item?.needExpandAction = true
  1786. let menuItem = NSMenuItem.init(title: "New PDF Document", action: nil, target: self)
  1787. let subMenu = NSMenu()
  1788. subMenu.addItem(title: NSLocalizedString("New Blank Page", comment: ""), action: #selector(_newPDFAction), target: self, tag: 1)
  1789. subMenu.addItem(title: NSLocalizedString("New From Images", comment: ""), action: #selector(_newPDFAction), target: self, tag: 2)
  1790. subMenu.addItem(NSMenuItem.separator())
  1791. subMenu.addItem(title: NSLocalizedString("New From Web Page", comment: ""), action: #selector(_newPDFAction), target: self, tag: 3)
  1792. subMenu.addItem(title: NSLocalizedString("Import From Camera", comment: ""), action: #selector(_newPDFAction), target: self, tag: 4)
  1793. subMenu.addItem(NSMenuItem.separator())
  1794. subMenu.addItem(title: NSLocalizedString("Import From Scanner", comment: ""), action: #selector(_newPDFAction), target: self, tag: 5)
  1795. menuItem.submenu = subMenu
  1796. item?.menuFormRepresentation = menuItem
  1797. }
  1798. }
  1799. @objc func convertToImage(_ sender: KMToolbarItem) {
  1800. // if let subMenu = sender.menuFormRepresentation?.submenu {
  1801. // subMenu.popUp(positioning: nil, at: sender.frame.origin, in: sender.superview)
  1802. // }
  1803. }
  1804. private func _setupFillSignItem(_ item: KMToolbarItemView?) {
  1805. let identifier = item?.itemIdentifier
  1806. if identifier == KMToolbarToolHookItemIdentifier {
  1807. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignTick)
  1808. item?.target = self
  1809. item?.btnTag = CAnnotationType.signTure.rawValue
  1810. item?.toolTip = NSLocalizedString("Signature", comment: "")
  1811. item?.titleName = NSLocalizedString("Signature", comment: "")
  1812. item?.boxImagePosition = .imageOnly
  1813. item?.btnAction = #selector(changeAnnotationMode)
  1814. } else if identifier == KMToolbarToolForkItemIdentifier {
  1815. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignFork)
  1816. item?.target = self
  1817. item?.btnTag = CAnnotationType.signFalse.rawValue
  1818. item?.toolTip = NSLocalizedString("Add X", comment: "")
  1819. item?.boxImagePosition = .imageOnly
  1820. item?.btnAction = #selector(changeAnnotationMode)
  1821. } else if identifier == KMToolbarToolLineItemIdentifier {
  1822. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignLine)
  1823. item?.target = self
  1824. item?.btnTag = CAnnotationType.signLine.rawValue
  1825. item?.toolTip = NSLocalizedString("Add line", comment: "")
  1826. item?.boxImagePosition = .imageOnly
  1827. item?.btnAction = #selector(changeAnnotationMode)
  1828. } else if identifier == KMToolbarToolDotItemIdentifier {
  1829. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignPoint)
  1830. item?.target = self
  1831. item?.btnTag = CAnnotationType.signDot.rawValue
  1832. item?.toolTip = NSLocalizedString("Add dot", comment: "")
  1833. item?.boxImagePosition = .imageOnly
  1834. item?.btnAction = #selector(changeAnnotationMode)
  1835. } else if identifier == KMToolbarToolRectangleIdentifier {
  1836. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignRoundedrec)
  1837. item?.target = self
  1838. item?.btnTag = CAnnotationType.signCircle.rawValue
  1839. item?.toolTip = NSLocalizedString("Add circle", comment: "")
  1840. item?.boxImagePosition = .imageOnly
  1841. item?.btnAction = #selector(changeAnnotationMode)
  1842. } else if identifier == KMToolbarToolTextIdentifier {
  1843. item?.image = NSImage(named: KMImageNameToolbarFreeText)
  1844. item?.target = self
  1845. item?.btnTag = CAnnotationType.signText.rawValue
  1846. item?.toolTip = NSLocalizedString("Add text", comment: "")
  1847. item?.titleName = NSLocalizedString("Text", comment: "")
  1848. item?.boxImagePosition = .imageOnly
  1849. item?.btnAction = #selector(changeAnnotationMode)
  1850. } else if identifier == KMToolbarToolDateIdentifier {
  1851. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignDate)
  1852. item?.target = self
  1853. item?.btnTag = CAnnotationType.signDate.rawValue
  1854. item?.toolTip = NSLocalizedString("Date", comment: "")
  1855. item?.boxImagePosition = .imageOnly
  1856. item?.btnAction = #selector(changeAnnotationMode)
  1857. } else if (identifier == KMToolbarToolProfileIdentifier) {
  1858. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignMyprofile)
  1859. item?.target = self
  1860. item?.toolTip = NSLocalizedString("My Profile", comment: "")
  1861. item?.boxImagePosition = .imageOnly
  1862. item?.btnAction = #selector(_itemAction)
  1863. }else if (identifier == KMToolbarFillSignSignatureIdentifier) {
  1864. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFillsignMysignature)
  1865. item?.target = self
  1866. item?.btnTag = CAnnotationType.signSignature.rawValue
  1867. item?.toolTip = NSLocalizedString("Sign document by typing or drawing a signature", comment: "")
  1868. item?.boxImagePosition = .imageOnly
  1869. item?.btnAction = #selector(changeAnnotationMode)
  1870. }
  1871. }
  1872. private func _setupFormItem(_ item: KMToolbarItemView?) {
  1873. let identifier = item?.itemIdentifier
  1874. if identifier == KMToolbarToolTextFieldItemIdentifier {
  1875. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormTextfield)
  1876. item?.target = self
  1877. item?.btnTag = CAnnotationType.textField.rawValue
  1878. item?.toolTip = NSLocalizedString("Text Field", comment: "")
  1879. item?.titleName = NSLocalizedString("Text Field", comment: "")
  1880. item?.boxImagePosition = .imageLeft
  1881. item?.btnAction = #selector(changeAnnotationMode)
  1882. let menuItem = NSMenuItem.init(title: "TextField", action: #selector(changeAnnotationMode), target: self)
  1883. menuItem.tag = item!.btnTag
  1884. item?.menuFormRepresentation = menuItem
  1885. } else if identifier == KMToolbarToolCheckBoxItemIdentifier {
  1886. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormCheckbox)
  1887. item?.target = self
  1888. item?.btnTag = CAnnotationType.checkBox.rawValue
  1889. item?.toolTip = NSLocalizedString("Check Box", comment: "")
  1890. item?.titleName = NSLocalizedString("Check Box", comment: "")
  1891. item?.boxImagePosition = .imageLeft
  1892. item?.btnAction = #selector(changeAnnotationMode)
  1893. } else if identifier == KMToolbarToolRadioButtonItemIdentifier {
  1894. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormRadiobutton)
  1895. item?.target = self
  1896. item?.btnTag = CAnnotationType.radioButton.rawValue
  1897. item?.toolTip = NSLocalizedString("Radio Button", comment: "")
  1898. item?.titleName = NSLocalizedString("Radio Button", comment: "")
  1899. item?.boxImagePosition = .imageLeft
  1900. item?.btnAction = #selector(changeAnnotationMode)
  1901. let menuItem = NSMenuItem.init(title: "RadioButton", action: #selector(changeAnnotationMode), target: self)
  1902. menuItem.tag = item!.btnTag
  1903. item?.menuFormRepresentation = menuItem
  1904. } else if identifier == KMToolbarToolListBoxItemIdentifier {
  1905. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormListbox)
  1906. item?.target = self
  1907. item?.btnTag = CAnnotationType.listMenu.rawValue
  1908. item?.toolTip = NSLocalizedString("List Box", comment: "")
  1909. item?.titleName = NSLocalizedString("List Box", comment: "")
  1910. item?.boxImagePosition = .imageLeft
  1911. item?.btnAction = #selector(changeAnnotationMode)
  1912. let menuItem = NSMenuItem.init(title: "List Box", action: #selector(changeAnnotationMode), target: self)
  1913. menuItem.tag = item!.btnTag
  1914. item?.menuFormRepresentation = menuItem
  1915. } else if identifier == KMToolbarToolPullDownmenuItemIdentifier {
  1916. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormPulldownmenu)
  1917. item?.target = self
  1918. item?.btnTag = CAnnotationType.comboBox.rawValue
  1919. item?.toolTip = NSLocalizedString("Combo Box", comment: "")
  1920. item?.titleName = NSLocalizedString("Combo Box", comment: "")
  1921. item?.boxImagePosition = .imageLeft
  1922. item?.btnAction = #selector(changeAnnotationMode)
  1923. let menuItem = NSMenuItem.init(title: "PullDownmenu", action: #selector(changeAnnotationMode), target: self)
  1924. menuItem.tag = item!.btnTag
  1925. item?.menuFormRepresentation = menuItem
  1926. } else if identifier == KMToolbarToolButtonIdentifier {
  1927. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormButton)
  1928. item?.target = self
  1929. item?.btnTag = CAnnotationType.actionButton.rawValue
  1930. item?.toolTip = NSLocalizedString("Button", comment: "")
  1931. item?.titleName = NSLocalizedString("Button", comment: "")
  1932. item?.boxImagePosition = .imageLeft
  1933. item?.btnAction = #selector(changeAnnotationMode)
  1934. let menuItem = NSMenuItem.init(title: "Button", action: #selector(changeAnnotationMode), target: self)
  1935. menuItem.tag = item!.btnTag
  1936. item?.menuFormRepresentation = menuItem
  1937. } else if identifier == KMToolbarToolSignBoxIdentifier {
  1938. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarFormSign)
  1939. item?.target = self
  1940. item?.btnTag = CAnnotationType.signature.rawValue
  1941. item?.toolTip = NSLocalizedString("Signature", comment: "")
  1942. item?.titleName = NSLocalizedString("Signature", comment: "")
  1943. item?.boxImagePosition = .imageLeft
  1944. item?.btnAction = #selector(changeAnnotationMode)
  1945. let menuItem = NSMenuItem.init(title: "SignBox", action: #selector(changeAnnotationMode), target: self)
  1946. menuItem.tag = item!.btnTag
  1947. item?.menuFormRepresentation = menuItem
  1948. } else if identifier == KMToolbarToolFormAlignIdentifier {
  1949. item?.image = NSImage(named: KMImageNameUXIconPropertybarAlignLeftToor)
  1950. item?.target = self
  1951. // item?.toolTip = NSLocalizedString("Alignment", comment: "")
  1952. item?.titleName = NSLocalizedString("", comment: "")
  1953. item?.boxImagePosition = .imageExpandLeft
  1954. item?.needExpandAction = true
  1955. item?.btnAction = #selector(formsAlignmentAction)
  1956. item?.pdfView = self.pdfView
  1957. let menuItem = NSMenuItem.init(title: "Alignment", action: nil, target: self)
  1958. let subMenu = NSMenu()
  1959. subMenu.addItem(title: NSLocalizedString("Align Left", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignLeftNor", action: #selector(formsAlignmentAction), target: self, tag: 0)
  1960. subMenu.addItem(title: NSLocalizedString("Align Vertically", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignVercenterNor", action: #selector(formsAlignmentAction), target: self, tag: 1)
  1961. subMenu.addItem(title: NSLocalizedString("Align Right", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignRightNor", action: #selector(formsAlignmentAction), target: self, tag: 2)
  1962. subMenu.addItem(NSMenuItem.separator())
  1963. subMenu.addItem(title: NSLocalizedString("Align Top", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignUpNor", action: #selector(formsAlignmentAction), target: self, tag: 3)
  1964. subMenu.addItem(title: NSLocalizedString("Align Horizontally", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignHorcenterNor", action: #selector(formsAlignmentAction), target: self, tag: 4)
  1965. subMenu.addItem(title: NSLocalizedString("Align Bottom", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignDownNor", action: #selector(formsAlignmentAction), target: self, tag: 5)
  1966. subMenu.addItem(NSMenuItem.separator())
  1967. subMenu.addItem(title: NSLocalizedString("Distribute Horizontally", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignHorsplitNor", action: #selector(formsAlignmentAction), target: self, tag: 6)
  1968. subMenu.addItem(title: NSLocalizedString("Distribute Vertically", comment: ""), imageNamed: "KMImageNameUXIconPropertybarAlignVersplitNor", action: #selector(formsAlignmentAction), target: self, tag: 7)
  1969. menuItem.submenu = subMenu
  1970. menuItem.tag = item!.btnTag
  1971. item?.menuFormRepresentation = menuItem
  1972. } else if identifier == KMToolbarToolFormMoreIdentifier {
  1973. item?.image = NSImage(named: "KMImageNameMainToolFormMore")
  1974. item?.target = self
  1975. item?.toolTip = NSLocalizedString("More", comment: "")
  1976. item?.titleName = NSLocalizedString("More", comment: "")
  1977. item?.boxImagePosition = .imageExpandLeft
  1978. item?.btnAction = #selector(formsMoreOperationAction)
  1979. let menuItem = NSMenuItem.init(title: "More", action: #selector(formsMoreOperationAction), target: self)
  1980. menuItem.tag = item!.btnTag
  1981. item?.menuFormRepresentation = menuItem
  1982. }
  1983. }
  1984. private func _setupViewDisplayItem(_ item: KMToolbarItemView?) {
  1985. let identifier = item?.itemIdentifier
  1986. if identifier == KMToolbarViewSettingIdentifier {
  1987. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarDisplayView)
  1988. item?.target = self
  1989. item?.toolTip = NSLocalizedString("View Settings", comment: "")
  1990. item?.titleName = NSLocalizedString("View Settings", comment: "")
  1991. item?.boxImagePosition = .imageLeft
  1992. item?.btnAction = #selector(_itemAction)
  1993. } else if identifier == KMToolbarCropIdentifier {
  1994. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarDisplayTailor)
  1995. item?.target = self
  1996. // item?.toolTip = NSLocalizedString("Note: Cropping a PDF hides some of the PDF’s content without removing it.", comment: "")
  1997. item?.titleName = NSLocalizedString("Crop", comment: "")
  1998. item?.boxImagePosition = .imageExpandLeft
  1999. item?.needExpandAction = true
  2000. // item?.btnAction = #selector(cropMenuAction)
  2001. let menuItem = NSMenuItem.init(title: "Crop", action: #selector(cropMenuAction), target: self)
  2002. let subMenu = NSMenu()
  2003. // subMenu.addItem(title: NSLocalizedString("Crop All Pages", comment: ""), action: #selector(_cropAction), target: self, tag: 1)
  2004. subMenu.addItem(title: NSLocalizedString("Crop Current Page", comment: ""), action: #selector(_cropAction), target: self, tag: 2)
  2005. subMenu.addItem(title: NSLocalizedString("Auto Crop – Separate", comment: ""), action: #selector(_cropAction), target: self, tag: 3)
  2006. subMenu.addItem(title: NSLocalizedString("Auto Crop – Combined", comment: ""), action: #selector(_cropAction), target: self, tag: 4)
  2007. subMenu.addItem(title: NSLocalizedString("Select Area", comment: ""), action: #selector(_cropAction), target: self, tag: 5)
  2008. // subMenu.addItem(title: NSLocalizedString("Crop Options...", comment: ""), action: #selector(_cropAction), target: self, tag: 6)
  2009. menuItem.submenu = subMenu
  2010. item?.menuFormRepresentation = menuItem
  2011. } else if identifier == KMToolbarSnapshotIdentifier {
  2012. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarDisplaySnapshot)
  2013. item?.target = self
  2014. item?.toolTip = NSLocalizedString("Take Snapshot", comment: "")
  2015. item?.titleName = NSLocalizedString("Snapshot", comment: "")
  2016. item?.boxImagePosition = .imageLeft
  2017. item?.btnAction = #selector(_itemAction)
  2018. } else if identifier == KMToolbarTTSIdentifier {
  2019. item?.image = NSImage(named: KMImageNameUXIconSubtoolbarDisplayTTS)
  2020. item?.target = self
  2021. item?.toolTip = NSLocalizedString("TTS (Text-to-speech)", comment: "")
  2022. item?.titleName = NSLocalizedString("TTS", comment: "")
  2023. item?.boxImagePosition = .imageLeft
  2024. item?.btnAction = #selector(_itemAction)
  2025. }
  2026. }
  2027. private func _setupToolItem(_ item: KMToolbarItemView?) {
  2028. let identifier = item?.itemIdentifier
  2029. if identifier == KMToolbarToolCropItemIdentifier {
  2030. item?.image = NSImage(named: "KMImageNameMainToolsCrop")
  2031. item?.alternateImage = NSImage(named: "KMImageNameMainToolsCrop")
  2032. item?.target = self
  2033. item?.btnTag = KMToolbarType.crop.rawValue
  2034. item?.toolTip = NSLocalizedString("Crop pages", comment: "")
  2035. item?.titleName = NSLocalizedString("Crop", comment: "")
  2036. item?.boxImagePosition = .imageExpandLeft
  2037. item?.btnAction = #selector(cropMenuAction)
  2038. let menuItem = NSMenuItem.init(title: "Crop", action: #selector(cropMenuAction), target: self)
  2039. item?.menuFormRepresentation = menuItem
  2040. } else if identifier == KMToolbarComparisonItemIdentifier {
  2041. item?.image = NSImage(named: KMImageNameUXIconBatchComparison)
  2042. item?.target = self
  2043. item?.btnTag = KMToolbarType.comparison.rawValue
  2044. // item?.toolTip = NSLocalizedString("Compare", comment: "")
  2045. item?.titleName = NSLocalizedString("Compare", comment: "")
  2046. item?.boxImagePosition = .imageExpandLeft
  2047. item?.needExpandAction = true
  2048. // item?.btnAction = #selector(_itemAction)
  2049. let menuItem = NSMenuItem.init(title: "Bates Number", action: #selector(_menuItemAction), target: self)
  2050. let subMenu = NSMenu()
  2051. subMenu.addItem(title: NSLocalizedString("Side-by-Side View", comment: ""), action: #selector(_comparisonAction), target: self, tag: 1)
  2052. subMenu.addItem(title: NSLocalizedString("Compare by File Overlay", comment: ""), action: #selector(_comparisonAction), target: self, tag: 2)
  2053. menuItem.submenu = subMenu
  2054. item?.menuFormRepresentation = menuItem
  2055. } else if identifier == KMToolbarToolBatesItemIdentifier {
  2056. item?.image = NSImage(named: "KMImageNameUXIconBatchBatesNor")
  2057. item?.target = self
  2058. item?.btnTag = KMToolbarType.bates.rawValue
  2059. // item?.toolTip = NSLocalizedString("Add PDF Bates numbering to your legal, medical, or business documents", comment: "")
  2060. item?.titleName = NSLocalizedString("Bates Numbers", comment: "")
  2061. item?.boxImagePosition = .imageExpandLeft
  2062. item?.needExpandAction = true
  2063. // item?.btnAction = #selector(batesMenuAction)
  2064. let menuItem = NSMenuItem.init(title: "Bates Numbers", action: #selector(batesMenuAction), target: self)
  2065. let subMenu = NSMenu()
  2066. subMenu.addItem(title: NSLocalizedString("Add Bates Numbers", comment: ""), action: #selector(_batesAction), target: self, tag: 1)
  2067. subMenu.addItem(title: NSLocalizedString("Remove Bates Numbers", comment: ""), action: #selector(_batesAction), target: self, tag: 2)
  2068. subMenu.addItem(title: NSLocalizedString("Batch Add Batch Numbers", comment: ""), action: #selector(_batesAction), target: self, tag: 3)
  2069. subMenu.addItem(title: NSLocalizedString("Batch Remove Bates Numbers", comment: ""), action: #selector(_batesAction), target: self, tag: 4)
  2070. subMenu.addItem(title: NSLocalizedString("Manage Templates", comment: ""), action: #selector(_batesAction), target: self, tag: 5)
  2071. menuItem.submenu = subMenu
  2072. item?.menuFormRepresentation = menuItem
  2073. } else if identifier == KMToolbarAIItemIdentifier {
  2074. item?.image = NSImage(named: "KMImageNameUXIconAINor")
  2075. item?.target = self
  2076. item?.btnTag = KMToolbarType.aiTools.rawValue
  2077. item?.titleName = NSLocalizedString("AI Tools", comment: "")
  2078. item?.boxImagePosition = .imageExpandLeft
  2079. item?.needExpandAction = true
  2080. let menuItem = NSMenuItem.init(title: "AI Tools", action: #selector(batesMenuAction), target: self)
  2081. let subMenu = NSMenu()
  2082. subMenu.addItem(title: NSLocalizedString("AI Summarize", comment: ""), action: #selector(_AIAction), target: self, tag: 1)
  2083. subMenu.addItem(title: NSLocalizedString("AI Rewrite", comment: ""), action: #selector(_AIAction), target: self, tag: 2)
  2084. subMenu.addItem(title: NSLocalizedString("AI Proofread", comment: ""), action: #selector(_AIAction), target: self, tag: 3)
  2085. subMenu.addItem(title: NSLocalizedString("AI Translate", comment: ""), action: #selector(_AIAction), target: self, tag: 4)
  2086. menuItem.submenu = subMenu
  2087. item?.menuFormRepresentation = menuItem
  2088. } else if identifier == KMToolbarToolHeaderFooterItemIdentifier {
  2089. item?.image = NSImage(named: "KMImageNameUXIconBatchHeaderandfooterNor")
  2090. item?.target = self
  2091. item?.btnTag = KMToolbarType.headerAndFooter.rawValue
  2092. // item?.toolTip = NSLocalizedString("Insert header, footer, and page numbers to PDF", comment: "")
  2093. item?.titleName = NSLocalizedString("Header & Footer", comment: "")
  2094. item?.boxImagePosition = .imageExpandLeft
  2095. item?.needExpandAction = true
  2096. // item?.btnAction = #selector(headerfooterMenuAction)
  2097. let menuItem = NSMenuItem.init(title: "Header & Footer", action: #selector(headerfooterMenuAction), target: self)
  2098. let subMenu = NSMenu()
  2099. subMenu.addItem(title: NSLocalizedString("Add Header & Footer", comment: ""), action: #selector(_headerfooterAction), target: self, tag: 1)
  2100. subMenu.addItem(title: NSLocalizedString("Remove Header & Footer", comment: ""), action: #selector(_headerfooterAction), target: self, tag: 2)
  2101. subMenu.addItem(title: NSLocalizedString("Batch Add Header & Footer", comment: ""), action: #selector(_headerfooterAction), target: self, tag: 3)
  2102. subMenu.addItem(title: NSLocalizedString("Batch Remove Header & Footer", comment: ""), action: #selector(_headerfooterAction), target: self, tag: 4)
  2103. subMenu.addItem(title: NSLocalizedString("Manage Templates", comment: ""), action: #selector(_headerfooterAction), target: self, tag: 5)
  2104. menuItem.submenu = subMenu
  2105. item?.menuFormRepresentation = menuItem
  2106. } else if identifier == KMToolbarToolBackgroundItemIdentifier {
  2107. item?.image = NSImage(named: "KMImageNameUXIconBatchBackgroundNor")
  2108. item?.target = self
  2109. item?.btnTag = KMToolbarType.background.rawValue
  2110. // item?.toolTip = NSLocalizedString("Insert PDF page background by color or image", comment: "")
  2111. item?.titleName = NSLocalizedString("Background", comment: "")
  2112. item?.boxImagePosition = .imageExpandLeft
  2113. item?.needExpandAction = true
  2114. // item?.btnAction = #selector(backgroundMenuAction)
  2115. let menuItem = NSMenuItem.init(title: "Background", action: #selector(backgroundMenuAction), target: self)
  2116. let subMenu = NSMenu()
  2117. subMenu.addItem(title: NSLocalizedString("Add Background", comment: ""), action: #selector(_backgroundAction), target: self, tag: 1)
  2118. subMenu.addItem(title: NSLocalizedString("Remove Background", comment: ""), action: #selector(_backgroundAction), target: self, tag: 2)
  2119. subMenu.addItem(title: NSLocalizedString("Batch Add Background", comment: ""), action: #selector(_backgroundAction), target: self, tag: 3)
  2120. subMenu.addItem(title: NSLocalizedString("Batch Remove Background", comment: ""), action: #selector(_backgroundAction), target: self, tag: 4)
  2121. subMenu.addItem(title: NSLocalizedString("Manage Templates", comment: ""), action: #selector(_backgroundAction), target: self, tag: 5)
  2122. menuItem.submenu = subMenu
  2123. item?.menuFormRepresentation = menuItem
  2124. } else if identifier == KMToolbarToolWatermarkItemIdentifier {
  2125. item?.image = NSImage(named: "KMImageNameUXIconBatchWatermarkNor")
  2126. item?.target = self
  2127. item?.btnTag = KMToolbarType.watermark.rawValue
  2128. // item?.toolTip = NSLocalizedString("Watermark", comment: "")
  2129. item?.titleName = NSLocalizedString("Watermark", comment: "")
  2130. item?.boxImagePosition = .imageExpandLeft
  2131. item?.needExpandAction = true
  2132. // item?.btnAction = #selector(watermarkMenuAction)
  2133. let menuItem = NSMenuItem.init(title: "Watermark", action: #selector(watermarkMenuAction), target: self)
  2134. let subMenu = NSMenu()
  2135. subMenu.addItem(title: NSLocalizedString("Add Watermark", comment: ""), action: #selector(_watermarkAction), target: self, tag: 1)
  2136. subMenu.addItem(title: NSLocalizedString("Remove Watermark", comment: ""), action: #selector(_watermarkAction), target: self, tag: 2)
  2137. subMenu.addItem(title: NSLocalizedString("Batch Add Watermarks", comment: ""), action: #selector(_watermarkAction), target: self, tag: 3)
  2138. subMenu.addItem(title: NSLocalizedString("Batch Remove Watermarks", comment: ""), action: #selector(_watermarkAction), target: self, tag: 4)
  2139. subMenu.addItem(title: NSLocalizedString("Manage Templates", comment: ""), action: #selector(_watermarkAction), target: self, tag: 5)
  2140. menuItem.submenu = subMenu
  2141. item?.menuFormRepresentation = menuItem
  2142. } else if identifier == KMToolbarToolCompressItemIdentifier {
  2143. item?.image = NSImage(named: "KMImageNameUXIconBatchOptimizeNor")
  2144. item?.target = self
  2145. item?.btnTag = KMToolbarType.compress.rawValue
  2146. item?.boxImagePosition = .imageLeft
  2147. item?.toolTip = NSLocalizedString("Reduce file size", comment: "")
  2148. item?.titleName = NSLocalizedString("Compress", comment: "")
  2149. item?.btnAction = #selector(compressMenuAction)
  2150. } else if identifier == KMToolbarToolSecureItemIdentifier {
  2151. item?.image = NSImage(named: "KMImageNameUXIconBatchSafeNor")
  2152. item?.target = self
  2153. // item?.toolTip = NSLocalizedString("Batch encrypting PDF documents", comment: "")
  2154. item?.titleName = NSLocalizedString("Security", comment: "")
  2155. item?.boxImagePosition = .imageExpandLeft
  2156. item?.needExpandAction = true
  2157. // item?.btnAction = #selector(secureMenuAction)
  2158. let menuItem = NSMenuItem.init(title: NSLocalizedString("Security", comment: ""), action: #selector(secureMenuAction), target: self)
  2159. let subMenu = NSMenu()
  2160. subMenu.addItem(title: NSLocalizedString("Set Passwords", comment: ""), action: #selector(_secureAction), target: self, tag: 1)
  2161. subMenu.addItem(title: NSLocalizedString("Remove Security", comment: ""), action: #selector(_secureAction), target: self, tag: 2)
  2162. menuItem.submenu = subMenu
  2163. item?.menuFormRepresentation = menuItem
  2164. }else if identifier == KMToolbarToolMergeItemIdentifier {
  2165. item?.image = NSImage(named: "KMImageNameUXIconBatchMergeNor")
  2166. item?.target = self
  2167. item?.btnTag = KMToolbarType.merge.rawValue
  2168. item?.toolTip = NSLocalizedString("Combine multiple documents into a new PDF, or borrow individual pages", comment: "")
  2169. item?.titleName = NSLocalizedString("Merge", comment: "")
  2170. item?.boxImagePosition = .imageLeft
  2171. item?.btnAction = #selector(mergeMenuItemAction)
  2172. }
  2173. }
  2174. }
  2175. extension KMToolbarViewController: NSMenuItemValidation {
  2176. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  2177. if menuItem.action == #selector(toggleCaseInsensitiveSearch) {
  2178. menuItem.state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey) == 1 ? .on : .off
  2179. } else if menuItem.action == #selector(formsAlignmentAction) {
  2180. let cnt = self.pdfView?.activeAnnotations.count ?? 0
  2181. if menuItem.tag < 6 {
  2182. if cnt >= 2 {
  2183. return true
  2184. }
  2185. } else {
  2186. if cnt >= 3 {
  2187. return true
  2188. }
  2189. }
  2190. return false
  2191. }
  2192. return true
  2193. }
  2194. }
  2195. extension KMToolbarViewController {
  2196. func isEnable(isEnable: Bool = true) {
  2197. for item in toolbarItems {
  2198. let itemView: KMToolbarMainItemView = item.value as? KMToolbarMainItemView ?? KMToolbarMainItemView()
  2199. itemView.unEnabled = !isEnable
  2200. itemView.isSelected = false
  2201. if let cv = itemView.customizeView {
  2202. self.subviewsdEnable(view: cv, isEnable: isEnable)
  2203. }
  2204. }
  2205. }
  2206. func subviewsdEnable(view: NSView, isEnable: Bool) {
  2207. if let data = view as? NSButton {
  2208. data.isEnabled = isEnable
  2209. }
  2210. if let data = (view as? NSTextField) {
  2211. data.isEnabled = isEnable
  2212. }
  2213. for subview in view.subviews {
  2214. if subview.subviews.count != 0 {
  2215. self.subviewsdEnable(view: subview, isEnable: isEnable)
  2216. }
  2217. if let data = subview as? NSButton {
  2218. data.isEnabled = isEnable
  2219. }
  2220. if let data = (subview as? NSTextField) {
  2221. data.isEnabled = isEnable
  2222. }
  2223. }
  2224. }
  2225. }
  2226. extension KMToolbarViewController {
  2227. func refreshSearchBarMenu() {
  2228. let words = UserDefaults.standard.array(forKey: "kmDocumentSearchWordArrays") as? [String] ?? []
  2229. let searchMenu = NSMenu()
  2230. for word in words {
  2231. var truncatedWord = word
  2232. if word.count > 15 {
  2233. truncatedWord = String(word.prefix(15)) + "..."
  2234. }
  2235. let item = searchMenu.addItem(withTitle: truncatedWord, action: #selector(searchPDFKeyWord(_:)), keyEquivalent: "")
  2236. item.representedObject = word
  2237. }
  2238. let attributes: [NSAttributedString.Key: Any] = [.font: NSFont(name: "Helvetica", size: 12.0)!]
  2239. if !words.isEmpty {
  2240. let attributedString = NSAttributedString(string: NSLocalizedString("Search History", comment: ""), attributes: attributes)
  2241. let item = NSMenuItem()
  2242. item.attributedTitle = attributedString
  2243. searchMenu.insertItem(item, at: 0)
  2244. searchMenu.addItem(NSMenuItem.separator())
  2245. let attributedString1 = NSAttributedString(string: NSLocalizedString("Clear Search History", comment: ""), attributes: attributes)
  2246. let item1 = NSMenuItem()
  2247. item1.attributedTitle = attributedString1
  2248. item1.action = #selector(clearSearchWordHistory(_:))
  2249. item1.target = self
  2250. searchMenu.addItem(item1)
  2251. searchMenu.insertItem(NSMenuItem.separator(), at: 0)
  2252. }
  2253. let attributedString2 = NSAttributedString(string: NSLocalizedString("Ignore Case", comment: ""), attributes: attributes)
  2254. let ignoreItem = NSMenuItem()
  2255. ignoreItem.attributedTitle = attributedString2
  2256. ignoreItem.action = #selector(toggleCaseInsensitiveSearch)
  2257. ignoreItem.target = self
  2258. searchMenu.insertItem(ignoreItem, at: 0)
  2259. (self.searchField.cell as? NSSearchFieldCell)?.searchMenuTemplate = searchMenu
  2260. }
  2261. @objc func toggleCaseInsensitiveSearch(_ sender: NSMenuItem) {
  2262. var value = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  2263. KMDataManager.ud_set(value == 0 ? 1 : 0, forKey: SKCaseInsensitiveSearchKey)
  2264. // let caseInsensitive = UserDefaults.standard.bool(forKey: "SKCaseInsensitiveFindKey")
  2265. // UserDefaults.standard.set(!caseInsensitive, forKey: "SKCaseInsensitiveFindKey")
  2266. self.searchPDFKey(self.searchField.stringValue)
  2267. }
  2268. @objc func searchPDFKeyWord(_ item: NSMenuItem) {
  2269. guard let word = item.representedObject as? String else {
  2270. return
  2271. }
  2272. self.searchPDFKey(word)
  2273. }
  2274. func searchPDFKey(_ string: String) {
  2275. self.searchField.stringValue = string
  2276. self.delegate?.toolbarViewController?(self, searchAction: string, forward: true)
  2277. let word = string
  2278. var words = UserDefaults.standard.array(forKey: "kmDocumentSearchWordArrays") as? [String] ?? []
  2279. var mutableWords = words
  2280. if let index = mutableWords.firstIndex(of: word) {
  2281. mutableWords.remove(at: index)
  2282. }
  2283. mutableWords.insert(word, at: 0)
  2284. if mutableWords.count > 10 {
  2285. mutableWords.removeLast()
  2286. }
  2287. UserDefaults.standard.set(mutableWords, forKey: "kmDocumentSearchWordArrays")
  2288. UserDefaults.standard.synchronize()
  2289. refreshSearchBarMenu()
  2290. }
  2291. @objc func clearSearchWordHistory(_ sender: Any) {
  2292. UserDefaults.standard.removeObject(forKey: "kmDocumentSearchWordArrays")
  2293. UserDefaults.standard.synchronize()
  2294. refreshSearchBarMenu()
  2295. }
  2296. }