// // KMHomeViewController.swift // PDF Master // // Created by wanjun on 2022/10/13. // import Cocoa import KMAdvertisement @objc enum KMHomeToolState : Int { case OpenPDF = 0 case CreatePDF case Home case PDFTools case FavoriteDocuments case CloudDocuments } @objc enum KMAIHomeToolState : Int { case Home = 0 case AITranslation case AIRewriting case AIErrorCorrection } @objcMembers class KMHomeViewController: NSViewController { @IBOutlet weak var leftBox: NSBox! @IBOutlet weak var rightBox: NSBox! @IBOutlet weak var homeSplitView: NSSplitView! @IBOutlet weak var homeRightScrollViewView: NSScrollView! @IBOutlet weak var openPDFBox: NSBox! // Open PDF @IBOutlet weak var openPDFBoxHeight: NSLayoutConstraint! @IBOutlet weak var createPDFBox: NSBox! // Create PDF @IBOutlet weak var createPDFView: NSBox! // Create PDF @IBOutlet weak var createPDFBoxHeight: NSLayoutConstraint! @IBOutlet weak var createPDFViewHeight: NSLayoutConstraint! @IBOutlet weak var homeBox: NSBox! // Home @IBOutlet weak var homeBoxHeight: NSLayoutConstraint! @IBOutlet weak var pdfToolsBox: NSBox! // PDF Tools @IBOutlet weak var pdfToolsBoxHeight: NSLayoutConstraint! @IBOutlet weak var favoriteDocumentsBox: KMBox! // Favorite Documents @IBOutlet weak var favoriteDocumentsLabel: NSTextField! @IBOutlet weak var favoriteDocumentsImageView: NSImageView! @IBOutlet weak var cloudDocumentsBox: NSBox! // Cloud Documents @IBOutlet weak var cloudDocumentsBoxHeight: NSLayoutConstraint! @IBOutlet weak var productPromotionBox: KMBox! // 产品推广 @IBOutlet weak var pdfSeriesBox: NSBox! @IBOutlet weak var pdfSeriesBoxHeight: NSLayoutConstraint! @IBOutlet weak var pdfProSeriesBox: NSBox! @IBOutlet weak var pdfProSeriesBoxHeightConstraint: NSLayoutConstraint! @IBOutlet weak var pdfOthersBox: NSBox! @IBOutlet weak var pdfOthersBoxHeightConstraint: NSLayoutConstraint! @IBOutlet weak var othersBox: NSBox! @IBOutlet weak var othersBoxHeightConstraint: NSLayoutConstraint! @IBOutlet weak var activitiesBox: KMBox! // 活动 @IBOutlet weak var product_active_box: NSBox! @IBOutlet weak var product_active_scrollView: NSScrollView! @IBOutlet weak var product_active_spacing: NSLayoutConstraint! @IBOutlet weak var rightTopBox: NSBox! @IBOutlet weak var rightBottomBox: NSBox! @IBOutlet weak var rightFullBox: NSBox! @IBOutlet weak var rightTopBoxHeightConstraint: NSLayoutConstraint! @IBOutlet var fastToolViewController: KMHomeFastToolViewController! @IBOutlet var historyFileViewController: KMHomeHistoryFileViewController! @IBOutlet var pdfToolsViewController: KMPDFToolsViewController! @IBOutlet var cloudDocumentsViewController: KMCloudDocumentsViewController! @IBOutlet weak var dragView: KMHomeDragView! // AI 临时UI @IBOutlet weak var aiHomeBox: KMBox! @IBOutlet weak var aiHomeImageView: NSImageView! @IBOutlet weak var aiHomeLabel: NSTextField! @IBOutlet weak var aiTranslationBox: KMBox! @IBOutlet weak var aiTranslationImageView: NSImageView! @IBOutlet weak var aiTranslationLabel: NSTextField! @IBOutlet weak var aiRewritingBox: KMBox! @IBOutlet weak var aiRewritingImageView: NSImageView! @IBOutlet weak var aiRewritingLabel: NSTextField! @IBOutlet weak var aiErrorCorrectionBox: KMBox! @IBOutlet weak var aiErrorCorrectionImageView: NSImageView! @IBOutlet weak var aiErrorCorrectionLabel: NSTextField! @IBOutlet var aiOpenPDFFilesViewController: KMAIOpenPDFFilesVC! @IBOutlet var aiTranslationViewController: KMAITranslationVC! @IBOutlet var aiRewritingViewController: KMAIRewritingVC! var aiHomeState: KMAIHomeToolState = .Home let ScrollerViewWidget: CGFloat = 15.0 var homeWindowController : MainWindowController! // 产品推广数据 var productPromotionPDFProSeries: NSArray! var productPromotionOthers: NSArray! var productPromotionData: NSDictionary! var isShowQuickTour: Bool = false var myDocument: NSDocument? var currentWindowController: NSWindowController? var homeState: KMHomeToolState = .Home var urlToPDFWindowController: KMURLToPDFWindowController? var openPDFButtonVC: KMDesignButton! var createPDFButtonVC: KMDesignButton! var createPDFImage: KMDesignButton! var createPDFAddButtonVC: KMDesignButton! var homeButtonVC: KMTextImageButtonVC! var pdfToolsButtonVC: KMTextImageButtonVC! var cloudDocumentsButtonVC: KMTextImageButtonVC! var pdfSeriesButtonVC: KMTextImageButtonVC! var pdfOthersButtonVC: KMTextImageButtonVC! var popover: NSPopover? var deviceBrowserWC: KMDeviceBrowserWindowController? var progressController: SKProgressController? override func viewWillAppear() { super.viewWillAppear() if !KMLightMemberManager.manager.isLogin() { KMLightMemberManager.manager.canShowAdvancedView = true } } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. openPDFButtonVC = KMDesignButton.init(withType: .Text) createPDFButtonVC = KMDesignButton.init(withType: .Text) createPDFImage = KMDesignButton.init(withType: .Image) createPDFAddButtonVC = KMDesignButton.init(withType: .Image) homeButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil) pdfToolsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil) cloudDocumentsButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil) pdfSeriesButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil) pdfOthersButtonVC = KMTextImageButtonVC.init(nibName: "KMTextImageButtonVC", bundle: nil) self.homeSplitView.setPosition(270.0, ofDividerAt: 0) self.leftBox.fillColor = NSColor(hex: "#F7F8FA") self.productPromotionPDFProSeries = ["Windows", "iPhone / iPad", "Android"] self.productPromotionOthers = ["ComPDFKit", "SignFlow", "FiImage Editor", "FiImage Screen", "Free PDF Templates"] self.productPromotionData = ["Windows": ["Name" : "Windows", "Image" : "icon_home_logo_windows", "link":""], "iPhone / iPad" : ["Name" : "iPhone / iPad", "Image" : "icon_home_logo_mac", "link":""], "Android" : ["Name" : "Android", "Image" : "icon_home_logo_android", "link":""], "ComPDFKit" : ["Name" : "ComPDFKit", "Image" : "icon_home_logo_comPDFKit", "link":""], "SignFlow" : ["Name" : "SignFlow", "Image" : "icon_home_logo_signflow", "link":""], "FiImage Editor" : ["Name" : "FiImage Editor", "Image" : "icon_home_logo_editor", "link":""], "FiImage Screen" : ["Name" : "FiImage Screen", "Image" : "icon_home_logo_screen", "link":""], "Free PDF Templates" : ["Name" : "Free PDF Templates", "Image" : "icon_home_logo_templates"], "link":""] NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { (aEvent) -> NSEvent? in self.otherMouseDown(with: aEvent) return aEvent } // self.initNetworkingData() // self.initLocalization() // self.initializeUI() self.initializeUI_DMG() // refreshUI() NotificationCenter.default.addObserver(self, selector: #selector(homeFileRectChange(_:)), name: Notification.Name("KMHomeFileRectChange"), object: nil) } func initNetworkingData() { KMAdvertisementManager.manager.fetchDataWithResponseObject { [unowned self] data, responseObject, error in print("获取广告数据成功") // print(error) // print(responseObject) if data != nil { for model in data! { guard let sectionContent = model.content else { break } if model.showType == .list { for section in sectionContent { print("list section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language)) guard let itemContent = section.content else { break } for item in itemContent { print("item 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language)) print("item 图片链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal)) print("item 点击链接" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL)) print("item tips" + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips)) print("item actionType" + String(Int(item.actionType.rawValue))) } } } if model.showType == .view { for section in sectionContent { print("view section 标题" + KMAdvertisementModelTransition.transitionLanguage(langeuage: section.sectionTitle?.language)) guard let itemContent = section.content else { break } for item in itemContent { print("item 标题 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.title?.language)) print("item 图片链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.imageURL?.normal?.universal)) print("item 点击链接 = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.linkURL)) print("item tips = " + KMAdvertisementModelTransition.transitionLanguage(langeuage: item.tips)) print("item actionType = " + String(Int(item.actionType.rawValue))) } } } } } } } // MARK: Init func initializeUI() { openPDFBox.fillColor = .clear openPDFBox.contentView = openPDFButtonVC.view openPDFButtonVC.target = self openPDFButtonVC.action = #selector(self.homeToolAction(_:)) openPDFButtonVC.button(type: .Brand, size: .l, height: openPDFBoxHeight) createPDFBox.fillColor = .clear createPDFBox.contentView = createPDFButtonVC.view createPDFButtonVC.target = self createPDFButtonVC.action = #selector(self.homeToolAction(_:)) createPDFButtonVC.button(type: .Sec, size: .l, height: createPDFBoxHeight) createPDFButtonVC.textButtonBox.move = { [unowned self](mouseEntered: Bool) -> Void in if mouseEntered { if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover { self.createPDFButtonVC.state = .Hov self.createPDFButtonVC.updateUI() self.createPDFImage.state = .Hov self.createPDFImage.updateUI() self.createPDFImage.canHover = false } } else { if self.createPDFButtonVC.state != .Sel && self.createPDFButtonVC.canHover { self.createPDFButtonVC.state = .Norm self.createPDFButtonVC.updateUI() self.createPDFImage.state = .Norm self.createPDFImage.updateUI() self.createPDFImage.canHover = true } } } createPDFView.contentView = createPDFImage.view createPDFImage.image = NSImage(named: "icon_btn_arrow_gray_down_s_norm_false")! createPDFImage.target = self createPDFImage.action = #selector(self.homeToolAction(_:)) createPDFImage.button(type: .Icon_Fill, size: .m, height: createPDFViewHeight) homeBox.fillColor = .clear homeBox.contentView = homeButtonVC.view homeButtonVC.target = self homeButtonVC.action = #selector(self.homeToolAction(_:)) homeButtonVC.image = NSImage(named: "icon_home_sidebar_home_false")! homeButtonVC.image_hover = NSImage(named: "icon_home_sidebar_home_false")! homeButtonVC.image_click = NSImage(named: "icon_home_sidebar_home_true")! homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: homeBoxHeight) homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov) homeButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel) pdfToolsBox.fillColor = .clear pdfToolsBox.contentView = pdfToolsButtonVC.view pdfToolsButtonVC.target = self pdfToolsButtonVC.action = #selector(self.homeToolAction(_:)) pdfToolsButtonVC.image = NSImage(named: "icon_home_sidebar_tool_false")! pdfToolsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_tool_false")! pdfToolsButtonVC.image_click = NSImage(named: "icon_home_sidebar_tool_true")! pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: pdfToolsBoxHeight) pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov) pdfToolsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel) cloudDocumentsBox.fillColor = .clear cloudDocumentsBox.contentView = cloudDocumentsButtonVC.view cloudDocumentsButtonVC.target = self cloudDocumentsButtonVC.action = #selector(self.homeToolAction(_:)) cloudDocumentsButtonVC.image = NSImage(named: "icon_home_sidebar_cloud_false")! cloudDocumentsButtonVC.image_hover = NSImage(named: "icon_home_sidebar_cloud_false")! cloudDocumentsButtonVC.image_click = NSImage(named: "icon_home_sidebar_cloud_true")! cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.norm", text: "navmenu.win-text.def", height: cloudDocumentsBoxHeight) cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.hov", text: "navmenu.win-text.def", state:.Hov) cloudDocumentsButtonVC.navMenu(bg: "navmenu.has-icon.m.bg.sel", text: "navmenu.win-text.sel", state:.Sel) pdfSeriesBox.fillColor = .clear pdfSeriesBox.contentView = pdfSeriesButtonVC.view pdfSeriesButtonVC.target = self pdfSeriesButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:)) pdfSeriesButtonVC.type = .Right pdfSeriesButtonVC.canHover = false pdfSeriesButtonVC.imageWidth = 12.0 pdfSeriesButtonVC.imageHeight = 12.0 pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")! pdfSeriesButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: pdfSeriesBoxHeight) pdfOthersBox.fillColor = .clear pdfOthersBox.contentView = pdfOthersButtonVC.view pdfOthersButtonVC.target = self pdfOthersButtonVC.action = #selector(self.productPromotionFoldOrUnfold(_:)) pdfOthersButtonVC.type = .Right pdfOthersButtonVC.canHover = false pdfOthersButtonVC.imageWidth = 12.0 pdfOthersButtonVC.imageHeight = 12.0 pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")! pdfOthersButtonVC.navMenu(bg: "collapse.ad-list.bg", text: "collapse.ad-list.mac-text.norm", height: othersBoxHeightConstraint) // if self.activitiesBox.isHidden { // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true) // } else { // self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: [], isInitialize: true) // } self.productPromotionShow(self.productPromotionPDFProSeries, withOthers: self.productPromotionOthers, isInitialize: true) self.activitiesBox.contentView = KMProductPromotionViewController.init().view activitiesBox.cornerRadius = 8.0 rightTopBox.fillColor = .white rightBottomBox.fillColor = .white rightFullBox.fillColor = .white productPromotionBox.fillColor = .clear product_active_scrollView.verticalScrollElasticity = .none self.refreshRightBoxUI(.Home) fastToolViewController.delete = self historyFileViewController.delete = self pdfToolsViewController.delete = self dragView.delete = self } func initLocalization() { self.favoriteDocumentsLabel.stringValue = NSLocalizedString("Favorite Documents", comment: "") homeButtonVC.stringValue = NSLocalizedString("Home", comment: "") pdfToolsButtonVC.stringValue = NSLocalizedString("PDF Tools", comment: "") cloudDocumentsButtonVC.stringValue = NSLocalizedString("Cloud Documents", comment: "") openPDFButtonVC.stringValue = NSLocalizedString("Open File", comment: "") openPDFBox.toolTip = NSLocalizedString("Open PDF", comment: "") createPDFButtonVC.stringValue = NSLocalizedString("Create PDF", comment: "") createPDFBox.toolTip = NSLocalizedString("Create PDF", comment: "") pdfSeriesButtonVC.stringValue = NSLocalizedString("PDF Pro Series", comment: "") pdfOthersButtonVC.stringValue = NSLocalizedString("Others", comment: "") } // MARK: Action func initializeUI_DMG() -> Void { rightTopBox.fillColor = .white rightBottomBox.fillColor = .white rightFullBox.fillColor = .white self.aiHomeBox.fillColor = .clear self.aiHomeBox.cornerRadius = 8.0 self.aiHomeImageView.image = NSImage(named: "icon_home") self.aiHomeLabel.stringValue = NSLocalizedString("Home", comment: "") self.aiHomeLabel.textColor = NSColor(hex: "#252629") self.aiHomeLabel.font = NSFont.SFProTextRegular(16.0) self.aiHomeBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if aiHomeState != .Home { if mouseEntered { self.aiHomeBox.fillColor = NSColor(hex: "#EDEEF0") } else { self.aiHomeBox.fillColor = .clear } } } self.aiHomeBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in if aiHomeState != .Home { if downEntered { self.aiHomeState = .Home self.refreshAIUI() } } } self.aiTranslationBox.fillColor = .clear self.aiTranslationBox.cornerRadius = 8.0 self.aiTranslationImageView.image = NSImage(named: "ic_function_other") self.aiTranslationLabel.stringValue = NSLocalizedString("AI Translation", comment: "") self.aiTranslationLabel.textColor = NSColor(hex: "#252629") self.aiTranslationLabel.font = NSFont.SFProTextRegular(16.0) self.aiTranslationBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if aiHomeState != .AITranslation { if mouseEntered { self.aiTranslationBox.fillColor = NSColor(hex: "#EDEEF0") } else { self.aiTranslationBox.fillColor = .clear } } } self.aiTranslationBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in if aiHomeState != .AITranslation { if downEntered { self.aiHomeState = .AITranslation self.refreshAIUI() } } } self.aiRewritingBox.fillColor = .clear self.aiRewritingBox.cornerRadius = 8.0 self.aiRewritingImageView.image = NSImage(named: "ic_ai_Rewriting") self.aiRewritingLabel.stringValue = NSLocalizedString("AI Rewriting", comment: "") self.aiRewritingLabel.textColor = NSColor(hex: "#252629") self.aiRewritingLabel.font = NSFont.SFProTextRegular(16.0) self.aiRewritingBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if aiHomeState != .AIRewriting { if mouseEntered { self.aiRewritingBox.fillColor = NSColor(hex: "#EDEEF0") } else { self.aiRewritingBox.fillColor = .clear } } } self.aiRewritingBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in if aiHomeState != .AIRewriting { if downEntered { self.aiHomeState = .AIRewriting self.refreshAIUI() } } } self.aiErrorCorrectionBox.fillColor = .clear self.aiErrorCorrectionBox.cornerRadius = 8.0 self.aiErrorCorrectionImageView.image = NSImage(named: "ic_ai_ErrorCorrection") self.aiErrorCorrectionLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "") self.aiErrorCorrectionLabel.textColor = NSColor(hex: "#252629") self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegular(16.0) self.aiErrorCorrectionBox.moveCallback = { [unowned self](mouseEntered: Bool, mouseBox: KMBox) -> Void in if aiHomeState != .AIErrorCorrection { if mouseEntered { self.aiErrorCorrectionBox.fillColor = NSColor(hex: "#EDEEF0") } else { self.aiErrorCorrectionBox.fillColor = .clear } } } self.aiErrorCorrectionBox.downCallback = { [unowned self](downEntered: Bool, mouseBox: KMBox, event) -> Void in if aiHomeState != .AIErrorCorrection { if downEntered { self.aiHomeState = .AIErrorCorrection self.refreshAIUI() } } } self.refreshAIUI() historyFileViewController.delete = self dragView.delete = self } // MARK: Action @IBAction func productPromotionButtonAction(_ sender: NSButton) { self.productPromotionFoldOrUnfold(sender) } // MARK: Public Methods func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void { DispatchQueue.global(qos: .`default`).async { var isSuccessfully = false if pdf.isEncrypted { let dic = [ CPDFDocumentWriteOption.userPasswordOption : password, CPDFDocumentWriteOption.ownerPasswordOption : password ] isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic) } else { isSuccessfully = pdf.write(to: pdf.documentURL) } if !isSuccessfully { isSuccessfully = NSData(data: pdf.dataRepresentation()).write(to: pdf.documentURL, atomically: true) } DispatchQueue.main.sync { if isSuccessfully { let workspace = NSWorkspace.shared let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "") workspace.activateFileViewerSelecting([url]) } else { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "") alert.runModal() } } } } // MARK: Private Methods func refreshUI() -> Void { homeButtonVC.updateUI() pdfToolsButtonVC.updateUI() cloudDocumentsButtonVC.updateUI() } func refreshAIUI() -> Void { self.aiHomeBox.fillColor = .clear self.aiHomeLabel.font = NSFont.SFProTextRegular(16.0) self.aiTranslationBox.fillColor = .clear self.aiTranslationLabel.font = NSFont.SFProTextRegular(16.0) self.aiRewritingBox.fillColor = .clear self.aiRewritingLabel.font = NSFont.SFProTextRegular(16.0) self.aiErrorCorrectionBox.fillColor = .clear self.aiErrorCorrectionLabel.font = NSFont.SFProTextRegular(16.0) rightFullBox.isHidden = true rightTopBox.isHidden = true rightBottomBox.isHidden = true switch self.aiHomeState { case .Home: self.aiHomeBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1) self.aiHomeLabel.font = NSFont.SFProTextSemibold(16.0) self.rightTopBox.isHidden = false self.rightBottomBox.isHidden = false self.rightTopBox.contentView = self.aiOpenPDFFilesViewController.view self.rightBottomBox.contentView = self.historyFileViewController.view self.rightFullBox.contentView = nil self.rightTopBoxHeightConstraint.constant = 388.0 self.dragView.isHidden = false break case .AITranslation: self.aiTranslationBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1) self.aiTranslationLabel.textColor = NSColor(hex: "#252629") self.aiTranslationLabel.font = NSFont.SFProTextSemibold(16.0) self.rightFullBox.isHidden = false self.rightTopBox.contentView = nil self.rightBottomBox.contentView = nil self.rightFullBox.contentView = self.aiTranslationViewController.view self.dragView.isHidden = false break case .AIRewriting: self.aiRewritingBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1) self.aiRewritingLabel.textColor = NSColor(hex: "#252629") self.aiRewritingLabel.font = NSFont.SFProTextSemibold(16.0) self.rightFullBox.isHidden = false self.rightTopBox.contentView = nil self.rightBottomBox.contentView = nil self.rightFullBox.contentView = self.aiRewritingViewController.view self.aiRewritingViewController.state = .AIRewriting self.aiRewritingViewController.initLocalization() self.aiRewritingViewController.acquisition_uiState() self.dragView.isHidden = true break case .AIErrorCorrection: self.aiErrorCorrectionBox.fillColor = NSColor(hex: "#1770F4", alpha: 0.1) self.aiErrorCorrectionLabel.textColor = NSColor(hex: "#252629") self.aiErrorCorrectionLabel.font = NSFont.SFProTextSemibold(16.0) self.rightFullBox.isHidden = false self.rightTopBox.contentView = nil self.rightBottomBox.contentView = nil self.rightFullBox.contentView = self.aiRewritingViewController.view self.aiRewritingViewController.state = .AIErrorCorrection self.aiRewritingViewController.initLocalization() self.aiRewritingViewController.acquisition_uiState() self.dragView.isHidden = true break default: break } } func refreshProductActiveSpacing() -> Void { let height = self.view.frame.height - 32.0 - openPDFBoxHeight.constant - 16.0 - createPDFBoxHeight.constant - 24.0 - pdfSeriesBoxHeight.constant - pdfProSeriesBoxHeightConstraint.constant - 8 - othersBoxHeightConstraint.constant - pdfOthersBoxHeightConstraint.constant - 242 - 30 // print("height == %f", height) if height > 24.0 { self.product_active_spacing.constant = height } else { self.product_active_spacing.constant = 24.0 } } func fetchDifferentFilePath(filePath: String) -> String { var resultFilePath = filePath var index: Int = 0 while (FileManager.default.fileExists(atPath: resultFilePath)) { index += 1 let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")" resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)! } return resultFilePath; } func showProgressWindow() { let progress = SKProgressController() progress.message = NSLocalizedString("Translating...", comment: "") progress.window?.backgroundColor = NSColor(hex: "#36383B") progress.window?.contentView?.wantsLayer = true progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor progress.progressField.textColor = NSColor.white progress.closeBlock = { [unowned self] in } self.progressController = progress self.view.window?.beginSheet(progress.window!) } func hiddenProgressWindow() { if (self.progressController != nil) { self.view.window?.endSheet((self.progressController?.window)!) self.progressController = nil } } func isFileGreaterThan10MB(atPath filePath: String) -> Bool { let fileManager = FileManager.default do { let fileAttributes = try fileManager.attributesOfItem(atPath: filePath) if let fileSize = fileAttributes[.size] as? UInt64 { let megabyteSize = fileSize / (1024 * 1024) return megabyteSize > 10 } } catch { print("Error: \(error)") } return false } func isPDFPageCountExceedsLimit(filePath: String) -> Bool { let url = URL(fileURLWithPath: filePath) guard let document = PDFDocument(url: url) else { return false } let pageCount = document.pageCount return pageCount > 30 } // MARK: Common methods func workSpaceOpenUrl(_ url: NSString) { let httpUrl: NSURL = NSURL.init(string: url as String)! let tWorkSpace: NSWorkspace = NSWorkspace.shared if !tWorkSpace.open(httpUrl as URL) { tWorkSpace.open(httpUrl as URL) } } func createPopoverAction(_ data: [String]) { if (self.popover != nil && self.popover!.isShown) { self.popover?.close() self.popover = nil return } let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data) let createFilePopover: NSPopover = NSPopover.init() self.popover = createFilePopover createFilePopover.contentViewController = vc createFilePopover.animates = true createFilePopover.behavior = .semitransient createFilePopover.setValue(true, forKey: "shouldHideAnchor") createFilePopover.show(relativeTo: CGRect(x: createPDFBox.bounds.origin.x, y: 10, width: createPDFBox.bounds.size.width, height: createPDFBox.bounds.size.height), of: createPDFBox, preferredEdge: .minY) vc.customBoxWidthLayoutConstraint.constant = createPDFBox.frame.width vc.downCallback = { [weak self] (downEntered: Bool, count: String) -> Void in if downEntered { self?.popover?.close() self?.popover = nil self?.popoverItemAction(count) } } } }