KMHomeContentView.swift 10 KB

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