KMBatchOperateAddWatermarkViewController.swift 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. //
  2. // KMBatchOperateAddWatermarkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/3.
  6. //
  7. import Cocoa
  8. enum KMWatermarkType: Int {
  9. case txt = 0
  10. case file
  11. }
  12. class KMCollectionViewFlowLayout: NSCollectionViewFlowLayout {
  13. override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
  14. self.collectionView?.reloadSections(IndexSet(integer: 0))
  15. return true
  16. }
  17. }
  18. class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController {
  19. var isBackground = false
  20. var onlyManagerTemplate = false
  21. weak var pdfView: CPDFView?
  22. var isBatchOperation = false //是否在批量界面
  23. @IBOutlet var topBaseView: NSView!
  24. @IBOutlet var titleLabel: NSTextField!
  25. @IBOutlet var addButton: NSButton!
  26. @IBOutlet var textButton: NSButton!
  27. @IBOutlet var fileButton: NSButton!
  28. @IBOutlet var textBox: NSBox!
  29. @IBOutlet var fileBox: NSBox!
  30. @IBOutlet var actionButton: NSButton!
  31. @IBOutlet var collectionView: NSCollectionView!
  32. @IBOutlet var bottomBaseView: NSView!
  33. @IBOutlet var managerTemplateTitleLabel: NSTextField!
  34. @IBOutlet var blankView: KMBlankView!
  35. @IBOutlet var managerTemplateButtonTopConstraint: NSLayoutConstraint!
  36. @IBOutlet var managerTemplateButtonHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet var topHeightConstraint: NSLayoutConstraint!
  38. @IBOutlet var addButtonHeightConstraint: NSLayoutConstraint!
  39. @IBOutlet var addButtonBottomConstraint: NSLayoutConstraint!
  40. private var _textTemplateArray: [NSObject]?
  41. private var _fileTemplateArray: [NSObject]?
  42. private var _currentTextData: KMWatermarkModel?
  43. private var _currentFileData: KMWatermarkModel?
  44. private var _currentColorData: KMBackgroundModel?
  45. private var _currentImageData: KMBackgroundModel?
  46. private var _currentBackgroundType: CPDFBackgroundType = .color
  47. var currentBackgroundType: CPDFBackgroundType {
  48. get {
  49. return self._currentBackgroundType
  50. }
  51. set {
  52. if (self._currentBackgroundType != newValue) {
  53. self._currentBackgroundType = newValue
  54. if (self._currentBackgroundType == .color) {
  55. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  56. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  57. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  58. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  59. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  60. self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
  61. } else {
  62. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  63. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  64. self.textBox.fillColor = KMAppearance.Layout.l_1Color()
  65. self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
  66. self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
  67. self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
  68. }
  69. self.collectionView.reloadData()
  70. }
  71. }
  72. }
  73. private var _currentType: KMWatermarkType = .txt
  74. var currentType: KMWatermarkType {
  75. set {
  76. if self.currentType != newValue {
  77. self._currentType = newValue
  78. if (currentType == .txt) {
  79. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  80. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  81. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  82. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  83. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  84. self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
  85. } else {
  86. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  87. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  88. self.textBox.fillColor = KMAppearance.Layout.l_1Color()
  89. self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
  90. self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
  91. self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
  92. }
  93. self.collectionView.reloadData()
  94. }
  95. }
  96. get {
  97. return self._currentType
  98. }
  99. }
  100. private var _haveFiles = false
  101. deinit {
  102. KMPrint("KMBatchOperateAddWatermarkViewController deinit.")
  103. NotificationCenter.default.removeObserver(self)
  104. }
  105. override func viewDidLoad() {
  106. super.viewDidLoad()
  107. self._localizedlanguage()
  108. self._configuiUI()
  109. self._loadData()
  110. self.interfaceStatus = .PrepareProcess
  111. NotificationCenter.default.addObserver(self, selector: #selector(_watermarksNotification), name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: nil)
  112. }
  113. func watermarkInterfaceSelectWatermark(_ watermark: KMWatermarkModel) {
  114. if(watermark.image != nil) {
  115. self._currentFileData = watermark
  116. }else {
  117. self._currentTextData = watermark
  118. }
  119. self._loadData()
  120. self.currentType = watermark.image != nil ? .file : .txt
  121. let arr = self.currentType == .file ? self._fileTemplateArray : self._textTemplateArray
  122. let index = arr?.firstIndex(of: watermark)
  123. if (index != NSNotFound) {
  124. let indexpath = IndexPath(item: index!, section: 0)
  125. var set = Set<IndexPath>()
  126. set.insert(indexpath)
  127. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  128. }
  129. }
  130. func backgroundInterfaceSelectBackGround(_ background: KMBackgroundModel) {
  131. if(background.type == .image) {
  132. self._currentImageData = background
  133. }else {
  134. self._currentColorData = background
  135. }
  136. self._loadData()
  137. self.currentBackgroundType = background.type
  138. let arr = self.currentBackgroundType == .image ? self._fileTemplateArray : self._textTemplateArray
  139. let index = arr?.firstIndex(of: background)
  140. if (index != NSNotFound) {
  141. let indexpath = IndexPath(item: index!, section: 0)
  142. var set = Set<IndexPath>()
  143. set.insert(indexpath)
  144. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  145. }
  146. }
  147. override var interfaceStatus: KMBatchOperateInterfaceStatus? {
  148. didSet {
  149. if (self.interfaceStatus == .PrepareProcess) {
  150. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  151. var files: [URL] = []
  152. for url in self.successFilePathURLArray ?? [] {
  153. if FileManager.default.fileExists(atPath: url.path) {
  154. files.append(url)
  155. }
  156. }
  157. if (files.count > 0) {
  158. let workspace = NSWorkspace.shared
  159. workspace.activateFileViewerSelecting(files)
  160. }
  161. }
  162. self.collectionView.isSelectable = true
  163. self.actionButton.tag = 1
  164. // if (self.isBackground) {
  165. // self.actionButton.title = NSLocalizedString(@"Add Background");
  166. // } else {
  167. // self.actionButton.title = NSLocalizedString(@"Add Watermark");
  168. // }
  169. self.actionButton.title = KMLocalizedString("Apply")
  170. self.addButton.isEnabled = true
  171. // if (self.onlyManagerTemplate) {
  172. // self.actionButton.title = NSLocalizedString(@"Add Template");
  173. // }
  174. } else {
  175. self.collectionView.isSelectable = false
  176. self.addButton.isEnabled = false
  177. self.actionButton.tag = 0
  178. self.actionButton.title = KMLocalizedString("Cancel");
  179. }
  180. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  181. }
  182. }
  183. //button Actions,切换水印类型
  184. @IBAction func buttonClicked_SwitchWaterMarkType(_ sender: NSButton) {
  185. if (self.isBackground) {
  186. } else {
  187. self.currentType = KMWatermarkType(rawValue: sender.tag) ?? .txt
  188. }
  189. self._updateActionButtonbackgroundColor()
  190. }
  191. @IBAction func buttonClicked_addWaterMark(_ sender: NSButton) {
  192. if (!self.onlyManagerTemplate) {
  193. if (!self._haveFiles) {
  194. return
  195. }
  196. }
  197. self.view.window?.makeFirstResponder(nil)
  198. if (self.onlyManagerTemplate) {
  199. if (sender.tag == 1) {
  200. var watermark: KMWatermarkModel?
  201. var background: KMBackgroundModel?
  202. let indexSet = self._isSelectIndex()
  203. if (!indexSet) {
  204. return
  205. } else {
  206. if (!self.isBackground) {
  207. if (self.currentType == .txt) {
  208. watermark = self._currentTextData
  209. } else {
  210. watermark = self._currentFileData
  211. }
  212. } else {
  213. if (self.currentBackgroundType == .color) {
  214. background = self._currentColorData
  215. } else {
  216. background = self._currentImageData
  217. }
  218. }
  219. }
  220. let openPanel = NSOpenPanel()
  221. openPanel.canChooseFiles = false
  222. openPanel.canChooseDirectories = true
  223. openPanel.canCreateDirectories = true
  224. openPanel.beginSheetModal(for: self.view.window!) { result in
  225. if (result == .OK) {
  226. for fileURL in openPanel.urls {
  227. self.hiddenWindowCloseButtonIfNeeded()
  228. self.successFilePathURLArray?.removeAll()
  229. if (!self.isBackground) {
  230. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddWatermark)
  231. file.addWatermarkInfo.savePath = fileURL.path
  232. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  233. file.addWatermarkInfo.pageRangeString = watermark?.pagesString ?? ""
  234. let op = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark!)
  235. op.delegate = self
  236. self.queue?.addOperation(op)
  237. } else {
  238. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBackground)
  239. file.addBackgroundInfo.savePath = fileURL.path
  240. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  241. let op = KMBatchAddBackgroundOperation(file: file, backgroundM: background!)
  242. op.delegate = self
  243. self.queue?.addOperation(op)
  244. }
  245. if let cnt = self.queue?.operations.count, cnt > 0 {
  246. self.interfaceStatus = .Processing
  247. }
  248. }
  249. }
  250. }
  251. } else if (sender.tag == 0) {
  252. if let cnt = self.queue?.operations.count, cnt > 0 {
  253. self.queue?.cancelAllOperations()
  254. }
  255. self.interfaceStatus = .PrepareProcess
  256. }
  257. } else {
  258. //点击开始
  259. if (sender.tag == 1) {
  260. if (!self._checkAndResetTask()) {
  261. return;
  262. }
  263. var watermark: KMWatermarkModel?
  264. var background: KMBackgroundModel?
  265. let indexSet = self._isSelectIndex()
  266. if (!indexSet || self.files!.count < 1) {
  267. return;
  268. } else {
  269. if (!self.isBackground) {
  270. if (self.currentType == .txt) {
  271. watermark = self._currentTextData
  272. } else {
  273. watermark = self._currentFileData
  274. }
  275. } else {
  276. if (self.currentBackgroundType == .color) {
  277. background = self._currentColorData
  278. } else {
  279. background = self._currentImageData
  280. }
  281. }
  282. }
  283. if (self.isBackground) {
  284. self._choosePathAndBeginOperation(background)
  285. } else {
  286. self._choosePathAndBeginOperation(watermark)
  287. }
  288. } else if (sender.tag == 0) {
  289. if let cnt = self.queue?.operations.count, cnt > 0 {
  290. self.queue?.cancelAllOperations()
  291. }
  292. self.interfaceStatus = .PrepareProcess
  293. }
  294. }
  295. }
  296. @IBAction func buttonClicked_addtemplate(_ sender: NSButton) {
  297. let filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  298. var cdocument = self.pdfView?.document
  299. if isBatchOperation {
  300. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  301. }
  302. if isBackground {
  303. }else{
  304. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  305. controller.isBatch = isBatchOperation
  306. controller.type = .add
  307. controller.pdfDocument = cdocument
  308. controller.currentType = self.currentType.rawValue
  309. controller.cancelAction = { [weak self] wmWindowC in
  310. self?.km_endSheet()
  311. }
  312. controller.operateCallBack = { [unowned self] controller, watermark, countType in
  313. self.currentType = KMWatermarkType(rawValue: countType) ?? .txt
  314. self._loadData()
  315. var haveWaters = false
  316. if self.currentType == .txt {
  317. self._currentTextData = watermark
  318. if self._textTemplateArray?.count ?? 0 > 0 {
  319. haveWaters = true
  320. }
  321. } else if self.currentType == .file {
  322. self._currentFileData = watermark
  323. if self._fileTemplateArray?.count ?? 0 > 0 {
  324. haveWaters = true
  325. }
  326. }
  327. if haveWaters {
  328. let indexPath = IndexPath(item: 0, section: 0)
  329. var set = Set<IndexPath>()
  330. set.insert(indexPath)
  331. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  332. }
  333. self._postNotification()
  334. }
  335. self.km_beginSheet(windowC: controller)
  336. }
  337. }
  338. }
  339. // MARK: - Private Methods
  340. extension KMBatchOperateAddWatermarkViewController {
  341. private func _localizedlanguage() {
  342. self.addButton.title = " " + KMLocalizedString("Add Template")
  343. self.addButton.imageHugsTitle = true
  344. self.fileButton.title = KMLocalizedString("File")
  345. self.actionButton.title = KMLocalizedString("Apply")
  346. if (!self.isBackground) {
  347. self.titleLabel.stringValue = KMLocalizedString("Watermark")
  348. self.textButton.title = KMLocalizedString("Text")
  349. // self.actionButton.title = NSLocalizedString(@"Add Watermark");
  350. } else {
  351. self.titleLabel.stringValue = KMLocalizedString("Background")
  352. self.textButton.title = KMLocalizedString("Color")
  353. // self.actionButton.title = NSLocalizedString(@"Add Background");
  354. }
  355. // if (self.onlyManagerTemplate) {
  356. // self.actionButton.title = NSLocalizedString(@"Add Template");
  357. // }
  358. if (self.onlyManagerTemplate) {
  359. // self.titleLabel.hidden = self.addButton.hidden = YES;
  360. // self.managerTemplateTitleLabel.hidden = NO;
  361. // self.addButtonHeightConstraint.constant = 0;
  362. // self.addButtonBottomConstraint.constant = 0;
  363. self.titleLabel.isHidden = true
  364. self.addButton.isHidden = false
  365. self.managerTemplateTitleLabel.isHidden = false
  366. } else {
  367. self.titleLabel.isHidden = false
  368. self.addButton.isHidden = false
  369. self.managerTemplateTitleLabel.isHidden = true
  370. }
  371. self.managerTemplateTitleLabel.stringValue = KMLocalizedString("Manage Templates")
  372. }
  373. private func _configuiUI() {
  374. self.topBaseView.wantsLayer = true
  375. self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  376. self.collectionView.wantsLayer = true
  377. self.collectionView.isSelectable = true
  378. self.collectionView.allowsMultipleSelection = false
  379. self.collectionView.allowsEmptySelection = false
  380. self.collectionView.enclosingScrollView?.borderType = .noBorder
  381. self.collectionView.enclosingScrollView?.drawsBackground = false
  382. self.collectionView.register(KMWatermarkCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"))
  383. let v = NSView(frame: NSMakeRect(0, 0, 100, 100))
  384. v.wantsLayer = true
  385. v.layer?.backgroundColor = .clear
  386. self.collectionView.backgroundView = v;
  387. self.collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
  388. self.collectionView.enclosingScrollView?.verticalScrollElasticity = .none
  389. self.titleLabel.font = .boldSystemFont(ofSize: 14)
  390. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  391. self.actionButton.font = .systemFont(ofSize: 13)
  392. self.fileButton.font = .systemFont(ofSize: 13)
  393. self.textButton.font = .systemFont(ofSize: 13)
  394. self.actionButton.wantsLayer = true
  395. self.addButton.wantsLayer = true
  396. if (self.onlyManagerTemplate) {
  397. // [self.actionButton setImage:[NSImage imageNamed:KMImageNameUXIconBtnAddWhite]];
  398. // self.actionButton.imagePosition = NSImageLeft;
  399. // [self.actionButton setTitleColor:[KMAppearance KMColor_Layout_W0]];
  400. self.topHeightConstraint.constant = 40
  401. self.addButton.imagePosition = .imageLeft
  402. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  403. // self.addButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_S0].CGColor;
  404. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  405. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  406. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  407. self.actionButton.imagePosition = .noImage
  408. } else {
  409. self.topHeightConstraint.constant = 0
  410. self.addButton.imagePosition = .imageLeft
  411. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  412. // self.addButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_S0].CGColor;
  413. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  414. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  415. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  416. self.actionButton.imagePosition = .noImage
  417. }
  418. if (!self.onlyManagerTemplate) {
  419. if let cnt = self.files?.count, cnt > 0 {
  420. self._haveFiles = true
  421. } else {
  422. self._haveFiles = false
  423. }
  424. self._updateActionButtonbackgroundColor()
  425. }
  426. self.addButton.layer?.cornerRadius = 1.0
  427. self.actionButton.layer?.cornerRadius = 1.0
  428. self.actionButton.layer?.cornerRadius = 1.0
  429. // self.bottomBaseView.wantsLayer = YES;
  430. // self.bottomBaseView.layer.backgroundColor = [KMAppearance KMColor_Layout_L0].CGColor;
  431. self.textButton.wantsLayer = true
  432. self.fileButton.wantsLayer = true
  433. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  434. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  435. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  436. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  437. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  438. self.fileButton.setTitleColor(KMAppearance.Layout.h0Color())
  439. self.managerTemplateTitleLabel.font = .systemFont(ofSize: 14)
  440. self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
  441. self.view.addSubview(self.blankView)
  442. self.blankView.mas_makeConstraints { make in
  443. make?.top.equalTo()(self.topBaseView.mas_bottom)
  444. make?.left.right().equalTo()(self.view)
  445. make?.bottom.equalTo()(self.bottomBaseView.mas_top)
  446. make?.height.greaterThanOrEqualTo()(200)
  447. }
  448. self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates")
  449. if (self.isBackground) {
  450. self.blankView.imageView.image = NSImage(named: KMImageNameEmptyBackground)
  451. }
  452. self.blankView.wantsLayer = true
  453. // self.blankView.layer.backgroundColor = [KMAppearance KMColor_Layout_L0].CGColor;
  454. // NSMenu * = [[NSMenu alloc]initWithTitle:@""];
  455. let menu = NSMenu()
  456. if(!self.isBackground) {
  457. if(!self.isBatchOperation) {
  458. _ = menu.addItem(title: KMLocalizedString("Batch Add Watermarks"), action: #selector(_buttonItemClick_addBatch), target: self)
  459. }
  460. _ = menu.addItem(title: KMLocalizedString("Remove All Watermark Templates"), action: #selector(_buttonItemClick_CleanAll), target: self)
  461. } else {
  462. if(!self.isBatchOperation) {
  463. _ = menu.addItem(title: KMLocalizedString("Batch Add Background"), action: #selector(_buttonItemClick_addBatch), target: self)
  464. }
  465. _ = menu.addItem(title: KMLocalizedString("Remove All Background Templates"), action: #selector(_buttonItemClick_CleanAll), target: self)
  466. }
  467. self.view.menu = menu;
  468. }
  469. private func _loadData() {
  470. self._textTemplateArray = []
  471. self._fileTemplateArray = []
  472. if (!self.isBackground) {
  473. let watermarkArr = KMWatermarkManager.defaultManager.watermarks
  474. for i in 0 ..< watermarkArr.count {
  475. let watermark = watermarkArr[i]
  476. if ((watermark.image) != nil) {
  477. self._fileTemplateArray?.append(watermark)
  478. } else {
  479. self._textTemplateArray?.append(watermark)
  480. }
  481. }
  482. } else {
  483. let arr = KMBackgroundManager.defaultManager.datas
  484. for i in 0 ..< arr.count {
  485. let obj = arr[i]
  486. if (obj.type == .color) {
  487. self._textTemplateArray?.append(obj)
  488. } else {
  489. self._fileTemplateArray?.append(obj)
  490. }
  491. }
  492. }
  493. self.collectionView.reloadData()
  494. self._updateActionButtonbackgroundColor()
  495. NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  496. }
  497. private func _isSelectIndex() -> Bool {
  498. var indexSet = false
  499. if (!self.isBackground) {
  500. if(self.currentType == .txt) {
  501. if let data = self._currentTextData {
  502. indexSet = (self._textTemplateArray ?? []).contains(data)
  503. }
  504. }else {
  505. if let data = self._currentFileData {
  506. indexSet = (self._fileTemplateArray ?? []).contains(data)
  507. }
  508. }
  509. } else {
  510. if(self.currentBackgroundType == .color) {
  511. if let data = self._currentColorData {
  512. indexSet = (self._textTemplateArray ?? []).contains(data)
  513. }
  514. }else {
  515. if let data = self._currentImageData {
  516. indexSet = (self._fileTemplateArray ?? []).contains(data)
  517. }
  518. }
  519. }
  520. return indexSet;
  521. }
  522. private func _updateActionButtonbackgroundColor() {
  523. let indexSet = self._isSelectIndex()
  524. if (self.files?.count ?? 0 > 0 || self.pdfView?.document.documentURL.path.count ?? 0 > 0 ) && indexSet {
  525. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  526. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  527. } else {
  528. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  529. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  530. }
  531. }
  532. @objc private func _buttonItemClick_addBatch(_ sender: Any?) {
  533. let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
  534. // NSMutableArray *arr = [NSMutableArray array];
  535. let file = KMBatchOperateFile(filePath: self.pdfView!.document.documentURL.path, type: self.isBackground ? .AddBackground : .AddWatermark)
  536. baseWindowController.window?.makeKeyAndOrderFront(nil)
  537. baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBackground ? .AddBackground : .AddWatermark, files: [file])
  538. }
  539. @objc private func _buttonItemClick_CleanAll(_ sender: Any?) {
  540. let alert = NSAlert()
  541. alert.alertStyle = .warning
  542. alert.messageText = ""
  543. alert.informativeText = KMLocalizedString("Are you sure to delete all templates?")
  544. alert.addButton(withTitle: KMLocalizedString("Delete"))
  545. alert.addButton(withTitle: KMLocalizedString("Cancel"))
  546. alert.beginSheetModal(for: NSApp.mainWindow!) { returnCode in
  547. if returnCode == .alertFirstButtonReturn {
  548. self._deleteAllWatermark()
  549. }
  550. }
  551. }
  552. func editBackground(_ background: KMBackgroundModel) {
  553. var filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  554. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  555. filePath = self.pdfView?.document.documentURL.path ?? ""
  556. }
  557. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  558. }
  559. private func deleteBackground(_ background: KMBackgroundModel) {
  560. let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background)
  561. self._loadData()
  562. self._postNotification()
  563. }
  564. func editWatermark(_ waterMark: KMWatermarkModel) {
  565. if !isBackground {
  566. var filePath: String = Bundle.main.path(forResource: NSLocalizedString("Quick Start Guide.pdf", comment: ""), ofType: "") ?? ""
  567. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  568. filePath = self.pdfView?.document.documentURL.path ?? ""
  569. }
  570. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  571. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  572. controller.watermark = waterMark
  573. controller.isBatch = isBatchOperation
  574. controller.type = .edit
  575. controller.pdfDocument = cdocument
  576. controller.currentType = self.currentType.rawValue
  577. controller.cancelAction = { [weak self] wmWindowC in
  578. self?.km_endSheet()
  579. }
  580. controller.operateCallBack = { controller, watermark, countType in
  581. self._loadData()
  582. var haveWaters = false
  583. if self.currentType == .txt {
  584. self._currentTextData = watermark
  585. if self._textTemplateArray?.count ?? 0 > 0 {
  586. haveWaters = true
  587. }
  588. } else if self.currentType == .file {
  589. self._currentFileData = watermark
  590. if self._fileTemplateArray?.count ?? 0 > 0 {
  591. haveWaters = true
  592. }
  593. }
  594. if haveWaters {
  595. let indexPath = IndexPath(item: 0, section: 0)
  596. var set = Set<IndexPath>()
  597. set.insert(indexPath)
  598. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  599. }
  600. self._postNotification()
  601. }
  602. self.km_beginSheet(windowC: controller)
  603. } else {
  604. // Handle background case
  605. }
  606. }
  607. private func _deleteAllWatermark() {
  608. if(!self.isBackground) {
  609. for waterMark in self._textTemplateArray ?? [] {
  610. // _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  611. }
  612. for waterMark in self._fileTemplateArray ?? [] {
  613. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  614. // _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  615. }
  616. } else {
  617. // for background in self._textTemplateArray ?? [] {
  618. //// [[KMBackgroundManager defaultManager] removeBackground:background];
  619. // _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  620. // }
  621. //
  622. // for background in self._fileTemplateArray ?? [] {
  623. //// [[KMBackgroundManager defaultManager] removeBackground:background];
  624. // _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  625. // }
  626. _ = KMBackgroundManager.defaultManager.deleteAllTemplates()
  627. }
  628. self._loadData()
  629. self._postNotification()
  630. }
  631. private func _deleteWatermark(_ waterMark: KMWatermarkModel) {
  632. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  633. // let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark)
  634. self._loadData()
  635. self._postNotification()
  636. }
  637. private func _postNotification() {
  638. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateWatermarksNotification"), object: self)
  639. }
  640. @objc private func _watermarksNotification(_ notification: NSNotification) {
  641. let addWatermark = notification.object as? KMBatchOperateAddWatermarkViewController
  642. if self.isEqual(to: addWatermark) == false {
  643. self._loadData()
  644. var haveWaters = false
  645. if (self.currentType == .txt) {
  646. if let cnt = self._textTemplateArray?.count, cnt > 0 {
  647. haveWaters = true
  648. }
  649. } else if (self.currentType == .file) {
  650. if let cnt = self._fileTemplateArray?.count, cnt > 0 {
  651. haveWaters = true
  652. }
  653. }
  654. if (haveWaters) {
  655. let indexpath = IndexPath(item: 0, section: 0)
  656. var set = Set<IndexPath>()
  657. set.insert(indexpath)
  658. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  659. }
  660. }
  661. }
  662. //检查是否有任务文件要加载,如果有,重置任务。如果返回YES,表示有任务并且任务已经重置,否则表示没有任务,需要中断操作
  663. private func _checkAndResetTask() -> Bool {
  664. if let cnt = self.files?.count, cnt < 1 {
  665. return false
  666. }
  667. for i in 0 ..< self.files!.count {
  668. let file = self.files![i]
  669. if (self.isBackground) {
  670. file.addBackgroundInfo.resetState()
  671. } else {
  672. file.addWatermarkInfo.resetState()
  673. }
  674. }
  675. return true
  676. }
  677. private func _choosePathAndBeginOperation(_ obj: Any?) {
  678. let openPanel = NSOpenPanel()
  679. openPanel.canChooseFiles = false
  680. openPanel.canChooseDirectories = true
  681. openPanel.canCreateDirectories = true
  682. openPanel.beginSheetModal(for: self.view.window!) { result in
  683. if (result == .OK) {
  684. for fileURL in openPanel.urls {
  685. self.choosePath = fileURL.path
  686. if (!self.isBackground) {
  687. self._beginAddWatermark(obj as! KMWatermarkModel)
  688. } else {
  689. self._beginAddBackground(obj as! KMBackgroundModel)
  690. }
  691. }
  692. }
  693. }
  694. }
  695. private func _beginAddWatermark(_ watermark: KMWatermarkModel) {
  696. self.hiddenWindowCloseButtonIfNeeded()
  697. self.successFilePathURLArray?.removeAll()
  698. for i in 0 ..< self.files!.count {
  699. let file = self.files![i]
  700. file.addWatermarkInfo.savePath = self.choosePath
  701. let operation = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark)
  702. operation.delegate = self
  703. self.queue?.addOperation(operation)
  704. }
  705. if let cnt = self.queue?.operations.count, cnt > 0 {
  706. self.interfaceStatus = .Processing
  707. }
  708. }
  709. private func _beginAddBackground(_ background: KMBackgroundModel) {
  710. self.hiddenWindowCloseButtonIfNeeded()
  711. self.successFilePathURLArray?.removeAll()
  712. for i in 0 ..< self.files!.count {
  713. let file = self.files![i]
  714. file.addBackgroundInfo.savePath = self.choosePath
  715. // KMBatchAddBackgroundOperation *operation = [[[KMBatchAddBackgroundOperation alloc] initWithFile:file background:background] autorelease];
  716. // operation.delegate = self;
  717. // [self.queue addOperation:operation];
  718. let operation = KMBatchAddBackgroundOperation(file: file, backgroundM: background)
  719. operation.delegate = self
  720. self.queue?.addOperation(operation)
  721. }
  722. if let cnt = self.queue?.operations.count, cnt > 0 {
  723. self.interfaceStatus = .Processing
  724. }
  725. }
  726. @objc private func _batchFilesCountNotification(_ notification: NSNotification) {
  727. let files: Array? = notification.object as? [KMBatchOperateFile]
  728. if let data = files?.first {
  729. if data.currentOperateType != self.operateType {
  730. return
  731. }
  732. }
  733. let viewC = notification.userInfo?[kObjectKey] as? NSViewController
  734. if let data = self.view.window?.isEqual(to: viewC?.view.window), data == false {
  735. return
  736. }
  737. if (!self.onlyManagerTemplate) {
  738. let files: Array? = notification.object as? [KMBatchOperateFile]
  739. self.files? = files ?? []
  740. if (files?.count ?? 0 > 0) {
  741. self._haveFiles = true
  742. } else {
  743. self._haveFiles = false
  744. }
  745. self._updateActionButtonbackgroundColor()
  746. }
  747. }
  748. }
  749. extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
  750. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  751. let action = menuItem.action
  752. if (action == #selector(_buttonItemClick_CleanAll)) {
  753. if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
  754. return false
  755. }
  756. return true
  757. }
  758. return true
  759. }
  760. }
  761. extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  762. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  763. var count = 0
  764. if (!self.isBackground) {
  765. if (self.currentType == .txt) {
  766. count = self._textTemplateArray?.count ?? 0
  767. } else {
  768. count = self._fileTemplateArray?.count ?? 0
  769. }
  770. } else {
  771. if (self.currentBackgroundType == .color) {
  772. count = self._textTemplateArray?.count ?? 0
  773. } else {
  774. count = self._fileTemplateArray?.count ?? 0
  775. }
  776. }
  777. self.blankView.isHidden = count != 0
  778. return count
  779. }
  780. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  781. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
  782. if (!self.isBackground) {
  783. var waterMark: KMWatermarkModel?
  784. if (self.currentType == .txt) {
  785. waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
  786. if(self._currentTextData == waterMark) {
  787. item.isSelected = true
  788. }else {
  789. item.isSelected = false
  790. }
  791. } else {
  792. waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
  793. if(self._currentFileData == waterMark) {
  794. item.isSelected = true
  795. }else{
  796. item.isSelected = false
  797. }
  798. }
  799. // __block typeof(self) blockSelf = self;
  800. let _item = item as? KMWatermarkCollectionViewItem
  801. _item?.updateInterface(waterMark!)
  802. _item?.waterMarkOprateCallback = { [unowned self] type, wm in
  803. if (wm != nil) {
  804. if (type == .Edit) {
  805. if (self.currentType == .txt) {
  806. self._currentTextData = waterMark
  807. } else {
  808. self._currentFileData = waterMark
  809. }
  810. collectionView.reloadData()
  811. self.editWatermark(waterMark!)
  812. } else {
  813. self._deleteWatermark(waterMark!)
  814. }
  815. }
  816. };
  817. } else {
  818. var background: KMBackgroundModel?
  819. if (self.currentBackgroundType == .color) {
  820. background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
  821. if(self._currentColorData == background) {
  822. item.isSelected = true
  823. }else {
  824. item.isSelected = false
  825. }
  826. } else {
  827. background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
  828. if(self._currentImageData == background) {
  829. item.isSelected = true
  830. }else {
  831. item.isSelected = false
  832. }
  833. }
  834. let _item = item as? KMWatermarkCollectionViewItem
  835. _item?.updateBackgroundInterface(background!)
  836. // __block typeof(self) blockSelf = self;
  837. _item?.backgroundOperateCallback = { [unowned self] type, bg in
  838. if (type == .Edit) {
  839. if (self.currentBackgroundType == .color) {
  840. self._currentColorData = background
  841. } else {
  842. self._currentImageData = background
  843. }
  844. collectionView.reloadData()
  845. self.editBackground(background!)
  846. } else {
  847. self.deleteBackground(background!)
  848. }
  849. } ;
  850. }
  851. self._updateActionButtonbackgroundColor()
  852. return item
  853. }
  854. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  855. if (collectionView.frame.size.width < 240) {
  856. return NSMakeSize(0, 0)
  857. } else {
  858. return NSMakeSize(104, 167)
  859. }
  860. }
  861. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  862. if (collectionView.frame.size.width < 32) {
  863. return NSEdgeInsetsMake(0, 0, 0, 0)
  864. }
  865. return NSEdgeInsetsMake(0, 16, 0, 16)
  866. }
  867. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  868. let indexPath = indexPaths.first
  869. guard let _indexPath = indexPath else {
  870. return
  871. }
  872. if (indexPath!.item < 0) {
  873. return
  874. }
  875. if (self.isBackground) {
  876. var background: KMBackgroundModel?
  877. if (self.currentBackgroundType == .color) {
  878. background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
  879. self._currentColorData = background
  880. } else {
  881. background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
  882. self._currentImageData = background
  883. }
  884. for i in 0 ..< self.files!.count {
  885. let file = self.files![i]
  886. // file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (background?.pageRangeType.rawValue)!)!
  887. // file.addBackgroundInfo.pageRangeString = background?.pageRangeString
  888. }
  889. } else {
  890. var waterMark: KMWatermarkModel?
  891. if (self.currentType == .txt) {
  892. waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
  893. self._currentTextData = waterMark
  894. } else {
  895. waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
  896. self._currentFileData = waterMark
  897. }
  898. for i in 0 ..< self.files!.count {
  899. let file = self.files![i]
  900. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (waterMark?.pageRangeType.rawValue)!)!
  901. file.addWatermarkInfo.pageRangeString = waterMark?.pageRangeString
  902. // if (file.addWatermarkInfo.pageChoice == KMBatchOperatePageChoice_Input) {
  903. // NSArray *arr = [self allPageNumbers:waterMark.pagesString];
  904. // NSArray *sortedArray = [file pagesArrayIntersectWithArray:arr];
  905. // if (sortedArray.count < 1) {
  906. // file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice_All;
  907. // } else {
  908. // file.addWatermarkInfo.pageRangeString = [sortedArray componentsJoinedByString:@","];
  909. // }
  910. // }
  911. }
  912. }
  913. NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
  914. collectionView.reloadData()
  915. self._updateActionButtonbackgroundColor()
  916. }
  917. }