// // KMCommonDefine.swift // PDF Reader Pro // // Created by tangchao on 2023/3/28. // import Foundation /*  KMCommonDefine [共用的定义]  ** 用于定义一些共用的函数、共用的通知名称、其他 */ // MARK: - 共用的函数 public func KMPrint(_ items: Any..., separator: String = " ", terminator: String = "\n", beep: Bool = false) { #if DEBUG print(items, separator: separator, terminator: terminator) #endif if beep { NSSound.beep() } } public func km_synchronized(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T { objc_sync_enter(lock) defer { objc_sync_exit(lock) } return try body() } // MARK: - 共用的通知名称 // MARK: - 共用的Block typealias KMItemClickBlock = (_ item: T?, _ index: Int)->() typealias KMCommonClickBlock = (_ index: Int, _ params: Any...)->() typealias KMCommonBlock = (_ params: Any...)->() typealias KMEmptyBlock = ()->Void typealias KMValueDidChangeBlock = (_ value: Any?, _ change: [NSKeyValueChangeKey : Any]?)->() typealias KMResultBlock = (_ result: KMResult?, _ params: Any...)->Void // MARK: - 其他 let kKMLocalForDraggedTypes = NSPasteboard.PasteboardType(rawValue: "KMLocalForDraggedTypes") let kKMMainMenuAccountRefreshItemTag = 16 let KMSelectorCopy = NSSelectorFromString("copy:") let KMNormalBlankSize = NSMakeSize(595, 842) let kControlString = "⌃" let kCommandString = "⌘" let kOptionString = "⌥" let kShiftString = "⇧" let kHTTPSPrefix = "https://" let kHTTPPrefix = "http://" let kFTPPrefix = "ftp://" let kEmailPrefix = "mailto:" let kWindowFrameKey = "windowFrame" // NSColorPanel let kColorPanel = NSColorPanel.shared public func KMColorPanelCloseIfNeed() { if NSColorPanel.sharedColorPanelExists { kColorPanel.setTarget(nil) kColorPanel.setAction(nil) kColorPanel.orderOut(nil) kColorPanel.setIsVisible(false) } } public func KMValidateEmail(email: String) -> Bool { let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let pred = NSPredicate(format: "SELF MATCHES %@", regex) return pred.evaluate(with: email) } // ColumnID let kPageColumnId = NSUserInterfaceItemIdentifier(rawValue: "page") let kLabelColumnId = NSUserInterfaceItemIdentifier(rawValue: "label") let kResultsColumnId = NSUserInterfaceItemIdentifier(rawValue: "results") // MARK: - 特定的Block typealias KMBatchActionBlock = (_ controller: NSWindowController, _ files: [KMFileAttribute]) -> Void // MARK: - KM & KMProtocol public struct KM { public let base: Base init(_ base: Base) { self.base = base } } // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性 // km 的类型是 KM, KM的关联类型(泛型)就是他自己 public protocol KMProtocol {} extension KMProtocol { public var km: KM { return KM(self) } public static var km: KM.Type { return KM.self } } // 所有 NSObject 的子类都默认遵循了 KMProtocol extension NSObject: KMProtocol {}