123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- //
- // ComponentGroup.swift
- // KMComponentLibrary
- //
- // Created by Niehaoyu on 2024/9/3.
- //
- import Cocoa
- import AppKit
- @objc public protocol ComponentGroupDelegate: AnyObject {
-
- @objc optional func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?)
-
- @objc optional func componentGroupDidDismiss(group: ComponentGroup?)
-
- }
- public class ComponentGroup: NSView, NibLoadable {
-
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var scrollView: NSScrollView!
- @IBOutlet var collectionView: NSCollectionView!
-
- var superBGView: ComponentBaseView = ComponentBaseView(frame: CGRectZero)
-
- // MARK: Private Property
- private var localMonitor: Any?
- private var globalMonitor: Any?
-
- private var _shadowInfo : ComponentShadowInfo?
-
- public var menuItemArr: [ComponentMenuitemProperty] = []
-
- public var clickedAutoHide = true //点击Item后,是否自动隐藏
-
- var subGroupView: ComponentGroup!
-
- weak open var groupDelegate: ComponentGroupDelegate?
-
- deinit {
- NotificationCenter.default.removeObserver(self)
-
- }
-
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
-
- if shadowInfo != nil {
- let shadow = NSShadow()
- shadow.shadowColor = shadowInfo?.shadowColor
- shadow.shadowOffset = shadowInfo?.shadowOffset ?? CGSizeZero
- shadow.shadowBlurRadius = shadowInfo?.shadowBlurRadius ?? 0
- shadow.set()
-
- let cornerRadius: CGFloat = shadowInfo?.cornerRadius ?? 0
- let roundedRect = NSBezierPath(roundedRect: bounds, xRadius: cornerRadius, yRadius: cornerRadius)
-
- if let color = shadowInfo?.color {
- color.setFill()
- roundedRect.fill()
- }
- }
-
- }
-
- public override func removeFromSuperview() {
- super.removeFromSuperview()
-
- clearMonitor()
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- shadowInfo = ComponentLibrary.shared.configShadowInfoWithKey("")
-
- if let value = ComponentLibrary.shared.getComponentValueFromKey("colorBg/popup") {
- let currentValue = value as! [String : Any]
- contendBox.fillColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
- } else {
- contendBox.fillColor = NSColor.clear
- }
- contendBox.borderColor = NSColor.clear
- contendBox.borderWidth = 0
-
- scrollView.scrollerStyle = .overlay
-
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.backgroundColors = [NSColor.clear]
- collectionView.wantsLayer = true
- collectionView.layer?.backgroundColor = NSColor.clear.cgColor
- collectionView.register(ComponentGroupItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "componentGroupItemID"))
-
- }
-
- //MARK: - Setter and Getter
- public var shadowInfo : ComponentShadowInfo? {
- get {
- return _shadowInfo
- }
- set {
- _shadowInfo = newValue
-
- display()
- }
- }
-
- //MARK: - Public Method
- public func updateGroupInfo(_ arr: [ComponentMenuitemProperty]) {
- menuItemArr.removeAll()
-
- for item in arr {
- menuItemArr.append(item)
- }
-
- reloadData()
- }
-
- public func reloadData() {
- collectionView.reloadData()
- }
-
- //MARK: - SubGroupView
- func removeSubGroupView() {
- if subGroupView != nil {
- subGroupView.removeGroupView()
- subGroupView = nil
- }
- }
-
- //MARK: - Show
-
- public func showWithPoint(_ point: CGPoint, relativeTo positioningView: NSView?) {
-
- let screenRect = NSScreen.main!.frame
- let contentWindow: NSWindow = NSWindow(contentRect: screenRect,
- styleMask: [.borderless],
- backing: .buffered, defer: false)
- contentWindow.level = .statusBar
- contentWindow.backgroundColor = NSColor.clear
- contentWindow.makeKeyAndOrderFront(nil)
-
- if let subView = positioningView {
- var rect = frame
- rect.origin.x = point.x
- rect.origin.y = point.y
- rect.origin.x += subView.window?.frame.origin.x ?? 0
- rect.origin.y += subView.window?.frame.origin.y ?? 0
-
- if(rect.origin.y < 0) {
- rect.origin.y = 0
- }
-
- if(CGRectGetMaxX(rect) > CGRectGetMaxX(screenRect)) {
- rect.origin.x = CGRectGetMaxX(screenRect) - rect.width
- }
- frame = rect
-
- contentWindow.contentView?.addSubview(self)
-
- clearMonitor()
-
- addMonitor()
- }
-
- }
-
- public func removeGroupView() {
- superBGView.removeFromSuperview()
-
- removeFromSuperview()
-
- clearMonitor()
-
- window?.close()
- }
-
- //MARK: - subGroupView
- func showSubGroupView(itemView: ComponentGroupItem) {
- if subGroupView == nil {
- subGroupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
- }
- var viewHeight: CGFloat = 8
- for property in itemView.properties.subPropertys {
- if property.type == .divider {
- viewHeight += 8
- } else if property.type == .normal {
- viewHeight += 36
- } else if property.type == .header {
- viewHeight += 36
- }
- }
- subGroupView?.frame = CGRectMake(0, 0, 160, viewHeight)
- subGroupView.updateGroupInfo(itemView.properties.subPropertys)
-
- var point = CGPoint(x: 0, y: 0)
- if let subViewRect = itemView.view.superview?.convert(itemView.view.frame, to: itemView.view.window?.contentView) {
- point.x = CGRectGetMaxX(subViewRect)
- point.y = CGRectGetMaxY(subViewRect) - viewHeight
- }
-
- guard let subView = itemView.view.window?.contentView else {
- return
- }
- if point.x + CGRectGetWidth(self.subGroupView.frame) > CGRectGetWidth(subView.frame) - 10 {
- if let selfConvertPoint = superview?.convert(self.frame.origin, to: subView) {
- point.x = selfConvertPoint.x - CGRectGetWidth(self.subGroupView.frame)
- }
- }
- subGroupView.showWithPoint(point, relativeTo: self)
- }
-
- //MARK: - Private Method
- func addMonitor() {
- if globalMonitor == nil {
- globalMonitor = NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseDown], handler: { (event) in
- if event.type == .leftMouseDown {
- self.leftMouseDownAction()
- }
- })
- }
-
- if localMonitor == nil {
- localMonitor = NSEvent.addLocalMonitorForEvents(matching: [.leftMouseDown, .scrollWheel], handler: { (event) in
- if event.type == .leftMouseDown {
- let point = self.convert(event.locationInWindow, from: nil)
- if CGRectContainsPoint(self.bounds, point) {
-
- } else {
- self.leftMouseDownAction()
- }
- } else if event.type == .scrollWheel {
- let point = self.convert(event.locationInWindow, from: nil)
- if CGRectContainsPoint(self.bounds, point) {
-
- } else {
- self.leftMouseDownAction()
- }
- }
- return event
- })
- }
- }
-
- func clearMonitor() {
- if localMonitor != nil {
- NSEvent.removeMonitor(self.localMonitor as Any)
- localMonitor = nil
- }
-
- if globalMonitor != nil {
- NSEvent.removeMonitor(self.globalMonitor as Any)
- globalMonitor = nil
- }
- }
-
- func leftMouseDownAction() {
-
- groupDelegate?.componentGroupDidDismiss?(group: self)
-
- removeGroupView()
-
- }
- }
- //MARK: - NSCollectionViewDelegate
- extension ComponentGroup: NSCollectionViewDelegate {
-
- }
- extension ComponentGroup: NSCollectionViewDataSource {
- public func numberOfSections(in collectionView: NSCollectionView) -> Int {
- return 1
- }
-
- public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
- return menuItemArr.count
- }
-
- public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
-
- let itemView = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "componentGroupItemID"), for: indexPath) as! ComponentGroupItem
- if indexPath.item < menuItemArr.count {
- let properties = menuItemArr[indexPath.item]
- itemView.setUpWithProperties(properties)
- }
- itemView.clickHandle = {[weak self] view in
- DispatchQueue.main.async {
- let property = view.properties
- if self?.clickedAutoHide == true {
- self?.groupDelegate?.componentGroupDidDismiss?(group: self)
- self?.removeGroupView()
- }
- self?.groupDelegate?.componentGroupDidSelect?(group: self, menuItemProperty: property)
- }
- }
-
- itemView.mouseHandle = {[weak self] view, mouseState, event in
- guard let weakSelf = self else { return }
- if view.properties.subPropertys.isEmpty == false {
- if mouseState == .enter {
- self?.showSubGroupView(itemView: view)
- } else if mouseState == .move {
- let point = weakSelf.convert(event.locationInWindow, from: nil)
-
- }
- } else {
- self?.removeSubGroupView()
- }
- }
-
- return itemView
- }
- }
- extension ComponentGroup: NSCollectionViewDelegateFlowLayout {
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
-
- var viewHeight: CGFloat = 36
- if indexPath.item < menuItemArr.count {
- let properties = menuItemArr[indexPath.item]
- if properties.type == .divider {
- viewHeight = 8
- }
- }
- return NSSize(width: NSWidth(self.collectionView.frame), height: viewHeight)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
- return NSSize(width: 0, height: 0)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
- return NSSize(width: 0, height: 0)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return 0
- }
-
- public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
- return NSEdgeInsetsMake(0, 0, 0, 0)
- }
-
- public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
-
- }
- }
|