KMAngleIndicateView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // KMAngleIndicateView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/11/22.
  6. //
  7. import Cocoa
  8. enum KMAngleIndicateViewStyle {
  9. case left45
  10. case horizontal
  11. case right45
  12. }
  13. class KMAngleIndicateView: NSView {
  14. var touchCallBack: (() -> Void)?
  15. private var gradient: NSGradient?
  16. var isSelcted: Bool = false {
  17. didSet {
  18. if isSelcted {
  19. layer?.borderWidth = 0.5
  20. } else {
  21. layer?.borderWidth = 0
  22. }
  23. needsDisplay = true
  24. }
  25. }
  26. var style: KMAngleIndicateViewStyle = .horizontal
  27. override func draw(_ dirtyRect: NSRect) {
  28. super.draw(dirtyRect)
  29. if isSelcted {
  30. let path = NSBezierPath(roundedRect: bounds, xRadius: 1, yRadius: 1)
  31. gradient?.draw(in: path, relativeCenterPosition: NSPoint(x: 0, y: 1))
  32. wantsLayer = true
  33. layer?.backgroundColor = NSColor.clear.cgColor
  34. layer?.cornerRadius = 0
  35. layer?.borderWidth = 0
  36. layer?.borderColor = NSColor.clear.cgColor
  37. shadow = NSShadow()
  38. layer?.shadowColor = NSColor.clear.cgColor
  39. layer?.shadowOffset = CGSize(width: 0, height: 0)
  40. layer?.shadowRadius = 0
  41. } else {
  42. wantsLayer = true
  43. layer?.backgroundColor = KMAppearance.KM_FFFFFF_Color35().cgColor
  44. layer?.cornerRadius = 1
  45. layer?.borderWidth = 0.5
  46. layer?.borderColor = KMAppearance.KM_000000_Color15Chang35().cgColor
  47. shadow = NSShadow()
  48. layer?.shadowColor = KMAppearance.KM_000000_Color20().cgColor
  49. layer?.shadowOffset = CGSize(width: 0, height: -1)
  50. layer?.shadowRadius = 0.5
  51. }
  52. if let context = NSGraphicsContext.current?.cgContext {
  53. // Middle color block
  54. context.saveGState()
  55. if style == .left45 {
  56. context.translateBy(x: bounds.width / 2, y: bounds.height / 2)
  57. context.rotate(by: CGFloat.pi / 4)
  58. context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12))
  59. context.setFillColor(KMAppearance.KM_757780_Color().cgColor)
  60. context.fillPath()
  61. } else if style == .horizontal {
  62. context.translateBy(x: bounds.width / 2, y: bounds.height / 2)
  63. context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12))
  64. context.setFillColor(KMAppearance.KM_757780_Color().cgColor)
  65. context.fillPath()
  66. } else if style == .right45 {
  67. context.translateBy(x: bounds.width / 2, y: bounds.height / 2)
  68. context.rotate(by: -CGFloat.pi / 4)
  69. context.addRect(CGRect(x: -4, y: -6, width: 8, height: 12))
  70. context.setFillColor(KMAppearance.KM_757780_Color().cgColor)
  71. context.fillPath()
  72. }
  73. context.restoreGState()
  74. }
  75. }
  76. override func awakeFromNib() {
  77. super.awakeFromNib()
  78. gradient = NSGradient(starting: KMAppearance.KM_EDECED_Color(),
  79. ending: KMAppearance.KM_D2D1D2_Color())
  80. }
  81. override func mouseUp(with event: NSEvent) {
  82. if !isSelcted, let callback = touchCallBack {
  83. isSelcted = true
  84. callback()
  85. }
  86. }
  87. }