KMBatchOperateAddWatermarkViewController.swift 42 KB

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