KMNLeftSideViewController.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // KMNLeftSideViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/11/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMNLeftSideViewControllerDelegate: AnyObject {
  10. @objc optional func enterPageEditLeftSideViewController(leftSideViewController:KMNLeftSideViewController)
  11. @objc optional func changeSelectePageLeftSideViewController(leftSideViewController:KMNLeftSideViewController,pageIndex:Int)
  12. @objc optional func addBookmarkForLeftC(controller: KMNLeftSideViewController, bookmark: CPDFBookmark?, info: [String : Any]?)
  13. @objc optional func switchSearchPopWindow(controller: KMNLeftSideViewController)
  14. @objc optional func searchTypeDidChange(controller: KMNLeftSideViewController)
  15. @objc optional func leftSideViewControllerThumbnailHaveChange(leftSideViewController: KMNLeftSideViewController)
  16. }
  17. class KMNLeftSideViewController: KMNBaseViewController {
  18. weak open var leftSideViewDelegate: KMNLeftSideViewControllerDelegate?
  19. @IBOutlet var backContentBox: NSBox!
  20. @IBOutlet var headerBox: NSBox!
  21. @IBOutlet var bottomBox: NSBox!
  22. @IBOutlet weak var headerBoxHeightConst: NSLayoutConstraint!
  23. private var currentDocument:CPDFDocument?
  24. public var thumnailViewController:KMNThumnailViewController?
  25. private lazy var bookmarkViewC_: KMBookMarkViewController = {
  26. let controller = KMBookMarkViewController()
  27. controller.delegate = self
  28. return controller
  29. }()
  30. private lazy var outlineViewC_: KMOutlineViewController = {
  31. let contr = KMOutlineViewController(nibName: "KMOutlineViewController", bundle: nil)
  32. return contr
  33. }()
  34. private lazy var searchViewC_: KMBotaSearchViewController = {
  35. let con = KMBotaSearchViewController()
  36. con.delegate = self
  37. return con
  38. }()
  39. private lazy var annotationViewC_: KMLeftSideViewController = {
  40. let leftSideViewController = KMLeftSideViewController()
  41. leftSideViewController.delegate = self
  42. return leftSideViewController
  43. }()
  44. private var aiInfoViewController: AINewConfigController = {
  45. let aiConfigWindowVC = AINewConfigController()
  46. return aiConfigWindowVC
  47. }()
  48. var outlineViewC: KMOutlineViewController {
  49. get {
  50. return outlineViewC_
  51. }
  52. }
  53. var bookmarkViewC: KMBookMarkViewController {
  54. get {
  55. return bookmarkViewC_
  56. }
  57. }
  58. var searchViewC: KMBotaSearchViewController {
  59. get {
  60. return searchViewC_
  61. }
  62. }
  63. var annoController: KMLeftSideViewController {
  64. get {
  65. return annotationViewC_
  66. }
  67. }
  68. lazy var thumbnailHeaderViewController: KMNThumnailHeaderViewController = {
  69. let controller = KMNThumnailHeaderViewController(nibName: "KMNThumnailHeaderViewController", bundle: nil)
  70. controller.view.frame = headerBox.bounds
  71. controller.view.autoresizingMask = [.width, .height] // Use flexible for better resizing
  72. return controller
  73. }()
  74. public var leftsideType:KMPDFSidebarType = .none {
  75. didSet {
  76. switch leftsideType {
  77. case .search:
  78. headerBoxHeightConst.constant = 0
  79. bottomBox.contentView = searchViewC_.view
  80. case .thumbnail:
  81. headerBoxHeightConst.constant = 40
  82. headerBox.contentView = thumbnailHeaderViewController.view
  83. bottomBox.contentView = thumnailViewController?.view
  84. self.view.layoutSubtreeIfNeeded()
  85. reloadData()
  86. case .bookmark:
  87. headerBoxHeightConst.constant = 0
  88. bottomBox.contentView = bookmarkViewC_.view
  89. bookmarkViewC_.document = currentDocument
  90. reloadData()
  91. case .outline:
  92. headerBoxHeightConst.constant = 0
  93. bottomBox.contentView = outlineViewC_.view
  94. reloadData()
  95. case .annotation:
  96. headerBoxHeightConst.constant = 0
  97. bottomBox.contentView = annotationViewC_.view
  98. case .aiTools:
  99. headerBoxHeightConst.constant = 0
  100. bottomBox.contentView = aiInfoViewController.view
  101. aiInfoViewController.loadData()
  102. default: break
  103. }
  104. }
  105. }
  106. public func reloadData() {
  107. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(reloadIfData), object: nil)
  108. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  109. self.reloadIfData()
  110. }
  111. }
  112. @objc public func reloadIfData() {
  113. switch leftsideType {
  114. case .search:
  115. break
  116. case .thumbnail:
  117. thumnailViewController?.reloadDatas()
  118. break
  119. case .bookmark:
  120. bookmarkViewC_.reloadData()
  121. break
  122. case .outline:
  123. outlineViewC_.reloadData()
  124. break
  125. case .annotation:
  126. annoController.note_reloadDataIfNeed()
  127. break
  128. case .aiTools:
  129. break
  130. default: break
  131. }
  132. }
  133. public func changeDocument(document: CPDFDocument?) {
  134. currentDocument = document
  135. }
  136. public func changeLeftSideBounds() {
  137. thumnailViewController?.updateThumbnailSize()
  138. }
  139. init(_ document: CPDFDocument) {
  140. super.init(nibName: "KMNLeftSideViewController", bundle: nil)
  141. currentDocument = document
  142. }
  143. required init?(coder: NSCoder) {
  144. fatalError("init(coder:) has not been implemented")
  145. }
  146. override func viewDidLoad() {
  147. super.viewDidLoad()
  148. initThumnailView()
  149. }
  150. override func updateUIThemeColor() {
  151. super.updateUIThemeColor()
  152. headerBox.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider")
  153. headerBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  154. bottomBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  155. }
  156. private func initThumnailView() {
  157. thumnailViewController = KMNThumnailViewController.init(currentDocument)
  158. thumnailViewController?.view.frame = bottomBox.bounds
  159. thumnailViewController?.thumnailViewDelegate = self
  160. thumnailViewController?.thumbnailBaseViewDelegate = self
  161. thumnailViewController?.view.autoresizingMask = [.width, .height]
  162. }
  163. // MARK: - Public Methods
  164. public func pageCountChangedAction(document: CPDFDocument?) {
  165. guard let theDocument = document, theDocument.isEqual(to: currentDocument) else {
  166. return
  167. }
  168. if leftsideType == .bookmark {
  169. bookmarkViewC.reloadData()
  170. }
  171. }
  172. public func currentPageDidChangedAction(listView: CPDFListView?) {
  173. guard let theDocument = listView?.document, theDocument.isEqual(to: currentDocument) else {
  174. return
  175. }
  176. if leftsideType == .bookmark {
  177. bookmarkViewC.currentPageDidChangedAction(document: currentDocument)
  178. } else if leftsideType == .thumbnail {
  179. var indexpaths: Set<IndexPath> = []
  180. indexpaths.insert(IndexPath(item: listView?.currentPageIndex ?? 0, section: 0))
  181. thumnailViewController?.selectionIndexPaths = indexpaths
  182. }
  183. }
  184. public func chooseAIFunctionWithType(_ aiConfigType: AIConfigType) -> Void {
  185. if self.leftsideType == .aiTools {
  186. aiInfoViewController.chooseAIFunctionWithType(aiConfigType)
  187. }
  188. }
  189. public func setCurrentPDFSelection(_ string: String) -> Void {
  190. if self.leftsideType == .aiTools {
  191. aiInfoViewController.aiInfoInputView.fileEmptyTextView.string = string
  192. }
  193. }
  194. }
  195. // MARK: - KMNThumbnailBaseViewDelegate
  196. extension KMNLeftSideViewController: KMNThumbnailBaseViewDelegate {
  197. func changeIndexPathsThumbnailViewControlle(pageEditVC:KMNThumbnailBaseViewController?,selectionIndexPaths: Set<IndexPath>,selectionStrings:String ) {
  198. if(selectionIndexPaths.count == 1) {
  199. leftSideViewDelegate?.changeSelectePageLeftSideViewController?(leftSideViewController: self, pageIndex: selectionIndexPaths.first?.item ?? 0)
  200. }
  201. }
  202. func thumbnailViewControlleHaveChange(pageEditVC: KMNThumbnailBaseViewController) {
  203. leftSideViewDelegate?.leftSideViewControllerThumbnailHaveChange?(leftSideViewController: self)
  204. }
  205. }
  206. // MARK: - KMNThumnailViewControllerDelegate
  207. extension KMNLeftSideViewController: KMNThumnailViewControllerDelegate {
  208. func enterPageEditThumnailViewController(thumnailViewController: KMNThumnailViewController) {
  209. leftSideViewDelegate?.enterPageEditLeftSideViewController?(leftSideViewController: self)
  210. }
  211. }
  212. // MARK: - KMBookMarkViewControllerDelegate
  213. extension KMNLeftSideViewController: KMBookMarkViewControllerDelegate {
  214. func bkControllerAddAction(controller: KMBookMarkViewController, bookmark: CPDFBookmark?, info: [String : Any]?) {
  215. leftSideViewDelegate?.addBookmarkForLeftC?(controller: self, bookmark: bookmark, info: info)
  216. }
  217. }
  218. // MARK: - KMBotaSearchViewControllerDelegate
  219. extension KMNLeftSideViewController: KMBotaSearchViewControllerDelegate {
  220. func switchSearchPopWindow(controller: KMBotaSearchViewController) {
  221. leftSideViewDelegate?.switchSearchPopWindow?(controller: self)
  222. }
  223. func searchTypeDidChange(controller: KMBotaSearchViewController) {
  224. leftSideViewDelegate?.searchTypeDidChange?(controller: self)
  225. }
  226. }
  227. // MARK: - KMLeftSideViewControllerDelegate
  228. extension KMNLeftSideViewController: KMLeftSideViewControllerDelegate {
  229. }