KMBatchOperateAddWatermarkViewController.swift 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. private func _deleteAllWatermark() {
  588. if(!self.isBackground) {
  589. for waterMark in self._textTemplateArray ?? [] {
  590. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  591. }
  592. for waterMark in self._fileTemplateArray ?? [] {
  593. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  594. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  595. }
  596. } else {
  597. for background in self._textTemplateArray ?? [] {
  598. // [[KMBackgroundManager defaultManager] removeBackground:background];
  599. _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  600. }
  601. for background in self._fileTemplateArray ?? [] {
  602. // [[KMBackgroundManager defaultManager] removeBackground:background];
  603. _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  604. }
  605. }
  606. self._loadData()
  607. self._postNotification()
  608. }
  609. private func _deleteWatermark(_ waterMark: KMPDFWatermarkData) {
  610. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  611. self._loadData()
  612. self._postNotification()
  613. }
  614. private func _postNotification() {
  615. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateWatermarksNotification"), object: self)
  616. }
  617. @objc private func _watermarksNotification(_ notification: NSNotification) {
  618. let addWatermark = notification.object as? KMBatchOperateAddWatermarkViewController
  619. if self.isEqual(to: addWatermark) == false {
  620. self._loadData()
  621. var haveWaters = false
  622. if (self.currentType == .txt) {
  623. if let cnt = self._textTemplateArray?.count, cnt > 0 {
  624. haveWaters = true
  625. }
  626. } else if (self.currentType == .file) {
  627. if let cnt = self._fileTemplateArray?.count, cnt > 0 {
  628. haveWaters = true
  629. }
  630. }
  631. if (haveWaters) {
  632. let indexpath = IndexPath(item: 0, section: 0)
  633. var set = Set<IndexPath>()
  634. set.insert(indexpath)
  635. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  636. }
  637. }
  638. }
  639. //检查是否有任务文件要加载,如果有,重置任务。如果返回YES,表示有任务并且任务已经重置,否则表示没有任务,需要中断操作
  640. private func _checkAndResetTask() -> Bool {
  641. if let cnt = self.files?.count, cnt < 1 {
  642. return false
  643. }
  644. for i in 0 ..< self.files!.count {
  645. let file = self.files![i]
  646. if (self.isBackground) {
  647. // [file.addBackgroundInfo resetState];
  648. } else {
  649. // [file.addWatermarkInfo resetState];
  650. }
  651. }
  652. return true
  653. }
  654. private func _choosePathAndBeginOperation(_ obj: Any?) {
  655. let openPanel = NSOpenPanel()
  656. openPanel.canChooseFiles = false
  657. openPanel.canChooseDirectories = true
  658. openPanel.canCreateDirectories = true
  659. openPanel.beginSheetModal(for: self.view.window!) { result in
  660. if (result == .OK) {
  661. for fileURL in openPanel.urls {
  662. self.choosePath = fileURL.path
  663. if (!self.isBackground) {
  664. self._beginAddWatermark(obj as! KMPDFWatermarkData)
  665. } else {
  666. self._beginAddBackground(obj as! KMBackgroundModel)
  667. }
  668. }
  669. }
  670. }
  671. }
  672. private func _beginAddWatermark(_ watermark: KMPDFWatermarkData) {
  673. self.hiddenWindowCloseButtonIfNeeded()
  674. // [self.successFilePathURLArray removeAllObjects];
  675. for i in 0 ..< self.files!.count {
  676. let file = self.files![i]
  677. file.addWatermarkInfo.savePath = self.choosePath
  678. // KMBatchAddWatermarkOperation *operation = [[[KMBatchAddWatermarkOperation alloc] initWithFile:file waterMark:watermark] autorelease];
  679. // operation.delegate = self;
  680. // [self.queue addOperation:operation];
  681. }
  682. if let cnt = self.queue?.operations.count, cnt > 0 {
  683. self.interfaceStatus = .Processing
  684. }
  685. }
  686. private func _beginAddBackground(_ background: KMBackgroundModel) {
  687. self.hiddenWindowCloseButtonIfNeeded()
  688. // [self.successFilePathURLArray removeAllObjects];
  689. for i in 0 ..< self.files!.count {
  690. let file = self.files![i]
  691. file.addBackgroundInfo.savePath = self.choosePath
  692. // KMBatchAddBackgroundOperation *operation = [[[KMBatchAddBackgroundOperation alloc] initWithFile:file background:background] autorelease];
  693. // operation.delegate = self;
  694. // [self.queue addOperation:operation];
  695. }
  696. if let cnt = self.queue?.operations.count, cnt > 0 {
  697. self.interfaceStatus = .Processing
  698. }
  699. }
  700. @objc private func _batchFilesCountNotification(_ notification: NSNotification) {
  701. if (!self.onlyManagerTemplate) {
  702. let files = notification.object as? [AnyObject] ?? []
  703. if (files.count > 0) {
  704. self._haveFiles = true
  705. } else {
  706. self._haveFiles = false
  707. }
  708. self._updateActionButtonbackgroundColor()
  709. }
  710. }
  711. }
  712. extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
  713. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  714. let action = menuItem.action
  715. if (action == #selector(_buttonItemClick_CleanAll)) {
  716. if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
  717. return false
  718. }
  719. return true
  720. }
  721. return true
  722. }
  723. }
  724. extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  725. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  726. var count = 0
  727. if (!self.isBackground) {
  728. if (self.currentType == .txt) {
  729. count = self._textTemplateArray?.count ?? 0
  730. } else {
  731. count = self._fileTemplateArray?.count ?? 0
  732. }
  733. } else {
  734. if (self.currentBackgroundType == .color) {
  735. count = self._textTemplateArray?.count ?? 0
  736. } else {
  737. count = self._fileTemplateArray?.count ?? 0
  738. }
  739. }
  740. self.blankView.isHidden = count != 0
  741. return count
  742. }
  743. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  744. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
  745. if (!self.isBackground) {
  746. var waterMark: KMWatermarkModel?
  747. if (self.currentType == .txt) {
  748. waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
  749. if(self._currentTextData == waterMark) {
  750. item.isSelected = true
  751. }else {
  752. item.isSelected = false
  753. }
  754. } else {
  755. waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
  756. if(self._currentFileData == waterMark) {
  757. item.isSelected = true
  758. }else{
  759. item.isSelected = false
  760. }
  761. }
  762. // __block typeof(self) blockSelf = self;
  763. let _item = item as? KMWatermarkCollectionViewItem
  764. _item?.updateInterface(waterMark!)
  765. _item?.waterMarkOprateCallback = { [unowned self] type, wm in
  766. if (wm != nil) {
  767. if (type == .Edit) {
  768. if (self.currentType == .txt) {
  769. self._currentTextData = waterMark
  770. } else {
  771. self._currentFileData = waterMark
  772. }
  773. collectionView.reloadData()
  774. // [blockSelf editWatermark:watermark];
  775. } else {
  776. // [blockSelf deleteWatermark:watermark];
  777. }
  778. }
  779. };
  780. } else {
  781. var background: KMBackgroundModel?
  782. if (self.currentBackgroundType == .color) {
  783. background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
  784. if(self._currentColorData == background) {
  785. item.isSelected = true
  786. }else {
  787. item.isSelected = false
  788. }
  789. } else {
  790. background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
  791. if(self._currentImageData == background) {
  792. item.isSelected = true
  793. }else {
  794. item.isSelected = false
  795. }
  796. }
  797. let _item = item as? KMWatermarkCollectionViewItem
  798. _item?.updateBackgroundInterface(background!)
  799. // __block typeof(self) blockSelf = self;
  800. _item?.backgroundOperateCallback = { [unowned self] type, bg in
  801. if (type == .Edit) {
  802. if (self.currentBackgroundType == .color) {
  803. self._currentColorData = background
  804. } else {
  805. self._currentImageData = background
  806. }
  807. collectionView.reloadData()
  808. // [blockSelf editBackground:background];
  809. } else {
  810. // [blockSelf deleteBackground:background];
  811. }
  812. } ;
  813. }
  814. self._updateActionButtonbackgroundColor()
  815. return item
  816. }
  817. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  818. if (collectionView.frame.size.width < 240) {
  819. return CGSizeMake(0, 0)
  820. } else {
  821. return CGSizeMake(104, 167)
  822. }
  823. }
  824. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  825. if (collectionView.frame.size.width < 32) {
  826. return NSEdgeInsetsMake(0, 0, 0, 0)
  827. }
  828. return NSEdgeInsetsMake(0, 16, 0, 16)
  829. }
  830. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  831. let indexPath = indexPaths.first
  832. guard let _indexPath = indexPath else {
  833. return
  834. }
  835. if (indexPath!.item < 0) {
  836. return
  837. }
  838. if (self.isBackground) {
  839. var background: KMBackgroundModel?
  840. if (self.currentBackgroundType == .color) {
  841. background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
  842. self._currentColorData = background
  843. } else {
  844. background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
  845. self._currentImageData = background
  846. }
  847. for i in 0 ..< self.files!.count {
  848. let file = self.files![i]
  849. // file.addBackgroundInfo.pageChoice = background.pageRangeType;
  850. // if (file.addBackgroundInfo.pageChoice == KMBatchOperatePageChoice_Input) {
  851. // NSArray *arr = [self allPageNumbers:background.pagesString];
  852. // NSArray *sortedArray = [file pagesArrayIntersectWithArray:arr];
  853. // if (sortedArray.count < 1) {
  854. // file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice_All;
  855. // } else {
  856. // file.addBackgroundInfo.pageRangeString = [sortedArray componentsJoinedByString:@","];
  857. // }
  858. // }
  859. }
  860. } else {
  861. var waterMark: KMWatermarkModel?
  862. if (self.currentType == .txt) {
  863. waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
  864. self._currentTextData = waterMark
  865. } else {
  866. waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
  867. self._currentFileData = waterMark
  868. }
  869. for i in 0 ..< self.files!.count {
  870. let file = self.files![i]
  871. // file.addWatermarkInfo.pageChoice = waterMark.pageRangeType;
  872. // if (file.addWatermarkInfo.pageChoice == KMBatchOperatePageChoice_Input) {
  873. // NSArray *arr = [self allPageNumbers:waterMark.pagesString];
  874. // NSArray *sortedArray = [file pagesArrayIntersectWithArray:arr];
  875. // if (sortedArray.count < 1) {
  876. // file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice_All;
  877. // } else {
  878. // file.addWatermarkInfo.pageRangeString = [sortedArray componentsJoinedByString:@","];
  879. // }
  880. // }
  881. }
  882. }
  883. NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
  884. collectionView.reloadData()
  885. self._updateActionButtonbackgroundColor()
  886. }
  887. }