KMToolbarRightView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // KMLightMemberManager.manager.canShowAdvancedView &&
  47. if canShowAdvanced() {
  48. if self.advancedLabel != nil {
  49. self.advancedContentView.isHidden = false
  50. self.advancedViewRightConstraint.constant = 8.0
  51. self.advancedLabel.font = NSFont.SFProTextRegular(12.0)
  52. self.advancedLabel.textColor = NSColor(hex: "#FFFFFF")
  53. var advancedString = NSLocalizedString("Upgrade", comment: "")
  54. // if KMLightMemberManager.manager.purchaseState == .trial {
  55. // advancedString = NSLocalizedString("Trialling", comment: "")
  56. // }
  57. self.advancedLabel.stringValue = advancedString
  58. }
  59. } else {
  60. if self.advancedContentView != nil {
  61. self.advancedContentView.isHidden = true
  62. self.advancedViewRightConstraint.constant = -163.0
  63. }
  64. }
  65. if self.userInfoButton != nil {
  66. if KMLightMemberManager.manager.info.email.count != 0 {
  67. self.userInfoButton.backgroundColor(NSColor(hex: "#1770F4"))
  68. self.userInfoButton.font = NSFont.SFProTextSemibold(14.0)
  69. self.userInfoButton.contentTintColor = NSColor(hex: "#FFFFFF")
  70. self.userInfoButton.border(NSColor(hex: "#1770F4"), 1, 12)
  71. self.userInfoButton.title = KMLightMemberManager.manager.info.email.substring(to: 1)
  72. self.userInfoButton.image = nil
  73. } else {
  74. self.userInfoButton.image = NSImage(named: "Icon_other_avatar_false")
  75. self.userInfoButton.title = ""
  76. self.userInfoButton.border(NSColor(hex: "#1770F4"), 0, 12)
  77. self.userInfoButton.backgroundColor(NSColor.clear)
  78. }
  79. }
  80. }
  81. func canShowAdvanced() -> Bool {
  82. #if VERSION_FREE
  83. return (KMLightMemberManager.manager.isLogin() &&
  84. (KMLightMemberManager.manager.purchaseState != .subscription &&
  85. KMLightMemberManager.manager.purchaseState != .trial))
  86. #else
  87. return (!KMLightMemberManager.manager.isLogin() ||
  88. (KMLightMemberManager.manager.purchaseState != .subscription &&
  89. KMLightMemberManager.manager.purchaseState != .trial))
  90. #endif
  91. }
  92. func fetchAdvancedViewSize() -> CGSize {
  93. var advancedString = NSLocalizedString("Upgrade", comment: "")
  94. // if KMLightMemberManager.manager.purchaseState == .trial {
  95. // advancedString = NSLocalizedString("Trialling", comment: "")
  96. // }
  97. let paragraphStyle = NSMutableParagraphStyle()
  98. paragraphStyle.lineHeightMultiple = 1.32
  99. paragraphStyle.alignment = .right
  100. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  101. NSAttributedString.Key.font : NSFont.SFProTextRegular(12.0)]
  102. let title: NSString = advancedString as NSString
  103. let size = title.boundingRect(with: CGSize(width: 300, height: 24) ,
  104. attributes: attributes).size
  105. let resultSize = CGSize(width: size.width + 8 + 16 + 8 + 8, height: size.height)
  106. if self.rightSegmentControlConstraint != nil {
  107. self.rightSegmentControlConstraint.constant = resultSize.width
  108. }
  109. return resultSize
  110. }
  111. func fetchAdvancedViewSize_update_rightSegmentControlConstraint() {
  112. if self.rightSegmentControlConstraint != nil {
  113. self.rightSegmentControlConstraint.constant = 89.0
  114. }
  115. }
  116. func addNotification() {
  117. self.removeNotification()
  118. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  119. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil)
  120. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil)
  121. }
  122. func updateAdvancedContentView() {
  123. self.updateView()
  124. self.delete?.homeRefreshButtonAction(nil)
  125. }
  126. func removeNotification() {
  127. NotificationCenter.default.removeObserver(self)
  128. }
  129. // MARK: Dark&Light
  130. // MARK: Action
  131. @IBAction func advancedButtonAction(_ sender: Any) {
  132. self.delete?.pdfRightSegmentedControlAction(nil)
  133. }
  134. @IBAction func homeRefreshButtonAction(_ sender: Any) {
  135. }
  136. @IBAction func homeUploadButtonAction(_ sender: Any) {
  137. }
  138. @IBAction func homeMenuSortAction(_ sender: Any) {
  139. }
  140. @IBAction func homeMenuFilterAction(_ sender: Any) {
  141. }
  142. @IBAction func userInfoButtonAction(_ sender: Any) {
  143. self.delete?.userInfoButtonAction(sender as! NSButton)
  144. }
  145. }