KMCommonDefine.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // ColumnID
  71. let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page")
  72. let kLabelColumnId = NSUserInterfaceItemIdentifier(rawValue: "label")
  73. let kResultsColumnId = NSUserInterfaceItemIdentifier(rawValue: "results")
  74. // MARK: - 特定的Block
  75. typealias KMBatchActionBlock = (_ controller: NSWindowController, _ files: [KMFileAttribute]) -> Void
  76. // MARK: - KM & KMProtocol
  77. public struct KM<Base> {
  78. public let base: Base
  79. init(_ base: Base) {
  80. self.base = base
  81. }
  82. }
  83. // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性
  84. // km 的类型是 KM, KM的关联类型(泛型)就是他自己
  85. public protocol KMProtocol {}
  86. extension KMProtocol {
  87. public var km: KM<Self> {
  88. return KM(self)
  89. }
  90. public static var km: KM<Self>.Type {
  91. return KM.self
  92. }
  93. }
  94. // 所有 NSObject 的子类都默认遵循了 KMProtocol
  95. extension NSObject: KMProtocol {}