123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // 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<T>(_ lock: AnyObject, _ body: () throws -> T) rethrows -> T {
- objc_sync_enter(lock)
- defer { objc_sync_exit(lock) }
- return try body()
- }
- // MARK: - 共用的通知名称
- // MARK: - 共用的Block
- typealias KMItemClickBlock<T> = (_ 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"
- let kObjectKey = "kObject"
- // 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)
- }
- public func KMMainThreadExecute(_ execute: @escaping (()->Void)) {
- if Thread.isMainThread {
- execute()
- } else {
- DispatchQueue.main.async(execute: execute)
- }
- }
- // 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<Base> {
- public let base: Base
-
- init(_ base: Base) {
- self.base = base
- }
- }
- // 凡是遵守了 KMProtocol 协议的类型就都获取了 km 属性
- // km 的类型是 KM, KM的关联类型(泛型)就是他自己
- public protocol KMProtocol {}
- extension KMProtocol {
-
- public var km: KM<Self> {
- return KM(self)
- }
-
- public static var km: KM<Self>.Type {
- return KM.self
- }
- }
- // 所有 NSObject 的子类都默认遵循了 KMProtocol
- extension NSObject: KMProtocol {}
|