// // KMToolbarRightView.swift // PDF Reader Pro // // Created by wanjun on 2022/12/9. // import Cocoa @objc protocol KMToolbarRightViewDelegate { @objc func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?) @objc func userInfoButtonAction(_ sender: NSButton) @objc func homeRefreshButtonAction(_ sender: NSButton?) @objc func homeUploadButtonAction(_ sender: NSButton) @objc func homeMenuSortAction(_ sender: NSPopUpButton) @objc func homeMenuFilterAction(_ sender: NSPopUpButton) } @objcMembers class KMToolbarRightView: NSView { open var delete: KMToolbarRightViewDelegate? @IBOutlet weak var homeRefreshButton: NSButton! @IBOutlet weak var homeUploadButton: NSButton! @IBOutlet weak var userInfoButton: NSButton! @IBOutlet weak var sortPopUpButton: NSPopUpButton! @IBOutlet weak var filterPopUpButton: NSPopUpButton! @IBOutlet weak var homeRightSearchField: NSSearchField! @IBOutlet weak var pdfRightSegmentedBox: KMSegmentedBox! @IBOutlet weak var rightSegmentControlConstraint: NSLayoutConstraint! @IBOutlet weak var advancedContentView: NSView! @IBOutlet weak var advancedLabel: NSTextField! @IBOutlet weak var trailPositionView: NSView! @IBOutlet weak var trailPositionWidthConst: NSLayoutConstraint! @IBOutlet weak var filterPopUpButtonWidth: NSLayoutConstraint! @IBOutlet weak var filterPopUpButtonLeft: NSLayoutConstraint! @IBOutlet weak var advancedViewRightConstraint: NSLayoutConstraint! deinit { self.removeNotification() } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func awakeFromNib() { super.awakeFromNib() self.updateView() self.addNotification() } @objc func updateView() -> Void { // KMLightMemberManager.manager.canShowAdvancedView && if canShowAdvanced() { if self.advancedLabel != nil { self.advancedContentView.isHidden = false self.advancedViewRightConstraint.constant = 8.0 self.advancedLabel.font = NSFont.SFProTextRegularFont(12.0) self.advancedLabel.textColor = NSColor.km_init(hex: "#FFFFFF") var advancedString = NSLocalizedString("Upgrade", comment: "") // if KMLightMemberManager.manager.purchaseState == .trial { // advancedString = NSLocalizedString("Trialling", comment: "") // } self.advancedLabel.stringValue = advancedString } } else { if self.advancedContentView != nil { self.advancedContentView.isHidden = true self.advancedViewRightConstraint.constant = -163.0 } } if self.userInfoButton != nil { if KMLightMemberManager.manager.info.email.count != 0 { self.userInfoButton.backgroundColor(NSColor.km_init(hex: "#1770F4")) self.userInfoButton.font = NSFont.SFProTextSemiboldFont(14.0) self.userInfoButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF") self.userInfoButton.border(NSColor.km_init(hex: "#1770F4"), 1, 12) self.userInfoButton.title = KMLightMemberManager.manager.info.email.substring(to: 1) self.userInfoButton.image = nil } else { self.userInfoButton.image = NSImage(named: "Icon_other_avatar_false") self.userInfoButton.title = "" self.userInfoButton.border(NSColor.km_init(hex: "#1770F4"), 0, 12) self.userInfoButton.backgroundColor(NSColor.clear) } } } func canShowAdvanced() -> Bool { #if VERSION_FREE return ( //KMLightMemberManager.manager.isLogin() && (KMLightMemberManager.manager.purchaseState != .subscription && KMLightMemberManager.manager.purchaseState != .trial)) #else return (!KMLightMemberManager.manager.isLogin() || (KMLightMemberManager.manager.purchaseState != .subscription && KMLightMemberManager.manager.purchaseState != .trial)) #endif } func fetchAdvancedViewSize() -> CGSize { var advancedString = NSLocalizedString("Upgrade", comment: "") // if KMLightMemberManager.manager.purchaseState == .trial { // advancedString = NSLocalizedString("Trialling", comment: "") // } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.32 paragraphStyle.alignment = .right let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.font : NSFont.SFProTextRegularFont(12.0)] let title: NSString = advancedString as NSString let size = title.boundingRect(with: CGSize(width: 300, height: 24) , attributes: attributes).size let resultSize = CGSize(width: size.width + 8 + 16 + 8 + 8, height: size.height) if self.rightSegmentControlConstraint != nil { self.rightSegmentControlConstraint.constant = resultSize.width } return resultSize } func fetchAdvancedViewSize_update_rightSegmentControlConstraint() { if self.rightSegmentControlConstraint != nil { self.rightSegmentControlConstraint.constant = 89.0 } } func addNotification() { self.removeNotification() NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil) } func updateAdvancedContentView() { self.updateView() self.delete?.homeRefreshButtonAction(nil) } func removeNotification() { NotificationCenter.default.removeObserver(self) } // MARK: Dark&Light // MARK: Action @IBAction func advancedButtonAction(_ sender: Any) { self.delete?.pdfRightSegmentedControlAction(nil) } @IBAction func homeRefreshButtonAction(_ sender: Any) { } @IBAction func homeUploadButtonAction(_ sender: Any) { } @IBAction func homeMenuSortAction(_ sender: Any) { } @IBAction func homeMenuFilterAction(_ sender: Any) { } @IBAction func userInfoButtonAction(_ sender: Any) { self.delete?.userInfoButtonAction(sender as! NSButton) } }