KMHistoryFileDeleteWindowController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = KMAppearance.Layout.h0Color()
  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. noLongerPromptVC.textColor = KMAppearance.Layout.h0Color()
  53. noLongerPromptVC.textColor_custom = KMAppearance.Layout.h0Color()
  54. noLongerPromptVC.textColor_none = KMAppearance.Layout.h0Color()
  55. noLongerPromptVC.textColor_hov = KMAppearance.Layout.h0Color()
  56. noLongerPromptVC.updateUI()
  57. }
  58. func initLocalization() {
  59. let firstPath = indexPaths.first?.path.lastPathComponent ?? ""
  60. // deleteOptionsLabel.stringValue = String(format: NSLocalizedString("Remove\"%@\"from your Recent Files?", comment: ""), firstPath)
  61. let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
  62. if urls.count == indexPaths.count {
  63. deleteOptionsLabel.stringValue = NSLocalizedString("Clear All Recents", comment: "")
  64. } else {
  65. deleteOptionsLabel.stringValue = NSLocalizedString("Remove from Recents", comment: "")
  66. }
  67. noLongerPromptVC.stringValue = NSLocalizedString("Do not show this message again", comment: "")
  68. cancelVC.stringValue = NSLocalizedString("Cancel", comment: "")
  69. deleteVC.stringValue = NSLocalizedString("Delete", comment: "")
  70. let mainBundle = Bundle.main
  71. if let infoDictionary = mainBundle.infoDictionary {
  72. if let iconFileName: String = infoDictionary["CFBundleIconName"] as? String {
  73. let iconImage = NSImage(named: iconFileName)
  74. self.iconImageView.image = iconImage
  75. }
  76. }
  77. }
  78. // MARK: Private Methods
  79. @objc func cancelAction(_ sender: NSButton) -> Void {
  80. NSApp.mainWindow!.endSheet(self.window!)
  81. self.window?.orderOut(self)
  82. }
  83. @objc func deleteAction(_ sender: NSButton) -> Void {
  84. if let callback = deleteCallback {
  85. callback(indexPaths, self)
  86. }
  87. NSApp.mainWindow!.endSheet(self.window!)
  88. self.window?.orderOut(self)
  89. }
  90. // MARK: Action Methods
  91. @IBAction func deleteHistoryFileAction(_ sender: NSButton) {
  92. if noLongerPromptVC.state != .Checked {
  93. noLongerPromptVC.state = .Checked
  94. UserDefaults.standard.setValue(true, forKey: "kHistoryDeleteNOReminderKey")
  95. } else {
  96. noLongerPromptVC.state = .Norm
  97. UserDefaults.standard.setValue(false, forKey: "kHistoryDeleteNOReminderKey")
  98. }
  99. UserDefaults.standard.synchronize()
  100. }
  101. }