KMHistoryFileDeleteWindowController.swift 4.2 KB

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