// // KMHomeContentView.swift // PDF Reader Pro // // Created by lizhe on 2023/10/27. // import Cocoa typealias KMHomeContentViewHistoryDidSelect = (_ view: KMHomeContentView, _ url: KMFile) -> Void typealias KMHomeContentViewHistoryShowPathAction = (_ view: KMHomeContentView, _ url: KMFile) -> Void typealias KMHomeContentViewHistoryrRemoveFileAction = (_ view: KMHomeContentView, _ url: KMFile) -> Void typealias KMHomeContentViewHistoryrRemoveAllFileAction = (_ view: KMHomeContentView) -> Void typealias KMHomeContentViewQucikToolsDidSelect = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void typealias KMHomeContentViewQucikToolsAddAction = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void typealias KMHomeContentViewQucikToolsRemoveAction = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void let kRecentlyListModel = "kRecentlyListModel" let kQucikToolsViewShow = "kQucikToolsViewShow" enum KMHomeQucikToolsShowType: Int { case expand = 0 case collapse } class KMHomeContentView: KMBaseXibView { @IBOutlet var lynxGuideContendView: NSView! @IBOutlet weak var qucikToolsLabel: NSTextField! @IBOutlet weak var qucikToolsView: KMQucikToolsView! @IBOutlet weak var leftButton: NSButton! @IBOutlet weak var pageLabel: NSTextField! @IBOutlet weak var rightButton: NSButton! @IBOutlet weak var moreButton: KMButton! @IBOutlet weak var qucikToolsConstraintHeight: NSLayoutConstraint! @IBOutlet var quickToolsTopConstraint: NSLayoutConstraint! @IBOutlet weak var recentlyLabel: NSTextField! @IBOutlet weak var historyListView: KMHomeHistoryListView! @IBOutlet weak var horizontalButton: KMButton! @IBOutlet weak var verticalButton: KMButton! @IBOutlet weak var cleanButton: KMButton! var lynxGuideView: HomeLynxGuide! var historyDidSelect: KMHomeContentViewHistoryDidSelect? var historyShowPathAction: KMHomeContentViewHistoryShowPathAction? var historyRemoveFileAction: KMHomeContentViewHistoryrRemoveFileAction? var historyRemoveAllFileAction: KMHomeContentViewHistoryrRemoveAllFileAction? var qucikToolsDidSelect: KMHomeContentViewQucikToolsDidSelect? var qucikToolsAddAction: KMHomeContentViewQucikToolsAddAction? var qucikToolsRemoveAction: KMHomeContentViewQucikToolsRemoveAction? var qucikToolsType: KMHomeQucikToolsShowType = .expand { didSet { self.qucikToolsView.type = qucikToolsType if qucikToolsType == .collapse { self.qucikToolsConstraintHeight.constant = 166 } else { self.qucikToolsConstraintHeight.constant = 262 } } } deinit { print("KMHomeContentView deinit") } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. self.setup() } override func setup() { self.qucikToolsView.didSelect = { [unowned self] view, item in guard let callBack = qucikToolsDidSelect else { return } callBack(self, item) } self.qucikToolsView.pageChange = { [unowned self] view in self.updatePageLabel() } self.qucikToolsView.addAction = { [unowned self] view, item in self.qucikToolsAddAction?(self, item) } self.qucikToolsView.removeAction = { [unowned self] view, item in self.qucikToolsRemoveAction?(self, item) } self.historyListView.didSelect = { [unowned self] view, item in guard let callBack = historyDidSelect else { return } callBack(self, item) } self.historyListView.removeFileAction = { [unowned self] view, item in guard let callBack = historyRemoveFileAction else { return } callBack(self, item) } self.historyListView.showPathAction = { [unowned self] view, item in guard let callBack = historyShowPathAction else { return } callBack(self, item) } cleanButton.mouseMoveCallback = { [unowned self] enter in if self.historyListView.data.count == 0 { self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletNor") } else { if enter { self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletHor") } else { self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletNor") } } } moreButton.mouseMoveCallback = { [unowned self] enter in if enter { self.moreButton.image = NSImage(named: "KMImageNameUXIconHomeMorePre") } else { self.moreButton.image = NSImage(named: "KMImageNameUXIconHomeMoreNor") } } let type = UserDefaults.standard.object(forKey: kRecentlyListModel) ?? 0 historyListView.showType = KMHomeHistoryListViewShowType.init(rawValue: type as! Int) ?? .horizontal let type2 = UserDefaults.standard.object(forKey: kQucikToolsViewShow) ?? 0 self.qucikToolsType = KMHomeQucikToolsShowType.init(rawValue: type2 as! Int) ?? .expand self.updatePageLabel() self.updateRecentlyListState() self.configLynxGuideView() } func updatePageLabel() { self.pageLabel.stringValue = "\(qucikToolsView.currentPage()) / \(max(qucikToolsView.pageCount(), 1))" } func updateRecentlyListState() { let showType = self.historyListView.showType if showType == .horizontal { verticalButton.image = NSImage(named: "KMImageNameUXIconHomeViewthumbnailNor") horizontalButton.image = NSImage(named: "KMImageNameUXIconHomeViewlistPre") } else if showType == .vertical { verticalButton.image = NSImage(named: "KMImageNameUXIconHomeViewthumbnailPre") horizontalButton.image = NSImage(named: "KMImageNameUXIconHomeViewlistNor") } UserDefaults.standard.setValue(showType.rawValue, forKey: kRecentlyListModel) } override func updateUI() { self.contentView.wantsLayer = true if(KMAppearance.isDarkMode()){ self.contentView.layer?.backgroundColor = NSColor.init(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor } else { self.contentView.layer?.backgroundColor = NSColor.init(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor } self.pageLabel.textColor = KMAppearance.Layout.h0Color() self.qucikToolsLabel.textColor = KMAppearance.Layout.h0Color() self.recentlyLabel.textColor = KMAppearance.Layout.h0Color() self.lynxGuideView.reloadData() } override func updateLanguage() { self.qucikToolsLabel.stringValue = NSLocalizedString("Advanced Editing Tools", comment: "") self.recentlyLabel.stringValue = NSLocalizedString("Recent", comment: ""); self.horizontalButton.toolTip = NSLocalizedString("Thumbnails", comment: "") self.verticalButton.toolTip = NSLocalizedString("List", comment: "") self.cleanButton.toolTip = NSLocalizedString("Delete",comment: "") } override func reloadData() { self.historyListView.reloadData() if self.historyListView.data.count == 0 { self.cleanButton.isEnabled = false } else { self.cleanButton.isEnabled = true } var showLynxGuide = true if VerificationManager.default().isVerificationCode { showLynxGuide = false } if showLynxGuide { self.quickToolsTopConstraint.constant = 250 self.lynxGuideContendView.isHidden = false } else { self.quickToolsTopConstraint.constant = 14 self.lynxGuideContendView.isHidden = true } } func configLynxGuideView() { if self.lynxGuideView == nil { self.lynxGuideView = HomeLynxGuide.createFromNib() } self.lynxGuideView?.frame = self.lynxGuideContendView.bounds self.lynxGuideView?.autoresizingMask = [.width, .height] self.lynxGuideContendView.addSubview(self.lynxGuideView) } } //MARK: Action extension KMHomeContentView { @IBAction func leftButtonAction(_ sender: Any) { self.qucikToolsView.previousPage() } @IBAction func rightButtonAction(_ sender: Any) { self.qucikToolsView.nextPage() } @IBAction func moreButtonAction(_ sender: NSButton) { let collapse = NSLocalizedString("Collapse All", comment: "") let quickTools = NSLocalizedString("Manage Quick Tools", comment: "") let expand = NSLocalizedString("Expand All", comment: "") var array: [String] = [] if qucikToolsType == .expand { array = [collapse, quickTools] } else { array = [expand, quickTools] } let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(array) vc.background = KMAppearance.Layout.bgColor() vc.textColor = KMAppearance.Layout.h0Color() vc.enterFillColor = KMAppearance.Interactive.s0Color() let createFilePopover: NSPopover = NSPopover.init() createFilePopover.contentViewController = vc createFilePopover.delegate = self createFilePopover.animates = true createFilePopover.behavior = .semitransient createFilePopover.setValue(true, forKey: "shouldHideAnchor") createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY) // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in if downEntered { createFilePopover.close() if count == collapse { self.qucikToolsType = .collapse } else if count == expand { self.qucikToolsType = .expand } else if count == quickTools { let homeQuickToolsWindowController: KMHomeQuickToolsWindowController = KMHomeQuickToolsWindowController.sharedHomeQuickTools homeQuickToolsWindowController.window?.center() NSWindow.currentWindow().addChildWindow(homeQuickToolsWindowController.window!, ordered: NSWindow.OrderingMode.above) homeQuickToolsWindowController.dataChange = { [unowned self] controller in self.qucikToolsView.reloadData() } } self.updatePageLabel() UserDefaults.standard.setValue(qucikToolsType.rawValue, forKey: kQucikToolsViewShow) } } } @IBAction func horizontalButtonAction(_ sender: Any) { self.historyListView.showType = .horizontal self.updateRecentlyListState() } @IBAction func verticalButtonAction(_ sender: Any) { self.historyListView.showType = .vertical self.updateRecentlyListState() } @IBAction func cleanButtonAction(_ sender: Any) { guard let callBack = historyRemoveAllFileAction else { return } callBack(self) } } extension KMHomeContentView: NSPopoverDelegate { }