KMBatchOperateAddWatermarkViewController.swift 47 KB

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