KMBatchOperateAddWatermarkViewController.swift 46 KB

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