// // KMRemovePasswordResultTipView.swift // PDF Master // // Created by tangchao on 2022/11/29. // import Cocoa enum KMRemovePasswordResult: Int { case success = 1 case failure = 2 } class KMRemovePasswordResultTipView: NSView { private var iconImageView = NSImageView() private var iconSelectImageView = NSImageView() private var label = NSTextField(labelWithString: "") private var myResult: KMRemovePasswordResult = .success var result: KMRemovePasswordResult { get { return myResult } set { myResult = newValue if myResult == .success { label.stringValue = NSLocalizedString("Successfully delete security settings.", comment: "") iconImageView.image = NSImage(named: "KMImageNameNewTipSelected") } else { label.stringValue = NSLocalizedString("Failed to delete security settings.", comment: "") iconImageView.image = NSImage(named: "KMImageNameSecureTipFailure") } } } override init(frame frameRect: NSRect) { super.init(frame: frameRect) wantsLayer = true self.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor layer?.cornerRadius = 4 self.shadow = NSShadow() self.layer?.shadowColor = NSColor(hex: "#00000029").cgColor self.layer?.shadowOpacity = 1 self.layer?.shadowRadius = 8 self.layer?.shadowOffset = CGSize(width: 0, height: -3) addSubview(iconImageView) addSubview(label) // self.iconImageView.addSubview(self.iconSelectImageView) self.label.textColor = NSColor.white label.font = NSFont.systemFont(ofSize: 14) // iconImageView.image = NSImage(named: "KMImageNameTipSelectedBackgroud") // self.iconSelectImageView.image = NSImage(named: "KMImageNameTipSelected") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func layout() { super.layout() let width = NSWidth(self.bounds) let heigth = NSHeight(self.bounds) let iconSize: CGFloat = 14 iconImageView.frame = NSMakeRect(17, (heigth-iconSize)*0.5, iconSize, iconSize) self.iconSelectImageView.frame = NSMakeRect(3, 3, 8, 8) let labelX = iconImageView.frame.maxX + 5 let labelH: CGFloat = 22 label.frame = NSMakeRect(labelX, (heigth-labelH)*0.5, width-labelX, labelH) } func showInView(superView: NSView) { var style = NSMutableParagraphStyle() style.lineBreakMode = .byWordWrapping var attribute = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14), NSAttributedString.Key.paragraphStyle : style] let size = label.stringValue.boundingRect(with: NSMakeSize(min(NSWidth(superView.frame)-80, 500), NSHeight(superView.frame)-40), options: NSString.DrawingOptions(rawValue: 3), attributes: attribute).size let offsetSize = NSMakeSize(size.width+60, size.height+20) self.frame = NSMakeRect((NSWidth(superView.frame)-offsetSize.width)*0.5, NSHeight(superView.frame)-offsetSize.height-10, offsetSize.width, offsetSize.height) superView.addSubview(self) let ani = CABasicAnimation(keyPath: "opacity") ani.fromValue = 0 ani.toValue = 1 ani.duration = 0.5 layer?.add(ani, forKey: "animation") DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.3, execute: { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2, execute: { let ani = CABasicAnimation(keyPath: "opacity") ani.fromValue = 1 ani.toValue = 0 ani.duration = 0.5 self.layer?.add(ani, forKey: "animation") DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.3, execute: { self.removeFromSuperview() }) }) }) } }