// // TeseCaseCell.swift // KdanAuto // // Created by 朱东勇 on 2022/11/21. // import Foundation import AppKit public protocol TestCaseCellViewDelegate : NSObjectProtocol { func selectKeyDidUpdate(_ cell:NSTableCellView?) } class TestCaseCellView : NSTableCellView, NSMenuDelegate { @IBOutlet var _titleLbl : NSTextField! @IBOutlet var _markLbl : NSTextField! @IBOutlet var _keyContentView : NSView! @IBOutlet var _sepLine : NSView! @IBOutlet var _activityView : ActivityView! @IBOutlet var _reportBtn : NSButton! @IBOutlet var _toggleBtn : NSButton! var _keyViews : NSMutableArray! var _autoTestObj : AutoTest? var _title : String! var _checkKeys : NSArray! // Menu Item var m_modifyItem:NSMenuItem! var m_clearOriginItem:NSMenuItem! var m_clearCheckItem:NSMenuItem! var m_clearResultItem:NSMenuItem! var m_deleteItem:NSMenuItem! var delegate : TestCaseCellViewDelegate? class func shared() -> TestCaseCellView? { var objects : NSArray! Bundle.main.loadNibNamed("TestCaseCellView", owner: nil, topLevelObjects: &objects) for tView in objects { if let tv = tView as? TestCaseCellView { return tv } } return nil } override func awakeFromNib() { _sepLine.wantsLayer = true; _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor // Add Advanced Setting Menu let _menu = NSMenu(title: "") m_modifyItem = NSMenuItem.init(title: "高级设置", action:#selector(self.setting(_:)), keyEquivalent: "") m_modifyItem.target = self; _menu.addItem(m_modifyItem) _menu.addItem(NSMenuItem.separator()) m_clearOriginItem = NSMenuItem.init(title: "清除原文件", action:#selector(self.clearOrigin(_:)), keyEquivalent: "") m_clearOriginItem.target = self; _menu.addItem(m_clearOriginItem) m_clearResultItem = NSMenuItem.init(title: "清除结果文件", action:#selector(self.clearResult(_:)), keyEquivalent: "") m_clearResultItem.target = self; _menu.addItem(m_clearResultItem) m_clearCheckItem = NSMenuItem.init(title: "清除对照文件", action:#selector(self.clearCheck(_:)), keyEquivalent: "") m_clearCheckItem.target = self; _menu.addItem(m_clearCheckItem) _menu.addItem(NSMenuItem.separator()) m_deleteItem = NSMenuItem.init(title: "删除测试项", action:#selector(self.delete(_:)), keyEquivalent: "") m_deleteItem.target = self; _menu.addItem(m_deleteItem) self.menu = _menu self.menu?.delegate = self; // Add right mouse event monitor NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask.rightMouseUp) { event in var point = event.locationInWindow; if (nil != self.window?.contentView) { point = self.window!.contentView!.convert(point, to: self) if (event.type == .rightMouseUp && CGRectContainsPoint(self.bounds, point) && (self.autoTestObj()?.status() == .Normal || self.autoTestObj()?.status() == .Finished)) { NSMenu.popUpContextMenu(self.menu!, with: event, for: self) return nil; } } return event } } // Setter & Getter public func setTitle(_ title:String) { _title = title; _titleLbl.stringValue = _title ?? "" let size = _titleLbl.sizeThatFits(_titleLbl.frame.size); _markLbl.frame = CGRectMake(_titleLbl.frame.origin.x + size.width, _titleLbl.frame.origin.y, _titleLbl.frame.size.width - size.width, _titleLbl.frame.size.height) } public func title() -> String? { return _title } let kTFDefaultHeight = 22.0 public func setCheckKeys(_ keys:NSArray) { _checkKeys = keys; if _keyViews == nil { _keyViews = NSMutableArray() } while (_keyViews.count != _checkKeys.count) { if (_keyViews.count > _checkKeys.count) { (_keyViews.lastObject as! NSTextField).removeFromSuperview() _keyViews.removeLastObject() }else { let tf = NSTextField.init(frame: CGRectMake(0, 4, 30, kTFDefaultHeight)); _keyContentView.addSubview(tf); tf.backgroundColor = .clear _keyViews.add(tf) } } var width = 0.0; // update key view's frame for tf in NSArray(array: _keyViews) { let i = _keyViews.index(of: tf) let tfView = tf as! NSTextField tfView.stringValue = _checkKeys[i] as! String; tfView.sizeToFit() tfView.setFrameOrigin(NSPoint.init(x: width, y: 0)) tfView.setFrameSize(NSSize.init(width: (tfView.frame.size.width.advanced(by: 2.0)), height: kTFDefaultHeight)); tfView.autoresizingMask = .maxXMargin.union(.maxYMargin) tfView.wantsLayer = true tfView.layer?.cornerRadius = 8 tfView.layer?.backgroundColor = NSColor.clear.cgColor tfView.layer?.borderColor = NSColor.lightGray.withAlphaComponent(0.6).cgColor tfView.layer?.borderWidth = 1 tfView.isEditable = false width = width + tfView.frame.size.width + 3 } // _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight) // _keyContentView.setFrameSize(NSSize.init(width: width, height: kTFDefaultHeight)) // update Toggle status if _checkKeys.count == 0 { _toggleBtn.image = NSImage(named: "Toggle_off.png") }else if _checkKeys.count != _autoTestObj?.keys().count { _toggleBtn.image = NSImage(named: "Toggle_half.png") }else { _toggleBtn.image = NSImage(named: "Toggle_on.png") } } public func checkKeys() -> NSArray? { return _checkKeys; } public func autoTestObj() -> AutoTest? { return _autoTestObj } public func setAutoTestObj(_ obj: AutoTest?) { _autoTestObj = obj // self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true)) if nil != _autoTestObj { self.setTitle((_autoTestObj?.name())!) self.setCheckKeys((_autoTestObj?.selectedKeys())!) // set the mark for (converter of) old library let useOldLibValue = _autoTestObj?.params().value(forKey: "useOldLib") var useOldLib = false if nil != useOldLibValue { if (useOldLibValue as? String) != nil { useOldLib = NSString(string: (useOldLibValue as! String)).boolValue }else if (useOldLibValue as? NSNumber) != nil { useOldLib = (useOldLibValue as! NSNumber).boolValue } } _markLbl.isHidden = useOldLib }else { self.setTitle("") self.setCheckKeys([]) _markLbl.isHidden = true; // self.wantsLayer = true // self.layer?.opacity = 0.5 } updateStatus() } public func updateStatus() { _activityView.setDegree(_autoTestObj?.degree() ?? 0) _activityView.setActivityStatus(ActivityStatus(rawValue: (_autoTestObj?.status())!.rawValue) ?? .Normal) _reportBtn.isHidden = (ActivityStatus(rawValue: (_autoTestObj?.status())!.rawValue) ?? .Normal) != .Finished } // IBAction @IBAction func toggleAction(_ sender:NSButton) { if _autoTestObj != nil { if _checkKeys.count == 0 { _autoTestObj?.setSelectedKeys((_autoTestObj?.keys())!) }else if _checkKeys.count != _autoTestObj?.keys().count { _autoTestObj?.setSelectedKeys((_autoTestObj?.keys())!) }else { _autoTestObj?.setSelectedKeys([]) } self.setCheckKeys((_autoTestObj?.selectedKeys())!) if delegate != nil { delegate?.selectKeyDidUpdate(self) } } } @IBAction func showCompareReportAction(_ sender:NSButton) { // show compare report let files = _autoTestObj?.compareFiles() ?? [] if nil != files && files.count > 0 { if (_autoTestObj as? StringAutoTest) != nil { var compareVC = StringCompareViewController.shared() compareVC.setFiles(files) let point = sender.convert(CGPoint(x: 0, y: 0), to: self.window?.contentView ?? self) compareVC.showIn(self.window?.contentView ?? self, rect: NSRect.init(origin: point, size: sender.frame.size)) }else { var compareVC = CompareViewController.shared() compareVC.setFiles(files) let point = sender.convert(CGPoint(x: 0, y: 0), to: self.window?.contentView ?? self) compareVC.showIn(self.window?.contentView ?? self, rect: NSRect.init(origin: point, size: sender.frame.size)) } } return } @IBAction func setting(_ sender:NSMenuItem) { // show property setting for current test case if (_autoTestObj != nil) { let vc = AdvanceSettingViewController.shared() let info = AdvanceSettingViewController.baseInfoFor(_autoTestObj!._fileType) if (info != nil) { vc.m_fileType = _autoTestObj!._fileType vc.setBaseInfo(info!) vc.setTestCase(_autoTestObj!.m_info as! NSDictionary) vc.m_testObj = _autoTestObj let callback:(_ testObj:AutoTest?, _ property:NSDictionary) -> () = { obj, property in obj?.update(property) } vc.setCallback(callback) let point = self.convert(CGPoint(x: 0, y: 0), to: self.window?.contentView ?? self) vc.showIn(self.window?.contentView ?? self, rect: NSRect.init(origin: point, size: self.frame.size)) } } } @IBAction func clearOrigin(_ sender:NSMenuItem) { // clear all origin files for current test case if (_autoTestObj != nil) { _autoTestObj?.clearOriginFiles() } } @IBAction func clearResult(_ sender:NSMenuItem) { // clear all result files for current test case if (_autoTestObj != nil) { _autoTestObj?.clearCacheFiles() } } @IBAction func clearCheck(_ sender:NSMenuItem) { // clear all refrence files for current test case if (_autoTestObj != nil) { _autoTestObj?.clearCompareFiles() } } @IBAction func delete(_ sender:NSMenuItem) { // delete current test case _autoTestObj?.delete() } }