KMBatchOperateWindowController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //
  2. // KMBatchOperateWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/24.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. let kBatchOperateToolbarIdentifier = "kBatchOperateToolbarIdentifier"
  10. let kBatchOperateToolbar_ConvertIdentifier = "kBatchOperateToolbar_ConvertIdentifier"
  11. let kBatchOperateToolbar_MergeIdentifier = "kBatchOperateToolbar_MergeIdentifier"
  12. let kBatchOperateToolbar_CompressIdentifier = "kBatchOperateToolbar_CompressIdentifier"
  13. let kBatchOperateToolbar_OCRIdentifier = "kBatchOperateToolbar_OCRIdentifier"
  14. let kBatchOperateToolbar_SecurityIdentifier = "kBatchOperateToolbar_SecurityIdentifier"
  15. let kBatchOperateToolbar_WatermarkIdentifier = "kBatchOperateToolbar_WatermarkIdentifier"
  16. let kBatchOperateToolbar_BackgroundIdentifier = "kBatchOperateToolbar_BackgroundIdentifier"
  17. let kBatchOperateToolbar_HeadFooterIdentifier = "kBatchOperateToolbar_HeadFooterIdentifier"
  18. let kBatchOperateToolbar_BatesIdentifier = "kBatchOperateToolbar_BatesIdentifier"
  19. @objcMembers final class KMBatchOperateWindowController: KMBatchOperateBaseWindowController, NSWindowDelegate, KMCustomButtonViewPopDelegate, KMCustomButtonViewPopDataSource, NSToolbarDelegate{
  20. func numberOfLine(in button: KMCustomViewButton) -> Int {
  21. return 2
  22. }
  23. func string(for button: KMCustomViewButton, index: Int) -> String? {
  24. if button.isEqual(self.convertToolbarItemView) {
  25. if index == 0 {
  26. return NSLocalizedString("Create PDF", comment: "")
  27. } else {
  28. return NSLocalizedString("Export to other format", comment: "")
  29. }
  30. } else if button.isEqual(self.securityToolbarItemView) {
  31. if index == 0 {
  32. return NSLocalizedString("Set Passwords", comment: "")
  33. } else {
  34. return NSLocalizedString("Remove Security", comment: "")
  35. }
  36. } else if button.isEqual(self.watermarkToolbarItemView) {
  37. if index == 0 {
  38. return NSLocalizedString("Add Watermark", comment: "")
  39. } else {
  40. return NSLocalizedString("Remove Watermark", comment: "")
  41. }
  42. } else if button.isEqual(self.backgroundToolbarItemView) {
  43. if index == 0 {
  44. return NSLocalizedString("Add Background", comment: "")
  45. } else {
  46. return NSLocalizedString("Remove Background", comment: "")
  47. }
  48. } else if button.isEqual(self.headerfooterToolbarItemView) {
  49. if index == 0 {
  50. return NSLocalizedString("Add Header & Footer", comment: "")
  51. } else {
  52. return NSLocalizedString("Remove Header & Footer", comment: "")
  53. }
  54. } else if button.isEqual(self.batesToolbarItemView) {
  55. if index == 0 {
  56. return NSLocalizedString("Add Bates Numbers", comment: "")
  57. } else {
  58. return NSLocalizedString("Remove Bates Numbers", comment: "")
  59. }
  60. }
  61. return ""
  62. }
  63. func needInsertSeperateLine(_ button: KMCustomViewButton, index: Int) -> Bool {
  64. return false
  65. }
  66. func needHightLightLine(_ button: KMCustomViewButton, index: Int) -> Bool {
  67. if button.isEqual(self.convertToolbarItemView) {
  68. if index == 0 {
  69. if self.operateType == .CreatePDF {
  70. return true
  71. }
  72. } else if index == 1 {
  73. if self.operateType == .Convert {
  74. return true
  75. }
  76. }
  77. } else if button.isEqual(self.securityToolbarItemView) {
  78. if index == 0 {
  79. if self.operateType == .AddPassword {
  80. return true
  81. }
  82. } else if index == 1 {
  83. if self.operateType == .RemovePassword {
  84. return true
  85. }
  86. }
  87. } else if button.isEqual(self.watermarkToolbarItemView) {
  88. if index == 0 {
  89. if self.operateType == .AddWatermark {
  90. return true
  91. }
  92. } else if index == 1 {
  93. if self.operateType == .RemoveWatermark {
  94. return true
  95. }
  96. }
  97. } else if button.isEqual(self.backgroundToolbarItemView) {
  98. if index == 0 {
  99. if self.operateType == .AddBackground {
  100. return true
  101. }
  102. } else if index == 1 {
  103. if self.operateType == .RemoveBackground {
  104. return true
  105. }
  106. }
  107. } else if button.isEqual(self.headerfooterToolbarItemView) {
  108. if index == 0 {
  109. if self.operateType == .AddHeaderFooter {
  110. return true
  111. }
  112. } else if index == 1 {
  113. if self.operateType == .RemoveHeaderFooter {
  114. return true
  115. }
  116. }
  117. } else if button.isEqual(self.batesToolbarItemView) {
  118. if index == 0 {
  119. if self.operateType == .AddBates {
  120. return true
  121. }
  122. } else if index == 1 {
  123. if self.operateType == .RemoveBates {
  124. return true
  125. }
  126. }
  127. }
  128. return false
  129. }
  130. func customViewButton(_ button: KMCustomViewButton, didSelectIndex index: Int) {
  131. if button.isEqual(self.convertToolbarItemView) {
  132. if index == 0 {
  133. self.createPDF(sender: "")
  134. } else if index == 1 {
  135. self.convertPDFToOtherFormat(sender: "")
  136. }
  137. } else if button.isEqual(self.securityToolbarItemView) {
  138. if index == 0 {
  139. self.setPDFPassword(sender: "")
  140. } else if index == 1 {
  141. self.removePDFPassword(sender: "")
  142. }
  143. } else if button.isEqual(self.watermarkToolbarItemView) {
  144. if index == 0 {
  145. self.addWatermark(sender: "")
  146. } else if index == 1 {
  147. self.removeWatermark(sender: "")
  148. }
  149. } else if button.isEqual(self.backgroundToolbarItemView) {
  150. if index == 0 {
  151. self.addBackgroud(sender: "")
  152. } else if index == 1 {
  153. self.removeBackground(sender: "")
  154. }
  155. } else if button.isEqual(self.headerfooterToolbarItemView) {
  156. if index == 0 {
  157. self.addHeaderFooter(sender: "")
  158. } else if index == 1 {
  159. self.removeHeaderFooter(sender: "")
  160. }
  161. } else if button.isEqual(self.batesToolbarItemView) {
  162. if index == 0 {
  163. self.addBates(sender: "")
  164. } else if index == 1 {
  165. self.removeBates(sender: "")
  166. }
  167. }
  168. }
  169. lazy var convertToolbarItemView: KMCustomViewButton = {
  170. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchConvert"), backImage: nil, title: NSLocalizedString("Convert", comment: ""), type: .batchToolbar)
  171. item.addTarget(self, action: #selector(convertPDFToOtherFormat(sender:)))
  172. self.window?.contentView?.addSubview(item)
  173. return item
  174. }()
  175. lazy var mergeToolbarItemView: KMCustomViewButton = {
  176. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchMerge"), backImage: nil, title: NSLocalizedString("Merge", comment: ""), type: .batchToolbar)
  177. item.addTarget(self, action: #selector(merge(sender:)))
  178. self.window?.contentView?.addSubview(item)
  179. return item
  180. }()
  181. lazy var compressToolbarItemView: KMCustomViewButton = {
  182. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchOptimize"), backImage: nil, title: NSLocalizedString("Compress", comment: ""), type: .batchToolbar)
  183. item.addTarget(self, action: #selector(compress(sender:)))
  184. self.window?.contentView?.addSubview(item)
  185. return item
  186. }()
  187. lazy var ocrToolbarItemView: KMCustomViewButton = {
  188. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchConvertOCR"), backImage: nil, title: NSLocalizedString("OCR", comment: ""), type: .batchToolbar)
  189. item.addTarget(self, action: #selector(ocr(sender:)))
  190. self.window?.contentView?.addSubview(item)
  191. return item
  192. }()
  193. lazy var securityToolbarItemView: KMCustomViewButton = {
  194. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchSafe"), backImage: NSImage(named: "KMImageNameUXIconBtnTriDownNor"), title: NSLocalizedString("Security", comment: ""), type: .batchToolbar)
  195. item.delegate = self
  196. item.dataSource = self
  197. self.window?.contentView?.addSubview(item)
  198. item.mas_makeConstraints { make in
  199. make?.center.equalTo()(self.window?.contentView)
  200. }
  201. return item
  202. }()
  203. lazy var watermarkToolbarItemView: KMCustomViewButton = {
  204. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchWatermark"), backImage: NSImage(named: "KMImageNameUXIconBtnTriDownNor"), title: NSLocalizedString("Watermark", comment: ""), type: .batchToolbar)
  205. item.delegate = self
  206. item.dataSource = self
  207. self.window?.contentView?.addSubview(item)
  208. item.mas_makeConstraints { make in
  209. make?.center.equalTo()(self.window?.contentView)
  210. }
  211. return item
  212. }()
  213. lazy var backgroundToolbarItemView: KMCustomViewButton = {
  214. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchBackground"), backImage: NSImage(named: "KMImageNameUXIconBtnTriDownNor"), title: NSLocalizedString("Background", comment: ""), type: .batchToolbar)
  215. item.delegate = self
  216. item.dataSource = self
  217. self.window?.contentView?.addSubview(item)
  218. item.mas_makeConstraints { make in
  219. make?.center.equalTo()(self.window?.contentView)
  220. }
  221. return item
  222. }()
  223. lazy var headerfooterToolbarItemView: KMCustomViewButton = {
  224. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchHeaderandfooter"), backImage: NSImage(named: "KMImageNameUXIconBtnTriDownNor"), title: NSLocalizedString("Header & Footer", comment: ""), type: .batchToolbar)
  225. item.delegate = self
  226. item.dataSource = self
  227. self.window?.contentView?.addSubview(item)
  228. item.mas_makeConstraints { make in
  229. make?.center.equalTo()(self.window?.contentView)
  230. }
  231. return item
  232. }()
  233. lazy var batesToolbarItemView: KMCustomViewButton = {
  234. let item = KMCustomViewButton.init(frontImage: NSImage(named: "KMImageNameUXIconBatchBates"), backImage: NSImage(named: "KMImageNameUXIconBtnTriDownNor"), title: NSLocalizedString("Bates Numbers", comment: ""), type: .batchToolbar)
  235. item.delegate = self
  236. item.dataSource = self
  237. self.window?.contentView?.addSubview(item)
  238. item.mas_makeConstraints { make in
  239. make?.center.equalTo()(self.window?.contentView)
  240. }
  241. return item
  242. }()
  243. lazy var toolbarItemViews: NSMutableArray = {
  244. let arr = [self.convertToolbarItemView,
  245. self.mergeToolbarItemView,
  246. self.compressToolbarItemView,
  247. self.ocrToolbarItemView,
  248. self.securityToolbarItemView,
  249. self.watermarkToolbarItemView,
  250. self.backgroundToolbarItemView,
  251. self.headerfooterToolbarItemView,
  252. self.batesToolbarItemView
  253. ]
  254. return NSMutableArray(array: arr)
  255. }()
  256. override var operateType: KMBatchOperationType?{
  257. willSet{
  258. }
  259. didSet {
  260. if operateType == oldValue{
  261. return
  262. }else{
  263. super.operateType = operateType
  264. }
  265. for i in 0..<self.toolbarItemViews.count {
  266. let btn = self.toolbarItemViews[i] as! KMCustomViewButton
  267. btn.isSelected = false
  268. }
  269. switch operateType {
  270. case .CreatePDF:
  271. self.convertToolbarItemView.isSelected = true
  272. case .Convert:
  273. self.convertToolbarItemView.isSelected = true
  274. case .Merge:
  275. self.mergeToolbarItemView.isSelected = true
  276. case .Compress:
  277. self.compressToolbarItemView.isSelected = true
  278. case .OCR:
  279. self.ocrToolbarItemView.isSelected = true
  280. case .AddPassword,.RemovePassword:
  281. self.securityToolbarItemView.isSelected = true
  282. case .AddWatermark,.RemoveWatermark:
  283. self.watermarkToolbarItemView.isSelected = true
  284. case .AddBackground,.RemoveBackground:
  285. self.backgroundToolbarItemView.isSelected = true
  286. case .AddHeaderFooter,.RemoveHeaderFooter:
  287. self.headerfooterToolbarItemView.isSelected = true
  288. case .AddBates,.RemoveBates:
  289. self.batesToolbarItemView.isSelected = true
  290. default :
  291. self.convertToolbarItemView.isSelected = true
  292. }
  293. (self.contentViewController as! KMBatchOperateSplitViewController).switchToOperateType(operateType!)
  294. }
  295. }
  296. static let sharedWindowController: KMBatchOperateWindowController = {
  297. let wc = KMBatchOperateWindowController(windowNibName: "KMBatchOperateWindowController")
  298. let actualWindow = wc.window as! KMBatchWindow
  299. actualWindow.isBatch = true
  300. wc.switchToOperateType(.Convert, files:KMBatchOperateManager.defaultManager.files!)
  301. wc.switchToConvertType(convertType: IAPProductsManager.default().isAvailableAdvancedPDFToOffice() ? .WordAdvance : .WordStandard)
  302. wc.updateViewColor()
  303. return wc
  304. }()
  305. override func switchToOperateType(_ operateType: KMBatchOperationType, files: [KMBatchOperateFile]) {
  306. super.switchToOperateType(operateType, files: files)
  307. self.operateType = operateType
  308. }
  309. func updateViewColor() {
  310. var color = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  311. if KMAppearance.isDarkMode() {
  312. color = NSColor(red: 0.149, green: 0.157, blue: 0.169, alpha: 1)
  313. } else {
  314. color = NSColor(red: 0.988, green: 0.992, blue: 1.0, alpha: 1)
  315. }
  316. if let themeView = self.window?.contentView?.superview {
  317. for subView in themeView.subviews {
  318. if NSStringFromClass(type(of: subView)) == "NSTitlebarContainerView" {
  319. subView.layer?.backgroundColor = color.cgColor
  320. break
  321. }
  322. }
  323. }
  324. }
  325. func windowShouldClose(_ sender: NSWindow) -> Bool {
  326. if self.operateType == .Compress {
  327. let vc: KMBatchOperateRightViewController = (self.contentViewController as! KMBatchOperateSplitViewController).rightPropertiesViewController!
  328. let compress = vc.compressViewController
  329. var begin = false
  330. for info in compress!.files! {
  331. if info.status == KMBatchOperateStatus.processing {
  332. begin = true
  333. break
  334. }
  335. }
  336. if begin {
  337. let alert = NSAlert()
  338. alert.alertStyle = .critical
  339. alert.messageText = NSLocalizedString("Do you want to cancel compressing?", comment: "")
  340. alert.informativeText = NSLocalizedString("Documents which have been compressed will still be retained.", comment: "")
  341. alert.addButton(withTitle: NSLocalizedString("Cancel Compressing", comment: ""))
  342. alert.addButton(withTitle: NSLocalizedString("Continue compressing", comment: ""))
  343. alert.beginSheetModal(for: self.window!, completionHandler: { (response) in
  344. if response == .alertFirstButtonReturn {
  345. compress!.cancelBatchOperation()
  346. self.close()
  347. } else if response == .alertSecondButtonReturn {
  348. compress!.beginBatchOperation()
  349. }
  350. })
  351. return false
  352. }
  353. }
  354. return true
  355. }
  356. override func windowDidLoad() {
  357. super.windowDidLoad()
  358. if #available(macOS 11.0, *) {
  359. self.window?.toolbarStyle = NSWindow.ToolbarStyle.expanded
  360. }
  361. self.updateViewColor()
  362. self.window?.titlebarAppearsTransparent = true
  363. // self.toolbarItemViews
  364. DispatchQueue.main.async {
  365. self.configuToolbar()
  366. }
  367. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  368. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  369. }
  370. @objc func themeChanged(notification: NSNotification) {
  371. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  372. self.updateViewColor()
  373. }
  374. }
  375. func configuToolbar() {
  376. let toolbar = NSToolbar(identifier: kBatchOperateToolbarIdentifier)
  377. toolbar.delegate = self
  378. self.window?.toolbar = toolbar
  379. toolbar.allowsUserCustomization = false
  380. toolbar.displayMode = .iconOnly
  381. }
  382. @objc func convertPDFToOtherFormat(sender: Any) {
  383. self.operateType = .Convert
  384. }
  385. @objc func createPDF(sender: Any) {
  386. self.operateType = .CreatePDF
  387. }
  388. @objc func merge(sender: Any) {
  389. self.operateType = .Merge
  390. }
  391. @objc func compress(sender: Any) {
  392. self.operateType = .Compress
  393. }
  394. @objc func ocr(sender: Any) {
  395. self.operateType = .OCR
  396. }
  397. @objc func setPDFPassword(sender: Any) {
  398. self.operateType = .AddPassword
  399. }
  400. @objc func removePDFPassword(sender: Any) {
  401. self.operateType = .RemovePassword
  402. }
  403. @objc func addWatermark(sender: Any) {
  404. self.operateType = .AddWatermark
  405. }
  406. @objc func removeWatermark(sender: Any) {
  407. self.operateType = .RemoveWatermark
  408. }
  409. @objc func addBackgroud(sender: Any) {
  410. self.operateType = .AddBackground
  411. }
  412. @objc func removeBackground(sender: Any) {
  413. self.operateType = .RemoveBackground
  414. }
  415. @objc func addHeaderFooter(sender: Any) {
  416. self.operateType = .AddHeaderFooter
  417. }
  418. @objc func removeHeaderFooter(sender: Any) {
  419. self.operateType = .RemoveHeaderFooter
  420. }
  421. @objc func addBates(sender: Any) {
  422. self.operateType = .AddBates
  423. }
  424. @objc func removeBates(sender: Any) {
  425. self.operateType = .RemoveBates
  426. }
  427. func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  428. let arr = [NSToolbarItem.Identifier.flexibleSpace,
  429. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_ConvertIdentifier),
  430. NSToolbarItem.Identifier.flexibleSpace,
  431. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_CompressIdentifier),
  432. NSToolbarItem.Identifier.flexibleSpace,
  433. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_SecurityIdentifier),
  434. NSToolbarItem.Identifier.flexibleSpace,
  435. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_WatermarkIdentifier),
  436. NSToolbarItem.Identifier.flexibleSpace,
  437. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_BackgroundIdentifier),
  438. NSToolbarItem.Identifier.flexibleSpace,
  439. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_HeadFooterIdentifier),
  440. NSToolbarItem.Identifier.flexibleSpace,
  441. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_BatesIdentifier),
  442. NSToolbarItem.Identifier.flexibleSpace
  443. ]
  444. return arr
  445. }
  446. func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
  447. let arr = [
  448. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_ConvertIdentifier),
  449. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_CompressIdentifier),
  450. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_SecurityIdentifier),
  451. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_WatermarkIdentifier),
  452. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_BackgroundIdentifier),
  453. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_HeadFooterIdentifier),
  454. NSToolbarItem.Identifier(rawValue: kBatchOperateToolbar_BatesIdentifier)
  455. ]
  456. return arr
  457. }
  458. func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
  459. let item = NSToolbarItem(itemIdentifier: itemIdentifier)
  460. if itemIdentifier.rawValue == kBatchOperateToolbar_ConvertIdentifier {
  461. item.view = self.convertToolbarItemView
  462. item.toolTip = NSLocalizedString("Convert", comment: "")
  463. let menuItem = NSMenuItem(title: NSLocalizedString("Convert", comment: ""), action: #selector(convertPDFToOtherFormat(sender:)), keyEquivalent: "")
  464. menuItem.target = self
  465. item.menuFormRepresentation = menuItem
  466. } else if itemIdentifier.rawValue == kBatchOperateToolbar_MergeIdentifier {
  467. item.view = self.mergeToolbarItemView
  468. item.toolTip = NSLocalizedString("Merge", comment: "")
  469. let menuItem = NSMenuItem(title: NSLocalizedString("Merge", comment: ""), action: #selector(merge(sender:)), keyEquivalent: "")
  470. menuItem.target = self
  471. item.menuFormRepresentation = menuItem
  472. } else if itemIdentifier.rawValue == kBatchOperateToolbar_CompressIdentifier {
  473. item.view = self.compressToolbarItemView
  474. item.toolTip = NSLocalizedString("Compress", comment: "")
  475. let menuItem = NSMenuItem(title: NSLocalizedString("Compress", comment: ""), action: #selector(compress(sender:)), keyEquivalent: "")
  476. menuItem.target = self
  477. item.menuFormRepresentation = menuItem
  478. } else if itemIdentifier.rawValue == kBatchOperateToolbar_OCRIdentifier {
  479. item.view = self.ocrToolbarItemView
  480. item.toolTip = NSLocalizedString("OCR", comment: "")
  481. let menuItem = NSMenuItem(title: NSLocalizedString("OCR", comment: ""), action: #selector(ocr(sender:)), keyEquivalent: "")
  482. menuItem.target = self
  483. item.menuFormRepresentation = menuItem
  484. } else if itemIdentifier.rawValue == kBatchOperateToolbar_SecurityIdentifier {
  485. item.view = self.securityToolbarItemView
  486. let menuItem = NSMenuItem(title: NSLocalizedString("Security", comment: ""), action: nil, keyEquivalent: "")
  487. let menu = NSMenu()
  488. menu.addItem(withTitle: NSLocalizedString("Set Passwords", comment: ""), action: #selector(setPDFPassword(sender:)), keyEquivalent: "")
  489. menu.addItem(withTitle: NSLocalizedString("Remove Security", comment: ""), action: #selector(removePDFPassword(sender:)), keyEquivalent: "")
  490. menuItem.submenu = menu
  491. menuItem.target = self
  492. item.menuFormRepresentation = menuItem
  493. } else if itemIdentifier.rawValue == kBatchOperateToolbar_WatermarkIdentifier {
  494. item.view = self.watermarkToolbarItemView
  495. let menuItem = NSMenuItem(title: NSLocalizedString("Watermark", comment: ""), action: nil, keyEquivalent: "")
  496. let menu = NSMenu()
  497. menu.addItem(withTitle: NSLocalizedString("Add Watermark", comment: ""), action: #selector(addWatermark(sender:)), keyEquivalent: "")
  498. menu.addItem(withTitle: NSLocalizedString("Remove Watermark", comment: ""), action: #selector(removeWatermark(sender:)), keyEquivalent: "")
  499. menuItem.submenu = menu
  500. menuItem.target = self
  501. item.menuFormRepresentation = menuItem
  502. } else if itemIdentifier.rawValue == kBatchOperateToolbar_BackgroundIdentifier {
  503. item.view = self.backgroundToolbarItemView
  504. let menuItem = NSMenuItem(title: NSLocalizedString("Background", comment: ""), action: nil, keyEquivalent: "")
  505. let menu = NSMenu()
  506. menu.addItem(withTitle: NSLocalizedString("Add Background", comment: ""), action: #selector(addBackgroud(sender:)), keyEquivalent: "")
  507. menu.addItem(withTitle: NSLocalizedString("Remove Background", comment: ""), action: #selector(removeBackground(sender:)), keyEquivalent: "")
  508. menuItem.submenu = menu
  509. menuItem.target = self
  510. item.menuFormRepresentation = menuItem
  511. } else if itemIdentifier.rawValue == kBatchOperateToolbar_HeadFooterIdentifier {
  512. item.view = self.headerfooterToolbarItemView
  513. let menuItem = NSMenuItem(title: NSLocalizedString("Header & Footer", comment: ""), action: nil, keyEquivalent: "")
  514. let menu = NSMenu()
  515. menu.addItem(withTitle: NSLocalizedString("Add Header & Footer", comment: ""), action: #selector(addHeaderFooter(sender:)), keyEquivalent: "")
  516. menu.addItem(withTitle: NSLocalizedString("Remove Header & Footer", comment: ""), action: #selector(removeHeaderFooter(sender:)), keyEquivalent: "")
  517. menuItem.submenu = menu
  518. menuItem.target = self
  519. item.menuFormRepresentation = menuItem
  520. } else if itemIdentifier.rawValue == kBatchOperateToolbar_BatesIdentifier {
  521. item.view = self.batesToolbarItemView
  522. let menuItem = NSMenuItem(title: NSLocalizedString("Bates Numbers", comment: ""), action: nil, keyEquivalent: "")
  523. let menu = NSMenu()
  524. menu.addItem(withTitle: NSLocalizedString("Add Bates Numbers", comment: ""), action: #selector(addBates(sender:)), keyEquivalent: "")
  525. menu.addItem(withTitle: NSLocalizedString("Remove Bates Numbers", comment: ""), action: #selector(removeBates(sender:)), keyEquivalent: "")
  526. menuItem.submenu = menu
  527. menuItem.target = self
  528. item.menuFormRepresentation = menuItem
  529. }
  530. return item
  531. }
  532. func showIsConvertingHint() {
  533. let texHintWindowController = KMTextHintWindowController(windowNibName: "KMTextHintWindowController")
  534. self.window?.beginSheet(texHintWindowController.window!)
  535. }
  536. func windowDidBecomeKey(_ notification: Notification) {
  537. for i in 0..<self.toolbarItemViews.count {
  538. var btn = self.toolbarItemViews[i] as! KMCustomViewButton
  539. btn.enable = true
  540. }
  541. }
  542. func windowDidResignKey(_ notification: Notification) {
  543. for i in 0..<self.toolbarItemViews.count {
  544. let btn = self.toolbarItemViews[i] as! KMCustomViewButton
  545. btn.enable = false
  546. }
  547. }
  548. }