KMHomeContentView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // KMHomeContentView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/27.
  6. //
  7. import Cocoa
  8. typealias KMHomeContentViewHistoryDidSelect = (_ view: KMHomeContentView, _ url: KMFile) -> Void
  9. typealias KMHomeContentViewHistoryShowPathAction = (_ view: KMHomeContentView, _ url: KMFile) -> Void
  10. typealias KMHomeContentViewHistoryrRemoveFileAction = (_ view: KMHomeContentView, _ url: KMFile) -> Void
  11. typealias KMHomeContentViewHistoryrRemoveAllFileAction = (_ view: KMHomeContentView) -> Void
  12. typealias KMHomeContentViewQucikToolsDidSelect = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void
  13. typealias KMHomeContentViewQucikToolsAddAction = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void
  14. typealias KMHomeContentViewQucikToolsRemoveAction = (_ view: KMHomeContentView, _ item: KMQucikToolsModel) -> Void
  15. let kRecentlyListModel = "kRecentlyListModel"
  16. let kQucikToolsViewShow = "kQucikToolsViewShow"
  17. enum KMHomeQucikToolsShowType: Int {
  18. case expand = 0
  19. case collapse
  20. }
  21. class KMHomeContentView: KMBaseXibView {
  22. @IBOutlet var lynxGuideContendView: NSView!
  23. @IBOutlet weak var qucikToolsLabel: NSTextField!
  24. @IBOutlet weak var qucikToolsView: KMQucikToolsView!
  25. @IBOutlet weak var leftButton: NSButton!
  26. @IBOutlet weak var pageLabel: NSTextField!
  27. @IBOutlet weak var rightButton: NSButton!
  28. @IBOutlet weak var moreButton: KMButton!
  29. @IBOutlet weak var qucikToolsConstraintHeight: NSLayoutConstraint!
  30. @IBOutlet var quickToolsTopConstraint: NSLayoutConstraint!
  31. @IBOutlet weak var recentlyLabel: NSTextField!
  32. @IBOutlet weak var historyListView: KMHomeHistoryListView!
  33. @IBOutlet weak var horizontalButton: KMButton!
  34. @IBOutlet weak var verticalButton: KMButton!
  35. @IBOutlet weak var cleanButton: KMButton!
  36. var lynxGuideView: HomeLynxGuide!
  37. var historyDidSelect: KMHomeContentViewHistoryDidSelect?
  38. var historyShowPathAction: KMHomeContentViewHistoryShowPathAction?
  39. var historyRemoveFileAction: KMHomeContentViewHistoryrRemoveFileAction?
  40. var historyRemoveAllFileAction: KMHomeContentViewHistoryrRemoveAllFileAction?
  41. var qucikToolsDidSelect: KMHomeContentViewQucikToolsDidSelect?
  42. var qucikToolsAddAction: KMHomeContentViewQucikToolsAddAction?
  43. var qucikToolsRemoveAction: KMHomeContentViewQucikToolsRemoveAction?
  44. var qucikToolsType: KMHomeQucikToolsShowType = .expand {
  45. didSet {
  46. self.qucikToolsView.type = qucikToolsType
  47. if qucikToolsType == .collapse {
  48. self.qucikToolsConstraintHeight.constant = 166
  49. } else {
  50. self.qucikToolsConstraintHeight.constant = 262
  51. }
  52. }
  53. }
  54. deinit {
  55. print("KMHomeContentView deinit")
  56. }
  57. override func draw(_ dirtyRect: NSRect) {
  58. super.draw(dirtyRect)
  59. // Drawing code here.
  60. self.setup()
  61. }
  62. override func setup() {
  63. self.qucikToolsView.didSelect = { [unowned self] view, item in
  64. guard let callBack = qucikToolsDidSelect else { return }
  65. callBack(self, item)
  66. }
  67. self.qucikToolsView.pageChange = { [unowned self] view in
  68. self.updatePageLabel()
  69. }
  70. self.qucikToolsView.addAction = { [unowned self] view, item in
  71. self.qucikToolsAddAction?(self, item)
  72. }
  73. self.qucikToolsView.removeAction = { [unowned self] view, item in
  74. self.qucikToolsRemoveAction?(self, item)
  75. }
  76. self.historyListView.didSelect = { [unowned self] view, item in
  77. guard let callBack = historyDidSelect else { return }
  78. callBack(self, item)
  79. }
  80. self.historyListView.removeFileAction = { [unowned self] view, item in
  81. guard let callBack = historyRemoveFileAction else { return }
  82. callBack(self, item)
  83. }
  84. self.historyListView.showPathAction = { [unowned self] view, item in
  85. guard let callBack = historyShowPathAction else { return }
  86. callBack(self, item)
  87. }
  88. cleanButton.mouseMoveCallback = { [unowned self] enter in
  89. if self.historyListView.data.count == 0 {
  90. self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletNor")
  91. } else {
  92. if enter {
  93. self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletHor")
  94. } else {
  95. self.cleanButton.image = NSImage(named: "KMImageNameHomeDeletNor")
  96. }
  97. }
  98. }
  99. moreButton.mouseMoveCallback = { [unowned self] enter in
  100. if enter {
  101. self.moreButton.image = NSImage(named: "KMImageNameUXIconHomeMorePre")
  102. } else {
  103. self.moreButton.image = NSImage(named: "KMImageNameUXIconHomeMoreNor")
  104. }
  105. }
  106. let type = UserDefaults.standard.object(forKey: kRecentlyListModel) ?? 0
  107. historyListView.showType = KMHomeHistoryListViewShowType.init(rawValue: type as! Int) ?? .horizontal
  108. let type2 = UserDefaults.standard.object(forKey: kQucikToolsViewShow) ?? 0
  109. self.qucikToolsType = KMHomeQucikToolsShowType.init(rawValue: type2 as! Int) ?? .expand
  110. self.updatePageLabel()
  111. self.updateRecentlyListState()
  112. self.configLynxGuideView()
  113. }
  114. func updatePageLabel() {
  115. self.pageLabel.stringValue = "\(qucikToolsView.currentPage()) / \(max(qucikToolsView.pageCount(), 1))"
  116. }
  117. func updateRecentlyListState() {
  118. let showType = self.historyListView.showType
  119. if showType == .horizontal {
  120. verticalButton.image = NSImage(named: "KMImageNameUXIconHomeViewthumbnailNor")
  121. horizontalButton.image = NSImage(named: "KMImageNameUXIconHomeViewlistPre")
  122. } else if showType == .vertical {
  123. verticalButton.image = NSImage(named: "KMImageNameUXIconHomeViewthumbnailPre")
  124. horizontalButton.image = NSImage(named: "KMImageNameUXIconHomeViewlistNor")
  125. }
  126. UserDefaults.standard.setValue(showType.rawValue, forKey: kRecentlyListModel)
  127. }
  128. override func updateUI() {
  129. self.contentView.wantsLayer = true
  130. if(KMAppearance.isDarkMode()){
  131. self.contentView.layer?.backgroundColor = NSColor.init(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  132. } else {
  133. self.contentView.layer?.backgroundColor = NSColor.init(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  134. }
  135. self.pageLabel.textColor = KMAppearance.Layout.h0Color()
  136. self.qucikToolsLabel.textColor = KMAppearance.Layout.h0Color()
  137. self.recentlyLabel.textColor = KMAppearance.Layout.h0Color()
  138. self.lynxGuideView.reloadData()
  139. }
  140. override func updateLanguage() {
  141. self.qucikToolsLabel.stringValue = NSLocalizedString("Advanced Editing Tools", comment: "")
  142. self.recentlyLabel.stringValue = NSLocalizedString("Recent", comment: "");
  143. self.horizontalButton.toolTip = NSLocalizedString("Thumbnails", comment: "")
  144. self.verticalButton.toolTip = NSLocalizedString("List", comment: "")
  145. self.cleanButton.toolTip = NSLocalizedString("Delete",comment: "")
  146. }
  147. override func reloadData() {
  148. self.historyListView.reloadData()
  149. if self.historyListView.data.count == 0 {
  150. self.cleanButton.isEnabled = false
  151. } else {
  152. self.cleanButton.isEnabled = true
  153. }
  154. var showLynxGuide = true
  155. if VerificationManager.default().isVerificationCode {
  156. showLynxGuide = false
  157. }
  158. if showLynxGuide {
  159. self.quickToolsTopConstraint.constant = 250
  160. self.lynxGuideContendView.isHidden = false
  161. } else {
  162. self.quickToolsTopConstraint.constant = 14
  163. self.lynxGuideContendView.isHidden = true
  164. }
  165. }
  166. func configLynxGuideView() {
  167. if self.lynxGuideView == nil {
  168. self.lynxGuideView = HomeLynxGuide.createFromNib()
  169. }
  170. self.lynxGuideView?.frame = self.lynxGuideContendView.bounds
  171. self.lynxGuideView?.autoresizingMask = [.width, .height]
  172. self.lynxGuideContendView.addSubview(self.lynxGuideView)
  173. }
  174. }
  175. //MARK: Action
  176. extension KMHomeContentView {
  177. @IBAction func leftButtonAction(_ sender: Any) {
  178. self.qucikToolsView.previousPage()
  179. }
  180. @IBAction func rightButtonAction(_ sender: Any) {
  181. self.qucikToolsView.nextPage()
  182. }
  183. @IBAction func moreButtonAction(_ sender: NSButton) {
  184. let collapse = NSLocalizedString("Collapse All", comment: "")
  185. let quickTools = NSLocalizedString("Manage Quick Tools", comment: "")
  186. let expand = NSLocalizedString("Expand All", comment: "")
  187. var array: [String] = []
  188. if qucikToolsType == .expand {
  189. array = [collapse, quickTools]
  190. } else {
  191. array = [expand, quickTools]
  192. }
  193. let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(array)
  194. vc.background = KMAppearance.Layout.bgColor()
  195. vc.textColor = KMAppearance.Layout.h0Color()
  196. vc.enterFillColor = KMAppearance.Interactive.s0Color()
  197. let createFilePopover: NSPopover = NSPopover.init()
  198. createFilePopover.contentViewController = vc
  199. createFilePopover.delegate = self
  200. createFilePopover.animates = true
  201. createFilePopover.behavior = .semitransient
  202. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  203. createFilePopover.show(relativeTo: CGRect(x: sender.bounds.origin.x, y: 10, width: sender.bounds.size.width, height: sender.bounds.size.height), of: sender, preferredEdge: .maxY)
  204. // vc.customBoxWidthLayoutConstraint.constant = sender.frame.width
  205. vc.downCallback = { [unowned self] (downEntered: Bool, count: String) -> Void in
  206. if downEntered {
  207. createFilePopover.close()
  208. if count == collapse {
  209. self.qucikToolsType = .collapse
  210. } else if count == expand {
  211. self.qucikToolsType = .expand
  212. } else if count == quickTools {
  213. let homeQuickToolsWindowController: KMHomeQuickToolsWindowController = KMHomeQuickToolsWindowController.sharedHomeQuickTools
  214. homeQuickToolsWindowController.window?.center()
  215. NSWindow.currentWindow().addChildWindow(homeQuickToolsWindowController.window!, ordered: NSWindow.OrderingMode.above)
  216. homeQuickToolsWindowController.dataChange = { [unowned self] controller in
  217. self.qucikToolsView.reloadData()
  218. }
  219. }
  220. self.updatePageLabel()
  221. UserDefaults.standard.setValue(qucikToolsType.rawValue, forKey: kQucikToolsViewShow)
  222. }
  223. }
  224. }
  225. @IBAction func horizontalButtonAction(_ sender: Any) {
  226. self.historyListView.showType = .horizontal
  227. self.updateRecentlyListState()
  228. }
  229. @IBAction func verticalButtonAction(_ sender: Any) {
  230. self.historyListView.showType = .vertical
  231. self.updateRecentlyListState()
  232. }
  233. @IBAction func cleanButtonAction(_ sender: Any) {
  234. guard let callBack = historyRemoveAllFileAction else { return }
  235. callBack(self)
  236. }
  237. }
  238. extension KMHomeContentView: NSPopoverDelegate {
  239. }