// // KMGuideInfoWindowController.swift // PDF Reader Pro Edition // // Created by Niehaoyu on 2023/12/5. // import Cocoa class KMGuideInfoWindowController: NSWindowController { @IBOutlet weak var contendBox: NSBox! @objc var coverView: KMGuideCoverView! @objc var openFileGuidePanel: KMOpenFileGuidePanel! @objc var openFileGuideProperty: KMOpenFileGuideProperty! @objc var openFileGuideToolbar: KMOpenFileGuideToolbar! @objc var openFileFunctionView: KMOpenFileFunctionView! @objc var convertGuideView: KMConvertGuideView! @objc var customColorGuideView: KMCustomColorGuideView! @objc var aiIconGuideView: KMAIIconGuideView! @objc var editPDFPopGuideView: KMEditPDFPopGuideView? @objc var _type: KMGuideInfoType = .none @objc var openPanelRect: CGRect = .zero @objc var rightPanelRect: CGRect = .zero @objc var _digitalBoxRect: CGRect = .zero @objc var compareItemRect: CGRect = .zero @objc var purchaseHandle: ((_ view: KMGuideInfoWindowController)->Void)? @objc var normalGuideFinishHandle: ((_ view: KMGuideInfoWindowController)->Void)? @objc var finishHandle: ((_ view: KMGuideInfoWindowController, _ actionType: KMGuideActionType)->Void)? @objc var openFileToggleHandle: ((_ view: KMGuideInfoWindowController, _ infoType: KMGuideInfoType)->Void)? @objc var settingCallback: (()->Void)? var editPDFPopWindowFlag: Bool = false //MARK: Static Method @objc static func availableShow(_ type: KMGuideInfoType) -> Bool { if type == .openFileNormal { if (UserDefaults.standard.object(forKey: kKMGuideInfoOpenFileNormalKey) == nil) { return true } } else if type == .digitalSignGuide { if (UserDefaults.standard.object(forKey: kKMGuideInfoDigitalSignKey) == nil) { return true } } else if type == .pdfCompareGuide { if (UserDefaults.standard.object(forKey: kKMGuideInfoPDFCompareKey) == nil) { return true } } else if type == .convertGuide { if (UserDefaults.standard.object(forKey: kKMGuideInfoFileConvertKey) == nil) { return true } } else if type == .customColor { if (UserDefaults.standard.object(forKey: kKMGuideInfoCustomColorKey) == nil) { return true } } else if type == .aiTipIconInfo { if (UserDefaults.standard.object(forKey: kKMGuideInfoAITipIconViewKey) == nil) { return true } } else if type == .editPDFPopWindow { if (UserDefaults.standard.object(forKey: kKMGuideInfoEditPDFPopWindowKey) == nil) { return true } } return false } static func setDidShowFor(_ type: KMGuideInfoType) -> Void { if type == .openFileNormal { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoOpenFileNormalKey) UserDefaults.standard.synchronize() } else if type == .digitalSignGuide { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoDigitalSignKey) UserDefaults.standard.synchronize() } else if type == .pdfCompareGuide { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoPDFCompareKey) UserDefaults.standard.synchronize() } else if type == .convertGuide { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoFileConvertKey) UserDefaults.standard.synchronize() } else if type == .customColor { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoCustomColorKey) UserDefaults.standard.synchronize() } else if type == .aiTipIconInfo { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoAITipIconViewKey) UserDefaults.standard.synchronize() } else if type == .editPDFPopWindow { UserDefaults.standard.setValue("Show", forKey: kKMGuideInfoEditPDFPopWindowKey) UserDefaults.standard.synchronize() } } static var currentWindowController: KMGuideInfoWindowController! @objc static func currentWC() -> KMGuideInfoWindowController { if currentWindowController != nil { return currentWindowController } else { let guideInfoWC: KMGuideInfoWindowController = KMGuideInfoWindowController.init(windowNibName: "KMGuideInfoWindowController") currentWindowController = guideInfoWC; return guideInfoWC } } deinit { print("deinit Action") } //MARK: SystemMethod override func showWindow(_ sender: Any?) { super.showWindow(sender) self.show() } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.window?.backgroundColor = NSColor.clear self.window?.contentView?.wantsLayer = true self.window?.contentView?.layer?.backgroundColor = NSColor.clear.cgColor; self.window?.isOpaque = false self.contendBox.fillColor = NSColor.clear self.coverView = KMGuideCoverView.createFromNib() self.coverView.frame = self.contendBox.bounds self.contendBox.contentView = self.coverView } //MARK: Setter @objc var type: KMGuideInfoType { set { _type = newValue; } get { return _type; } } @objc var digitalBoxRect: CGRect { set { _digitalBoxRect = newValue; } get { return _digitalBoxRect; } } //MARK: Public Method @objc func show() { if self.type == .openFileNormal { self.type = .openFileNormalPanel } self.loadGuideInfoView() } func clearViews() { let viewsArr = NSArray.init(array: self.coverView.subviews) for view in viewsArr { if view is KMOpenFileGuidePanel { (view as AnyObject).removeFromSuperview() } else if view is KMOpenFileGuideProperty { (view as AnyObject).removeFromSuperview() } else if view is KMOpenFileGuideToolbar { (view as AnyObject).removeFromSuperview() } else if view is KMOpenFileFunctionView { (view as AnyObject).removeFromSuperview() } else if view is KMConvertGuideView { (view as AnyObject).removeFromSuperview() } else if view is KMCustomColorGuideView { (view as AnyObject).removeFromSuperview() } else if view is KMAIIconGuideView { (view as AnyObject).removeFromSuperview() } else if view is KMEditPDFPopGuideView { (view as AnyObject).removeFromSuperview() } } self.coverView.display() if self.window != nil { var rect = self.window?.frame rect?.size.width += 1 self.window?.setFrame(rect!, display: true) } } func loadGuideInfoView() { DispatchQueue.main.async { self.clearViews() self.contendBox.contentView = self.coverView if self.type == .openFileNormalPanel { if self.openFileGuidePanel == nil { self.openFileGuidePanel = KMOpenFileGuidePanel.createFromNib() } self.openFileGuidePanel.frame = self.coverView.bounds self.openFileGuidePanel.autoresizingMask = [.width, .height] self.openFileGuidePanel.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if actionType == .skip { //Skip->数字签名 //Skip-> guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) if KMGuideInfoWindowController.availableShow(.digitalSignGuide) && (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) && NSMinX(self.digitalBoxRect) > 450 { self.type = .digitalSignGuide self.loadGuideInfoView() } else { guard let callBack = self.finishHandle else { return } callBack(self, .windowNewFinish) self.closeAction() } } else if actionType == .next { //Next self.type = .openFileNormalProperty self.loadGuideInfoView() // guard let callBack = self.openFileToggleHandle else { // return // } // callBack(self, .openFileNormalProperty) // self.closeAction() } } self.openFileGuidePanel.circleRect = self.openPanelRect self.coverView.addSubview(self.openFileGuidePanel) self.openFileGuidePanel.updateViewColor() KMGuideInfoWindowController.setDidShowFor(.openFileNormal)//记录布局介绍 } else if self.type == .openFileNormalProperty { guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) if self.openFileGuideProperty == nil { self.openFileGuideProperty = KMOpenFileGuideProperty.createFromNib() } self.openFileGuideProperty.frame = self.coverView.bounds self.openFileGuideProperty.autoresizingMask = [.width, .height] self.openFileGuideProperty.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if actionType == .skip { //Skip->数字签名 //Skip-> guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) if KMGuideInfoWindowController.availableShow(.digitalSignGuide) && (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) && NSMinX(self.digitalBoxRect) > 450 { self.type = .digitalSignGuide self.loadGuideInfoView() } else { guard let callBack = self.finishHandle else { return } callBack(self, .windowNewFinish) self.closeAction() } } else if actionType == .previous { //Previous self.type = .openFileNormalPanel self.loadGuideInfoView() guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) // guard let callBack = self.openFileToggleHandle else { // return // } // callBack(self, .openFileNormalPanel) // self.closeAction() } else if actionType == .next { //Next self.type = .openFileNormalToolbar self.loadGuideInfoView() // guard let callBack = self.openFileToggleHandle else { // return // } // callBack(self, .openFileNormalToolbar) // self.closeAction() } } self.openFileGuideProperty.circleRect = self.rightPanelRect self.coverView.addSubview(self.openFileGuideProperty) self.openFileGuideProperty.updateViewColor() } else if self.type == .openFileNormalToolbar { if self.openFileGuideToolbar == nil { self.openFileGuideToolbar = KMOpenFileGuideToolbar.createFromNib() } self.openFileGuideToolbar.frame = self.coverView.bounds self.openFileGuideToolbar.autoresizingMask = [.width, .height] self.openFileGuideToolbar.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if actionType == .skip { //Skip->数字签名 //Skip-> guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) if KMGuideInfoWindowController.availableShow(.digitalSignGuide) && (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) && NSMinX(self.digitalBoxRect) > 450 { self.type = .digitalSignGuide self.loadGuideInfoView() } else { guard let callBack = self.finishHandle else { return } callBack(self, .windowNewFinish) self.closeAction() } } else if actionType == .previous { //Next self.type = .openFileNormalProperty self.loadGuideInfoView() // guard let callBack = self.openFileToggleHandle else { // return // } // callBack(self, .openFileNormalProperty) // self.closeAction() } else if actionType == .finish { guard let callBack = self.normalGuideFinishHandle else { return } callBack(self) if KMGuideInfoWindowController.availableShow(.digitalSignGuide) && (NSWidth(self.window!.frame) > NSMinX(self.digitalBoxRect) + 260) && NSMinX(self.digitalBoxRect) > 450 { self.type = .digitalSignGuide self.loadGuideInfoView() } else { guard let callBack = self.finishHandle else { return } callBack(self, .windowNewFinish) self.closeAction() } } } self.coverView.addSubview(self.openFileGuideToolbar) } else if self.type == .digitalSignGuide || self.type == .pdfCompareGuide { if self.type == .digitalSignGuide { KMGuideInfoWindowController.setDidShowFor(.digitalSignGuide)//记录数字签名介绍 } else if self.type == .pdfCompareGuide { KMGuideInfoWindowController.setDidShowFor(.pdfCompareGuide)//记录文件对比介绍 } if self.openFileFunctionView == nil { self.openFileFunctionView = KMOpenFileFunctionView.createFromNib() } self.openFileFunctionView.circleRect = self.digitalBoxRect self.openFileFunctionView.frame = self.coverView.bounds self.openFileFunctionView.autoresizingMask = [.width, .height] self.openFileFunctionView.type = self.type self.openFileFunctionView.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if self.type == .digitalSignGuide { guard let callBack = self.finishHandle else { return } callBack(self, .windowDigitalFinish) if actionType == .getIt { self.closeAction() } } else if self.type == .pdfCompareGuide { if actionType == .getIt { self.closeAction() } } } self.coverView.addSubview(self.openFileFunctionView) self.openFileFunctionView.updateViewColor() } else if self.type == .convertGuide { if self.convertGuideView == nil { self.convertGuideView = KMConvertGuideView.createFromNib() } self.convertGuideView.frame = self.coverView.bounds self.convertGuideView.autoresizingMask = [.width, .height] self.convertGuideView.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if actionType == .skip { self.closeAction() } else if actionType == .purchase { guard let callBack = self.purchaseHandle else { return } callBack(self) self.closeAction() } } self.convertGuideView.circleRect = self.digitalBoxRect self.coverView.addSubview(self.convertGuideView) self.convertGuideView.updateViewColor() KMGuideInfoWindowController.setDidShowFor(.convertGuide)//记录转档介绍 } else if self.type == .customColor { if self.customColorGuideView == nil { self.customColorGuideView = KMCustomColorGuideView.createFromNib() } self.customColorGuideView.frame = self.coverView.bounds self.customColorGuideView.autoresizingMask = [.width, .height] self.customColorGuideView.clickHandle = {[weak self] view, actionType in if actionType == .getIt { self?.closeAction() } } self.customColorGuideView.circleRect = self.digitalBoxRect self.customColorGuideView.updateViewColor() self.coverView.addSubview(self.customColorGuideView) KMGuideInfoWindowController.setDidShowFor(.customColor)//记录自定义颜色介绍 } else if self.type == .aiTipIconInfo { if self.aiIconGuideView == nil { self.aiIconGuideView = KMAIIconGuideView.createFromNib() } self.aiIconGuideView.frame = self.coverView.bounds self.aiIconGuideView.autoresizingMask = [.width, .height] self.aiIconGuideView.clickHandle = {[weak self] view, actionType in guard let self = self else { return } if actionType == .getIt { self.closeAction() } } self.aiIconGuideView.updateViewColor() self.aiIconGuideView.wantsLayer = true self.coverView.addSubview(self.aiIconGuideView) KMGuideInfoWindowController.setDidShowFor(.aiTipIconInfo)//记录自定义颜色介绍 } else if self.type == .editPDFPopWindow { self.editPDFPopGuideView = KMEditPDFPopGuideView(contentTop: self.editPDFPopWindowFlag) self.editPDFPopGuideView?.frame.origin = self.digitalBoxRect.origin // self.editPDFPopGuideView?.autoresizingMask = [.width, .height] self.editPDFPopGuideView?.itemClick = { [weak self] idx, _ in self?.closeAction() if idx == 0 { // 去设置 self?.settingCallback?() } } // self.aiIconGuideView.updateViewColor() // self.aiIconGuideView.wantsLayer = true self.coverView.addSubview(self.editPDFPopGuideView!) KMGuideInfoWindowController.setDidShowFor(.editPDFPopWindow)//记录 } } } func closeAction() { self.clearViews() self.close() } } extension KMGuideInfoWindowController: KMInterfaceThemeChangedProtocol { func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { self.editPDFPopGuideView?.interfaceThemeDidChanged(appearance) } }