KMBatchOperateAddWatermarkViewController.swift 47 KB

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