KMBookmarkSheetView.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // KMBookmarkSheetView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/6.
  6. //
  7. import Cocoa
  8. typealias KMBookmarkSheetViewDoneAction = (_ view: KMBookmarkSheetView, _ type: KMBookmarkType, _ label: String) -> ()
  9. typealias KMBookmarkSheetViewCancelAction = (_ view: KMBookmarkSheetView, _ type: KMBookmarkType) -> ()
  10. class KMBookmarkSheetView: BaseXibView {
  11. @IBOutlet weak var folderPopUp: NSPopUpButton!
  12. @IBOutlet weak var bookmarkTextField: NSTextField!
  13. @IBOutlet weak var bookmarkLabel: NSTextField!
  14. @IBOutlet weak var addToLabel: NSTextField!
  15. @IBOutlet weak var cancelButton: NSButton!
  16. @IBOutlet weak var okButton: NSButton!
  17. var type: KMBookmarkType = .bookmark
  18. var doneAction: KMBookmarkSheetViewDoneAction?
  19. var cancelAction: KMBookmarkSheetViewCancelAction?
  20. override func draw(_ dirtyRect: NSRect) {
  21. super.draw(dirtyRect)
  22. // Drawing code here.
  23. }
  24. func setup() {
  25. let root = KMBookmarkManager.manager.rootBookmark
  26. self.folderPopUp.removeAllItems()
  27. addMenuItems(forBookmarks: [root], level: 0, toMenu: self.folderPopUp.menu!)
  28. self.folderPopUp.selectItem(at: 0)
  29. self.bookmarkLabel.stringValue = NSLocalizedString("Bookmarks", comment: "")
  30. self.addToLabel.stringValue = NSLocalizedString("Add to", comment:"")
  31. self.cancelButton.title = NSLocalizedString("Cancel", comment:"")
  32. self.okButton.title = NSLocalizedString("OK", comment:"")
  33. }
  34. func addMenuItems(forBookmarks bookmarks: [KMBookmark], level: Int, toMenu menu: NSMenu) {
  35. for bm in bookmarks {
  36. if bm.bookmarkType == .folder {
  37. let label = bm.label
  38. let item = menu.addItem(withTitle: label, action: nil, keyEquivalent: "")
  39. item.setImageAndSize(bm.icon)
  40. item.indentationLevel = level
  41. item.representedObject = bm
  42. addMenuItems(forBookmarks: bm.children, level: level + 1, toMenu: menu)
  43. }
  44. }
  45. }
  46. @IBAction func cancelButtonAction(_ sender: Any) {
  47. self.cancelAction?(self, type)
  48. }
  49. @IBAction func doneButtonAction(_ sender: Any) {
  50. self.doneAction?(self, type, bookmarkTextField.stringValue)
  51. }
  52. }