KMHomeViewController+UI.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. let dict: Dictionary = self.productPromotionData[name] as! Dictionary<NSString, NSString>
  17. let subBox = KMBox(frame: CGRect(origin: CGPoint(x: 0, y: height), size: CGSize(width: self.pdfProSeriesBox.frame.width, height: 32.0)))
  18. subBox.boxType = .custom
  19. subBox.borderWidth = 0.0
  20. self.pdfProSeriesBox.contentView?.addSubview(subBox)
  21. subBox.downCallback = {(downEntered, mouseBox, event) -> Void in
  22. if downEntered {
  23. self.productPromotionClickAction(dict["Name"]!)
  24. }
  25. }
  26. let subImageView = NSImageView(frame: NSZeroRect)
  27. subImageView.image = NSImage(named: dict["Image"]! as NSImage.Name)
  28. subBox.addSubview(subImageView)
  29. subImageView.mas_makeConstraints { (make) in
  30. make?.width.height().equalTo()(16)
  31. make?.centerY.equalTo()(0)
  32. make?.left.equalTo()(18)
  33. }
  34. let subLabel = NSTextField(frame: NSZeroRect)
  35. subLabel.isBezeled = false
  36. subLabel.isEditable = false
  37. subLabel.backgroundColor = .clear
  38. let paragraphStyle = NSMutableParagraphStyle()
  39. paragraphStyle.lineSpacing = 22.0
  40. subLabel.attributedStringValue = NSAttributedString(string: dict["Name"]! as String, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  41. subLabel.font = NSFont(name: "SFProText-Regular", size: 14)
  42. subLabel.textColor = NSColor(hex: "#616469")
  43. subLabel.sizeToFit()
  44. subBox.addSubview(subLabel)
  45. subLabel.mas_makeConstraints { (make) in
  46. make?.left.equalTo()(subImageView.mas_right)?.offset()(10)
  47. make?.centerY.equalTo()(0)
  48. }
  49. }
  50. pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  51. self.pdfProSeriesBoxHeightConstraint.constant = CGFloat(pdfProSeries.count) * 32.0
  52. }
  53. } else {
  54. pdfSeriesButtonVC.image = NSImage(named: "icon_btn_expand_lv2_right_false")!
  55. self.pdfProSeriesBox.contentView = NSView.init()
  56. self.pdfProSeriesBoxHeightConstraint.constant = 0.0
  57. }
  58. if others.count > 0 {
  59. if (self.othersBoxHeightConstraint.constant == 0.0) || initialize {
  60. var height = CGFloat(others.count) * 32.0
  61. for name in others {
  62. height -= 32.0
  63. let dict: Dictionary = self.productPromotionData[name] as! Dictionary<NSString, NSString>
  64. let subBox = KMBox(frame: CGRect(x: 0, y: height, width: self.othersBox.frame.width, height: 32.0))
  65. subBox.boxType = .custom
  66. subBox.borderWidth = 0.0
  67. self.othersBox.contentView?.addSubview(subBox)
  68. subBox.downCallback = {(downEntered, mouseBox, event) -> Void in
  69. if downEntered {
  70. self.productPromotionClickAction(dict["Name"]!)
  71. }
  72. }
  73. let subImageView = NSImageView(frame: NSZeroRect)
  74. subImageView.image = NSImage(named: dict["Image"]! as NSImage.Name)
  75. subImageView.imageScaling = .scaleProportionallyDown
  76. subImageView.imageAlignment = .alignCenter
  77. subImageView.sizeToFit()
  78. subBox.addSubview(subImageView)
  79. subImageView.mas_makeConstraints { (make) in
  80. make?.width.height().equalTo()(16)
  81. make?.centerY.equalTo()(0)
  82. make?.left.equalTo()(18)
  83. }
  84. let subLabel = NSTextField(frame: NSZeroRect)
  85. subLabel.isBezeled = false
  86. subLabel.isEditable = false
  87. subLabel.backgroundColor = .clear
  88. let paragraphStyle = NSMutableParagraphStyle()
  89. paragraphStyle.lineSpacing = 22.0
  90. subLabel.attributedStringValue = NSAttributedString(string: dict["Name"]! as String, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  91. subLabel.font = NSFont(name: "SFProText-Regular", size: 14)
  92. subLabel.textColor = NSColor(red: 0.38, green: 0.392, blue: 0.412, alpha: 1.0)
  93. subLabel.sizeToFit()
  94. subBox.addSubview(subLabel)
  95. subLabel.mas_makeConstraints { (make) in
  96. make?.left.equalTo()(subImageView.mas_right)?.offset()(10)
  97. make?.centerY.equalTo()(0)
  98. }
  99. }
  100. self.pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_down_false")!
  101. self.othersBoxHeightConstraint.constant = CGFloat(others.count) * 32.0
  102. }
  103. } else {
  104. self.pdfOthersButtonVC.image = NSImage(named: "icon_btn_expand_lv2_right_false")!
  105. self.othersBox.contentView = NSView.init()
  106. self.othersBoxHeightConstraint.constant = 0.0
  107. }
  108. pdfSeriesButtonVC.updateUI()
  109. pdfOthersButtonVC.updateUI()
  110. }
  111. func refreshRightBoxUI(_ state: KMHomeToolState) {
  112. rightFullBox.isHidden = true
  113. rightTopBox.isHidden = true
  114. rightBottomBox.isHidden = true
  115. homeButtonVC.state = .Norm
  116. pdfToolsButtonVC.state = .Norm
  117. cloudDocumentsButtonVC.state = .Norm
  118. switch state {
  119. case .Home:
  120. rightTopBox.isHidden = false
  121. rightBottomBox.isHidden = false
  122. rightTopBox.contentView = fastToolViewController.view
  123. rightBottomBox.contentView = historyFileViewController.view
  124. rightFullBox.contentView = nil
  125. let isQuickToolsType:Bool = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey")
  126. if isQuickToolsType {
  127. rightTopBoxHeightConstraint.constant = 230 + ScrollerViewWidget
  128. } else {
  129. rightTopBoxHeightConstraint.constant = 326 + ScrollerViewWidget
  130. }
  131. homeButtonVC.state = .Sel
  132. refreshUI()
  133. break
  134. case .PDFTools:
  135. rightFullBox.isHidden = false
  136. rightFullBox.contentView = pdfToolsViewController.view
  137. rightTopBox.contentView = nil
  138. rightBottomBox.contentView = nil
  139. pdfToolsButtonVC.state = .Sel
  140. refreshUI()
  141. break
  142. case .CloudDocuments:
  143. rightFullBox.isHidden = false
  144. rightFullBox.contentView = cloudDocumentsViewController.view
  145. rightTopBox.contentView = nil
  146. rightBottomBox.contentView = nil
  147. cloudDocumentsButtonVC.state = .Sel
  148. refreshUI()
  149. break
  150. default:
  151. break
  152. }
  153. }
  154. }
  155. // MARK: - NSSplitViewDelegate
  156. extension KMHomeViewController: NSSplitViewDelegate {
  157. func splitView(_ splitView: NSSplitView, constrainMinCoordinate proposedMinimumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  158. return 270.0
  159. }
  160. func splitView(_ splitView: NSSplitView, constrainMaxCoordinate proposedMaximumPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  161. return 270.0
  162. }
  163. func splitView(_ splitView: NSSplitView, shouldAdjustSizeOfSubview view: NSView) -> Bool {
  164. return false
  165. }
  166. func splitViewDidResizeSubviews(_ notification: Notification) {
  167. let rect = self.homeSplitView.frame
  168. self.leftBox.setFrameSize(CGSize(width: 270, height: rect.size.height))
  169. self.rightBox.frame = CGRect(origin: CGPoint(x: 271.0, y: 0), size: CGSize(width: rect.width - 271.0, height: rect.height))
  170. refreshProductActiveSpacing()
  171. }
  172. func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
  173. return proposedPosition
  174. }
  175. func splitView(_ splitView: NSSplitView, canCollapseSubview subview: NSView) -> Bool {
  176. return false
  177. }
  178. func splitView(_ splitView: NSSplitView, shouldCollapseSubview subview: NSView, forDoubleClickOnDividerAt dividerIndex: Int) -> Bool {
  179. return false
  180. }
  181. func splitView(_ splitView: NSSplitView, shouldHideDividerAt dividerIndex: Int) -> Bool {
  182. return splitView.isEqual(to: self.homeSplitView)
  183. }
  184. func splitView(_ splitView: NSSplitView, effectiveRect proposedEffectiveRect: NSRect, forDrawnRect drawnRect: NSRect, ofDividerAt dividerIndex: Int) -> NSRect {
  185. return proposedEffectiveRect
  186. }
  187. func splitView(_ splitView: NSSplitView, additionalEffectiveRectOfDividerAt dividerIndex: Int) -> NSRect {
  188. return NSMakeRect(0, 0, 0, 0)
  189. }
  190. }
  191. // MARK: - KMHomeFastToolViewControllerDelegate
  192. extension KMHomeViewController: KMHomeFastToolViewControllerDelegate {
  193. func fastToolViewController(_ viewController: KMHomeFastToolViewController, foldChangeState: Bool) {
  194. if foldChangeState {
  195. rightTopBoxHeightConstraint.constant = 230.0 + ScrollerViewWidget
  196. } else {
  197. rightTopBoxHeightConstraint.constant = 326.0 + ScrollerViewWidget
  198. }
  199. }
  200. func fastToolDidSelectAllTools(_ viewController: KMHomeFastToolViewController) {
  201. fastToolDidSelectAllTools()
  202. }
  203. func fastToolViewController(_ viewController: KMHomeFastToolViewController, didSelectItemsAt indexPaths: [Int]) {
  204. for index in indexPaths {
  205. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  206. }
  207. }
  208. }
  209. // MARK: - KMHomeHistoryFileViewControllerDelegate
  210. extension KMHomeViewController: KMHomeHistoryFileViewControllerDelegate {
  211. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, deleteDocuments indexPaths: [URL]) {
  212. historyFile(deleteDocuments: indexPaths)
  213. }
  214. func historyFileViewController(_ viewController: KMHomeHistoryFileViewController, didSelectItemsAt indexPaths: [URL]) {
  215. for url in indexPaths {
  216. openHistoryFilePath(url: url)
  217. }
  218. }
  219. }
  220. // MARK: - KMPDFToolsViewControllerDelegate
  221. extension KMHomeViewController: KMPDFToolsViewControllerDelegate {
  222. func pdfToolsViewController(_ viewController: KMPDFToolsViewController, didSelectItemsAt indexPaths: [Int]) {
  223. for index in indexPaths {
  224. fastToolItemAction(DataNavigationViewButtonActionType(rawValue: index) ?? .Batch)
  225. }
  226. }
  227. }
  228. // MARK: - KMHomeDragViewDelegate
  229. extension KMHomeViewController: KMHomeDragViewDelegate {
  230. func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
  231. self.openFile(withFilePath: filePath)
  232. }
  233. }