KMHomeViewController+UI.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // KMHomeViewController+UI.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/10/13.
  6. //
  7. import Foundation
  8. extension KMHomeViewController {
  9. func refreshRightBoxUI(_ state: KMHomeToolState) {
  10. rightFullBox.isHidden = true
  11. rightTopBox.isHidden = true
  12. rightBottomBox.isHidden = true
  13. homeButtonVC?.state = .Norm
  14. pdfToolsButtonVC?.state = .Norm
  15. cloudDocumentsButtonVC?.state = .Norm
  16. switch state {
  17. case .Home:
  18. rightTopBox.isHidden = false
  19. rightBottomBox.isHidden = false
  20. rightTopBox.contentView = fastToolViewController.view
  21. rightBottomBox.contentView = historyFileViewController.view
  22. rightFullBox.contentView = nil
  23. let isQuickToolsType:Bool = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  24. if isQuickToolsType {
  25. rightTopBoxHeightConstraint.constant = 230 + ScrollerViewWidget
  26. } else {
  27. rightTopBoxHeightConstraint.constant = 326 + ScrollerViewWidget
  28. }
  29. homeButtonVC?.state = .Sel
  30. refreshUI()
  31. break
  32. case .PDFTools:
  33. rightFullBox.isHidden = false
  34. rightFullBox.contentView = pdfToolsViewController.view
  35. rightTopBox.contentView = nil
  36. rightBottomBox.contentView = nil
  37. pdfToolsButtonVC?.state = .Sel
  38. refreshUI()
  39. break
  40. case .CloudDocuments:
  41. rightFullBox.isHidden = false
  42. rightFullBox.contentView = cloudDocumentsViewController.view
  43. rightTopBox.contentView = nil
  44. rightBottomBox.contentView = nil
  45. cloudDocumentsButtonVC?.state = .Sel
  46. refreshUI()
  47. break
  48. default:
  49. break
  50. }
  51. }
  52. func showConvertWindow(type: KMPDFConvertType) {
  53. Task { @MainActor in
  54. self.km_secure_openPanel_convert(type: type)
  55. }
  56. }
  57. func km_secure_openPanel_convert(type: KMPDFConvertType) {
  58. DispatchQueue.main.async {
  59. NSOpenPanel.km_secure_openPanel_success(window: self.view.window!) { url, password in
  60. if (url.path.isPDFValid() == false) {
  61. let alert = NSAlert()
  62. alert.alertStyle = .critical
  63. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  64. alert.runModal()
  65. return
  66. }
  67. self._showConvertWindow(type: type, url: url, password: password)
  68. }
  69. }
  70. }
  71. func showSecurityWindow() {
  72. // Task { @MainActor in
  73. // #if VERSION_DMG
  74. // if await (KMLightMemberManager.manager.canUseAdvanced() == false) {
  75. // let _ = KMComparativeTableViewController.show(window: self.view.window!, .merge)
  76. // return
  77. // }
  78. // #endif
  79. //
  80. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  81. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, isContinue: true) { isSubscribeSuccess, isWaterMarkExport, isClose in
  82. // if (isClose) {
  83. // return
  84. // }
  85. // self.km_secure_openPanel_security(limit: true)
  86. // }
  87. // return
  88. // }
  89. // self.km_secure_openPanel_security()
  90. // }
  91. }
  92. func km_secure_openPanel_security(limit: Bool = false) {
  93. DispatchQueue.main.async {
  94. NSOpenPanel.km_secure_openPanel_success(window: self.view.window!, needOwner: true) { url, password in
  95. self._showSecurityWindow(url: url, password: password, limit: limit)
  96. }
  97. }
  98. }
  99. func refreshScrollView() -> Void {
  100. let rect = self.homeSplitView.frame
  101. let rightWidth = Int(rect.width) - 271
  102. if self.aiOpenPDFFilesViewController != nil {
  103. if rightWidth >= (360 + 20 + 328) {
  104. self.rightTopBoxHeightConstraint.constant = 348
  105. self.aiOpenPDFFilesViewController.refreshLayout(isLimit: false)
  106. } else {
  107. self.rightTopBoxHeightConstraint.constant = 348 * 2
  108. self.aiOpenPDFFilesViewController.refreshLayout(isLimit: true)
  109. }
  110. // let scrollView: NSScrollView = self.homeRightScrollViewView
  111. // let targetOrigin: NSPoint = NSPoint(x: 271, y: (self.homeRightScrollViewView.documentView?.frame.height)!)
  112. //
  113. // let documentVisibleRect = scrollView.documentVisibleRect
  114. // let documentSize = scrollView.documentView?.frame.size ?? .zero
  115. //
  116. // // 确保 targetOrigin 在有效的滚动范围内
  117. // let maxX = max(0, documentSize.width - documentVisibleRect.size.width)
  118. // let maxY = max(0, documentSize.height - documentVisibleRect.size.height)
  119. // let clampedOrigin = NSPoint(x: min(targetOrigin.x, maxX), y: min(targetOrigin.y, maxY))
  120. //
  121. //
  122. //// self.homeRightScrollViewView.documentView?.layoutSubtreeIfNeeded()
  123. // self.homeRightScrollViewView.contentView.scroll(to:clampedOrigin)
  124. }
  125. var contentViewHeight = 0
  126. if self.historyFileViewController.files.count > 0 {
  127. if self.historyFileViewController.showMode == .Thumbnail {
  128. if self.historyFileViewController.thumbnailSCrollView != nil {
  129. var count = rightWidth/(226 + 32)
  130. if count <= 0 {
  131. count = 1
  132. }
  133. var row = self.historyFileViewController.files.count/count
  134. if self.historyFileViewController.files.count%count > 0 {
  135. row += 1
  136. }
  137. let height2 = (248 + 16) * row
  138. contentViewHeight = Int(self.rightTopBoxHeightConstraint.constant + CGFloat(height2) + 76)
  139. self.rightBottonHeight.constant = CGFloat(height2) + 76
  140. }
  141. } else {
  142. if self.historyFileViewController.listScrollView != nil {
  143. let height2 = self.historyFileViewController.files.count * 72 + 32
  144. contentViewHeight = Int(self.rightTopBoxHeightConstraint.constant + CGFloat(height2) + 76)
  145. self.rightBottonHeight.constant = CGFloat(height2) + 76
  146. }
  147. }
  148. } else {
  149. contentViewHeight = Int(rect.height - self.rightTopBox.frame.height)
  150. self.rightBottonHeight.constant = CGFloat(contentViewHeight)
  151. }
  152. if contentViewHeight < Int(rect.height) {
  153. contentViewHeight = Int(rect.height)
  154. self.rightBottonHeight.constant = rect.height - self.rightTopBoxHeightConstraint.constant
  155. }
  156. self.homeRightScrollViewView.documentView!.frame = NSRect(x: 271, y: 0, width: rightWidth, height: contentViewHeight)
  157. let scrollView: NSScrollView = self.homeRightScrollViewView
  158. let targetOrigin: NSPoint = NSPoint(x: 271, y: (self.homeRightScrollViewView.documentView?.frame.height)!)
  159. let documentVisibleRect = scrollView.documentVisibleRect
  160. let documentSize = scrollView.documentView?.frame.size ?? .zero
  161. // 确保 targetOrigin 在有效的滚动范围内
  162. let maxX = max(271, documentSize.width - documentVisibleRect.size.width)
  163. let maxY = max(0, documentSize.height - documentVisibleRect.size.height)
  164. let clampedOrigin = NSPoint(x: min(targetOrigin.x, maxX), y: min(targetOrigin.y, maxY))
  165. self.homeRightScrollViewView.contentView.scroll(to:clampedOrigin)
  166. }
  167. // MARK: - Private Methods
  168. fileprivate func _showConvertWindow(type: KMPDFConvertType, url: URL, password: String? = nil) {
  169. // var windowController: KMConvertBaseWindowController?
  170. // if (type == .word) { /// Word
  171. // windowController = KMConvertWordWindowController()
  172. // } else if (type == .excel) {
  173. // windowController = KMConvertExcelWindowController()
  174. // } else if (type == .ppt || type == .rtf || type == .html || type == .text) {
  175. // windowController = KMConvertPPTsWindowController()
  176. // if (type == .ppt) {
  177. // windowController?.subType = 1
  178. // } else if (type == .rtf) {
  179. // windowController?.subType = 2
  180. // } else if (type == .html) {
  181. // windowController?.subType = 3
  182. // } else if (type == .text) {
  183. // windowController?.subType = 4
  184. // }
  185. // } else if (type == .csv) {
  186. // windowController = KMConvertCSVWindowController()
  187. // } else if (type == .image) {
  188. // windowController = KMConvertImageWindowController()
  189. // }
  190. // windowController?.subscribeWaterMarkType = type.toSubscribeWaterMarkType()
  191. //
  192. // let model = KMDocumentModel(url: url)
  193. // if (password != nil) {
  194. // let _ = model.unlock(password!)
  195. // }
  196. // windowController?.documentModel = model
  197. //
  198. // windowController?.itemClick = { [weak self] _ in
  199. // self?.km_endSheet()
  200. // }
  201. //
  202. // self.km_safe_beginSheet(windowC: windowController)
  203. }
  204. fileprivate func _showSecurityWindow(url: URL, password: String?, limit: Bool = false) { // 安全
  205. // let windowC = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
  206. // windowC.documentURL = url
  207. // windowC.myDocument = CPDFDocument(url: url)
  208. // if let _password = password {
  209. // windowC.myDocument.unlock(withPassword: _password)
  210. // }
  211. //
  212. // let newDocument: CPDFDocument = CPDFDocument(url: url)
  213. // windowC.itemClick = { [weak self] _ in
  214. // self?.km_endSheet()
  215. // }
  216. //
  217. // windowC.resultCallback = { [weak self] _ in
  218. // let windowController_secure: KMSecureEncryptWindowController = self?.kmCurrentWindowC as! KMSecureEncryptWindowController
  219. // self?.km_endSheet()
  220. // if let _password = password {
  221. // newDocument.unlock(withPassword: _password)
  222. // }
  223. //
  224. // if (!limit) {
  225. // newDocument.setPasswordOptions(windowController_secure.options)
  226. // let result = KMPasswordInputWindow.saveDocument(newDocument)
  227. // if result {
  228. // NSWorkspace.shared.activateFileViewerSelecting([newDocument.documentURL])
  229. // } else {
  230. // let alert = NSAlert()
  231. // alert.messageText = NSLocalizedString("Failure", comment: "")
  232. // alert.runModal()
  233. // }
  234. // return
  235. // }
  236. //
  237. // NSPanel.savePanel((self?.view.window)!, panel: { panel in
  238. // panel.nameFieldStringValue = url.lastPathComponent
  239. // }) { response, saveUrl in
  240. // if (response == .cancel) {
  241. // return
  242. // }
  243. //
  244. // if let fileUrl = KMTools.saveWatermarkDocument(document: newDocument, to: saveUrl!, secureOptions: windowController_secure.options) {
  245. // NSWorkspace.shared.activateFileViewerSelecting([fileUrl])
  246. // } else {
  247. // let alert = NSAlert()
  248. // alert.messageText = NSLocalizedString("Failure", comment: "")
  249. // alert.runModal()
  250. // }
  251. // }
  252. // }
  253. //
  254. // self.km_beginSheet(windowC: windowC)
  255. }
  256. }
  257. // MARK: - NSSplitViewDelegate
  258. extension KMHomeViewController: NSSplitViewDelegate {
  259. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  260. return 270.0
  261. }
  262. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  263. return 270.0
  264. }
  265. func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
  266. return false
  267. }
  268. func splitViewDidResizeSubviews(_ notification: Notification) {
  269. let rect = self.homeSplitView.frame
  270. self.leftBox.setFrameSize(CGSize(width: 270, height: rect.size.height))
  271. self.rightBox.frame = CGRect(origin: CGPoint(x: 271.0, y: 0), size: CGSize(width: rect.width - 271.0, height: rect.height))
  272. }
  273. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  274. return proposedPosition
  275. }
  276. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  277. return false
  278. }
  279. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  280. return false
  281. }
  282. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  283. return splitView.isEqual(to: self.homeSplitView)
  284. }
  285. func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
  286. return proposedEffectiveRect
  287. }
  288. func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
  289. return NSMakeRect(0, 0, 0, 0)
  290. }
  291. }
  292. // MARK: - KMHomeFastToolViewControllerDelegate
  293. extension KMHomeViewController: KMHomeFastToolViewControllerDelegate {
  294. func fastToolViewController(_ viewController: KMHomeFastToolViewController, foldChangeState: Bool) {
  295. if foldChangeState {
  296. rightTopBoxHeightConstraint.constant = 230.0 + ScrollerViewWidget
  297. } else {
  298. rightTopBoxHeightConstraint.constant = 326.0 + ScrollerViewWidget
  299. }
  300. }
  301. func fastToolDidSelectAllTools(_ viewController: KMHomeFastToolViewController) {
  302. fastToolDidSelectAllTools()
  303. }
  304. func fastToolViewController(_ viewController: KMHomeFastToolViewController, didSelectItemsAt indexPaths: [Int]) {
  305. for index in indexPaths {
  306. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  307. }
  308. }
  309. }
  310. // MARK: - KMHomeHistoryFileViewControllerDelegate
  311. extension KMHomeViewController: KMHomeHistoryFileViewControllerDelegate {
  312. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, deleteDocuments indexPaths: [URL]) {
  313. historyFile(deleteDocuments: indexPaths)
  314. }
  315. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, didSelectItemsAt indexPaths: [URL]) {
  316. for url in indexPaths {
  317. openHistoryFilePath(url: url)
  318. }
  319. }
  320. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, refreshLayout refresh: Bool) {
  321. self.refreshScrollView()
  322. }
  323. }
  324. // MARK: - KMPDFToolsViewControllerDelegate
  325. extension KMHomeViewController: KMPDFToolsViewControllerDelegate {
  326. func pdfToolsViewController(_ viewController: KMPDFToolsViewController, didSelectItemsAt indexPaths: [Int]) {
  327. for index in indexPaths {
  328. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  329. }
  330. }
  331. }
  332. // MARK: - KMHomeDragViewDelegate
  333. extension KMHomeViewController: KMHomeDragViewDelegate {
  334. func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
  335. if self.aiHomeState == .AITranslation {
  336. self.aiTranslation(withFilePath: filePath.path)
  337. } else {
  338. self.openFile(withFilePath: filePath)
  339. }
  340. }
  341. func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {
  342. // if notSupport {
  343. // let alert = NSAlert()
  344. // alert.alertStyle = .critical
  345. // alert.messageText = NSLocalizedString("This file format is not supported. Please enter PDF, picture, or Office file", comment: "")
  346. // alert.runModal()
  347. // }
  348. }
  349. }