// // KMProfileInfoWindowController.swift // PDF Reader Pro // // Created by tangchao on 2023/11/9. // import Cocoa class KMProfileInfoWindowController: KMBaseWindowController { var callBack: ((String) -> Void)? @IBOutlet var titleLabel: NSTextField! @IBOutlet var tableView: NSTableView! @IBOutlet var backButton: NSButton! @IBOutlet var addButton: NSButton! @IBOutlet var addNewInfoButton: NSButton! @IBOutlet var removeNewInfoButton: NSButton! var tableContents: [String] = [] private let TitleCellIdentifier = "TitleCell" private let NormalCellIdentifier = "Normal" override func windowDidLoad() { super.windowDidLoad() self._configuViews() self._fetchDisplayKeys() self.tableView.reloadData() } @IBAction func addFileName_Add(_ sender: NSButton) { self.window?.makeFirstResponder(nil) let tag = sender.tag let cellView = self.tableView.view(atColumn: 0, row: tag, makeIfNecessary: false) as? KMProfileCellView if let _ = cellView { if let rowV = self.tableView.rowView(atRow: tag, makeIfNecessary: false), rowV.isSelected == false { self.tableView.km_safe_selectRowIndexes(.init(integer: tag), byExtendingSelection: false) } } if let data = cellView?.isKind(of: KMProfileCellView.self), data { cellView?.addButton.isHidden = true cellView?.detailtextField.isHidden = false cellView?.detailtextField.becomeFirstResponder() } } @IBAction func buttonClicked_back(_ sender: AnyObject?) { guard let block = self.callBack else { return } block("") } @IBAction func buttonClicked_add(_ sender: AnyObject?) { let selectRow = self.tableView.selectedRow if (selectRow >= 0) { if (self.callBack != nil) { let cellView = self.tableView.view(atColumn: 0, row: selectRow, makeIfNecessary: false) as? KMProfileCellView self.callBack!(cellView?.detailtextField.stringValue ?? "") } } } @IBAction func buttonClicked_addNewInfo(_ sender: AnyObject?) { let dict: [String : String] = ["" : ""] KMProfileInfo.shared().insertObject(dict, inCustomInfoArrayAt: KMProfileInfo.shared().customInfoArray.count) self._fetchDisplayKeys() self.tableView.reloadData() if (self.tableView.numberOfRows > 1) { self.tableView.scrollRowToVisible(self.tableView.numberOfRows - 1) let set = NSIndexSet(index: self.tableView.numberOfRows - 1) self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false) let cellView = self.tableView.view(atColumn: 0, row: self.tableView.selectedRow, makeIfNecessary: false) as? KMProfileCellView cellView?.addButton.isHidden = true cellView?.textField?.becomeFirstResponder() } } @IBAction func buttonClicked_RemoveNewInfo(_ sender: AnyObject?) { let editIndex = self.tableView.selectedRow let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound if (editIndex - customIndex - 1 < KMProfileInfo.shared().customInfoArray.count) { KMProfileInfo.shared().removeObjectFromCustomInfoArray(at: editIndex - customIndex - 1) self._fetchDisplayKeys() self.tableView.reloadData() self.removeNewInfoButton.isEnabled = false } } // MARK: - NSMenu Action @IBAction func menuItemClicked_Edit(_ sender: AnyObject?) { let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? KMProfileCellView let set = IndexSet(integer: self.tableView.clickedRow) self.tableView.selectRowIndexes(set, byExtendingSelection: false) if let data = cellView?.textField?.isEditable, data { if let data = cellView?.textField?.stringValue.isEmpty, data { cellView?.textField?.becomeFirstResponder() }else { cellView?.detailtextField.becomeFirstResponder() } } else { cellView?.detailtextField.becomeFirstResponder() } } @IBAction func menuItemClicked_Add(_ sender: AnyObject?) { let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? KMProfileCellView if (self.callBack != nil) { self.callBack!(cellView?.detailtextField.stringValue ?? "") } } @IBAction func menuItemClicked_Delete(_ sender: AnyObject?) { let editIndex = self.tableView.clickedRow let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound if (editIndex < customIndex) { switch (editIndex) { case 1: KMProfileInfo.shared().fullName = "" break; case 2: KMProfileInfo.shared().firstName = "" break; case 3: KMProfileInfo.shared().middleName = "" break; case 4: KMProfileInfo.shared().lastName = "" break; case 6: KMProfileInfo.shared().street1 = "" break; case 7: KMProfileInfo.shared().street2 = "" break; case 8: KMProfileInfo.shared().city = "" break; case 9: KMProfileInfo.shared().state = "" break; case 10: KMProfileInfo.shared().zip = "" break; case 11: KMProfileInfo.shared().country = "" break; case 13: KMProfileInfo.shared().email = "" break; case 14: KMProfileInfo.shared().tel = "" break; case 16: // [KMProfileInfo.shared().date = @""; break; case 17: KMProfileInfo.shared().birthDate = "" break; default: break; } } else { KMProfileInfo.shared().removeObjectFromCustomInfoArray(at: editIndex - customIndex - 1) } self._fetchDisplayKeys() self.tableView.reloadData() } override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) { super.interfaceThemeDidChanged(appearance) DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self._updateViewColor() } } } // MARK: - Private Methods extension KMProfileInfoWindowController { private func _configuViews() { self.titleLabel.stringValue = KMLocalizedString("My Profile", nil) //Button Part self.backButton.title = KMLocalizedString("Cancel", nil) self.addButton.title = KMLocalizedString("Add to Doc", nil) self.addButton.isEnabled = false self.removeNewInfoButton.isEnabled = false //NSMenu Part let editItem = self.tableView.menu?.item(at: 0) let addItem = self.tableView.menu?.item(at: 1) let deleteItem = self.tableView.menu?.item(at: 2) editItem?.title = KMLocalizedString("Edit", nil) addItem?.title = KMLocalizedString("Add to Doc", nil) deleteItem?.title = KMLocalizedString("Delete", nil) } private func _fetchDisplayKeys() { self.tableContents = [] self.tableContents.append(KMLocalizedString("Name", nil)) self.tableContents.append(KMLocalizedString("Full Name", nil)) self.tableContents.append(KMLocalizedString("First Name", nil)) self.tableContents.append(KMLocalizedString("Middle Name", nil)) self.tableContents.append(KMLocalizedString("Last Name", nil)) self.tableContents.append(KMLocalizedString("Address", nil)) self.tableContents.append(KMLocalizedString("Street 1", nil)) self.tableContents.append(KMLocalizedString("Street 2", nil)) self.tableContents.append(KMLocalizedString("City", nil)) self.tableContents.append(KMLocalizedString("State", nil)) self.tableContents.append(KMLocalizedString("Zip", nil)) self.tableContents.append(KMLocalizedString("Country", nil)) self.tableContents.append(KMLocalizedString("Contact Information", nil)) self.tableContents.append(KMLocalizedString("Email", nil)) self.tableContents.append(KMLocalizedString("Tel", nil)) self.tableContents.append(KMLocalizedString("Dates", nil)) self.tableContents.append(KMLocalizedString("Date", nil)) self.tableContents.append(KMLocalizedString("Birth Date", nil)) if (KMProfileInfo.shared().customInfoArray.count > 0) { self.tableContents.append(KMLocalizedString("Custom", nil)) for i in 0 ..< KMProfileInfo.shared().customInfoArray.count { if let dict: [String : Any] = KMProfileInfo.shared().customInfoArray[i] as? [String : Any], dict.isEmpty == false { self.tableContents.append(dict.keys.first!) } } } } private func _updateViewColor() { self.tableView.reloadData() } private func _configuTextField(_ cellView: KMProfileCellView?) { let textField = cellView?.detailtextField let tag = textField?.tag ?? 0 var placeHoldString: String = "" var nameString: String = "" if (tag == 1) { placeHoldString = KMLocalizedString("Add Full Name", nil) nameString = KMProfileInfo.shared().fullName } else if (tag == 2) { placeHoldString = KMLocalizedString("Add First Name", nil) nameString = KMProfileInfo.shared().firstName } else if (tag == 3) { placeHoldString = KMLocalizedString("Add Middle Name", nil) nameString = KMProfileInfo.shared().middleName } else if (tag == 4) { placeHoldString = KMLocalizedString("Add Last Name", nil) nameString = KMProfileInfo.shared().lastName } else if (tag == 6) { placeHoldString = KMLocalizedString("Add Street 1", nil) nameString = KMProfileInfo.shared().street1 } else if (tag == 7) { placeHoldString = KMLocalizedString("Add Street 2", nil) nameString = KMProfileInfo.shared().street2 } else if (tag == 8) { placeHoldString = KMLocalizedString("Add City", nil) nameString = KMProfileInfo.shared().city } else if (tag == 9) { placeHoldString = KMLocalizedString("Add State", nil) nameString = KMProfileInfo.shared().state } else if (tag == 10) { placeHoldString = KMLocalizedString("Add Zip", nil) nameString = KMProfileInfo.shared().zip } else if (tag == 11) { placeHoldString = KMLocalizedString("Add Country", nil) nameString = KMProfileInfo.shared().country } else if (tag == 13) { placeHoldString = KMLocalizedString("Add Email", nil) nameString = KMProfileInfo.shared().email } else if (tag == 14) { placeHoldString = KMLocalizedString("Add Tel", nil) nameString = KMProfileInfo.shared().tel } else if (tag == 16) { placeHoldString = KMLocalizedString("Add Date", nil) let date = Date() let formatter = DateFormatter() formatter.dateFormat = "YYYY/MM/dd" nameString = formatter.string(from: date) } else if (tag == 17) { placeHoldString = KMLocalizedString("Add Birth Date", nil) nameString = KMProfileInfo.shared().birthDate } textField?.stringValue = nameString if (nameString.isEmpty == false) { textField?.isHidden = false cellView?.addButton.isHidden = true } else { cellView?.addButton.title = placeHoldString textField?.placeholderString = placeHoldString textField?.isHidden = true cellView?.addButton.tag = tag cellView?.addButton.target = self cellView?.addButton.action = #selector(addFileName_Add) cellView?.addButton.setTitleColor(NSColor.systemBlue) cellView?.addButton.isHidden = false } } } extension KMProfileInfoWindowController: NSTableViewDelegate, NSTableViewDataSource { func numberOfRows(in tableView: NSTableView) -> Int { self.tableContents.count } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { let addingString = self.tableContents[row] let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex { return 24.0 } return 36.0 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let addingString = self.tableContents[row] let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound var tableCellView: NSTableCellView? if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex { tableCellView = (tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: TitleCellIdentifier), owner: tableColumn) as! NSTableCellView) let cellView = tableCellView as! KMProfileTitleCellView cellView.textField?.stringValue = addingString cellView.textField?.isEditable = false cellView.wantsLayer = true if (KMAppearance.isSupportNewColor()) { if (KMAppearance.isDarkMode()) { cellView.layer?.backgroundColor = NSColor(red: 73/255.0, green: 75/255.0, blue: 77/255.0, alpha: 1).cgColor } else { cellView.layer?.backgroundColor = NSColor(red: 219/255.0, green: 219/255.0, blue: 219/255.0, alpha: 1).cgColor } } else { cellView.layer?.backgroundColor = NSColor(red: 235.0/255.0, green: 235.0/255.0, blue: 235.0/255.0, alpha: 1).cgColor } } else { tableCellView = (tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: NormalCellIdentifier), owner: tableColumn) as! NSTableCellView) let cellView = tableCellView as? KMProfileCellView cellView?.textField?.stringValue = addingString if let data = cellView?.isKind(of: KMProfileCellView.self), data { cellView?.detailtextField.isHidden = false } cellView?.textField?.textColor = KMAppearance.Layout.h0Color() if (customIndex > row) { cellView?.detailtextField.tag = row self._configuTextField(cellView) cellView?.textField?.isEditable = false cellView?.textField?.isSelectable = false if addingString == KMLocalizedString("Date", nil) { cellView?.detailtextField.isEditable = false cellView?.detailtextField.isSelectable = false } else { cellView?.detailtextField.isEditable = true cellView?.detailtextField.isSelectable = true } cellView?.detailtextField.delegate = self } else { let index = row - customIndex - 1 let dict = KMProfileInfo.shared().customInfoArray[index] as? [String : Any] cellView?.addButton.isHidden = true cellView?.detailtextField.stringValue = dict?.values.first as? String ?? "" cellView?.detailtextField.placeholderString = KMLocalizedString("Type Value Here", nil) cellView?.textField?.placeholderString = KMLocalizedString("Type Label Here", nil) cellView?.textField?.isEditable = true cellView?.textField?.isSelectable = true cellView?.detailtextField.isEditable = true cellView?.detailtextField.isSelectable = true cellView?.textField?.delegate = self cellView?.detailtextField.delegate = self } } return tableCellView; } func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool { let addingString = self.tableContents[row] let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex { return false } return true } func tableViewSelectionDidChange(_ notification: Notification) { if (self.tableView.selectedRow == NSNotFound || self.tableView.selectedRow == -1) { return } let cellView = self.tableView.view(atColumn: 0, row: self.tableView.selectedRow, makeIfNecessary: false) as? KMProfileCellView self.addButton.isEnabled = cellView?.detailtextField.stringValue.isEmpty == false for i in 0 ..< self.tableView.numberOfRows { let cellView = self.tableView.view(atColumn: 0, row: i, makeIfNecessary: false) as? NSTableCellView if let data = cellView?.isKind(of: KMProfileCellView.self), data { let cellView1 = cellView as! KMProfileCellView if (i == self.tableView.selectedRow) { // cellView1.detailtextField.textColor = .white cellView1.addButton.setTitleColor(NSColor.white) } else { // cellView1.detailtextField.textColor = NSColor.secondaryLabelColor cellView1.addButton.setTitleColor(NSColor.systemBlue) } } } let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound if (self.tableView.selectedRow > customIndex) { self.removeNewInfoButton.isEnabled = true } else { self.removeNewInfoButton.isEnabled = false } } } extension KMProfileInfoWindowController: NSTextFieldDelegate { func controlTextDidChange(_ obj: Notification) { let currentRow = self.tableView.selectedRow if (currentRow == -1) { return } guard let textField = obj.object as? NSTextField else { return } let cellView = self.tableView.view(atColumn: 0, row: currentRow, makeIfNecessary: false) as? KMProfileCellView if (currentRow == 1) { KMProfileInfo.shared().fullName = textField.stringValue } else if (currentRow == 2) { KMProfileInfo.shared().firstName = textField.stringValue } else if (currentRow == 3) { KMProfileInfo.shared().middleName = textField.stringValue } else if (currentRow == 4) { KMProfileInfo.shared().lastName = textField.stringValue } else if (currentRow == 6) { KMProfileInfo.shared().street1 = textField.stringValue } else if (currentRow == 7) { KMProfileInfo.shared().street2 = textField.stringValue } else if (currentRow == 8) { KMProfileInfo.shared().city = textField.stringValue } else if (currentRow == 9) { KMProfileInfo.shared().state = textField.stringValue } else if (currentRow == 10) { KMProfileInfo.shared().zip = textField.stringValue } else if (currentRow == 11) { KMProfileInfo.shared().country = textField.stringValue } else if (currentRow == 13) { KMProfileInfo.shared().email = textField.stringValue } else if (currentRow == 14) { KMProfileInfo.shared().tel = textField.stringValue } else if (currentRow == 16) { // [KMProfileInfo.shared().date = textField.stringValue; } else if (currentRow == 17) { KMProfileInfo.shared().birthDate = textField.stringValue } else if (currentRow >= 19 ) { let customIndex = currentRow - 19 if (cellView != nil) { let dict = KMProfileInfo.shared().customInfoArray[customIndex] as? [String : Any] let originalKey = dict?.keys.first ?? "" let originalValue = dict?.values.first as? String ?? "" if textField.isEqual(cellView?.textField) { let newDict = [textField.stringValue : originalValue] KMProfileInfo.shared().replaceObjectInCustomInfoArray(at: customIndex, with: newDict) } else if textField.isEqual(cellView?.detailtextField) { let newDict = [originalKey : textField.stringValue] KMProfileInfo.shared().replaceObjectInCustomInfoArray(at: customIndex, with: newDict) } } } if cellView?.detailtextField.stringValue.isEmpty == false { self.addButton.isEnabled = true } else { self.addButton.isEnabled = false } } func controlTextDidBeginEditing(_ obj: Notification) { let textField = obj.object as? NSTextField textField?.textColor = NSColor.labelColor } func controlTextDidEndEditing(_ obj: Notification) { let textField = obj.object as? NSTextField let tag = textField?.tag ?? 0 let cellView = self.tableView.view(atColumn: 0, row: tag, makeIfNecessary: false) as? KMProfileCellView if let data = cellView?.isKind(of: KMProfileCellView.self), data, tag > 0 { var placeHoldString = "" if (tag == 1) { placeHoldString = KMLocalizedString("Add Full Name", nil) } else if (tag == 2) { placeHoldString = KMLocalizedString("Add First Name", nil) } else if (tag == 3) { placeHoldString = KMLocalizedString("Add Middle Name", nil) } else if (tag == 4) { placeHoldString = KMLocalizedString("Add Last Name", nil) } else if (tag == 6) { placeHoldString = KMLocalizedString("Add Street 1", nil) } else if (tag == 7) { placeHoldString = KMLocalizedString("Add Street 2", nil) } else if (tag == 8) { placeHoldString = KMLocalizedString("Add City", nil) } else if (tag == 9) { placeHoldString = KMLocalizedString("Add State", nil) } else if (tag == 10) { placeHoldString = KMLocalizedString("Add Zip", nil) } else if (tag == 11) { placeHoldString = KMLocalizedString("Add Country", nil) } else if (tag == 13) { placeHoldString = KMLocalizedString("Add Email", nil) } else if (tag == 14) { placeHoldString = KMLocalizedString("Add Tel", nil) } else if (tag == 16) { placeHoldString = KMLocalizedString("Add Date", nil) } else if (tag == 17) { placeHoldString = KMLocalizedString("Add Birth Date", nil) } if (textField?.stringValue.isEmpty == false) { textField?.textColor = NSColor.secondaryLabelColor textField?.isHidden = false cellView?.addButton.isHidden = true } else { // textField.hidden = YES; //自定义新增,输入Label后,value为空时推出编辑,就无法填写了 textField?.placeholderString = "" cellView?.addButton.isHidden = false cellView?.addButton.tag = tag cellView?.addButton.target = self cellView?.addButton.action = #selector(addFileName_Add) cellView?.addButton.title = placeHoldString cellView?.addButton.setTitleColor(NSColor.systemBlue) } } } } extension KMProfileInfoWindowController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? NSTableCellView if (menuItem.action == #selector(menuItemClicked_Edit)) { if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data { return false } else { let cellView1 = cellView as? KMProfileCellView if (cellView1?.textField?.stringValue == KMLocalizedString("Date", nil)) { return false } return true } } else if (menuItem.action == #selector(menuItemClicked_Add)) { // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) { // return NO; // } else { // KMProfileCellView *cell = (KMProfileCellView *)cellView; // return cell.detailtextField.stringValue.length > 0; // } if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data { return false } else { let cellView1 = cellView as? KMProfileCellView if let data = cellView1?.detailtextField?.stringValue.isEmpty, data { return false } return true } } else if (menuItem.action == #selector(menuItemClicked_Delete)) { // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) { // return NO; // } else { // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView; // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) { // return NO; // } // return YES; // } if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data { return false } else { let cellView1 = cellView as? KMProfileCellView if (cellView1?.textField?.stringValue == KMLocalizedString("Date", nil)) { return false } return true } } return false } }