KMProfileInfoWindowController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //
  2. // KMProfileInfoWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/9.
  6. //
  7. import Cocoa
  8. class KMProfileInfoWindowController: KMBaseWindowController {
  9. var callBack: ((String) -> Void)?
  10. @IBOutlet var titleLabel: NSTextField!
  11. @IBOutlet var tableView: NSTableView!
  12. @IBOutlet var backButton: NSButton!
  13. @IBOutlet var addButton: NSButton!
  14. @IBOutlet var addNewInfoButton: NSButton!
  15. @IBOutlet var removeNewInfoButton: NSButton!
  16. var tableContents: [String] = []
  17. private let TitleCellIdentifier = "TitleCell"
  18. private let NormalCellIdentifier = "Normal"
  19. override func windowDidLoad() {
  20. super.windowDidLoad()
  21. self._configuViews()
  22. self._fetchDisplayKeys()
  23. self.tableView.reloadData()
  24. }
  25. @IBAction func addFileName_Add(_ sender: NSButton) {
  26. self.window?.makeFirstResponder(nil)
  27. let tag = sender.tag
  28. let cellView = self.tableView.view(atColumn: 0, row: tag, makeIfNecessary: false) as? KMProfileCellView
  29. if let _ = cellView {
  30. if let rowV = self.tableView.rowView(atRow: tag, makeIfNecessary: false), rowV.isSelected == false {
  31. self.tableView.km_safe_selectRowIndexes(.init(integer: tag), byExtendingSelection: false)
  32. }
  33. }
  34. if let data = cellView?.isKind(of: KMProfileCellView.self), data {
  35. cellView?.addButton.isHidden = true
  36. cellView?.detailtextField.isHidden = false
  37. cellView?.detailtextField.becomeFirstResponder()
  38. }
  39. }
  40. @IBAction func buttonClicked_back(_ sender: AnyObject?) {
  41. guard let block = self.callBack else {
  42. return
  43. }
  44. block("")
  45. }
  46. @IBAction func buttonClicked_add(_ sender: AnyObject?) {
  47. let selectRow = self.tableView.selectedRow
  48. if (selectRow >= 0) {
  49. if (self.callBack != nil) {
  50. let cellView = self.tableView.view(atColumn: 0, row: selectRow, makeIfNecessary: false) as? KMProfileCellView
  51. self.callBack!(cellView?.detailtextField.stringValue ?? "")
  52. }
  53. }
  54. }
  55. @IBAction func buttonClicked_addNewInfo(_ sender: AnyObject?) {
  56. let dict: [String : String] = ["" : ""]
  57. KMProfileInfo.shared().insertObject(dict, inCustomInfoArrayAt: KMProfileInfo.shared().customInfoArray.count)
  58. self._fetchDisplayKeys()
  59. self.tableView.reloadData()
  60. if (self.tableView.numberOfRows > 1) {
  61. self.tableView.scrollRowToVisible(self.tableView.numberOfRows - 1)
  62. let set = NSIndexSet(index: self.tableView.numberOfRows - 1)
  63. self.tableView.selectRowIndexes(set as IndexSet, byExtendingSelection: false)
  64. let cellView = self.tableView.view(atColumn: 0, row: self.tableView.selectedRow, makeIfNecessary: false) as? KMProfileCellView
  65. cellView?.addButton.isHidden = true
  66. cellView?.textField?.becomeFirstResponder()
  67. }
  68. }
  69. @IBAction func buttonClicked_RemoveNewInfo(_ sender: AnyObject?) {
  70. let editIndex = self.tableView.selectedRow
  71. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  72. if (editIndex - customIndex - 1 < KMProfileInfo.shared().customInfoArray.count) {
  73. KMProfileInfo.shared().removeObjectFromCustomInfoArray(at: editIndex - customIndex - 1)
  74. self._fetchDisplayKeys()
  75. self.tableView.reloadData()
  76. self.removeNewInfoButton.isEnabled = false
  77. }
  78. }
  79. // MARK: - NSMenu Action
  80. @IBAction func menuItemClicked_Edit(_ sender: AnyObject?) {
  81. let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? KMProfileCellView
  82. let set = IndexSet(integer: self.tableView.clickedRow)
  83. self.tableView.selectRowIndexes(set, byExtendingSelection: false)
  84. if let data = cellView?.textField?.isEditable, data {
  85. if let data = cellView?.textField?.stringValue.isEmpty, data {
  86. cellView?.textField?.becomeFirstResponder()
  87. }else {
  88. cellView?.detailtextField.becomeFirstResponder()
  89. }
  90. } else {
  91. cellView?.detailtextField.becomeFirstResponder()
  92. }
  93. }
  94. @IBAction func menuItemClicked_Add(_ sender: AnyObject?) {
  95. let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? KMProfileCellView
  96. if (self.callBack != nil) {
  97. self.callBack!(cellView?.detailtextField.stringValue ?? "")
  98. }
  99. }
  100. @IBAction func menuItemClicked_Delete(_ sender: AnyObject?) {
  101. let editIndex = self.tableView.clickedRow
  102. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  103. if (editIndex < customIndex) {
  104. switch (editIndex) {
  105. case 1:
  106. KMProfileInfo.shared().fullName = ""
  107. break;
  108. case 2:
  109. KMProfileInfo.shared().firstName = ""
  110. break;
  111. case 3:
  112. KMProfileInfo.shared().middleName = ""
  113. break;
  114. case 4:
  115. KMProfileInfo.shared().lastName = ""
  116. break;
  117. case 6:
  118. KMProfileInfo.shared().street1 = ""
  119. break;
  120. case 7:
  121. KMProfileInfo.shared().street2 = ""
  122. break;
  123. case 8:
  124. KMProfileInfo.shared().city = ""
  125. break;
  126. case 9:
  127. KMProfileInfo.shared().state = ""
  128. break;
  129. case 10:
  130. KMProfileInfo.shared().zip = ""
  131. break;
  132. case 11:
  133. KMProfileInfo.shared().country = ""
  134. break;
  135. case 13:
  136. KMProfileInfo.shared().email = ""
  137. break;
  138. case 14:
  139. KMProfileInfo.shared().tel = ""
  140. break;
  141. case 16:
  142. // [KMProfileInfo.shared().date = @"";
  143. break;
  144. case 17:
  145. KMProfileInfo.shared().birthDate = ""
  146. break;
  147. default:
  148. break;
  149. }
  150. } else {
  151. KMProfileInfo.shared().removeObjectFromCustomInfoArray(at: editIndex - customIndex - 1)
  152. }
  153. self._fetchDisplayKeys()
  154. self.tableView.reloadData()
  155. }
  156. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  157. super.interfaceThemeDidChanged(appearance)
  158. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  159. self._updateViewColor()
  160. }
  161. }
  162. }
  163. // MARK: - Private Methods
  164. extension KMProfileInfoWindowController {
  165. private func _configuViews() {
  166. self.titleLabel.stringValue = KMLocalizedString("My Profile", nil)
  167. //Button Part
  168. self.backButton.title = KMLocalizedString("Cancel", nil)
  169. self.addButton.title = KMLocalizedString("Add to Doc", nil)
  170. self.addButton.isEnabled = false
  171. self.removeNewInfoButton.isEnabled = false
  172. //NSMenu Part
  173. let editItem = self.tableView.menu?.item(at: 0)
  174. let addItem = self.tableView.menu?.item(at: 1)
  175. let deleteItem = self.tableView.menu?.item(at: 2)
  176. editItem?.title = KMLocalizedString("Edit", nil)
  177. addItem?.title = KMLocalizedString("Add to Doc", nil)
  178. deleteItem?.title = KMLocalizedString("Delete", nil)
  179. }
  180. private func _fetchDisplayKeys() {
  181. self.tableContents = []
  182. self.tableContents.append(KMLocalizedString("Name", nil))
  183. self.tableContents.append(KMLocalizedString("Full Name", nil))
  184. self.tableContents.append(KMLocalizedString("First Name", nil))
  185. self.tableContents.append(KMLocalizedString("Middle Name", nil))
  186. self.tableContents.append(KMLocalizedString("Last Name", nil))
  187. self.tableContents.append(KMLocalizedString("Address", nil))
  188. self.tableContents.append(KMLocalizedString("Street 1", nil))
  189. self.tableContents.append(KMLocalizedString("Street 2", nil))
  190. self.tableContents.append(KMLocalizedString("City", nil))
  191. self.tableContents.append(KMLocalizedString("State", nil))
  192. self.tableContents.append(KMLocalizedString("Zip", nil))
  193. self.tableContents.append(KMLocalizedString("Country", nil))
  194. self.tableContents.append(KMLocalizedString("Contact Information", nil))
  195. self.tableContents.append(KMLocalizedString("Email", nil))
  196. self.tableContents.append(KMLocalizedString("Tel", nil))
  197. self.tableContents.append(KMLocalizedString("Dates", nil))
  198. self.tableContents.append(KMLocalizedString("Date", nil))
  199. self.tableContents.append(KMLocalizedString("Birth Date", nil))
  200. if (KMProfileInfo.shared().customInfoArray.count > 0) {
  201. self.tableContents.append(KMLocalizedString("Custom", nil))
  202. for i in 0 ..< KMProfileInfo.shared().customInfoArray.count {
  203. if let dict: [String : Any] = KMProfileInfo.shared().customInfoArray[i] as? [String : Any], dict.isEmpty == false {
  204. self.tableContents.append(dict.keys.first!)
  205. }
  206. }
  207. }
  208. }
  209. private func _updateViewColor() {
  210. self.tableView.reloadData()
  211. }
  212. private func _configuTextField(_ cellView: KMProfileCellView?) {
  213. let textField = cellView?.detailtextField
  214. let tag = textField?.tag ?? 0
  215. var placeHoldString: String = ""
  216. var nameString: String = ""
  217. if (tag == 1) {
  218. placeHoldString = KMLocalizedString("Add Full Name", nil)
  219. nameString = KMProfileInfo.shared().fullName
  220. } else if (tag == 2) {
  221. placeHoldString = KMLocalizedString("Add First Name", nil)
  222. nameString = KMProfileInfo.shared().firstName
  223. } else if (tag == 3) {
  224. placeHoldString = KMLocalizedString("Add Middle Name", nil)
  225. nameString = KMProfileInfo.shared().middleName
  226. } else if (tag == 4) {
  227. placeHoldString = KMLocalizedString("Add Last Name", nil)
  228. nameString = KMProfileInfo.shared().lastName
  229. } else if (tag == 6) {
  230. placeHoldString = KMLocalizedString("Add Street 1", nil)
  231. nameString = KMProfileInfo.shared().street1
  232. } else if (tag == 7) {
  233. placeHoldString = KMLocalizedString("Add Street 2", nil)
  234. nameString = KMProfileInfo.shared().street2
  235. } else if (tag == 8) {
  236. placeHoldString = KMLocalizedString("Add City", nil)
  237. nameString = KMProfileInfo.shared().city
  238. } else if (tag == 9) {
  239. placeHoldString = KMLocalizedString("Add State", nil)
  240. nameString = KMProfileInfo.shared().state
  241. } else if (tag == 10) {
  242. placeHoldString = KMLocalizedString("Add Zip", nil)
  243. nameString = KMProfileInfo.shared().zip
  244. } else if (tag == 11) {
  245. placeHoldString = KMLocalizedString("Add Country", nil)
  246. nameString = KMProfileInfo.shared().country
  247. } else if (tag == 13) {
  248. placeHoldString = KMLocalizedString("Add Email", nil)
  249. nameString = KMProfileInfo.shared().email
  250. } else if (tag == 14) {
  251. placeHoldString = KMLocalizedString("Add Tel", nil)
  252. nameString = KMProfileInfo.shared().tel
  253. } else if (tag == 16) {
  254. placeHoldString = KMLocalizedString("Add Date", nil)
  255. let date = Date()
  256. let formatter = DateFormatter()
  257. formatter.dateFormat = "YYYY/MM/dd"
  258. nameString = formatter.string(from: date)
  259. } else if (tag == 17) {
  260. placeHoldString = KMLocalizedString("Add Birth Date", nil)
  261. nameString = KMProfileInfo.shared().birthDate
  262. }
  263. textField?.stringValue = nameString
  264. if (nameString.isEmpty == false) {
  265. textField?.isHidden = false
  266. cellView?.addButton.isHidden = true
  267. } else {
  268. cellView?.addButton.title = placeHoldString
  269. textField?.placeholderString = placeHoldString
  270. textField?.isHidden = true
  271. cellView?.addButton.tag = tag
  272. cellView?.addButton.target = self
  273. cellView?.addButton.action = #selector(addFileName_Add)
  274. cellView?.addButton.setTitleColor(NSColor.systemBlue)
  275. cellView?.addButton.isHidden = false
  276. }
  277. }
  278. }
  279. extension KMProfileInfoWindowController: NSTableViewDelegate, NSTableViewDataSource {
  280. func numberOfRows(in tableView: NSTableView) -> Int {
  281. self.tableContents.count
  282. }
  283. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  284. let addingString = self.tableContents[row]
  285. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  286. if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex {
  287. return 24.0
  288. }
  289. return 36.0
  290. }
  291. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  292. let addingString = self.tableContents[row]
  293. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  294. var tableCellView: NSTableCellView?
  295. if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex {
  296. tableCellView = (tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: TitleCellIdentifier), owner: tableColumn) as! NSTableCellView)
  297. let cellView = tableCellView as! KMProfileTitleCellView
  298. cellView.textField?.stringValue = addingString
  299. cellView.textField?.isEditable = false
  300. cellView.wantsLayer = true
  301. if (KMAppearance.isSupportNewColor()) {
  302. if (KMAppearance.isDarkMode()) {
  303. cellView.layer?.backgroundColor = NSColor(red: 73/255.0, green: 75/255.0, blue: 77/255.0, alpha: 1).cgColor
  304. } else {
  305. cellView.layer?.backgroundColor = NSColor(red: 219/255.0, green: 219/255.0, blue: 219/255.0, alpha: 1).cgColor
  306. }
  307. } else {
  308. cellView.layer?.backgroundColor = NSColor(red: 235.0/255.0, green: 235.0/255.0, blue: 235.0/255.0, alpha: 1).cgColor
  309. }
  310. } else {
  311. tableCellView = (tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: NormalCellIdentifier), owner: tableColumn) as! NSTableCellView)
  312. let cellView = tableCellView as? KMProfileCellView
  313. cellView?.textField?.stringValue = addingString
  314. if let data = cellView?.isKind(of: KMProfileCellView.self), data {
  315. cellView?.detailtextField.isHidden = false
  316. }
  317. cellView?.textField?.textColor = KMAppearance.Layout.h0Color()
  318. if (customIndex > row) {
  319. cellView?.detailtextField.tag = row
  320. self._configuTextField(cellView)
  321. cellView?.textField?.isEditable = false
  322. cellView?.textField?.isSelectable = false
  323. if addingString == KMLocalizedString("Date", nil) {
  324. cellView?.detailtextField.isEditable = false
  325. cellView?.detailtextField.isSelectable = false
  326. } else {
  327. cellView?.detailtextField.isEditable = true
  328. cellView?.detailtextField.isSelectable = true
  329. }
  330. cellView?.detailtextField.delegate = self
  331. } else {
  332. let index = row - customIndex - 1
  333. let dict = KMProfileInfo.shared().customInfoArray[index] as? [String : Any]
  334. cellView?.addButton.isHidden = true
  335. cellView?.detailtextField.stringValue = dict?.values.first as? String ?? ""
  336. cellView?.detailtextField.placeholderString = KMLocalizedString("Type Value Here", nil)
  337. cellView?.textField?.placeholderString = KMLocalizedString("Type Label Here", nil)
  338. cellView?.textField?.isEditable = true
  339. cellView?.textField?.isSelectable = true
  340. cellView?.detailtextField.isEditable = true
  341. cellView?.detailtextField.isSelectable = true
  342. cellView?.textField?.delegate = self
  343. cellView?.detailtextField.delegate = self
  344. }
  345. }
  346. return tableCellView;
  347. }
  348. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  349. let addingString = self.tableContents[row]
  350. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  351. if (addingString == KMLocalizedString("Name", nil) || addingString == KMLocalizedString("Address", nil) || addingString == KMLocalizedString("Contact Information", nil) || addingString == KMLocalizedString("Dates", nil) || addingString == KMLocalizedString("Custom", nil)) && row <= customIndex {
  352. return false
  353. }
  354. return true
  355. }
  356. func tableViewSelectionDidChange(_ notification: Notification) {
  357. if (self.tableView.selectedRow == NSNotFound || self.tableView.selectedRow == -1) {
  358. return
  359. }
  360. let cellView = self.tableView.view(atColumn: 0, row: self.tableView.selectedRow, makeIfNecessary: false) as? KMProfileCellView
  361. self.addButton.isEnabled = cellView?.detailtextField.stringValue.isEmpty == false
  362. for i in 0 ..< self.tableView.numberOfRows {
  363. let cellView = self.tableView.view(atColumn: 0, row: i, makeIfNecessary: false) as? NSTableCellView
  364. if let data = cellView?.isKind(of: KMProfileCellView.self), data {
  365. let cellView1 = cellView as! KMProfileCellView
  366. if (i == self.tableView.selectedRow) {
  367. // cellView1.detailtextField.textColor = .white
  368. cellView1.addButton.setTitleColor(NSColor.white)
  369. } else {
  370. // cellView1.detailtextField.textColor = NSColor.secondaryLabelColor
  371. cellView1.addButton.setTitleColor(NSColor.systemBlue)
  372. }
  373. }
  374. }
  375. let customIndex = self.tableContents.index(of: KMLocalizedString("Custom", nil)) ?? NSNotFound
  376. if (self.tableView.selectedRow > customIndex) {
  377. self.removeNewInfoButton.isEnabled = true
  378. } else {
  379. self.removeNewInfoButton.isEnabled = false
  380. }
  381. }
  382. }
  383. extension KMProfileInfoWindowController: NSTextFieldDelegate {
  384. func controlTextDidChange(_ obj: Notification) {
  385. let currentRow = self.tableView.selectedRow
  386. if (currentRow == -1) {
  387. return
  388. }
  389. guard let textField = obj.object as? NSTextField else {
  390. return
  391. }
  392. let cellView = self.tableView.view(atColumn: 0, row: currentRow, makeIfNecessary: false) as? KMProfileCellView
  393. if (currentRow == 1) {
  394. KMProfileInfo.shared().fullName = textField.stringValue
  395. } else if (currentRow == 2) {
  396. KMProfileInfo.shared().firstName = textField.stringValue
  397. } else if (currentRow == 3) {
  398. KMProfileInfo.shared().middleName = textField.stringValue
  399. } else if (currentRow == 4) {
  400. KMProfileInfo.shared().lastName = textField.stringValue
  401. } else if (currentRow == 6) {
  402. KMProfileInfo.shared().street1 = textField.stringValue
  403. } else if (currentRow == 7) {
  404. KMProfileInfo.shared().street2 = textField.stringValue
  405. } else if (currentRow == 8) {
  406. KMProfileInfo.shared().city = textField.stringValue
  407. } else if (currentRow == 9) {
  408. KMProfileInfo.shared().state = textField.stringValue
  409. } else if (currentRow == 10) {
  410. KMProfileInfo.shared().zip = textField.stringValue
  411. } else if (currentRow == 11) {
  412. KMProfileInfo.shared().country = textField.stringValue
  413. } else if (currentRow == 13) {
  414. KMProfileInfo.shared().email = textField.stringValue
  415. } else if (currentRow == 14) {
  416. KMProfileInfo.shared().tel = textField.stringValue
  417. } else if (currentRow == 16) {
  418. // [KMProfileInfo.shared().date = textField.stringValue;
  419. } else if (currentRow == 17) {
  420. KMProfileInfo.shared().birthDate = textField.stringValue
  421. } else if (currentRow >= 19 ) {
  422. let customIndex = currentRow - 19
  423. if (cellView != nil) {
  424. let dict = KMProfileInfo.shared().customInfoArray[customIndex] as? [String : Any]
  425. let originalKey = dict?.keys.first ?? ""
  426. let originalValue = dict?.values.first as? String ?? ""
  427. if textField.isEqual(cellView?.textField) {
  428. let newDict = [textField.stringValue : originalValue]
  429. KMProfileInfo.shared().replaceObjectInCustomInfoArray(at: customIndex, with: newDict)
  430. } else if textField.isEqual(cellView?.detailtextField) {
  431. let newDict = [originalKey : textField.stringValue]
  432. KMProfileInfo.shared().replaceObjectInCustomInfoArray(at: customIndex, with: newDict)
  433. }
  434. }
  435. }
  436. if cellView?.detailtextField.stringValue.isEmpty == false {
  437. self.addButton.isEnabled = true
  438. } else {
  439. self.addButton.isEnabled = false
  440. }
  441. }
  442. func controlTextDidBeginEditing(_ obj: Notification) {
  443. let textField = obj.object as? NSTextField
  444. textField?.textColor = NSColor.labelColor
  445. }
  446. func controlTextDidEndEditing(_ obj: Notification) {
  447. let textField = obj.object as? NSTextField
  448. let tag = textField?.tag ?? 0
  449. let cellView = self.tableView.view(atColumn: 0, row: tag, makeIfNecessary: false) as? KMProfileCellView
  450. if let data = cellView?.isKind(of: KMProfileCellView.self), data, tag > 0 {
  451. var placeHoldString = ""
  452. if (tag == 1) {
  453. placeHoldString = KMLocalizedString("Add Full Name", nil)
  454. } else if (tag == 2) {
  455. placeHoldString = KMLocalizedString("Add First Name", nil)
  456. } else if (tag == 3) {
  457. placeHoldString = KMLocalizedString("Add Middle Name", nil)
  458. } else if (tag == 4) {
  459. placeHoldString = KMLocalizedString("Add Last Name", nil)
  460. } else if (tag == 6) {
  461. placeHoldString = KMLocalizedString("Add Street 1", nil)
  462. } else if (tag == 7) {
  463. placeHoldString = KMLocalizedString("Add Street 2", nil)
  464. } else if (tag == 8) {
  465. placeHoldString = KMLocalizedString("Add City", nil)
  466. } else if (tag == 9) {
  467. placeHoldString = KMLocalizedString("Add State", nil)
  468. } else if (tag == 10) {
  469. placeHoldString = KMLocalizedString("Add Zip", nil)
  470. } else if (tag == 11) {
  471. placeHoldString = KMLocalizedString("Add Country", nil)
  472. } else if (tag == 13) {
  473. placeHoldString = KMLocalizedString("Add Email", nil)
  474. } else if (tag == 14) {
  475. placeHoldString = KMLocalizedString("Add Tel", nil)
  476. } else if (tag == 16) {
  477. placeHoldString = KMLocalizedString("Add Date", nil)
  478. } else if (tag == 17) {
  479. placeHoldString = KMLocalizedString("Add Birth Date", nil)
  480. }
  481. if (textField?.stringValue.isEmpty == false) {
  482. textField?.textColor = NSColor.secondaryLabelColor
  483. textField?.isHidden = false
  484. cellView?.addButton.isHidden = true
  485. } else {
  486. // textField.hidden = YES; //自定义新增,输入Label后,value为空时推出编辑,就无法填写了
  487. textField?.placeholderString = ""
  488. cellView?.addButton.isHidden = false
  489. cellView?.addButton.tag = tag
  490. cellView?.addButton.target = self
  491. cellView?.addButton.action = #selector(addFileName_Add)
  492. cellView?.addButton.title = placeHoldString
  493. cellView?.addButton.setTitleColor(NSColor.systemBlue)
  494. }
  495. }
  496. }
  497. }
  498. extension KMProfileInfoWindowController: NSMenuItemValidation {
  499. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  500. let cellView = self.tableView.view(atColumn: 0, row: self.tableView.clickedRow, makeIfNecessary: false) as? NSTableCellView
  501. if (menuItem.action == #selector(menuItemClicked_Edit)) {
  502. if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data {
  503. return false
  504. } else {
  505. let cellView1 = cellView as? KMProfileCellView
  506. if (cellView1?.textField?.stringValue == KMLocalizedString("Date", nil)) {
  507. return false
  508. }
  509. return true
  510. }
  511. } else if (menuItem.action == #selector(menuItemClicked_Add)) {
  512. // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
  513. // return NO;
  514. // } else {
  515. // KMProfileCellView *cell = (KMProfileCellView *)cellView;
  516. // return cell.detailtextField.stringValue.length > 0;
  517. // }
  518. if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data {
  519. return false
  520. } else {
  521. let cellView1 = cellView as? KMProfileCellView
  522. if let data = cellView1?.detailtextField?.stringValue.isEmpty, data {
  523. return false
  524. }
  525. return true
  526. }
  527. } else if (menuItem.action == #selector(menuItemClicked_Delete)) {
  528. // if ([cellView isKindOfClass:[KMProfileTitleCellView class]]) {
  529. // return NO;
  530. // } else {
  531. // KMProfileCellView *cellView1 = (KMProfileCellView *)cellView;
  532. // if ([cellView1.textField.stringValue isEqualToString:NSLocalizedString(@"Date", nil)]) {
  533. // return NO;
  534. // }
  535. // return YES;
  536. // }
  537. if let data = cellView?.isKind(of: KMProfileTitleCellView.self), data {
  538. return false
  539. } else {
  540. let cellView1 = cellView as? KMProfileCellView
  541. if (cellView1?.textField?.stringValue == KMLocalizedString("Date", nil)) {
  542. return false
  543. }
  544. return true
  545. }
  546. }
  547. return false
  548. }
  549. }