KMBatchOperateAddWatermarkViewController.swift 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. //
  2. // KMBatchOperateAddWatermarkViewController.swift
  3. // PDF Reader Pro
  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. self.interfaceStatus = .PrepareProcess
  111. NotificationCenter.default.addObserver(self, selector: #selector(_watermarksNotification), name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: nil)
  112. }
  113. func watermarkInterfaceSelectWatermark(_ watermark: KMWatermarkModel) {
  114. if(watermark.image != nil) {
  115. self._currentFileData = watermark
  116. }else {
  117. self._currentTextData = watermark
  118. }
  119. self._loadData()
  120. self.currentType = watermark.image != nil ? .file : .txt
  121. let arr = self.currentType == .file ? self._fileTemplateArray : self._textTemplateArray
  122. let index = arr?.firstIndex(of: watermark)
  123. if (index != NSNotFound) {
  124. let indexpath = IndexPath(item: index!, section: 0)
  125. var set = Set<IndexPath>()
  126. set.insert(indexpath)
  127. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  128. }
  129. }
  130. func backgroundInterfaceSelectBackGround(_ background: KMBackgroundModel) {
  131. if(background.type == .file) {
  132. self._currentImageData = background
  133. }else {
  134. self._currentColorData = background
  135. }
  136. self._loadData()
  137. self.currentBackgroundType = background.type
  138. let arr = self.currentBackgroundType == .file ? self._fileTemplateArray : self._textTemplateArray
  139. let index = arr?.firstIndex(of: background)
  140. if (index != NSNotFound) {
  141. let indexpath = IndexPath(item: index!, section: 0)
  142. var set = Set<IndexPath>()
  143. set.insert(indexpath)
  144. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  145. }
  146. }
  147. override var interfaceStatus: KMBatchOperateInterfaceStatus? {
  148. didSet {
  149. if (self.interfaceStatus == .PrepareProcess) {
  150. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  151. var files: [URL] = []
  152. for url in self.successFilePathURLArray ?? [] {
  153. if FileManager.default.fileExists(atPath: url.path) {
  154. files.append(url)
  155. }
  156. }
  157. if (files.count > 0) {
  158. let workspace = NSWorkspace.shared
  159. workspace.activateFileViewerSelecting(files)
  160. }
  161. }
  162. self.collectionView.isSelectable = true
  163. self.actionButton.tag = 1
  164. // if (self.isBackground) {
  165. // self.actionButton.title = NSLocalizedString(@"Add Background", nil);
  166. // } else {
  167. // self.actionButton.title = NSLocalizedString(@"Add Watermark", nil);
  168. // }
  169. self.actionButton.title = KMLocalizedString("Apply", nil)
  170. self.addButton.isEnabled = true
  171. // if (self.onlyManagerTemplate) {
  172. // self.actionButton.title = NSLocalizedString(@"Add Template", nil);
  173. // }
  174. } else {
  175. self.collectionView.isSelectable = false
  176. self.addButton.isEnabled = false
  177. self.actionButton.tag = 0
  178. self.actionButton.title = KMLocalizedString("Cancel", nil);
  179. }
  180. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  181. }
  182. }
  183. //button Actions,切换水印类型
  184. @IBAction func buttonClicked_SwitchWaterMarkType(_ sender: NSButton) {
  185. if (self.isBackground) {
  186. self.currentBackgroundType = KMBackgroundType(rawValue: sender.tag) ?? .color
  187. } else {
  188. self.currentType = KMWatermarkType(rawValue: sender.tag) ?? .txt
  189. }
  190. self._updateActionButtonbackgroundColor()
  191. }
  192. @IBAction func buttonClicked_addWaterMark(_ sender: NSButton) {
  193. if (!self.onlyManagerTemplate) {
  194. if (!self._haveFiles) {
  195. return
  196. }
  197. }
  198. self.view.window?.makeFirstResponder(nil)
  199. if (self.onlyManagerTemplate) {
  200. if (sender.tag == 1) {
  201. var watermark: KMWatermarkModel?
  202. var background: KMBackgroundModel?
  203. let indexSet = self._isSelectIndex()
  204. if (!indexSet) {
  205. return
  206. } else {
  207. if (!self.isBackground) {
  208. if (self.currentType == .txt) {
  209. watermark = self._currentTextData
  210. } else {
  211. watermark = self._currentFileData
  212. }
  213. } else {
  214. if (self.currentBackgroundType == .color) {
  215. background = self._currentColorData
  216. } else {
  217. background = self._currentImageData
  218. }
  219. }
  220. }
  221. let openPanel = NSOpenPanel()
  222. openPanel.canChooseFiles = false
  223. openPanel.canChooseDirectories = true
  224. openPanel.canCreateDirectories = true
  225. openPanel.beginSheetModal(for: self.view.window!) { result in
  226. if (result == .OK) {
  227. for fileURL in openPanel.urls {
  228. self.hiddenWindowCloseButtonIfNeeded()
  229. self.successFilePathURLArray?.removeAll()
  230. if (!self.isBackground) {
  231. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddWatermark)
  232. file.addWatermarkInfo.savePath = fileURL.path
  233. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  234. file.addWatermarkInfo.pageRangeString = watermark?.pagesString ?? ""
  235. let op = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark!)
  236. op.delegate = self
  237. self.queue?.addOperation(op)
  238. } else {
  239. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBackground)
  240. file.addBackgroundInfo.savePath = fileURL.path
  241. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  242. file.addBackgroundInfo.pageRangeString = background?.pagesString ?? ""
  243. let op = KMBatchAddBackgroundOperation(file: file, backgroundM: background!)
  244. op.delegate = self
  245. self.queue?.addOperation(op)
  246. }
  247. if let cnt = self.queue?.operations.count, cnt > 0 {
  248. self.interfaceStatus = .Processing
  249. }
  250. }
  251. }
  252. }
  253. } else if (sender.tag == 0) {
  254. if let cnt = self.queue?.operations.count, cnt > 0 {
  255. self.queue?.cancelAllOperations()
  256. }
  257. self.interfaceStatus = .PrepareProcess
  258. }
  259. } else {
  260. //点击开始
  261. if (sender.tag == 1) {
  262. if (!self._checkAndResetTask()) {
  263. return;
  264. }
  265. var watermark: KMWatermarkModel?
  266. var background: KMBackgroundModel?
  267. let indexSet = self._isSelectIndex()
  268. if (!indexSet || self.files!.count < 1) {
  269. return;
  270. } else {
  271. if (!self.isBackground) {
  272. if (self.currentType == .txt) {
  273. watermark = self._currentTextData
  274. } else {
  275. watermark = self._currentFileData
  276. }
  277. } else {
  278. if (self.currentBackgroundType == .color) {
  279. background = self._currentColorData
  280. } else {
  281. background = self._currentImageData
  282. }
  283. }
  284. }
  285. if (self.isBackground) {
  286. self._choosePathAndBeginOperation(background)
  287. } else {
  288. self._choosePathAndBeginOperation(watermark)
  289. }
  290. } else if (sender.tag == 0) {
  291. if let cnt = self.queue?.operations.count, cnt > 0 {
  292. self.queue?.cancelAllOperations()
  293. }
  294. self.interfaceStatus = .PrepareProcess
  295. }
  296. }
  297. }
  298. @IBAction func buttonClicked_addtemplate(_ sender: NSButton) {
  299. let filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  300. var cdocument = self.pdfView?.document
  301. if isBatchOperation {
  302. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  303. }
  304. if isBackground {
  305. let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
  306. controller.isBatch = isBatchOperation
  307. controller.type = .add
  308. controller.currentType = self.currentBackgroundType.rawValue
  309. controller.pdfDocument = cdocument
  310. controller.cancelAction = { [weak self] controller in
  311. self?.km_endSheet()
  312. }
  313. self.km_beginSheet(windowC: controller)
  314. controller.operateCallBack = { controller, background, countType in
  315. self.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
  316. self._loadData()
  317. var haveBackgrounds = false
  318. if self.currentBackgroundType == .color {
  319. self._currentColorData = background
  320. if self._textTemplateArray?.count ?? 0 > 0 {
  321. haveBackgrounds = true
  322. }
  323. } else if self.currentBackgroundType == .file {
  324. self._currentImageData = background
  325. if self._fileTemplateArray?.count ?? 0 > 0 {
  326. haveBackgrounds = true
  327. }
  328. }
  329. if haveBackgrounds {
  330. let indexPath = IndexPath(item: 0, section: 0)
  331. var set = Set<IndexPath>()
  332. set.insert(indexPath)
  333. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  334. }
  335. self._postNotification()
  336. }
  337. }else{
  338. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  339. controller.isBatch = isBatchOperation
  340. controller.type = .add
  341. controller.pdfDocument = cdocument
  342. controller.currentType = self.currentType.rawValue
  343. controller.cancelAction = { [weak self] wmWindowC in
  344. self?.km_endSheet()
  345. }
  346. controller.operateCallBack = { [unowned self] controller, watermark, countType in
  347. self.currentType = KMWatermarkType(rawValue: countType) ?? .txt
  348. self._loadData()
  349. var haveWaters = false
  350. if self.currentType == .txt {
  351. self._currentTextData = watermark
  352. if self._textTemplateArray?.count ?? 0 > 0 {
  353. haveWaters = true
  354. }
  355. } else if self.currentType == .file {
  356. self._currentFileData = watermark
  357. if self._fileTemplateArray?.count ?? 0 > 0 {
  358. haveWaters = true
  359. }
  360. }
  361. if haveWaters {
  362. let indexPath = IndexPath(item: 0, section: 0)
  363. var set = Set<IndexPath>()
  364. set.insert(indexPath)
  365. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  366. }
  367. self._postNotification()
  368. }
  369. self.km_beginSheet(windowC: controller)
  370. }
  371. }
  372. }
  373. // MARK: - Private Methods
  374. extension KMBatchOperateAddWatermarkViewController {
  375. private func _localizedlanguage() {
  376. self.addButton.title = " " + KMLocalizedString("Add Template", nil)
  377. self.addButton.imageHugsTitle = true
  378. self.fileButton.title = KMLocalizedString("File", nil)
  379. self.actionButton.title = KMLocalizedString("Apply", nil)
  380. if (!self.isBackground) {
  381. self.titleLabel.stringValue = KMLocalizedString("Watermark", nil)
  382. self.textButton.title = KMLocalizedString("Text", nil)
  383. // self.actionButton.title = NSLocalizedString(@"Add Watermark", nil);
  384. } else {
  385. self.titleLabel.stringValue = KMLocalizedString("Background", nil)
  386. self.textButton.title = KMLocalizedString("Color", nil)
  387. // self.actionButton.title = NSLocalizedString(@"Add Background", nil);
  388. }
  389. // if (self.onlyManagerTemplate) {
  390. // self.actionButton.title = NSLocalizedString(@"Add Template", nil);
  391. // }
  392. if (self.onlyManagerTemplate) {
  393. // self.titleLabel.hidden = self.addButton.hidden = YES;
  394. // self.managerTemplateTitleLabel.hidden = NO;
  395. // self.addButtonHeightConstraint.constant = 0;
  396. // self.addButtonBottomConstraint.constant = 0;
  397. self.titleLabel.isHidden = true
  398. self.addButton.isHidden = false
  399. self.managerTemplateTitleLabel.isHidden = false
  400. } else {
  401. self.titleLabel.isHidden = false
  402. self.addButton.isHidden = false
  403. self.managerTemplateTitleLabel.isHidden = true
  404. }
  405. self.managerTemplateTitleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  406. }
  407. private func _configuiUI() {
  408. self.topBaseView.wantsLayer = true
  409. self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  410. self.collectionView.wantsLayer = true
  411. self.collectionView.isSelectable = true
  412. self.collectionView.allowsMultipleSelection = false
  413. self.collectionView.allowsEmptySelection = false
  414. self.collectionView.enclosingScrollView?.borderType = .noBorder
  415. self.collectionView.enclosingScrollView?.drawsBackground = false
  416. self.collectionView.register(KMWatermarkCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"))
  417. let v = NSView(frame: NSMakeRect(0, 0, 100, 100))
  418. v.wantsLayer = true
  419. v.layer?.backgroundColor = .clear
  420. self.collectionView.backgroundView = v;
  421. self.collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
  422. self.collectionView.enclosingScrollView?.verticalScrollElasticity = .none
  423. self.titleLabel.font = .boldSystemFont(ofSize: 14)
  424. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  425. self.actionButton.font = .systemFont(ofSize: 13)
  426. self.fileButton.font = .systemFont(ofSize: 13)
  427. self.textButton.font = .systemFont(ofSize: 13)
  428. self.actionButton.wantsLayer = true
  429. self.addButton.wantsLayer = true
  430. if (self.onlyManagerTemplate) {
  431. // [self.actionButton setImage:[NSImage imageNamed:KMImageNameUXIconBtnAddWhite]];
  432. // self.actionButton.imagePosition = NSImageLeft;
  433. // [self.actionButton setTitleColor:[KMAppearance KMColor_Layout_W0]];
  434. self.topHeightConstraint.constant = 40
  435. self.addButton.imagePosition = .imageLeft
  436. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  437. // self.addButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_S0].CGColor;
  438. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  439. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  440. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  441. self.actionButton.imagePosition = .noImage
  442. } else {
  443. self.topHeightConstraint.constant = 0
  444. self.addButton.imagePosition = .imageLeft
  445. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  446. // self.addButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_S0].CGColor;
  447. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  448. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  449. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  450. self.actionButton.imagePosition = .noImage
  451. }
  452. if (!self.onlyManagerTemplate) {
  453. if let cnt = self.files?.count, cnt > 0 {
  454. self._haveFiles = true
  455. } else {
  456. self._haveFiles = false
  457. }
  458. self._updateActionButtonbackgroundColor()
  459. }
  460. self.addButton.layer?.cornerRadius = 1.0
  461. self.actionButton.layer?.cornerRadius = 1.0
  462. self.actionButton.layer?.cornerRadius = 1.0
  463. // self.bottomBaseView.wantsLayer = YES;
  464. // self.bottomBaseView.layer.backgroundColor = [KMAppearance KMColor_Layout_L0].CGColor;
  465. self.textButton.wantsLayer = true
  466. self.fileButton.wantsLayer = true
  467. // self.textButton.layer.backgroundColor = [KMAppearance KMColor_Interactive_A0].CGColor;
  468. // self.fileButton.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  469. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  470. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  471. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  472. self.fileButton.setTitleColor(KMAppearance.Layout.h0Color())
  473. self.managerTemplateTitleLabel.font = .systemFont(ofSize: 14)
  474. self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
  475. self.view.addSubview(self.blankView)
  476. self.blankView.mas_makeConstraints { make in
  477. make?.top.equalTo()(self.topBaseView.mas_bottom)
  478. make?.left.right().equalTo()(self.view)
  479. make?.bottom.equalTo()(self.bottomBaseView.mas_top)
  480. make?.height.greaterThanOrEqualTo()(200)
  481. }
  482. self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates", nil)
  483. if (self.isBackground) {
  484. self.blankView.imageView.image = NSImage(named: KMImageNameEmptyBackground)
  485. }
  486. self.blankView.wantsLayer = true
  487. // self.blankView.layer.backgroundColor = [KMAppearance KMColor_Layout_L0].CGColor;
  488. // NSMenu * = [[NSMenu alloc]initWithTitle:@""];
  489. let menu = NSMenu()
  490. if(!self.isBackground) {
  491. if(!self.isBatchOperation) {
  492. _ = menu.addItem(title: KMLocalizedString("Batch Add Watermarks", nil), action: #selector(_buttonItemClick_addBatch), target: self)
  493. }
  494. _ = menu.addItem(title: KMLocalizedString("Remove All Watermark Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
  495. } else {
  496. if(!self.isBatchOperation) {
  497. _ = menu.addItem(title: KMLocalizedString("Batch Add Background", nil), action: #selector(_buttonItemClick_addBatch), target: self)
  498. }
  499. _ = menu.addItem(title: KMLocalizedString("Remove All Background Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
  500. }
  501. self.view.menu = menu;
  502. }
  503. private func _loadData() {
  504. self._textTemplateArray = []
  505. self._fileTemplateArray = []
  506. if (!self.isBackground) {
  507. let watermarkArr = KMWatermarkManager.defaultManager.watermarks
  508. for i in 0 ..< watermarkArr.count {
  509. let watermark = watermarkArr[i]
  510. if ((watermark.image) != nil) {
  511. self._fileTemplateArray?.append(watermark)
  512. } else {
  513. self._textTemplateArray?.append(watermark)
  514. }
  515. }
  516. } else {
  517. let arr = KMBackgroundManager.defaultManager.datas
  518. for i in 0 ..< arr.count {
  519. let obj = arr[i]
  520. if (obj.type == .color) {
  521. self._textTemplateArray?.append(obj)
  522. } else {
  523. self._fileTemplateArray?.append(obj)
  524. }
  525. }
  526. }
  527. self.collectionView.reloadData()
  528. self._updateActionButtonbackgroundColor()
  529. NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  530. }
  531. private func _isSelectIndex() -> Bool {
  532. var indexSet = false
  533. if (!self.isBackground) {
  534. if(self.currentType == .txt) {
  535. if let data = self._currentTextData {
  536. indexSet = (self._textTemplateArray ?? []).contains(data)
  537. }
  538. }else {
  539. if let data = self._currentFileData {
  540. indexSet = (self._fileTemplateArray ?? []).contains(data)
  541. }
  542. }
  543. } else {
  544. if(self.currentBackgroundType == .color) {
  545. if let data = self._currentColorData {
  546. indexSet = (self._textTemplateArray ?? []).contains(data)
  547. }
  548. }else {
  549. if let data = self._currentImageData {
  550. indexSet = (self._fileTemplateArray ?? []).contains(data)
  551. }
  552. }
  553. }
  554. return indexSet;
  555. }
  556. private func _updateActionButtonbackgroundColor() {
  557. let indexSet = self._isSelectIndex()
  558. if (self.files?.count ?? 0 > 0 || self.pdfView?.document.documentURL.path.count ?? 0 > 0 ) && indexSet {
  559. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  560. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  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. // NSMutableArray *arr = [NSMutableArray array];
  569. let file = KMBatchOperateFile(filePath: self.pdfView!.document.documentURL.path, type: self.isBackground ? .AddBackground : .AddWatermark)
  570. baseWindowController.window?.makeKeyAndOrderFront(nil)
  571. baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBackground ? .AddBackground : .AddWatermark, files: [file])
  572. }
  573. @objc private func _buttonItemClick_CleanAll(_ sender: Any?) {
  574. let alert = NSAlert()
  575. alert.alertStyle = .warning
  576. alert.messageText = ""
  577. alert.informativeText = KMLocalizedString("Are you sure to delete all templates?", nil)
  578. alert.addButton(withTitle: KMLocalizedString("Delete", nil))
  579. alert.addButton(withTitle: KMLocalizedString("Cancel", nil))
  580. alert.beginSheetModal(for: NSApp.mainWindow!) { returnCode in
  581. if returnCode == .alertFirstButtonReturn {
  582. self._deleteAllWatermark()
  583. }
  584. }
  585. }
  586. func editBackground(_ background: KMBackgroundModel) {
  587. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  588. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  589. filePath = self.pdfView?.document.documentURL.path ?? ""
  590. }
  591. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  592. let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
  593. controller.isBatch = isBatchOperation
  594. controller.type = .edit
  595. controller.background = background
  596. controller.currentType = self.currentBackgroundType.rawValue
  597. controller.pdfDocument = cdocument
  598. controller.cancelAction = { [weak self] controller in
  599. self?.km_endSheet()
  600. }
  601. self.km_beginSheet(windowC: controller)
  602. controller.operateCallBack = { controller, background, countType in
  603. self.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
  604. self._loadData()
  605. var haveBackgrounds = false
  606. if self.currentBackgroundType == .color {
  607. self._currentColorData = background
  608. if self._textTemplateArray?.count ?? 0 > 0 {
  609. haveBackgrounds = true
  610. }
  611. } else if self.currentBackgroundType == .file {
  612. self._currentImageData = background
  613. if self._fileTemplateArray?.count ?? 0 > 0 {
  614. haveBackgrounds = true
  615. }
  616. }
  617. if haveBackgrounds {
  618. let indexPath = IndexPath(item: 0, section: 0)
  619. var set = Set<IndexPath>()
  620. set.insert(indexPath)
  621. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  622. }
  623. self._postNotification()
  624. }
  625. }
  626. private func deleteBackground(_ background: KMBackgroundModel) {
  627. let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background)
  628. self._loadData()
  629. self._postNotification()
  630. }
  631. func editWatermark(_ waterMark: KMWatermarkModel) {
  632. if !isBackground {
  633. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  634. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  635. filePath = self.pdfView?.document.documentURL.path ?? ""
  636. }
  637. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  638. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  639. controller.watermark = waterMark
  640. controller.isBatch = isBatchOperation
  641. controller.type = .edit
  642. controller.pdfDocument = cdocument
  643. controller.currentType = self.currentType.rawValue
  644. controller.cancelAction = { [weak self] wmWindowC in
  645. self?.km_endSheet()
  646. }
  647. controller.operateCallBack = { controller, watermark, countType in
  648. self._loadData()
  649. var haveWaters = false
  650. if self.currentType == .txt {
  651. self._currentTextData = watermark
  652. if self._textTemplateArray?.count ?? 0 > 0 {
  653. haveWaters = true
  654. }
  655. } else if self.currentType == .file {
  656. self._currentFileData = watermark
  657. if self._fileTemplateArray?.count ?? 0 > 0 {
  658. haveWaters = true
  659. }
  660. }
  661. if haveWaters {
  662. let indexPath = IndexPath(item: 0, section: 0)
  663. var set = Set<IndexPath>()
  664. set.insert(indexPath)
  665. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  666. }
  667. self._postNotification()
  668. }
  669. self.km_beginSheet(windowC: controller)
  670. } else {
  671. // Handle background case
  672. }
  673. }
  674. private func _deleteAllWatermark() {
  675. if(!self.isBackground) {
  676. for waterMark in self._textTemplateArray ?? [] {
  677. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  678. }
  679. for waterMark in self._fileTemplateArray ?? [] {
  680. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  681. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  682. }
  683. } else {
  684. // for background in self._textTemplateArray ?? [] {
  685. //// [[KMBackgroundManager defaultManager] removeBackground:background];
  686. // _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  687. // }
  688. //
  689. // for background in self._fileTemplateArray ?? [] {
  690. //// [[KMBackgroundManager defaultManager] removeBackground:background];
  691. // _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background as! KMBackgroundModel)
  692. // }
  693. _ = KMBackgroundManager.defaultManager.deleteAllTemplates()
  694. }
  695. self._loadData()
  696. self._postNotification()
  697. }
  698. private func _deleteWatermark(_ waterMark: KMWatermarkModel) {
  699. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  700. let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark)
  701. self._loadData()
  702. self._postNotification()
  703. }
  704. private func _postNotification() {
  705. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateWatermarksNotification"), object: self)
  706. }
  707. @objc private func _watermarksNotification(_ notification: NSNotification) {
  708. let addWatermark = notification.object as? KMBatchOperateAddWatermarkViewController
  709. if self.isEqual(to: addWatermark) == false {
  710. self._loadData()
  711. var haveWaters = false
  712. if (self.currentType == .txt) {
  713. if let cnt = self._textTemplateArray?.count, cnt > 0 {
  714. haveWaters = true
  715. }
  716. } else if (self.currentType == .file) {
  717. if let cnt = self._fileTemplateArray?.count, cnt > 0 {
  718. haveWaters = true
  719. }
  720. }
  721. if (haveWaters) {
  722. let indexpath = IndexPath(item: 0, section: 0)
  723. var set = Set<IndexPath>()
  724. set.insert(indexpath)
  725. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  726. }
  727. }
  728. }
  729. //检查是否有任务文件要加载,如果有,重置任务。如果返回YES,表示有任务并且任务已经重置,否则表示没有任务,需要中断操作
  730. private func _checkAndResetTask() -> Bool {
  731. if let cnt = self.files?.count, cnt < 1 {
  732. return false
  733. }
  734. for i in 0 ..< self.files!.count {
  735. let file = self.files![i]
  736. if (self.isBackground) {
  737. file.addBackgroundInfo.resetState()
  738. } else {
  739. file.addWatermarkInfo.resetState()
  740. }
  741. }
  742. return true
  743. }
  744. private func _choosePathAndBeginOperation(_ obj: Any?) {
  745. let openPanel = NSOpenPanel()
  746. openPanel.canChooseFiles = false
  747. openPanel.canChooseDirectories = true
  748. openPanel.canCreateDirectories = true
  749. openPanel.beginSheetModal(for: self.view.window!) { result in
  750. if (result == .OK) {
  751. for fileURL in openPanel.urls {
  752. self.choosePath = fileURL.path
  753. if (!self.isBackground) {
  754. self._beginAddWatermark(obj as! KMWatermarkModel)
  755. } else {
  756. self._beginAddBackground(obj as! KMBackgroundModel)
  757. }
  758. }
  759. }
  760. }
  761. }
  762. private func _beginAddWatermark(_ watermark: KMWatermarkModel) {
  763. self.hiddenWindowCloseButtonIfNeeded()
  764. self.successFilePathURLArray?.removeAll()
  765. for i in 0 ..< self.files!.count {
  766. let file = self.files![i]
  767. file.addWatermarkInfo.savePath = self.choosePath
  768. let operation = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark)
  769. operation.delegate = self
  770. self.queue?.addOperation(operation)
  771. }
  772. if let cnt = self.queue?.operations.count, cnt > 0 {
  773. self.interfaceStatus = .Processing
  774. }
  775. }
  776. private func _beginAddBackground(_ background: KMBackgroundModel) {
  777. self.hiddenWindowCloseButtonIfNeeded()
  778. self.successFilePathURLArray?.removeAll()
  779. for i in 0 ..< self.files!.count {
  780. let file = self.files![i]
  781. file.addBackgroundInfo.savePath = self.choosePath
  782. // KMBatchAddBackgroundOperation *operation = [[[KMBatchAddBackgroundOperation alloc] initWithFile:file background:background] autorelease];
  783. // operation.delegate = self;
  784. // [self.queue addOperation:operation];
  785. let operation = KMBatchAddBackgroundOperation(file: file, backgroundM: background)
  786. operation.delegate = self
  787. self.queue?.addOperation(operation)
  788. }
  789. if let cnt = self.queue?.operations.count, cnt > 0 {
  790. self.interfaceStatus = .Processing
  791. }
  792. }
  793. @objc private func _batchFilesCountNotification(_ notification: NSNotification) {
  794. if (!self.onlyManagerTemplate) {
  795. let files: Array? = notification.object as? [KMBatchOperateFile]
  796. self.files? = files ?? []
  797. if (files?.count ?? 0 > 0) {
  798. self._haveFiles = true
  799. } else {
  800. self._haveFiles = false
  801. }
  802. self._updateActionButtonbackgroundColor()
  803. }
  804. }
  805. }
  806. extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
  807. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  808. let action = menuItem.action
  809. if (action == #selector(_buttonItemClick_CleanAll)) {
  810. if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
  811. return false
  812. }
  813. return true
  814. }
  815. return true
  816. }
  817. }
  818. extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  819. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  820. var count = 0
  821. if (!self.isBackground) {
  822. if (self.currentType == .txt) {
  823. count = self._textTemplateArray?.count ?? 0
  824. } else {
  825. count = self._fileTemplateArray?.count ?? 0
  826. }
  827. } else {
  828. if (self.currentBackgroundType == .color) {
  829. count = self._textTemplateArray?.count ?? 0
  830. } else {
  831. count = self._fileTemplateArray?.count ?? 0
  832. }
  833. }
  834. self.blankView.isHidden = count != 0
  835. return count
  836. }
  837. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  838. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
  839. if (!self.isBackground) {
  840. var waterMark: KMWatermarkModel?
  841. if (self.currentType == .txt) {
  842. waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
  843. if(self._currentTextData == waterMark) {
  844. item.isSelected = true
  845. }else {
  846. item.isSelected = false
  847. }
  848. } else {
  849. waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
  850. if(self._currentFileData == waterMark) {
  851. item.isSelected = true
  852. }else{
  853. item.isSelected = false
  854. }
  855. }
  856. // __block typeof(self) blockSelf = self;
  857. let _item = item as? KMWatermarkCollectionViewItem
  858. _item?.updateInterface(waterMark!)
  859. _item?.waterMarkOprateCallback = { [unowned self] type, wm in
  860. if (wm != nil) {
  861. if (type == .Edit) {
  862. if (self.currentType == .txt) {
  863. self._currentTextData = waterMark
  864. } else {
  865. self._currentFileData = waterMark
  866. }
  867. collectionView.reloadData()
  868. self.editWatermark(waterMark!)
  869. } else {
  870. self._deleteWatermark(waterMark!)
  871. }
  872. }
  873. };
  874. } else {
  875. var background: KMBackgroundModel?
  876. if (self.currentBackgroundType == .color) {
  877. background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
  878. if(self._currentColorData == background) {
  879. item.isSelected = true
  880. }else {
  881. item.isSelected = false
  882. }
  883. } else {
  884. background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
  885. if(self._currentImageData == background) {
  886. item.isSelected = true
  887. }else {
  888. item.isSelected = false
  889. }
  890. }
  891. let _item = item as? KMWatermarkCollectionViewItem
  892. _item?.updateBackgroundInterface(background!)
  893. // __block typeof(self) blockSelf = self;
  894. _item?.backgroundOperateCallback = { [unowned self] type, bg in
  895. if (type == .Edit) {
  896. if (self.currentBackgroundType == .color) {
  897. self._currentColorData = background
  898. } else {
  899. self._currentImageData = background
  900. }
  901. collectionView.reloadData()
  902. self.editBackground(background!)
  903. } else {
  904. self.deleteBackground(background!)
  905. }
  906. } ;
  907. }
  908. self._updateActionButtonbackgroundColor()
  909. return item
  910. }
  911. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  912. if (collectionView.frame.size.width < 240) {
  913. return NSMakeSize(0, 0)
  914. } else {
  915. return NSMakeSize(104, 167)
  916. }
  917. }
  918. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  919. if (collectionView.frame.size.width < 32) {
  920. return NSEdgeInsetsMake(0, 0, 0, 0)
  921. }
  922. return NSEdgeInsetsMake(0, 16, 0, 16)
  923. }
  924. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  925. let indexPath = indexPaths.first
  926. guard let _indexPath = indexPath else {
  927. return
  928. }
  929. if (indexPath!.item < 0) {
  930. return
  931. }
  932. if (self.isBackground) {
  933. var background: KMBackgroundModel?
  934. if (self.currentBackgroundType == .color) {
  935. background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
  936. self._currentColorData = background
  937. } else {
  938. background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
  939. self._currentImageData = background
  940. }
  941. for i in 0 ..< self.files!.count {
  942. let file = self.files![i]
  943. file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (background?.pageRangeType.rawValue)!)!
  944. file.addBackgroundInfo.pageRangeString = background?.pageRangeString
  945. }
  946. } else {
  947. var waterMark: KMWatermarkModel?
  948. if (self.currentType == .txt) {
  949. waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
  950. self._currentTextData = waterMark
  951. } else {
  952. waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
  953. self._currentFileData = waterMark
  954. }
  955. for i in 0 ..< self.files!.count {
  956. let file = self.files![i]
  957. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (waterMark?.pageRangeType.rawValue)!)!
  958. file.addWatermarkInfo.pageRangeString = waterMark?.pageRangeString
  959. // if (file.addWatermarkInfo.pageChoice == KMBatchOperatePageChoice_Input) {
  960. // NSArray *arr = [self allPageNumbers:waterMark.pagesString];
  961. // NSArray *sortedArray = [file pagesArrayIntersectWithArray:arr];
  962. // if (sortedArray.count < 1) {
  963. // file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice_All;
  964. // } else {
  965. // file.addWatermarkInfo.pageRangeString = [sortedArray componentsJoinedByString:@","];
  966. // }
  967. // }
  968. }
  969. }
  970. NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
  971. collectionView.reloadData()
  972. self._updateActionButtonbackgroundColor()
  973. }
  974. }