KMComboBox.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // KMComboBox.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/11/29.
  6. //
  7. import Cocoa
  8. enum KMComboBoxType : Int {
  9. case none = 0
  10. }
  11. class KMComboBox: NSComboBox {
  12. var comboxRect: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
  13. var _type: KMComboBoxType = .none
  14. override func draw(_ dirtyRect: NSRect) {
  15. super.draw(dirtyRect)
  16. switch type {
  17. case .none:
  18. var p = comboxRect.origin
  19. p.x += comboxRect.width - 23
  20. p.y += (comboxRect.height - 16) / 2
  21. // NSColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0).setFill()
  22. KMAppearance.Layout.l1Color().setFill()
  23. NSRect(x: p.x, y: 0, width: 16.0, height: comboxRect.height).fill()
  24. let image = NSImage(named: "KMImageNameUXIconBtnArrowDown")
  25. var rect = NSRect.zero
  26. rect.size = CGSize(width: 16.0, height: 16.0)
  27. image!.draw(in: NSRect(x: p.x, y: p.y, width: 16.0, height: 16.0), from: rect, operation: .sourceOver, fraction: 1.0, respectFlipped: true, hints: nil)
  28. default:
  29. break
  30. }
  31. }
  32. // MARK: Private Methods
  33. var type: KMComboBoxType {
  34. get {
  35. return _type
  36. }
  37. set {
  38. _type = newValue
  39. switch newValue {
  40. case .none:
  41. self.wantsLayer = true
  42. self.layer?.cornerRadius = 1.0
  43. self.layer?.borderWidth = 0.5
  44. self.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor
  45. self.backgroundColor = KMAppearance.Layout.w0Color()
  46. self.isButtonBordered = false
  47. default:
  48. break
  49. }
  50. }
  51. }
  52. }
  53. class KMComboBoxCell: NSComboBoxCell {
  54. private func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
  55. let offset = floor((frame.size.height / 2 - (font!.ascender + font!.descender)))
  56. return NSInsetRect(frame, 0.0, offset)
  57. }
  58. override func edit(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, event: NSEvent?) {
  59. super.edit(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, event: event)
  60. }
  61. override func select(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, start selStart: Int, length selLength: Int) {
  62. super.select(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, start: selStart, length: selLength)
  63. }
  64. override func drawInterior(withFrame frame: NSRect, in controlView: NSView) {
  65. super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(frame), in: controlView)
  66. }
  67. }
  68. class KMStampComboBoxCell: NSComboBoxCell {
  69. override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
  70. backgroundColor = KMAppearance.Layout.l1Color()
  71. super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(cellFrame), in: controlView)
  72. }
  73. func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
  74. let offset = floor((frame.height / 2 - (font?.ascender ?? 0.0 + font!.descender)))
  75. return NSInsetRect(frame, 0.0, offset)
  76. }
  77. }