123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- //
- // AutoTestAdvanceSettingView.swift
- // KdanAuto
- //
- // Created by 朱东勇 on 2022/11/25.
- //
- import Foundation
- import AppKit
- let kItemCountPerRow = 3
- public protocol AutoTestAdvanceSettingViewDelegate : NSObjectProtocol {
- func advanceSettingDidUpdate(_ settingView:NSView?)
- }
- class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDelegate, TestFileCellViewDelegate {
-
- @IBOutlet var _titleLbl : NSTextField!
- @IBOutlet var _keyScrollView : NSScrollView!
- @IBOutlet var _keyContentView : NSView!
-
- @IBOutlet var _fileList : NSTableView!
- @IBOutlet var _replaceAllBtn : NSButton!
-
- var _keyViews : NSMutableArray!
- var _autoTestObj : AutoTest?
-
- var _files : [String]! = []
-
- var delegate : AutoTestAdvanceSettingViewDelegate?
-
-
- override func awakeFromNib() {
- self.wantsLayer = true;
- self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
- }
-
-
- // Setter
- let kCheckBtnDefaultHeight = 25.0
- public func setAutoTestObj(_ obj:AutoTest?) {
- _autoTestObj = obj;
-
- // Set Title
- // self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true))
- //
- if nil != _autoTestObj {
- _titleLbl.stringValue = String("[\(_autoTestObj?.fileType() as! String)]\(_autoTestObj?.name() as! String)")
- let checkKeys = _autoTestObj?.keys() as! NSArray
- let selectKeys = _autoTestObj?.selectedKeys() as! NSArray
-
- if _keyViews == nil {
- _keyViews = NSMutableArray()
- }
- while (_keyViews.count != checkKeys.count) {
- if (_keyViews.count > checkKeys.count) {
- (_keyViews.lastObject as! NSButton).removeFromSuperview()
- _keyViews.removeLastObject()
- }else {
- let checkBtn = NSButton(checkboxWithTitle: "", target: self, action: #selector(keyChecked))
-
- _keyContentView.addSubview(checkBtn);
- _keyViews.add(checkBtn)
- }
- }
-
- var height = CGFloat((_keyViews.count + 1)/kItemCountPerRow * (Int(kCheckBtnDefaultHeight) + 5));
-
- // _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight)
- if (height > _keyScrollView.frame.height) {
- _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: height))
- }else {
- height = _keyScrollView.frame.height
- _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: _keyScrollView.frame.height))
- }
-
- for btn in _keyViews {
- let i = _keyViews.index(of: btn)
- let checkBtn = btn as! NSButton
-
- checkBtn.title = checkKeys[i] as! String;
-
- // Update Check Status
- if selectKeys.contains(checkKeys[i]) {
- checkBtn.state = .on
- }else {
- checkBtn.state = .off
- }
-
- checkBtn.sizeToFit()
-
- checkBtn.frame = CGRect.init(x: CGFloat(i % kItemCountPerRow) * _keyContentView.frame.size.width/CGFloat(kItemCountPerRow), y: CGFloat(height - kCheckBtnDefaultHeight), width: checkBtn.frame.width, height: kCheckBtnDefaultHeight)
- checkBtn.autoresizingMask = .maxXMargin.union(.minYMargin)
-
- if i % kItemCountPerRow == (kItemCountPerRow - 1) {
- height = height - checkBtn.frame.height - 5
- }
- }
-
- _keyScrollView.documentView = _keyContentView;
-
- _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
- type: String(_autoTestObj?.type() ?? ""))
- _fileList.reloadData()
-
- _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
- }else {
- _titleLbl.stringValue = ""
- if _keyViews == nil {
- _keyViews = NSMutableArray()
- }
- while (_keyViews.count != 0) {
- (_keyViews.lastObject as! NSView).removeFromSuperview()
- _keyViews.removeLastObject()
- }
-
- _files = []
- _fileList.reloadData()
- _replaceAllBtn.isEnabled = false
- }
- }
-
-
- /// IBActionn
- @IBAction func keyChecked(_ sender:NSButton) {
- var currentSelectedKeys = NSMutableArray()
-
- let checkKeys = _autoTestObj?.keys() as! NSArray
-
- for btn in _keyViews {
- let cBtn = btn as! NSButton
-
- if cBtn.state == .on {
- let index = _keyViews.index(of: btn)
-
- currentSelectedKeys.add(checkKeys[index])
- }
- }
-
- _autoTestObj?.setSelectedKeys(currentSelectedKeys)
-
- if delegate != nil {
- delegate?.advanceSettingDidUpdate(self)
- }
- }
-
- @IBAction func replaceAllAction(_ sender:NSButton) {
- if nil != _autoTestObj {
- _autoTestObj?.updateRefImage()
- }
- }
-
-
- // TableView Delegate
- func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
- let cellView = TestFileCellView.shared()
-
- if (_files.count >= row) {
- let title = _files[row]
-
- cellView?.setTitle(title)
- cellView?._delegate = self
-
- if _autoTestObj != nil {
- cellView?.setNeedReplaceBtn(_autoTestObj!.canUpdateRefImage(title))
- }else {
- cellView?.setNeedReplaceBtn(false)
- }
- }
-
- return cellView
- }
-
- func selectionShouldChange(in tableView: NSTableView) -> Bool {
-
- return true
- }
-
- func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
-
- return false
- }
-
- func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
-
- return false
- }
-
- func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
-
- }
-
- func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
-
- }
-
- func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
-
- }
-
- func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
- return 30
- }
-
- func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
- return false
- }
-
- func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
- return tableView.frame.width
- }
-
- func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
- return []
- }
-
- func tableViewSelectionDidChange(_ notification: Notification) {
-
- }
-
- // TableView Data Source
- func numberOfRows(in tableView: NSTableView) -> Int {
- return _files.count
- }
-
- // TestFileCellViewDelegate
- func fileCellNeedReplace(_ cell: TestFileCellView, fileName: String) {
- if (_autoTestObj != nil) {
- _autoTestObj?.updateRefImage(fileName)
- }
- _fileList.reloadData()
- }
-
- }
|