KMToolbarRightView.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.SFProTextRegularFont(12.0)
  52. self.advancedLabel.textColor = NSColor.km_init(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.km_init(hex: "#1770F4"))
  68. self.userInfoButton.font = NSFont.SFProTextSemiboldFont(14.0)
  69. self.userInfoButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  70. self.userInfoButton.border(NSColor.km_init(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.km_init(hex: "#1770F4"), 0, 12)
  77. self.userInfoButton.backgroundColor(NSColor.clear)
  78. }
  79. }
  80. }
  81. func canShowAdvanced() -> Bool {
  82. #if VERSION_FREE
  83. return (
  84. //KMLightMemberManager.manager.isLogin() &&
  85. (KMLightMemberManager.manager.purchaseState != .subscription &&
  86. KMLightMemberManager.manager.purchaseState != .trial))
  87. #else
  88. return (!KMLightMemberManager.manager.isLogin() ||
  89. (KMLightMemberManager.manager.purchaseState != .subscription &&
  90. KMLightMemberManager.manager.purchaseState != .trial))
  91. #endif
  92. }
  93. func fetchAdvancedViewSize() -> CGSize {
  94. var advancedString = NSLocalizedString("Upgrade", comment: "")
  95. // if KMLightMemberManager.manager.purchaseState == .trial {
  96. // advancedString = NSLocalizedString("Trialling", comment: "")
  97. // }
  98. let paragraphStyle = NSMutableParagraphStyle()
  99. paragraphStyle.lineHeightMultiple = 1.32
  100. paragraphStyle.alignment = .right
  101. let attributes = [NSAttributedString.Key.paragraphStyle: paragraphStyle,
  102. NSAttributedString.Key.font : NSFont.SFProTextRegularFont(12.0)]
  103. let title: NSString = advancedString as NSString
  104. let size = title.boundingRect(with: CGSize(width: 300, height: 24) ,
  105. attributes: attributes).size
  106. let resultSize = CGSize(width: size.width + 8 + 16 + 8 + 8, height: size.height)
  107. if self.rightSegmentControlConstraint != nil {
  108. self.rightSegmentControlConstraint.constant = resultSize.width
  109. }
  110. return resultSize
  111. }
  112. func fetchAdvancedViewSize_update_rightSegmentControlConstraint() {
  113. if self.rightSegmentControlConstraint != nil {
  114. self.rightSegmentControlConstraint.constant = 89.0
  115. }
  116. }
  117. func addNotification() {
  118. self.removeNotification()
  119. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: ADVANCED_NOTIFICATION), object: nil)
  120. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_INFO_CHANGE_NOTIFICATION), object: nil)
  121. NotificationCenter.default.addObserver(self, selector: #selector(updateAdvancedContentView), name: NSNotification.Name(rawValue: USER_LOGINSTATE_NOTIFICATION), object: nil)
  122. }
  123. func updateAdvancedContentView() {
  124. self.updateView()
  125. self.delete?.homeRefreshButtonAction(nil)
  126. }
  127. func removeNotification() {
  128. NotificationCenter.default.removeObserver(self)
  129. }
  130. // MARK: Dark&Light
  131. // MARK: Action
  132. @IBAction func advancedButtonAction(_ sender: Any) {
  133. self.delete?.pdfRightSegmentedControlAction(nil)
  134. }
  135. @IBAction func homeRefreshButtonAction(_ sender: Any) {
  136. }
  137. @IBAction func homeUploadButtonAction(_ sender: Any) {
  138. }
  139. @IBAction func homeMenuSortAction(_ sender: Any) {
  140. }
  141. @IBAction func homeMenuFilterAction(_ sender: Any) {
  142. }
  143. @IBAction func userInfoButtonAction(_ sender: Any) {
  144. self.delete?.userInfoButtonAction(sender as! NSButton)
  145. }
  146. }