KMComboBox.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // KMComboBox.swift
  3. // PDF Master
  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 - 20
  20. p.y += (comboxRect.height - 16) / 2
  21. NSColor.clear.setFill()
  22. NSRect(x: p.x, y: 0, width: 16.0, height: comboxRect.height).fill()
  23. let image = NSImage(named: "KMImageNameUXIconBtnArrowDown")
  24. var rect = NSRect.zero
  25. rect.size = CGSize(width: 16.0, height: 16.0)
  26. 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)
  27. default:
  28. break
  29. }
  30. }
  31. // MARK: Private Methods
  32. var type: KMComboBoxType {
  33. get {
  34. return _type
  35. }
  36. set {
  37. _type = newValue
  38. switch newValue {
  39. case .none:
  40. self.wantsLayer = true
  41. self.layer?.cornerRadius = 1.0
  42. self.layer?.borderWidth = 0.5
  43. self.layer?.borderColor = .black
  44. self.backgroundColor = .white
  45. self.isButtonBordered = false
  46. default:
  47. break
  48. }
  49. }
  50. }
  51. }
  52. class KMComboBoxCell: NSComboBoxCell {
  53. private func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
  54. let offset = floor((frame.size.height / 2 - (font!.ascender + font!.descender)))
  55. let rect = NSRect(x: 8, y: 0, width: frame.size.width, height: frame.size.height)
  56. return NSInsetRect(rect, 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. self.backgroundColor = NSColor.white
  71. super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(cellFrame), in: controlView)
  72. }
  73. func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect {
  74. let offset = floor((NSHeight(frame) / 2 - (font!.ascender + font!.descender)))
  75. return frame.insetBy(dx: 0.0, dy: offset)
  76. }
  77. }