KMHomeViewController+UI.swift 17 KB

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