KMToolbarPreviousNextItemView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KMToolbarPreviousNextItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/15.
  6. //
  7. import Cocoa
  8. private func _KMPreviousNextString() -> String {
  9. return "\(NSLocalizedString("Previous", comment: ""))/\(NSLocalizedString("Next", comment: ""))"
  10. }
  11. private let _minWidth: CGFloat = 24 * 2
  12. class KMToolbarPreviousNextItemView: NSView {
  13. var callback: KMCommonClickBlock?
  14. lazy var previousButton: KMCoverButton = {
  15. let button = KMCoverButton()
  16. button.wantsLayer = true
  17. button.isBordered = false
  18. button.layer?.cornerRadius = 6
  19. button.image = NSImage(named: "KMImageNameToolbarPagepreviousNor")
  20. button.toolTip = NSLocalizedString("Go To Previous Page", comment: "")
  21. button.imagePosition = .imageOnly
  22. button.target = self
  23. button.action = #selector(buttonClicked)
  24. button.coverAction = { cbtn, caction in
  25. if caction == .enter {
  26. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  27. } else if caction == .exit {
  28. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  29. }
  30. }
  31. return button
  32. }()
  33. lazy var nextButton: KMCoverButton = {
  34. let button = KMCoverButton()
  35. button.wantsLayer = true
  36. button.isBordered = false
  37. button.layer?.cornerRadius = 6
  38. button.image = NSImage(named: "KMImageNameToolbarPagenextNor")
  39. button.toolTip = NSLocalizedString("Go To Next Page", comment: "")
  40. button.imagePosition = .imageOnly
  41. button.target = self
  42. button.action = #selector(buttonClicked)
  43. button.coverAction = { cbtn, caction in
  44. if caction == .enter {
  45. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  46. } else if caction == .exit {
  47. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  48. }
  49. }
  50. return button
  51. }()
  52. lazy var titleLabel: NSTextField = {
  53. let label = NSTextField(labelWithString: _KMPreviousNextString())
  54. label.font = KMToolbarMainItemView.textFont
  55. label.textColor = KMToolbarMainItemView.fetchTextNormalColor()
  56. return label
  57. }()
  58. convenience init() {
  59. self.init(frame: NSMakeRect(0, 0, Self.itemWidth, 40))
  60. }
  61. override init(frame frameRect: NSRect) {
  62. super.init(frame: frameRect)
  63. self.initSubview()
  64. }
  65. required init?(coder: NSCoder) {
  66. super.init(coder: coder)
  67. self.initSubview()
  68. }
  69. func initSubview() {
  70. self.addSubview(self.previousButton)
  71. self.addSubview(self.nextButton)
  72. self.addSubview(self.titleLabel)
  73. self.previousButton.km_add_left_constraint()
  74. self.previousButton.km_add_right_constraint(equalTo: self.nextButton, attribute: .left)
  75. self.previousButton.km_add_top_constraint()
  76. self.previousButton.km_add_height_constraint(constant: 24)
  77. self.nextButton.km_add_right_constraint()
  78. self.nextButton.km_add_width_constraint(equalTo: self.previousButton, attribute: .width)
  79. self.nextButton.km_add_top_constraint()
  80. self.nextButton.km_add_height_constraint(constant: 24)
  81. self.titleLabel.km_add_bottom_constraint()
  82. self.titleLabel.km_add_height_constraint(constant: 14)
  83. self.titleLabel.km_add_left_constraint()
  84. self.titleLabel.km_add_right_constraint()
  85. self.titleLabel.km_add_width_constraint(constant: Self.itemWidth)
  86. // self.titleLabel.km_add_top_constraint(equalTo: self.previousButton, attribute: .bottom)
  87. }
  88. class var itemWidth: CGFloat {
  89. get {
  90. let string = _KMPreviousNextString()
  91. let width = string.boundingRect(with: NSSize(width: CGFLOAT_MAX, height: 20), options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], attributes: [.font : KMToolbarMainItemView.textFont]).size.width + 5 * 2
  92. if width < _minWidth {
  93. return _minWidth
  94. }
  95. return width
  96. }
  97. }
  98. override func draw(_ dirtyRect: NSRect) {
  99. super.draw(dirtyRect)
  100. // Drawing code here.
  101. }
  102. @objc func buttonClicked(_ sender: NSButton) {
  103. guard let block = self.callback else {
  104. return
  105. }
  106. if self.previousButton.isEqual(to: sender) {
  107. block(1)
  108. } else if self.nextButton.isEqual(to: sender) {
  109. block(2)
  110. }
  111. }
  112. }