KMPageNumberDisplayView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // KMPageNumberDisplayView.swift
  3. // PDF Master
  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: KMBaseXibView {
  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. override func setup() {
  62. self.backgroundColor(NSColor(hex: "#36383B"))
  63. self.shadow = NSShadow()
  64. self.wantsLayer = true
  65. self.layer?.shadowColor = NSColor(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. override func reloadData() {
  74. }
  75. override func updateLanguage() {
  76. }
  77. override func updateUI() {
  78. // NSString(string: self.currentPageLabel.stringValue).boundingRect(with: NSSize(width: 1000, height: 1000)).size.width + 8
  79. self.totalPageCountLabel.textColor = NSColor(hex: "#FFFFFF")
  80. self.totalPageCountLabel.font = NSFont.SFProTextRegular(12.0)
  81. self.lineLabel.textColor = NSColor(hex: "#FFFFFF")
  82. self.lineLabel.font = NSFont.SFProTextRegular(12.0)
  83. if isEdit {
  84. currentPageIndexLabel.textColor = NSColor(hex: "#252629")
  85. currentPageIndexLabel.font = NSFont.SFProTextRegular(12.0)
  86. currentPageContentView.backgroundColor(NSColor(hex: "#FFFFFF"))
  87. currentPageContentView.border(NSColor(hex: "#DFE1E5"), 1, 4)
  88. } else {
  89. currentPageIndexLabel.textColor = NSColor(hex: "#FFFFFF")
  90. currentPageIndexLabel.font = NSFont.SFProTextRegular(12.0)
  91. currentPageContentView.backgroundColor(NSColor(hex: "#FFFFFF", alpha: 0))
  92. currentPageContentView.border(NSColor(hex: "#DFE1E5"), 0, 4)
  93. }
  94. self.delegate?.updateWidth(view: self, width: self.fetchWith(edit: self.isEdit))
  95. }
  96. override func resetData() {
  97. }
  98. override func addNotification() {
  99. }
  100. override func removeNotification() {
  101. }
  102. func fetchWith(edit: Bool = false) -> CGFloat {
  103. let currentIndexString: NSString = self.currentPageIndexLabel.stringValue as NSString
  104. let totalPageCountString: NSString = self.totalPageCountLabel.stringValue as NSString
  105. let attributes = [NSAttributedString.Key.font : NSFont.SFProTextRegular(12.0), NSAttributedString.Key.foregroundColor: NSColor(hex: "#FFFFFF")]
  106. let currentIndexStringWidth = currentIndexString.boundingRect(with: CGSize(width: 100, height: 36), attributes: attributes).width
  107. let totalPageCountStringWidth = totalPageCountString.boundingRect(with: CGSize(width: 100, height: 36), attributes: attributes).width
  108. var width = 16 + currentIndexStringWidth + 8 + 4 + 8 + totalPageCountStringWidth + 16
  109. if edit {
  110. self.pageWidthConstraint.constant = currentIndexStringWidth + 40
  111. self.pageLabelConstraint.constant = currentIndexStringWidth + 24
  112. width += 40
  113. } else {
  114. self.pageWidthConstraint.constant = currentIndexStringWidth
  115. self.pageLabelConstraint.constant = currentIndexStringWidth
  116. }
  117. return width
  118. }
  119. }
  120. //MARK: mouse
  121. extension KMPageNumberDisplayView {
  122. override func mouseDown(with event: NSEvent) {
  123. print("mouseDown")
  124. self.isEdit = true
  125. self.delegate?.updateWidth(view: self, width: self.fetchWith(edit: self.isEdit))
  126. }
  127. }
  128. //MARK:
  129. extension KMPageNumberDisplayView: NSTextFieldDelegate {
  130. func controlTextDidEndEditing(_ obj: Notification) {
  131. // self.toPageIndex()
  132. }
  133. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  134. switch commandSelector {
  135. case #selector(NSResponder.insertNewline(_:)):
  136. if let inputView = control as? NSTextField {
  137. //当当前TextField按下enter
  138. if (textView.string == self.currentPageIndexLabel.stringValue) {
  139. print("按下 enter")
  140. self.isEdit = false
  141. self.toPageIndex()
  142. }
  143. }
  144. return true
  145. default:
  146. return false
  147. }
  148. }
  149. func toPageIndex() {
  150. let index = max((Int(self.currentPageIndexLabel.stringValue) ?? 1) - 1, 0)
  151. self.delegate?.gotoPageIndex(view: self, pageIndex: index)
  152. self.updateUI()
  153. }
  154. }
  155. //MARK: In out
  156. extension KMPageNumberDisplayView {
  157. @objc fileprivate func inView() {
  158. self.dismiss()
  159. NSAnimationContext.runAnimationGroup { NSAnimationContext in
  160. self.animator().alphaValue = 1
  161. }
  162. }
  163. @objc fileprivate func outView() {
  164. NSAnimationContext.runAnimationGroup { NSAnimationContext in
  165. self.animator().alphaValue = 0
  166. self.dismiss()
  167. self.isEdit = false
  168. }
  169. }
  170. func reset() {
  171. self.dismiss()
  172. self.show()
  173. }
  174. func show(type: KMPreferencePageIndicatorDisplayType = .automatic) {
  175. self.displayType = type
  176. }
  177. func dismiss() {
  178. self.timer?.invalidate()
  179. self.timer = nil
  180. }
  181. func updateViewDispaly() {
  182. var type = self.displayType
  183. if hover {
  184. type = .always
  185. }
  186. if type == .automatic {
  187. if self.timer != nil {
  188. self.dismiss()
  189. }
  190. self.inView()
  191. self.timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(outView), userInfo: nil, repeats: true)
  192. } else if type == .never {
  193. self.dismiss()
  194. } else if type == .always {
  195. self.inView()
  196. }
  197. }
  198. }