KMBatchOperateAddWatermarkViewController.swift 46 KB

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