KMCommonDefine.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // KMCommonDefine.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/3/28.
  6. //
  7. import Foundation
  8. /*  KMCommonDefine [共用的定义] 
  9. ** 用于定义一些共用的函数、共用的通知名称、其他
  10. */
  11. // MARK: - 共用的函数
  12. public func KMPrint(_ items: Any..., separator: String = " ", terminator: String = "\n", beep: Bool = false) {
  13. #if DEBUG
  14. print(items, separator: separator, terminator: terminator)
  15. #endif
  16. if beep {
  17. NSSound.beep()
  18. }
  19. }
  20. public func km_synchronized<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T {
  21. objc_sync_enter(lock)
  22. defer { objc_sync_exit(lock) }
  23. return try body()
  24. }
  25. // MARK: - 共用的通知名称
  26. // MARK: - 共用的Block
  27. typealias KMItemClickBlock<T> = (_ item: T?, _ index: Int)->()
  28. typealias KMCommonClickBlock = (_ index: Int, _ params: Any...)->()
  29. typealias KMCommonBlock = (_ params: Any...)->()
  30. typealias KMEmptyBlock = ()->Void
  31. typealias KMValueDidChangeBlock = (_ value: Any?, _ change: [NSKeyValueChangeKey : Any]?)->()
  32. typealias KMResultBlock = (_ result: KMResult?, _ params: Any...)->Void
  33. // MARK: - 其他
  34. let kKMLocalForDraggedTypes = NSPasteboard.PasteboardType(rawValue: "KMLocalForDraggedTypes")
  35. let kKMMainMenuAccountRefreshItemTag = 16
  36. let KMSelectorCopy = NSSelectorFromString("copy:")
  37. let KMNormalBlankSize = NSMakeSize(595, 842)
  38. let kControlString = "⌃"
  39. let kCommandString = "⌘"
  40. let kOptionString = "⌥"
  41. let kShiftString = "⇧"
  42. let kHTTPSPrefix = "https://"
  43. let kHTTPPrefix = "http://"
  44. let kFTPPrefix = "ftp://"
  45. let kEmailPrefix = "mailto:"
  46. let kWindowFrameKey = "windowFrame"
  47. let kObjectKey = "kObject"
  48. // NSColorPanel
  49. let kColorPanel = NSColorPanel.shared
  50. public func KMColorPanelCloseIfNeed() {
  51. if NSColorPanel.sharedColorPanelExists {
  52. kColorPanel.setTarget(nil)
  53. kColorPanel.setAction(nil)
  54. kColorPanel.orderOut(nil)
  55. kColorPanel.setIsVisible(false)
  56. }
  57. }
  58. public func KMValidateEmail(email: String) -> Bool {
  59. let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  60. let pred = NSPredicate(format: "SELF MATCHES %@", regex)
  61. return pred.evaluate(with: email)
  62. }
  63. public func KMMainThreadExecute(_ execute: @escaping (()->Void)) {
  64. if Thread.isMainThread {
  65. execute()
  66. } else {
  67. DispatchQueue.main.async(execute: execute)
  68. }
  69. }
  70. public func KMFormatNSNumber(_ number: NSNumber, to n: Int) -> String? {
  71. let formatter = NumberFormatter()
  72. formatter.minimumIntegerDigits = 1
  73. formatter.maximumIntegerDigits = 20 // or however many digits you need
  74. formatter.minimumFractionDigits = n
  75. formatter.maximumFractionDigits = n
  76. return formatter.string(from: number)
  77. }
  78. // ColumnID
  79. let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page")
  80. let kLabelColumnId = NSUserInterfaceItemIdentifier(rawValue: "label")
  81. let kResultsColumnId = NSUserInterfaceItemIdentifier(rawValue: "results")
  82. // MARK: - 特定的Block
  83. typealias KMBatchActionBlock = (_ controller: NSWindowController, _ files: [KMFileAttribute]) -> Void
  84. // MARK: - KM & KMProtocol
  85. public struct KM<Base> {
  86. public let base: Base
  87. init(_ base: Base) {
  88. self.base = base
  89. }
  90. }
  91. // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性
  92. // km 的类型是 KM, KM的关联类型(泛型)就是他自己
  93. public protocol KMProtocol {}
  94. extension KMProtocol {
  95. public var km: KM<Self> {
  96. return KM(self)
  97. }
  98. public static var km: KM<Self>.Type {
  99. return KM.self
  100. }
  101. }
  102. // 所有 NSObject 的子类都默认遵循了 KMProtocol
  103. extension NSObject: KMProtocol {}