KMAlertWindowController.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // KMAlertWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/10/27.
  6. //
  7. import Cocoa
  8. typealias alertConfirmCallback = () -> ()
  9. let kShowRemoveAllFilesHintinterfaceKey = "kShowRemoveAllFilesHintinterfaceKey"
  10. class KMAlertWindowController: NSWindowController{
  11. var alertConfirmCallback: alertConfirmCallback?
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var alertCheckButton: NSButton!
  14. @IBOutlet var cancelButton: NSButton!
  15. @IBOutlet var actionButton: NSButton!
  16. @objc class func swiftLoad() {
  17. let dict: NSDictionary = UserDefaults.standard.dictionaryRepresentation() as NSDictionary
  18. var X = false
  19. for key in dict.allKeys {
  20. if let data = key as? String, data == kShowRemoveAllFilesHintinterfaceKey{
  21. X = true
  22. }
  23. }
  24. if !X {
  25. UserDefaults().set(true, forKey: kShowRemoveAllFilesHintinterfaceKey)
  26. }
  27. }
  28. override func windowDidLoad() {
  29. super.windowDidLoad()
  30. self.titleLabel.stringValue = NSLocalizedString("Remove All", comment: "")
  31. self.alertCheckButton.title = NSLocalizedString("No longer prompt", comment: "")
  32. self.cancelButton.title = NSLocalizedString("Cancel", comment: "")
  33. self.actionButton.title = NSLocalizedString("Remove All", comment: "")
  34. }
  35. @IBAction func buttonClicked_Cancel(_ sender: NSButton) {
  36. self.setNeedShowRemoveAllFilesHint()
  37. self.dismissSheet(sender)
  38. }
  39. @IBAction func buttonClicked_OK(_ sender: NSButton) {
  40. self.setNeedShowRemoveAllFilesHint()
  41. setNeedShowRemoveAllFilesHint()
  42. self.alertConfirmCallback?()
  43. self.dismissSheet(sender)
  44. }
  45. func setNeedShowRemoveAllFilesHint() {
  46. UserDefaults.standard.set(self.alertCheckButton.state == .on ? false : true, forKey: kShowRemoveAllFilesHintinterfaceKey)
  47. UserDefaults.standard.synchronize()
  48. }
  49. func dismissSheet(_ sender: NSButton) {
  50. if #available(macOS 10.13, *) {
  51. self.window?.endSheet(sender.window!)
  52. } else {
  53. NSApp.endSheet(self.window!)
  54. }
  55. self.window?.orderOut(self)
  56. }
  57. class func needShowRemoveAllFilesHint() -> Bool {
  58. return UserDefaults().bool(forKey: kShowRemoveAllFilesHintinterfaceKey)
  59. }
  60. }