// // KMPreferenceSegementControl.swift // PDF Master // // Created by tangchao on 2023/2/3. // import Cocoa typealias KMPreferenceSegementItemClick = (_ index: Int) -> () class KMPreferenceSegementItem: NSView { var imageView = NSImageView() var label = NSTextField(labelWithString: "") var itemID: Int = 0 var itemClick: KMPreferenceSegementItemClick? var selectedImage: NSImage? var unSelectedImage: NSImage? var selectedTextColor: NSColor? var unSelectedTextColor: NSColor? private var mySelected: Bool = false var isSelected: Bool { get { return self.mySelected } set { self.mySelected = newValue if (newValue) { self.layer?.backgroundColor = NSColor(white: 0, alpha: 0.05).cgColor self.imageView.image = self.selectedImage self.label.textColor = self.selectedTextColor } else { self.layer?.backgroundColor = NSColor.clear.cgColor self.imageView.image = self.unSelectedImage self.label.textColor = self.unSelectedTextColor } } } override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubViews() } override var isFlipped: Bool { return true } public func initSubViews() { self.addSubview(self.imageView) self.addSubview(self.label) self.label.alignment = .center self.wantsLayer = true self.layer?.cornerRadius = 7 /// 新增点击手势 let gesture = NSClickGestureRecognizer(target: self, action: #selector(gestureAction)) self.addGestureRecognizer(gesture) } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let size: CGFloat = 17 self.imageView.frame = NSMakeRect((width-size)*0.5, 6, size, size) self.label.frame = NSMakeRect(0, self.imageView.frame.maxY+5, width, 14) } @objc private func gestureAction() { guard let callback = self.itemClick else { return } callback(self.itemID) } } class KMPreferenceSegementControl: NSControl { var items: Array = [] var selectedIndex: Int = 0 var selectedItem: KMPreferenceSegementItem? override init(frame frameRect: NSRect) { super.init(frame: frameRect) initSubViews() } required init?(coder: NSCoder) { super.init(coder: coder) initSubViews() } override var isFlipped: Bool { return true } func initSubViews() { for item in self.items { item.removeFromSuperview() } self.items.removeAll() for i in 0 ... 2 { let item = KMPreferenceSegementItem() item.itemID = i + 1 self.addSubview(item) self.items.append(item) item.itemClick = { [weak self] index in self?.selectItem(index) let _ = self?.target?.perform(self?.action, with: self) } } } override func layout() { super.layout() let width: CGFloat = NSWidth(self.bounds) let itemSize: NSSize = NSSize(width: 75, height: 45) let leftMargin: CGFloat = (width-CGFloat(self.items.count)*itemSize.width)*0.5 let topMargin: CGFloat = 8 var preItem: KMPreferenceSegementItem? for item in self.items { if (preItem != nil) { item.frame = NSMakeRect((preItem?.frame.maxX)!, topMargin, itemSize.width, itemSize.height) } else { item.frame = NSMakeRect(leftMargin, topMargin, itemSize.width, itemSize.height) } preItem = item } } public func selectItem(_ index: Int) { self.selectedIndex = index for item in items { if (item.itemID == index) { item.isSelected = true self.selectedItem = item } else { item.isSelected = false } } } }