// // KMNCustomAlertView.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/10/14. // import Cocoa import KMComponentLibrary class KMNCustomAlertView: NSView { override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) } static func alertView(message: String, type: ComponentMessageType, fromView supverView: NSView, point:NSPoint) -> ComponentMessage? { let properties: ComponentMessageProperty = ComponentMessageProperty(messageType: type, title: message) let componentMessage = ComponentMessage() componentMessage.properties = properties let style = NSMutableParagraphStyle() style.lineBreakMode = .byWordWrapping style.alignment = .center let font = componentMessage.properties.propertyInfo.textFont var offsetSize = CGSize.zero let attributes: [NSAttributedString.Key: Any] = [ .font: font, .paragraphStyle: style ] let maxSize = CGSize(width: min(supverView.frame.size.width - 80, 500), height: supverView.frame.size.height - 40) let size = message.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: attributes).size let ceilSize = NSSize(width: ceil(size.width), height: ceil(size.height)) offsetSize = NSSize(width: ceilSize.width + 60, height: ceilSize.height + 30) componentMessage.frame = NSRect(x: (point.x - offsetSize.width/2), y: (point.y - offsetSize.height/2), width: offsetSize.width, height: offsetSize.height) supverView.addSubview(componentMessage) componentMessage.alphaValue = 0.0 NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 // 设置动画持续时间 context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) componentMessage.animator().alphaValue = 1.0 // 动画化透明度 }, completionHandler: { }) DispatchQueue.main.asyncAfter(deadline: .now() + 2) { NSAnimationContext.runAnimationGroup({ context in context.duration = 0.3 // 设置动画持续时间 context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) componentMessage.animator().alphaValue = 0.0 // 动画化透明度 }, completionHandler: { componentMessage.removeFromSuperview() }) } return componentMessage } }