KMHomeFilesHeaderView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // KMHomeFilesHeaderView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMHomeFilesHeaderViewDelegate: AnyObject {
  10. @objc optional func homeFilesShowModeDidUpdate(_ view: KMHomeFilesHeaderView)
  11. @objc optional func homeFilesHeaderViewDeleteButtonClicked(_ view: KMHomeFilesHeaderView)
  12. }
  13. public class KMHomeFilesHeaderView: BaseXibView {
  14. @IBOutlet var contendBox: NSBox!
  15. @IBOutlet var titleLabel: NSTextField!
  16. @IBOutlet var typeSegmented: ComponentSegmented!
  17. @IBOutlet var deleteButton: ComponentButton!
  18. let thumbProperty = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "file_thumb"), iconImage_active: NSImage(named: "file_thumb_active"))
  19. let listProperty = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "file_list"), iconImage_active: NSImage(named: "file_list_active"))
  20. weak open var delegate: KMHomeFilesHeaderViewDelegate?
  21. public override func draw(_ dirtyRect: NSRect) {
  22. super.draw(dirtyRect)
  23. // Drawing code here.
  24. }
  25. public required init?(coder decoder: NSCoder) {
  26. super.init(coder: decoder)
  27. }
  28. override init(frame frameRect: NSRect) {
  29. super.init(frame: frameRect)
  30. }
  31. public override func awakeFromNib() {
  32. super.awakeFromNib()
  33. titleLabel.stringValue = KMLocalizedString("Recent")
  34. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  35. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  36. setUpProperty()
  37. reloadData()
  38. updateDeleteButtonState()
  39. }
  40. func setUpProperty() {
  41. if true {
  42. var itemArr: [ComponentSegmentedProperty] = []
  43. itemArr.append(thumbProperty)
  44. itemArr.append(listProperty)
  45. typeSegmented.updateItemProperty(itemArr)
  46. }
  47. typeSegmented.delegate = self
  48. deleteButton.properties = ComponentButtonProperty(type: .default_tertiary,
  49. size: .xxs,
  50. state: .normal,
  51. onlyIcon: true,
  52. keepPressState: false)
  53. deleteButton.properties.propertyInfo.leftIcon_nor = NSImage(named: "file_delete")
  54. deleteButton.properties.propertyInfo.leftIcon_dis = NSImage(named: "file_delete_dis")
  55. deleteButton.reloadData()
  56. deleteButton.setTarget(self, action: #selector(deleteButtonClicked(_:)))
  57. }
  58. private func reloadData() {
  59. if HistoryFilesManager.manager.showMode == .Thumbnail {
  60. thumbProperty.active = true
  61. listProperty.active = false
  62. } else if HistoryFilesManager.manager.showMode == .List {
  63. thumbProperty.active = false
  64. listProperty.active = true
  65. }
  66. typeSegmented.reloadData()
  67. }
  68. func updateDeleteButtonState() {
  69. deleteButton.properties.isDisabled = false
  70. deleteButton.reloadData()
  71. }
  72. @objc func deleteButtonClicked(_ sender: NSView) {
  73. delegate?.homeFilesHeaderViewDeleteButtonClicked?(self)
  74. }
  75. //MARK: - MouseEvent
  76. }
  77. extension KMHomeFilesHeaderView: ComponentSegmentedDelegate {
  78. public func componentSegmentedDidSelected(_ view: ComponentSegmented, item: ComponentSegmentedItem) {
  79. if item.properties == thumbProperty {
  80. HistoryFilesManager.manager.showMode = .Thumbnail
  81. } else if item.properties == listProperty {
  82. HistoryFilesManager.manager.showMode = .List
  83. }
  84. self.reloadData()
  85. self.delegate?.homeFilesShowModeDidUpdate?(self)
  86. }
  87. }