KMCommonDefine.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // NSColorPanel
  48. let kColorPanel = NSColorPanel.shared
  49. public func KMColorPanelCloseIfNeed() {
  50. if NSColorPanel.sharedColorPanelExists {
  51. kColorPanel.setTarget(nil)
  52. kColorPanel.setAction(nil)
  53. kColorPanel.orderOut(nil)
  54. kColorPanel.setIsVisible(false)
  55. }
  56. }
  57. public func KMValidateEmail(email: String) -> Bool {
  58. let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
  59. let pred = NSPredicate(format: "SELF MATCHES %@", regex)
  60. return pred.evaluate(with: email)
  61. }
  62. // ColumnID
  63. let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page")
  64. let kLabelColumnId = NSUserInterfaceItemIdentifier(rawValue: "label")
  65. let kResultsColumnId = NSUserInterfaceItemIdentifier(rawValue: "results")
  66. // MARK: - 特定的Block
  67. typealias KMBatchActionBlock = (_ controller: NSWindowController, _ files: [KMFileAttribute]) -> Void
  68. // MARK: - KM & KMProtocol
  69. public struct KM<Base> {
  70. public let base: Base
  71. init(_ base: Base) {
  72. self.base = base
  73. }
  74. }
  75. // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性
  76. // km 的类型是 KM, KM的关联类型(泛型)就是他自己
  77. public protocol KMProtocol {}
  78. extension KMProtocol {
  79. public var km: KM<Self> {
  80. return KM(self)
  81. }
  82. public static var km: KM<Self>.Type {
  83. return KM.self
  84. }
  85. }
  86. // 所有 NSObject 的子类都默认遵循了 KMProtocol
  87. extension NSObject: KMProtocol {}