KMCommonDefine.swift 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // ColumnID
  48. let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page")
  49. let kLabelColumnId = NSUserInterfaceItemIdentifier(rawValue: "label")
  50. let kResultsColumnId = NSUserInterfaceItemIdentifier(rawValue: "results")
  51. // MARK: - 特定的Block
  52. typealias KMBatchActionBlock = (_ controller: NSWindowController, _ files: [KMFileAttribute]) -> Void
  53. // MARK: - KM & KMProtocol
  54. public struct KM<Base> {
  55. public let base: Base
  56. init(_ base: Base) {
  57. self.base = base
  58. }
  59. }
  60. // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性
  61. // km 的类型是 KM, KM的关联类型(泛型)就是他自己
  62. public protocol KMProtocol {}
  63. extension KMProtocol {
  64. public var km: KM<Self> {
  65. return KM(self)
  66. }
  67. public static var km: KM<Self>.Type {
  68. return KM.self
  69. }
  70. }
  71. // 所有 NSObject 的子类都默认遵循了 KMProtocol
  72. extension NSObject: KMProtocol {}