KMToolbarZoomItemView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }