KMHistoryFileDeleteWindowController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // KMHistoryFileDeleteWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/11/8.
  6. //
  7. import Cocoa
  8. typealias historyFileDeleteCallback = (_ indexPaths: [URL], _ windowController: KMHistoryFileDeleteWindowController) -> Void
  9. class KMHistoryFileDeleteWindowController: NSWindowController {
  10. @IBOutlet weak var deleteOptionsLabel: NSTextField!
  11. @IBOutlet weak var noLongerPromptView: NSView!
  12. @IBOutlet weak var cancelBox: NSBox!
  13. @IBOutlet weak var deleteBox: NSBox!
  14. var cancelVC: KMDesignButton!
  15. var deleteVC: KMDesignButton!
  16. var noLongerPromptVC: KMDesignButton!
  17. var deleteCallback: historyFileDeleteCallback?
  18. var indexPaths: [URL] = []
  19. override func windowDidLoad() {
  20. super.windowDidLoad()
  21. self.cancelVC = KMDesignButton.init(withType: .Text)
  22. self.deleteVC = KMDesignButton.init(withType: .Text)
  23. self.noLongerPromptVC = KMDesignButton.init(withType: .CheckBox)
  24. initLocalization()
  25. initializeUI()
  26. }
  27. // MARK: Init
  28. func initializeUI() {
  29. self.window?.backgroundColor = .white
  30. deleteOptionsLabel.font = NSFont(name: "PingFangSC-Semibold", size: 15)
  31. deleteOptionsLabel.textColor = .black
  32. cancelBox.fillColor = NSColor(hex: "#F5F5F5")
  33. cancelBox.cornerRadius = 5.0
  34. cancelBox.contentView = cancelVC.view
  35. cancelVC.target = self
  36. cancelVC.action = #selector(cancelAction(_:))
  37. cancelVC.updateUI()
  38. deleteBox.fillColor = .clear
  39. deleteBox.contentView = deleteVC.view
  40. deleteVC.target = self
  41. deleteVC.action = #selector(deleteAction(_:))
  42. deleteVC.button(type: .Cta, size: .m)
  43. noLongerPromptView.addSubview(noLongerPromptVC.view)
  44. noLongerPromptVC.view.mas_makeConstraints { (make) in
  45. make?.top.bottom().offset()(0)
  46. make?.centerX.offset()(0)
  47. }
  48. noLongerPromptVC.target = self
  49. noLongerPromptVC.action = #selector(deleteHistoryFileAction(_:))
  50. noLongerPromptVC.checkbox_radio()
  51. }
  52. func initLocalization() {
  53. let firstPath = indexPaths.first?.path.lastPathComponent ?? ""
  54. // deleteOptionsLabel.stringValue = String(format: NSLocalizedString("Remove\"%@\"from your Recent Files?", comment: ""), firstPath)
  55. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  56. if urls.count == indexPaths.count {
  57. deleteOptionsLabel.stringValue = NSLocalizedString("Clear All Recents", comment: "")
  58. } else {
  59. deleteOptionsLabel.stringValue = NSLocalizedString("Delete selected file from the recents", comment: "")
  60. }
  61. noLongerPromptVC.stringValue = NSLocalizedString("Don't ask again", comment: "")
  62. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  63. deleteVC.stringValue = NSLocalizedString("Delete", comment: "")
  64. }
  65. // MARK: Private Methods
  66. @objc func cancelAction(_ sender: NSButton) -> Void {
  67. NSApp.mainWindow!.endSheet(self.window!)
  68. self.window?.orderOut(self)
  69. }
  70. @objc func deleteAction(_ sender: NSButton) -> Void {
  71. if let callback = deleteCallback {
  72. callback(indexPaths, self)
  73. }
  74. NSApp.mainWindow!.endSheet(self.window!)
  75. self.window?.orderOut(self)
  76. }
  77. // MARK: Action Methods
  78. @IBAction func deleteHistoryFileAction(_ sender: NSButton) {
  79. if noLongerPromptVC.state == .Norm {
  80. noLongerPromptVC.state = .Checked
  81. UserDefaults.standard.setValue(true, forKey: "kHistoryDeleteNOReminderKey")
  82. } else {
  83. noLongerPromptVC.state = .Norm
  84. UserDefaults.standard.setValue(false, forKey: "kHistoryDeleteNOReminderKey")
  85. }
  86. UserDefaults.standard.synchronize()
  87. }
  88. }