KMToolbarPageInputItemView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // KMToolbarPageInputItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/10/12.
  6. //
  7. import Cocoa
  8. class KMToolbarPageInputItemView: NSView {
  9. private lazy var contentView_: NSView = {
  10. let view = NSView()
  11. return view
  12. }()
  13. private lazy var inputBox_: NSBox = {
  14. let box = NSBox()
  15. box.boxType = .custom
  16. return box
  17. }()
  18. private lazy var inputTF_: NSTextField = {
  19. let view = NSTextField()
  20. view.formatter = NumberFormatter()
  21. view.drawsBackground = false
  22. view.isBordered = false
  23. view.focusRingType = .none
  24. view.delegate = self
  25. return view
  26. }()
  27. private lazy var numberLabel_: NSTextField = {
  28. let view = NSTextField(labelWithString: "")
  29. view.font = KMToolbarMainItemView.textFont
  30. view.textColor = KMAppearance.subtitleColor()
  31. return view
  32. }()
  33. private lazy var titleLabel_: NSTextField = {
  34. let view = NSTextField(labelWithString: NSLocalizedString("Page", comment: ""))
  35. view.font = KMToolbarMainItemView.textFont
  36. view.textColor = KMAppearance.subtitleColor()
  37. return view
  38. }()
  39. var totalNumber: Int = 0 {
  40. didSet {
  41. self.numberLabel_.stringValue = "/" + "\(self.totalNumber)"
  42. }
  43. }
  44. var currentPageIndex: Int = 0 {
  45. didSet {
  46. self.inputTF_.stringValue = "\(self.currentPageIndex)"
  47. }
  48. }
  49. var enterAction: ((String)->Void)?
  50. convenience init() {
  51. self.init(frame: NSMakeRect(0, 0, 80, 40))
  52. }
  53. override init(frame frameRect: NSRect) {
  54. super.init(frame: frameRect)
  55. self.initSubviews()
  56. }
  57. required init?(coder: NSCoder) {
  58. super.init(coder: coder)
  59. self.initSubviews()
  60. }
  61. override func draw(_ dirtyRect: NSRect) {
  62. super.draw(dirtyRect)
  63. // Drawing code here.
  64. }
  65. func initSubviews() {
  66. self.addSubview(self.contentView_)
  67. self.contentView_.addSubview(self.inputBox_)
  68. self.contentView_.addSubview(self.numberLabel_)
  69. self.contentView_.addSubview(self.titleLabel_)
  70. self.inputBox_.contentView?.addSubview(self.inputTF_)
  71. self.contentView_.km_add_inset_constraint()
  72. self.inputBox_.km_add_leading_constraint(constant: 2)
  73. self.inputBox_.km_add_top_constraint(constant: 2)
  74. self.inputBox_.km_add_width_constraint(constant: 45)
  75. self.inputBox_.km_add_height_constraint(constant: 22)
  76. self.numberLabel_.km_add_leading_constraint(equalTo: self.inputBox_, attribute: .trailing, constant: 2)
  77. self.numberLabel_.km_add_centerY_constraint(equalTo: self.inputBox_)
  78. self.titleLabel_.km_add_top_constraint(equalTo: self.inputBox_, attribute: .bottom, constant: 2)
  79. self.titleLabel_.km_add_centerX_constraint()
  80. self.inputTF_.km_add_leading_constraint()
  81. self.inputTF_.km_add_centerX_constraint()
  82. self.inputTF_.km_add_centerY_constraint()
  83. }
  84. }
  85. extension KMToolbarPageInputItemView: NSTextFieldDelegate {
  86. // func controlTextDidEndEditing(_ obj: Notification) {
  87. //
  88. // }
  89. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  90. switch commandSelector {
  91. case #selector(NSResponder.insertNewline(_:)):
  92. if let inputView = control as? NSTextField {
  93. if inputView == self.inputTF_ {
  94. self.enterAction?(self.inputTF_.stringValue)
  95. }
  96. }
  97. return true
  98. default:
  99. return false
  100. }
  101. }
  102. }