KMToolbarZoomItemView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // KMToolbarZoomItemView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/28.
  6. //
  7. import Cocoa
  8. class KMToolbarZoomItemView: NSView {
  9. lazy var boxView: NSView = {
  10. let view = NSView()
  11. view.frame = NSMakeRect(0, 16, 70, 22)
  12. return view
  13. }()
  14. weak var zoomView: NSView?
  15. lazy var titleLabel: NSTextField = {
  16. let label = NSTextField(labelWithString: NSLocalizedString("Zoom", comment: ""))
  17. label.frame = NSMakeRect(0, 0, 70, 14)
  18. label.alignment = .right
  19. label.font = KMToolbarMainItemView.textFont
  20. label.textColor = KMAppearance.subtitleColor()
  21. return label
  22. }()
  23. lazy var zoomInButton: NSButton = {
  24. let button = KMCoverButton()
  25. button.wantsLayer = true
  26. button.layer?.cornerRadius = 4
  27. button.isBordered = false
  28. button.title = ""
  29. button.image = NSImage(named: "KMImageNameUXIconToolbarZoominNor")
  30. button.target = self
  31. button.action = #selector(_buttonClicked)
  32. button.coverAction = { cbtn, caction in
  33. if caction == .enter {
  34. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  35. } else if caction == .exit {
  36. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  37. }
  38. }
  39. return button
  40. }()
  41. lazy var zoomOutButton: NSButton = {
  42. let button = KMCoverButton()
  43. button.isBordered = false
  44. button.wantsLayer = true
  45. button.layer?.cornerRadius = 4
  46. button.title = ""
  47. button.image = NSImage(named: "KMImageNameUXIconToolbarZoomoutNor")
  48. button.target = self
  49. button.action = #selector(_buttonClicked)
  50. button.coverAction = { cbtn, caction in
  51. if caction == .enter {
  52. cbtn.layer?.backgroundColor = KMToolbarItemView.selectedBackgroundColor.cgColor
  53. } else if caction == .exit {
  54. cbtn.layer?.backgroundColor = KMToolbarItemView.normalBackgroundColor.cgColor
  55. }
  56. }
  57. return button
  58. }()
  59. var callback: KMCommonClickBlock?
  60. convenience init(zoomView: NSView?) {
  61. let vspace: CGFloat = 2
  62. let zoomButtonW: CGFloat = 20
  63. self.init(frame: NSMakeRect(0, 0, 70+2*zoomButtonW+vspace, 40))
  64. self.zoomView = zoomView
  65. self.addSubview(self.boxView)
  66. if let zview = self.zoomView {
  67. self.boxView.addSubview(zview)
  68. self.zoomView?.frame = NSMakeRect(10, 3, NSWidth(zview.frame), NSHeight(zview.frame))
  69. }
  70. self.addSubview(self.titleLabel)
  71. self.addSubview(self.zoomInButton)
  72. self.addSubview(self.zoomOutButton)
  73. self.zoomOutButton.frame = NSMakeRect(CGRectGetMaxX(self.boxView.frame)+vspace, 17, zoomButtonW, zoomButtonW)
  74. self.zoomInButton.frame = NSMakeRect(CGRectGetMaxX(self.zoomOutButton.frame), 17, zoomButtonW, zoomButtonW)
  75. self.boxView.wantsLayer = true
  76. self.boxView.layer?.borderWidth = 1
  77. self.boxView.layer?.borderColor = NSColor.lightGray.cgColor
  78. }
  79. override func draw(_ dirtyRect: NSRect) {
  80. super.draw(dirtyRect)
  81. }
  82. }
  83. // MARK: - Private Methods
  84. extension KMToolbarZoomItemView {
  85. @objc private func _buttonClicked(_ sender: NSButton) {
  86. var idx = 1
  87. if self.zoomInButton.isEqual(to: sender) {
  88. idx = 2
  89. }
  90. guard let block = self.callback else {
  91. return
  92. }
  93. block(idx)
  94. }
  95. }
  96. class KMToolbarPageIndicatorItemView: NSView {
  97. lazy var boxView: NSView = {
  98. let view = NSView()
  99. view.frame = NSMakeRect(0, 16, 50, 22)
  100. return view
  101. }()
  102. weak var zoomView: NSView?
  103. lazy var titleLabel: NSTextField = {
  104. let label = NSTextField(labelWithString: NSLocalizedString("Page", comment: ""))
  105. label.frame = NSMakeRect(0, 0, 50, 14)
  106. label.alignment = .right
  107. label.font = KMToolbarMainItemView.textFont
  108. label.textColor = KMAppearance.subtitleColor()
  109. return label
  110. }()
  111. lazy var numLabel: NSTextField = {
  112. let label = NSTextField(labelWithString: NSLocalizedString("/ 29", comment: ""))
  113. label.frame = NSMakeRect(0, 0, 40, 14)
  114. label.alignment = .right
  115. label.font = KMToolbarMainItemView.textFont
  116. label.textColor = KMAppearance.subtitleColor()
  117. return label
  118. }()
  119. var callback: KMCommonClickBlock?
  120. convenience init(zoomView: NSView?) {
  121. let vspace: CGFloat = 2
  122. let zoomButtonW: CGFloat = 40
  123. self.init(frame: NSMakeRect(0, 0, 50+zoomButtonW+vspace, 40))
  124. self.zoomView = zoomView
  125. self.addSubview(self.boxView)
  126. if let zview = self.zoomView {
  127. self.boxView.addSubview(zview)
  128. self.zoomView?.frame = NSMakeRect(10, 3, NSWidth(zview.frame), NSHeight(zview.frame))
  129. }
  130. self.addSubview(self.titleLabel)
  131. self.addSubview(self.numLabel)
  132. self.numLabel.frame = NSMakeRect(CGRectGetMaxX(self.boxView.frame)+vspace, 13, zoomButtonW, 20)
  133. self.boxView.wantsLayer = true
  134. self.boxView.layer?.borderWidth = 1
  135. self.boxView.layer?.borderColor = NSColor.lightGray.cgColor
  136. }
  137. override func draw(_ dirtyRect: NSRect) {
  138. super.draw(dirtyRect)
  139. }
  140. }
  141. // MARK: - Private Methods
  142. extension KMToolbarPageIndicatorItemView {
  143. // @objc private func _buttonClicked(_ sender: NSButton) {
  144. // var idx = 1
  145. // if self.zoomInButton.isEqual(to: sender) {
  146. // idx = 2
  147. // }
  148. //
  149. // guard let block = self.callback else {
  150. // return
  151. // }
  152. // block(idx)
  153. // }
  154. }