KMToolbarPageInputItemView.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return view
  25. }()
  26. private lazy var numberLabel_: NSTextField = {
  27. let view = NSTextField(labelWithString: "")
  28. view.font = KMToolbarMainItemView.textFont
  29. view.textColor = KMAppearance.subtitleColor()
  30. return view
  31. }()
  32. private lazy var titleLabel_: NSTextField = {
  33. let view = NSTextField(labelWithString: NSLocalizedString("Page", comment: ""))
  34. view.font = KMToolbarMainItemView.textFont
  35. view.textColor = KMAppearance.subtitleColor()
  36. return view
  37. }()
  38. var totalNumber: Int = 0 {
  39. didSet {
  40. self.numberLabel_.stringValue = "/" + "\(self.totalNumber)"
  41. }
  42. }
  43. convenience init() {
  44. self.init(frame: NSMakeRect(0, 0, 80, 40))
  45. }
  46. override init(frame frameRect: NSRect) {
  47. super.init(frame: frameRect)
  48. self.initSubviews()
  49. }
  50. required init?(coder: NSCoder) {
  51. super.init(coder: coder)
  52. self.initSubviews()
  53. }
  54. override func draw(_ dirtyRect: NSRect) {
  55. super.draw(dirtyRect)
  56. // Drawing code here.
  57. }
  58. func initSubviews() {
  59. self.addSubview(self.contentView_)
  60. self.contentView_.addSubview(self.inputBox_)
  61. self.contentView_.addSubview(self.numberLabel_)
  62. self.contentView_.addSubview(self.titleLabel_)
  63. self.inputBox_.contentView?.addSubview(self.inputTF_)
  64. self.contentView_.km_add_inset_constraint()
  65. self.inputBox_.km_add_leading_constraint(constant: 2)
  66. self.inputBox_.km_add_top_constraint(constant: 2)
  67. self.inputBox_.km_add_width_constraint(constant: 45)
  68. self.inputBox_.km_add_height_constraint(constant: 22)
  69. self.numberLabel_.km_add_leading_constraint(equalTo: self.inputBox_, attribute: .trailing, constant: 2)
  70. self.numberLabel_.km_add_centerY_constraint(equalTo: self.inputBox_)
  71. self.titleLabel_.km_add_top_constraint(equalTo: self.inputBox_, attribute: .bottom, constant: 2)
  72. self.titleLabel_.km_add_centerX_constraint()
  73. self.inputTF_.km_add_leading_constraint()
  74. self.inputTF_.km_add_centerX_constraint()
  75. self.inputTF_.km_add_centerY_constraint()
  76. }
  77. }