KMPageNumberDisplayView.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // KMPageNumberDisplayView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/4/25.
  6. //
  7. import Cocoa
  8. protocol KMPageNumberDisplayViewDelegate: NSObject {
  9. func gotoPageIndex(view: KMPageNumberDisplayView, pageIndex: Int)
  10. func updateWidth(view: KMPageNumberDisplayView, width: CGFloat)
  11. }
  12. class KMPageNumberDisplayView: BaseXibView {
  13. @IBOutlet weak var totalPageCountLabel: NSTextField!
  14. @IBOutlet weak var pageWidthConstraint: NSLayoutConstraint!
  15. @IBOutlet weak var lineLabel: NSTextFieldCell!
  16. @IBOutlet weak var currentPageIndexLabel: FocusAwareTextField!
  17. @IBOutlet weak var currentPageContentView: NSView!
  18. @IBOutlet weak var pageLabelConstraint: NSLayoutConstraint!
  19. var displayType: KMPreferencePageIndicatorDisplayType? {
  20. didSet {
  21. self.updateViewDispaly()
  22. }
  23. }
  24. var timer: Timer?
  25. weak var delegate: KMPageNumberDisplayViewDelegate?
  26. var currentPageIndex: Int? {
  27. didSet {
  28. let string = (currentPageIndex! + 1).description
  29. self.currentPageIndexLabel.stringValue = string
  30. self.updateUI()
  31. }
  32. }
  33. var totalPagesCount: Int? {
  34. didSet {
  35. self.totalPageCountLabel.stringValue = totalPagesCount?.description ?? "1"
  36. self.updateUI()
  37. }
  38. }
  39. var hover: Bool = false {
  40. didSet{
  41. self.updateViewDispaly()
  42. }
  43. }
  44. var isEdit: Bool = false {
  45. didSet {
  46. self.currentPageIndexLabel.isEditable = isEdit
  47. if isEdit {
  48. _ = self.currentPageIndexLabel.becomeFirstResponder()
  49. self.currentPageIndexLabel.currentEditor()?.selectAll(nil)
  50. }
  51. self.updateUI()
  52. }
  53. }
  54. deinit {
  55. self.dismiss()
  56. }
  57. override func draw(_ dirtyRect: NSRect) {
  58. super.draw(dirtyRect)
  59. // Drawing code here.
  60. }
  61. func setup() {
  62. self.backgroundColor(NSColor.km_init(hex: "#36383B"))
  63. self.shadow = NSShadow()
  64. self.wantsLayer = true
  65. self.layer?.shadowColor = NSColor.km_init(hex: "#000000", alpha: 0.16).cgColor
  66. self.layer?.shadowOffset = CGSize(width: 0, height: -2)
  67. self.layer?.shadowRadius = 8
  68. self.layer?.shadowOpacity = 1
  69. self.layer?.cornerRadius = 4
  70. self.currentPageIndexLabel.delegate = self
  71. }
  72. //刷新界面UI 和 数据
  73. func reloadData() {
  74. }
  75. func updateLanguage() {
  76. }
  77. func updateUI() {
  78. // NSString(string: self.currentPageLabel.stringValue).boundingRect(with: NSSize(width: 1000, height: 1000)).size.width + 8
  79. self.totalPageCountLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  80. self.totalPageCountLabel.font = NSFont.SFProTextRegularFont(12.0)
  81. self.lineLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  82. self.lineLabel.font = NSFont.SFProTextRegularFont(12.0)
  83. if isEdit {
  84. currentPageIndexLabel.textColor = NSColor.km_init(hex: "#252629")
  85. currentPageIndexLabel.font = NSFont.SFProTextRegularFont(12.0)
  86. currentPageContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  87. currentPageContentView.border(NSColor.km_init(hex: "#DFE1E5"), 1, 4)
  88. } else {
  89. currentPageIndexLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  90. currentPageIndexLabel.font = NSFont.SFProTextRegularFont(12.0)
  91. currentPageContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF", alpha: 0))
  92. currentPageContentView.border(NSColor.km_init(hex: "#DFE1E5"), 0, 4)
  93. }
  94. self.delegate?.updateWidth(view: self, width: self.fetchWith(edit: self.isEdit))
  95. }
  96. func fetchWith(edit: Bool = false) -> CGFloat {
  97. let currentIndexString: NSString = self.currentPageIndexLabel.stringValue as NSString
  98. let totalPageCountString: NSString = self.totalPageCountLabel.stringValue as NSString
  99. let attributes = [NSAttributedString.Key.font : NSFont.SFProTextRegularFont(12.0), NSAttributedString.Key.foregroundColor: NSColor.km_init(hex: "#FFFFFF")]
  100. let currentIndexStringWidth = currentIndexString.boundingRect(with: CGSize(width: 100, height: 36), attributes: attributes).width
  101. let totalPageCountStringWidth = totalPageCountString.boundingRect(with: CGSize(width: 100, height: 36), attributes: attributes).width
  102. var width = 16 + currentIndexStringWidth + 8 + 4 + 8 + totalPageCountStringWidth + 16
  103. if edit {
  104. self.pageWidthConstraint.constant = currentIndexStringWidth + 40
  105. self.pageLabelConstraint.constant = currentIndexStringWidth + 24
  106. width += 40
  107. } else {
  108. self.pageWidthConstraint.constant = currentIndexStringWidth
  109. self.pageLabelConstraint.constant = currentIndexStringWidth
  110. }
  111. return width
  112. }
  113. }
  114. //MARK: mouse
  115. extension KMPageNumberDisplayView {
  116. override func mouseDown(with event: NSEvent) {
  117. KMPrint("mouseDown")
  118. if !self.isEdit {
  119. self.isEdit = true
  120. self.delegate?.updateWidth(view: self, width: self.fetchWith(edit: self.isEdit))
  121. }
  122. }
  123. }
  124. //MARK:
  125. extension KMPageNumberDisplayView: NSTextFieldDelegate {
  126. func controlTextDidEndEditing(_ obj: Notification) {
  127. if obj.object is FocusAwareTextField {
  128. self.toPageIndex()
  129. }
  130. }
  131. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  132. switch commandSelector {
  133. case #selector(NSResponder.insertNewline(_:)):
  134. if let _ = control as? NSTextField {
  135. //当当前TextField按下enter
  136. if (textView.string == self.currentPageIndexLabel.stringValue) {
  137. KMPrint("按下 enter")
  138. self.isEdit = false
  139. self.toPageIndex()
  140. }
  141. }
  142. return true
  143. default:
  144. return false
  145. }
  146. }
  147. func toPageIndex() {
  148. var index = (self.currentPageIndex ?? 0) + 1
  149. if Int(self.currentPageIndexLabel.stringValue) != nil {
  150. index = min(max(1, Int(self.currentPageIndexLabel.stringValue)!), totalPagesCount!)
  151. }
  152. self.currentPageIndexLabel.stringValue = String(index)
  153. self.delegate?.gotoPageIndex(view: self, pageIndex: index - 1)
  154. self.updateUI()
  155. }
  156. }
  157. //MARK: In out
  158. extension KMPageNumberDisplayView {
  159. @objc fileprivate func inView() {
  160. self.dismiss()
  161. NSAnimationContext.runAnimationGroup { NSAnimationContext in
  162. self.animator().alphaValue = 1
  163. }
  164. }
  165. @objc func outView() {
  166. NSAnimationContext.runAnimationGroup { NSAnimationContext in
  167. self.animator().alphaValue = 0
  168. self.dismiss()
  169. self.isEdit = false
  170. }
  171. }
  172. func reset() {
  173. self.dismiss()
  174. self.show()
  175. }
  176. func show(type: KMPreferencePageIndicatorDisplayType = .automatic) {
  177. self.displayType = type
  178. }
  179. func dismiss() {
  180. self.timer?.invalidate()
  181. self.timer = nil
  182. }
  183. func updateViewDispaly() {
  184. var type = self.displayType
  185. if hover {
  186. type = .always
  187. }
  188. if type == .automatic {
  189. if self.timer != nil {
  190. self.dismiss()
  191. }
  192. self.inView()
  193. self.timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(outView), userInfo: nil, repeats: true)
  194. } else if type == .never {
  195. self.dismiss()
  196. } else if type == .always {
  197. self.inView()
  198. }
  199. }
  200. }