KMHomeViewController+UI.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. // MARK: Private Methods
  10. func productPromotionShow(_ pdfProSeries: NSArray, withOthers others: NSArray, isInitialize initialize: Bool) {
  11. // if pdfProSeries.count > 0 {
  12. // if (self.pdfProSeriesBoxHeightConstraint.constant == 0.0) || initialize {
  13. // var height = CGFloat(pdfProSeries.count) * 32.0
  14. // for name in pdfProSeries {
  15. // height -= 32.0
  16. //
  17. // let dict: Dictionary = self.productPromotionData[name] as! Dictionary<NSString, NSString>
  18. // let subBox = KMBox(frame: CGRect(origin: CGPoint(x: 0, y: height), size: CGSize(width: self.pdfProSeriesBox.frame.width, height: 32.0)))
  19. // subBox.boxType = .custom
  20. // subBox.borderWidth = 0.0
  21. // self.pdfProSeriesBox.contentView?.addSubview(subBox)
  22. // subBox.downCallback = {(downEntered, mouseBox, event) -> Void in
  23. // if downEntered {
  24. // self.productPromotionClickAction(dict["Name"]!)
  25. // }
  26. // }
  27. //
  28. // let subImageView = NSImageView(frame: NSZeroRect)
  29. // subImageView.image = NSImage(named: dict["Image"]! as NSImage.Name)
  30. // subBox.addSubview(subImageView)
  31. // subImageView.mas_makeConstraints { (make) in
  32. // make?.width.height().equalTo()(16)
  33. // make?.centerY.equalTo()(0)
  34. // make?.left.equalTo()(18)
  35. // }
  36. //
  37. // let subLabel = NSTextField(frame: NSZeroRect)
  38. // subLabel.isBezeled = false
  39. // subLabel.isEditable = false
  40. // subLabel.backgroundColor = .clear
  41. // let paragraphStyle = NSMutableParagraphStyle()
  42. // paragraphStyle.lineSpacing = 22.0
  43. // subLabel.attributedStringValue = NSAttributedString(string: dict["Name"]! as String, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  44. // subLabel.font = NSFont(name: "SFProText-Regular", size: 14)
  45. // subLabel.textColor = NSColor(hex: "#616469")
  46. // subLabel.sizeToFit()
  47. // subBox.addSubview(subLabel)
  48. // subLabel.mas_makeConstraints { (make) in
  49. // make?.left.equalTo()(subImageView.mas_right)?.offset()(10)
  50. // make?.centerY.equalTo()(0)
  51. // }
  52. // }
  53. //
  54. // pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  55. // self.pdfProSeriesBoxHeightConstraint.constant = CGFloat(pdfProSeries.count) * 32.0
  56. // }
  57. // } else {
  58. // pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_right_false")!
  59. // self.pdfProSeriesBox.contentView = NSView.init()
  60. // self.pdfProSeriesBoxHeightConstraint.constant = 0.0
  61. // }
  62. //
  63. // if others.count > 0 {
  64. // if (self.othersBoxHeightConstraint.constant == 0.0) || initialize {
  65. // var height = CGFloat(others.count) * 32.0
  66. // for name in others {
  67. // height -= 32.0
  68. //
  69. // let dict: Dictionary = self.productPromotionData[name] as! Dictionary<NSString, NSString>
  70. // let subBox = KMBox(frame: CGRect(x: 0, y: height, width: self.othersBox.frame.width, height: 32.0))
  71. // subBox.boxType = .custom
  72. // subBox.borderWidth = 0.0
  73. // self.othersBox.contentView?.addSubview(subBox)
  74. // subBox.downCallback = {(downEntered, mouseBox, event) -> Void in
  75. // if downEntered {
  76. // self.productPromotionClickAction(dict["Name"]!)
  77. // }
  78. // }
  79. //
  80. // let subImageView = NSImageView(frame: NSZeroRect)
  81. // subImageView.image = NSImage(named: dict["Image"]! as NSImage.Name)
  82. // subImageView.imageScaling = .scaleProportionallyDown
  83. // subImageView.imageAlignment = .alignCenter
  84. // subImageView.sizeToFit()
  85. // subBox.addSubview(subImageView)
  86. // subImageView.mas_makeConstraints { (make) in
  87. // make?.width.height().equalTo()(16)
  88. // make?.centerY.equalTo()(0)
  89. // make?.left.equalTo()(18)
  90. // }
  91. //
  92. // let subLabel = NSTextField(frame: NSZeroRect)
  93. // subLabel.isBezeled = false
  94. // subLabel.isEditable = false
  95. // subLabel.backgroundColor = .clear
  96. // let paragraphStyle = NSMutableParagraphStyle()
  97. // paragraphStyle.lineSpacing = 22.0
  98. // subLabel.attributedStringValue = NSAttributedString(string: dict["Name"]! as String, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  99. // subLabel.font = NSFont(name: "SFProText-Regular", size: 14)
  100. // subLabel.textColor = NSColor(red: 0.38, green: 0.392, blue: 0.412, alpha: 1.0)
  101. // subLabel.sizeToFit()
  102. // subBox.addSubview(subLabel)
  103. // subLabel.mas_makeConstraints { (make) in
  104. // make?.left.equalTo()(subImageView.mas_right)?.offset()(10)
  105. // make?.centerY.equalTo()(0)
  106. // }
  107. // }
  108. //
  109. // self.pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  110. // self.othersBoxHeightConstraint.constant = CGFloat(others.count) * 32.0
  111. // }
  112. // } else {
  113. // self.pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_right_false")!
  114. // self.othersBox.contentView = NSView.init()
  115. // self.othersBoxHeightConstraint.constant = 0.0
  116. // }
  117. //
  118. // pdfSeriesButtonVC.updateUI()
  119. // pdfOthersButtonVC.updateUI()
  120. }
  121. func refreshRightBoxUI(_ state: KMHomeToolState) {
  122. rightFullBox.isHidden = true
  123. rightTopBox.isHidden = true
  124. rightBottomBox.isHidden = true
  125. homeButtonVC.state = .Norm
  126. pdfToolsButtonVC.state = .Norm
  127. cloudDocumentsButtonVC.state = .Norm
  128. switch state {
  129. case .Home:
  130. rightTopBox.isHidden = false
  131. rightBottomBox.isHidden = false
  132. rightTopBox.contentView = fastToolViewController.view
  133. rightBottomBox.contentView = historyFileViewController.view
  134. rightFullBox.contentView = nil
  135. let isQuickToolsType:Bool = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  136. if isQuickToolsType {
  137. rightTopBoxHeightConstraint.constant = 230 + ScrollerViewWidget
  138. } else {
  139. rightTopBoxHeightConstraint.constant = 326 + ScrollerViewWidget
  140. }
  141. homeButtonVC.state = .Sel
  142. refreshUI()
  143. break
  144. case .PDFTools:
  145. rightFullBox.isHidden = false
  146. rightFullBox.contentView = pdfToolsViewController.view
  147. rightTopBox.contentView = nil
  148. rightBottomBox.contentView = nil
  149. pdfToolsButtonVC.state = .Sel
  150. refreshUI()
  151. break
  152. case .CloudDocuments:
  153. rightFullBox.isHidden = false
  154. rightFullBox.contentView = cloudDocumentsViewController.view
  155. rightTopBox.contentView = nil
  156. rightBottomBox.contentView = nil
  157. cloudDocumentsButtonVC.state = .Sel
  158. refreshUI()
  159. break
  160. default:
  161. break
  162. }
  163. }
  164. func showConvertWindow(type: KMPDFConvertType) {
  165. // Task { @MainActor in
  166. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  167. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, isContinue: true, type: type.toSubscribeWaterMarkType()) { isSubscribeSuccess, isWaterMarkExport, isClose in
  168. // if (isClose) {
  169. // return
  170. // }
  171. // self.km_secure_openPanel_convert(type: type)
  172. // }
  173. // return
  174. // }
  175. self.km_secure_openPanel_convert(type: type)
  176. // }
  177. }
  178. func km_secure_openPanel_convert(type: KMPDFConvertType) {
  179. DispatchQueue.main.async {
  180. NSOpenPanel.km_secure_openPanel_success(window: self.view.window!) { url, password in
  181. if (url.path.isPDFValid() == false) {
  182. let alert = NSAlert()
  183. alert.alertStyle = .critical
  184. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  185. alert.runModal()
  186. return
  187. }
  188. self._showConvertWindow(type: type, url: url, password: password)
  189. }
  190. }
  191. }
  192. func showSecurityWindow() {
  193. Task { @MainActor in
  194. if await (KMLightMemberManager.manager.canPayFunction() == false) {
  195. let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, isContinue: true) { isSubscribeSuccess, isWaterMarkExport, isClose in
  196. if (isClose) {
  197. return
  198. }
  199. self.km_secure_openPanel_security(limit: true)
  200. }
  201. return
  202. }
  203. self.km_secure_openPanel_security()
  204. }
  205. }
  206. func km_secure_openPanel_security(limit: Bool = false) {
  207. DispatchQueue.main.async {
  208. NSOpenPanel.km_secure_openPanel_success(window: self.view.window!, needOwner: true) { url, password in
  209. self._showSecurityWindow(url: url, password: password, limit: limit)
  210. }
  211. }
  212. }
  213. func refreshScrollView() -> Void {
  214. let rect = self.homeSplitView.frame
  215. let rightWidth = Int(rect.width) - 271
  216. if self.aiOpenPDFFilesViewController != nil {
  217. if rightWidth >= (360 + 20 + 328) {
  218. self.rightTopBoxHeightConstraint.constant = 348
  219. self.aiOpenPDFFilesViewController.refreshLayout(isLimit: false)
  220. } else {
  221. self.rightTopBoxHeightConstraint.constant = 348 * 2
  222. self.aiOpenPDFFilesViewController.refreshLayout(isLimit: true)
  223. }
  224. // let scrollView: NSScrollView = self.homeRightScrollViewView
  225. // let targetOrigin: NSPoint = NSPoint(x: 271, y: (self.homeRightScrollViewView.documentView?.frame.height)!)
  226. //
  227. // let documentVisibleRect = scrollView.documentVisibleRect
  228. // let documentSize = scrollView.documentView?.frame.size ?? .zero
  229. //
  230. // // 确保 targetOrigin 在有效的滚动范围内
  231. // let maxX = max(0, documentSize.width - documentVisibleRect.size.width)
  232. // let maxY = max(0, documentSize.height - documentVisibleRect.size.height)
  233. // let clampedOrigin = NSPoint(x: min(targetOrigin.x, maxX), y: min(targetOrigin.y, maxY))
  234. //
  235. //
  236. //// self.homeRightScrollViewView.documentView?.layoutSubtreeIfNeeded()
  237. // self.homeRightScrollViewView.contentView.scroll(to:clampedOrigin)
  238. }
  239. var contentViewHeight = 0
  240. if self.historyFileViewController.files.count > 0 {
  241. if self.historyFileViewController.showMode == .Thumbnail {
  242. if self.historyFileViewController.thumbnailSCrollView != nil {
  243. var count = rightWidth/(226 + 32)
  244. if count <= 0 {
  245. count = 1
  246. }
  247. var row = self.historyFileViewController.files.count/count
  248. if self.historyFileViewController.files.count%count > 0 {
  249. row += 1
  250. }
  251. let height2 = (248 + 16) * row
  252. contentViewHeight = Int(self.rightTopBoxHeightConstraint.constant + CGFloat(height2) + 76)
  253. self.rightBottonHeight.constant = CGFloat(height2) + 76
  254. }
  255. } else {
  256. if self.historyFileViewController.listScrollView != nil {
  257. let height2 = self.historyFileViewController.files.count * 72 + 32
  258. contentViewHeight = Int(self.rightTopBoxHeightConstraint.constant + CGFloat(height2) + 76)
  259. self.rightBottonHeight.constant = CGFloat(height2) + 76
  260. }
  261. }
  262. } else {
  263. contentViewHeight = Int(rect.height - self.rightTopBox.frame.height)
  264. self.rightBottonHeight.constant = CGFloat(contentViewHeight)
  265. }
  266. if contentViewHeight < Int(rect.height) {
  267. contentViewHeight = Int(rect.height)
  268. self.rightBottonHeight.constant = rect.height - self.rightTopBoxHeightConstraint.constant
  269. }
  270. self.homeRightScrollViewView.documentView!.frame = NSRect(x: 271, y: 0, width: rightWidth, height: contentViewHeight)
  271. let scrollView: NSScrollView = self.homeRightScrollViewView
  272. let targetOrigin: NSPoint = NSPoint(x: 271, y: (self.homeRightScrollViewView.documentView?.frame.height)!)
  273. let documentVisibleRect = scrollView.documentVisibleRect
  274. let documentSize = scrollView.documentView?.frame.size ?? .zero
  275. // 确保 targetOrigin 在有效的滚动范围内
  276. let maxX = max(271, documentSize.width - documentVisibleRect.size.width)
  277. let maxY = max(0, documentSize.height - documentVisibleRect.size.height)
  278. let clampedOrigin = NSPoint(x: min(targetOrigin.x, maxX), y: min(targetOrigin.y, maxY))
  279. self.homeRightScrollViewView.contentView.scroll(to:clampedOrigin)
  280. }
  281. // MARK: - Private Methods
  282. fileprivate func _showConvertWindow(type: KMPDFConvertType, url: URL, password: String? = nil) {
  283. var windowController: KMConvertBaseWindowController?
  284. if (type == .word) { /// Word
  285. windowController = KMConvertWordWindowController()
  286. } else if (type == .excel) {
  287. windowController = KMConvertExcelWindowController()
  288. } else if (type == .ppt || type == .rtf || type == .html || type == .text) {
  289. windowController = KMConvertPPTsWindowController()
  290. if (type == .ppt) {
  291. windowController?.subType = 1
  292. } else if (type == .rtf) {
  293. windowController?.subType = 2
  294. } else if (type == .html) {
  295. windowController?.subType = 3
  296. } else if (type == .text) {
  297. windowController?.subType = 4
  298. }
  299. } else if (type == .csv) {
  300. windowController = KMConvertCSVWindowController()
  301. } else if (type == .image) {
  302. windowController = KMConvertImageWindowController()
  303. }
  304. windowController?.subscribeWaterMarkType = type.toSubscribeWaterMarkType()
  305. let model = KMDocumentModel(url: url)
  306. if (password != nil) {
  307. let _ = model.unlock(password!)
  308. }
  309. windowController?.documentModel = model
  310. windowController?.itemClick = { [weak self] index in
  311. if (self?.currentWindowController == nil) {
  312. return
  313. }
  314. self?.view.window?.endSheet((self?.currentWindowController?.window)!)
  315. self?.currentWindowController = nil
  316. }
  317. self.view.window?.beginSheet((windowController?.window)!)
  318. self.currentWindowController = windowController
  319. }
  320. fileprivate func _showSecurityWindow(url: URL, password: String?, limit: Bool = false) { // 安全
  321. let windowC = KMSecureEncryptWindowController(windowNibName: "KMSecureEncryptWindowController")
  322. windowC.documentURL = url
  323. windowC.myDocument = CPDFDocument(url: url)
  324. if let _password = password {
  325. windowC.myDocument.unlock(withPassword: _password)
  326. }
  327. self.currentWindowController = windowC
  328. let newDocument: CPDFDocument = CPDFDocument(url: url)
  329. windowC.itemClick = { [weak self] _ in
  330. if let _currentWindow = self?.currentWindowController?.window {
  331. self?.view.window?.endSheet(_currentWindow)
  332. }
  333. self?.currentWindowController = nil
  334. }
  335. windowC.resultCallback = { [weak self] _ in
  336. let windowController_secure: KMSecureEncryptWindowController = self?.currentWindowController as! KMSecureEncryptWindowController
  337. if let _currentWindow = self?.currentWindowController?.window {
  338. self?.view.window?.endSheet(_currentWindow)
  339. }
  340. self?.currentWindowController = nil
  341. if let _password = password {
  342. newDocument.unlock(withPassword: _password)
  343. }
  344. if (!limit) {
  345. newDocument.setPasswordOptions(windowController_secure.options)
  346. let result = KMPasswordInputWindow.saveDocument(newDocument)
  347. if result {
  348. NSWorkspace.shared.activateFileViewerSelecting([newDocument.documentURL])
  349. } else {
  350. let alert = NSAlert()
  351. alert.messageText = NSLocalizedString("Failure", comment: "")
  352. alert.runModal()
  353. }
  354. return
  355. }
  356. NSPanel.savePanel((self?.view.window)!, panel: { panel in
  357. panel.nameFieldStringValue = url.lastPathComponent
  358. }) { response, saveUrl in
  359. if (response == .cancel) {
  360. return
  361. }
  362. if let fileUrl = KMTools.saveWatermarkDocument(document: newDocument, to: saveUrl!, secureOptions: windowController_secure.options) {
  363. NSWorkspace.shared.activateFileViewerSelecting([fileUrl])
  364. } else {
  365. let alert = NSAlert()
  366. alert.messageText = NSLocalizedString("Failure", comment: "")
  367. alert.runModal()
  368. }
  369. }
  370. }
  371. self.view.window?.beginSheet(windowC.window!)
  372. }
  373. }
  374. // MARK: - NSSplitViewDelegate
  375. extension KMHomeViewController: NSSplitViewDelegate {
  376. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  377. return 270.0
  378. }
  379. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  380. return 270.0
  381. }
  382. func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
  383. return false
  384. }
  385. func splitViewDidResizeSubviews(_ notification: Notification) {
  386. let rect = self.homeSplitView.frame
  387. self.leftBox.setFrameSize(CGSize(width: 270, height: rect.size.height))
  388. self.rightBox.frame = CGRect(origin: CGPoint(x: 271.0, y: 0), size: CGSize(width: rect.width - 271.0, height: rect.height))
  389. refreshProductActiveSpacing()
  390. // refreshScrollView()
  391. }
  392. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  393. return proposedPosition
  394. }
  395. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  396. return false
  397. }
  398. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  399. return false
  400. }
  401. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  402. return splitView.isEqual(to: self.homeSplitView)
  403. }
  404. func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
  405. return proposedEffectiveRect
  406. }
  407. func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
  408. return NSMakeRect(0, 0, 0, 0)
  409. }
  410. }
  411. // MARK: - KMHomeFastToolViewControllerDelegate
  412. extension KMHomeViewController: KMHomeFastToolViewControllerDelegate {
  413. func fastToolViewController(_ viewController: KMHomeFastToolViewController, foldChangeState: Bool) {
  414. if foldChangeState {
  415. rightTopBoxHeightConstraint.constant = 230.0 + ScrollerViewWidget
  416. } else {
  417. rightTopBoxHeightConstraint.constant = 326.0 + ScrollerViewWidget
  418. }
  419. }
  420. func fastToolDidSelectAllTools(_ viewController: KMHomeFastToolViewController) {
  421. fastToolDidSelectAllTools()
  422. }
  423. func fastToolViewController(_ viewController: KMHomeFastToolViewController, didSelectItemsAt indexPaths: [Int]) {
  424. for index in indexPaths {
  425. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  426. }
  427. }
  428. }
  429. // MARK: - KMHomeHistoryFileViewControllerDelegate
  430. extension KMHomeViewController: KMHomeHistoryFileViewControllerDelegate {
  431. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, deleteDocuments indexPaths: [URL]) {
  432. historyFile(deleteDocuments: indexPaths)
  433. }
  434. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, didSelectItemsAt indexPaths: [URL]) {
  435. for url in indexPaths {
  436. openHistoryFilePath(url: url)
  437. }
  438. }
  439. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, refreshLayout refresh: Bool) {
  440. self.refreshScrollView()
  441. }
  442. }
  443. // MARK: - KMPDFToolsViewControllerDelegate
  444. extension KMHomeViewController: KMPDFToolsViewControllerDelegate {
  445. func pdfToolsViewController(_ viewController: KMPDFToolsViewController, didSelectItemsAt indexPaths: [Int]) {
  446. for index in indexPaths {
  447. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  448. }
  449. }
  450. }
  451. // MARK: - KMHomeDragViewDelegate
  452. extension KMHomeViewController: KMHomeDragViewDelegate {
  453. func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
  454. if self.aiHomeState == .AITranslation {
  455. self.aiTranslation(withFilePath: filePath.path)
  456. } else {
  457. self.openFile(withFilePath: filePath)
  458. }
  459. }
  460. func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {
  461. if notSupport {
  462. let alert = NSAlert()
  463. alert.alertStyle = .critical
  464. alert.messageText = NSLocalizedString("This file format is not supported. Please enter PDF, picture, or Office file", comment: "")
  465. alert.runModal()
  466. }
  467. }
  468. }