KMBatchOperateAddWatermarkViewController.swift 45 KB

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