// // KMComboBox.swift // PDF Reader Pro // // Created by wanjun on 2022/11/29. // import Cocoa enum KMComboBoxType : Int { case none = 0 } class KMComboBox: NSComboBox { var comboxRect: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0) var _type: KMComboBoxType = .none override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) switch type { case .none: var p = comboxRect.origin p.x += comboxRect.width - 23 p.y += (comboxRect.height - 16) / 2 // NSColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0).setFill() KMAppearance.Layout.l1Color().setFill() NSRect(x: p.x, y: 0, width: 16.0, height: comboxRect.height).fill() let image = NSImage(named: "KMImageNameUXIconBtnArrowDown") var rect = NSRect.zero rect.size = CGSize(width: 16.0, height: 16.0) 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) default: break } } // MARK: Private Methods var type: KMComboBoxType { get { return _type } set { _type = newValue switch newValue { case .none: self.wantsLayer = true self.layer?.cornerRadius = 1.0 self.layer?.borderWidth = 0.5 self.layer?.borderColor = KMAppearance.Interactive.s0Color().cgColor self.backgroundColor = KMAppearance.Layout.w0Color() self.isButtonBordered = false default: break } } } } class KMComboBoxCell: NSComboBoxCell { private func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect { let offset = floor((frame.size.height / 2 - (font!.ascender + font!.descender))) return NSInsetRect(frame, 0.0, offset) } override func edit(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, event: NSEvent?) { super.edit(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, event: event) } override func select(withFrame aRect: NSRect, in controlView: NSView, editor textObj: NSText, delegate anObject: Any?, start selStart: Int, length selLength: Int) { super.select(withFrame: adjustedFrameToVerticallyCenterText(aRect), in: controlView, editor: textObj, delegate: anObject, start: selStart, length: selLength) } override func drawInterior(withFrame frame: NSRect, in controlView: NSView) { super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(frame), in: controlView) } } class KMStampComboBoxCell: NSComboBoxCell { override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { backgroundColor = KMAppearance.Layout.l1Color() super.drawInterior(withFrame: adjustedFrameToVerticallyCenterText(cellFrame), in: controlView) } func adjustedFrameToVerticallyCenterText(_ frame: NSRect) -> NSRect { let offset = floor((frame.height / 2 - (font?.ascender ?? 0.0 + font!.descender))) return NSInsetRect(frame, 0.0, offset) } }