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