KMHomeViewController+UI.swift 17 KB

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