KMToolbarRightView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // KMToolbarRightView.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/12/9.
  6. //
  7. import Cocoa
  8. @objc protocol KMToolbarRightViewDelegate {
  9. @objc func pdfRightSegmentedControlAction(_ sender: KMSegmentedBox?)
  10. @objc func userInfoButtonAction(_ sender: NSButton)
  11. @objc func homeRefreshButtonAction(_ sender: NSButton?)
  12. @objc func homeUploadButtonAction(_ sender: NSButton)
  13. @objc func homeMenuSortAction(_ sender: NSPopUpButton)
  14. @objc func homeMenuFilterAction(_ sender: NSPopUpButton)
  15. }
  16. @objcMembers class KMToolbarRightView: NSView {
  17. open var delete: KMToolbarRightViewDelegate?
  18. @IBOutlet weak var homeRefreshButton: NSButton!
  19. @IBOutlet weak var homeUploadButton: NSButton!
  20. @IBOutlet weak var userInfoButton: NSButton!
  21. @IBOutlet weak var sortPopUpButton: NSPopUpButton!
  22. @IBOutlet weak var filterPopUpButton: NSPopUpButton!
  23. @IBOutlet weak var homeRightSearchField: NSSearchField!
  24. @IBOutlet weak var pdfRightSegmentedBox: KMSegmentedBox!
  25. @IBOutlet weak var rightSegmentControlConstraint: NSLayoutConstraint!
  26. @IBOutlet weak var advancedContentView: NSView!
  27. @IBOutlet weak var advancedLabel: NSTextField!
  28. @IBOutlet weak var trailPositionView: NSView!
  29. @IBOutlet weak var trailPositionWidthConst: NSLayoutConstraint!
  30. @IBOutlet weak var filterPopUpButtonWidth: NSLayoutConstraint!
  31. @IBOutlet weak var filterPopUpButtonLeft: NSLayoutConstraint!
  32. @IBOutlet weak var advancedViewRightConstraint: NSLayoutConstraint!
  33. deinit {
  34. self.removeNotification()
  35. }
  36. override func draw(_ dirtyRect: NSRect) {
  37. super.draw(dirtyRect)
  38. // Drawing code here.
  39. }
  40. override func awakeFromNib() {
  41. super.awakeFromNib()
  42. self.updateView()
  43. self.addNotification()
  44. }
  45. @objc func updateView() -> Void {
  46. if KMLightMemberManager.manager.canShowAdvancedView && !KMLightMemberManager.manager.isLogin() {
  47. if self.advancedLabel != nil {
  48. self.advancedContentView.isHidden = false
  49. self.advancedViewRightConstraint.constant = 4.0
  50. self.advancedLabel.font = NSFont.SFProTextRegular(12.0)
  51. self.advancedLabel.textColor = NSColor(hex: "#FFFFFF")
  52. self.advancedLabel.stringValue = NSLocalizedString("Enjoy Advanced Features", comment: "")
  53. }
  54. if self.rightSegmentControlConstraint != nil {
  55. let size = self.fetchAdvancedViewSize()
  56. self.rightSegmentControlConstraint.constant = size.width + 20
  57. }
  58. } else {
  59. if self.advancedContentView != nil {
  60. self.advancedContentView.isHidden = true
  61. self.advancedViewRightConstraint.constant = -163.0
  62. }
  63. }
  64. if self.userInfoButton != nil {
  65. if KMLightMemberManager.manager.info.email.count != 0 {
  66. self.userInfoButton.backgroundColor(NSColor(hex: "#1770F4"))
  67. self.userInfoButton.font = NSFont.SFProTextSemibold(14.0)
  68. self.userInfoButton.contentTintColor = NSColor(hex: "#FFFFFF")
  69. self.userInfoButton.border(NSColor(hex: "#1770F4"), 1, 12)
  70. self.userInfoButton.title = KMLightMemberManager.manager.info.email.substring(to: 1)
  71. self.userInfoButton.image = nil
  72. } else {
  73. self.userInfoButton.image = NSImage(named: "Icon_other_avatar_false")
  74. self.userInfoButton.title = ""
  75. self.userInfoButton.border(NSColor(hex: "#1770F4"), 0, 12)
  76. self.userInfoButton.backgroundColor(NSColor.clear)
  77. }
  78. }
  79. }
  80. func fetchAdvancedViewSize() -> CGSize {
  81. let paragraphStyle = NSMutableParagraphStyle()
  82. paragraphStyle.lineHeightMultiple = 1.32
  83. paragraphStyle.alignment = .right
  84. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  85. NSAttributedString.Key.font : NSFont.SFProTextRegular(12.0)]
  86. let title: NSString = NSLocalizedString("Enjoy Advanced Features", comment: "") as NSString
  87. let size = title.boundingRect(with: CGSize(width: 300, height: 24) ,
  88. attributes: attributes).size
  89. return size
  90. }
  91. func addNotification() {
  92. self.removeNotification()
  93. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  94. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil)
  95. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil)
  96. }
  97. func updateAdvancedContentView() {
  98. self.updateView()
  99. self.delete?.homeRefreshButtonAction(nil)
  100. }
  101. func removeNotification() {
  102. NotificationCenter.default.removeObserver(self)
  103. }
  104. // MARK: Dark&Light
  105. // MARK: Action
  106. @IBAction func advancedButtonAction(_ sender: Any) {
  107. self.delete?.pdfRightSegmentedControlAction(nil)
  108. }
  109. @IBAction func homeRefreshButtonAction(_ sender: Any) {
  110. }
  111. @IBAction func homeUploadButtonAction(_ sender: Any) {
  112. }
  113. @IBAction func homeMenuSortAction(_ sender: Any) {
  114. }
  115. @IBAction func homeMenuFilterAction(_ sender: Any) {
  116. }
  117. @IBAction func userInfoButtonAction(_ sender: Any) {
  118. self.delete?.userInfoButtonAction(sender as! NSButton)
  119. }
  120. }