KMPopMenuButtonCell.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // KMPopMenuButtonCell.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. class KMPopMenuButtonCell: NSButtonCell {
  9. override func titleRect(forBounds rect: NSRect) -> NSRect {
  10. var rect1 = super.titleRect(forBounds: rect)
  11. if ((self.image?.size.width) != nil) {
  12. return CGRectMake(21 + 5 + (self.image?.size.width)!, rect1.origin.y, self.attributedTitle.size().width, self.attributedTitle.size().height)
  13. } else {
  14. return CGRectMake(21 + 5, rect1.origin.y, self.attributedTitle.size().width, self.attributedTitle.size().height)
  15. }
  16. }
  17. override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
  18. if !isEnabled {
  19. var txtColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.8)
  20. if #available(macOS 10.14, *) {
  21. if let appearanceName = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) {
  22. if appearanceName == .darkAqua {
  23. txtColor = NSColor(white: 1.0, alpha: 0.5)
  24. let myAttr = NSMutableAttributedString(attributedString: title)
  25. myAttr.addAttribute(.foregroundColor, value: txtColor, range: NSRange(location: 0, length: myAttr.length))
  26. return super.drawTitle(myAttr, withFrame: frame, in: controlView)
  27. }
  28. }
  29. }
  30. }
  31. return super.drawTitle(title, withFrame: frame, in: controlView)
  32. }
  33. override func imageRect(forBounds rect: NSRect) -> NSRect {
  34. if let image = self.image {
  35. var imageRect = super.imageRect(forBounds: rect)
  36. imageRect.origin.x = 21
  37. return imageRect
  38. }
  39. return NSRect.zero
  40. }
  41. }