|
@@ -8,10 +8,10 @@
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
|
-typealias KMHomeHistoryCollectionItemShowPathAction = (_ view: KMHomeHistoryCollectionItem) -> Void
|
|
|
-typealias KMHomeHistoryCollectionItemRemovePathAction = (_ view: KMHomeHistoryCollectionItem) -> Void
|
|
|
-typealias KMHomeHistoryCollectionItemMouseMoveAction = (_ view: KMHomeHistoryCollectionItem, _ enter: Bool) -> Void
|
|
|
-typealias KMHomeHistoryCollectionItemMouseDobleClickAction = (_ view: KMHomeHistoryCollectionItem, _ url: URL) -> Void
|
|
|
+typealias KMHomeHistoryCollectionItemShowPathAction = (_ view: KMHomeHistoryCollectionItem, _ item: KMHomeHistoryListViewModel) -> Void
|
|
|
+typealias KMHomeHistoryCollectionItemRemovePathAction = (_ view: KMHomeHistoryCollectionItem, _ item: KMHomeHistoryListViewModel) -> Void
|
|
|
+//typealias KMHomeHistoryCollectionItemMouseMoveAction = (_ view: KMHomeHistoryCollectionItem, _ enter: Bool) -> Void
|
|
|
+typealias KMHomeHistoryCollectionItemMouseDobleClickAction = (_ view: KMHomeHistoryCollectionItem, _ item: KMHomeHistoryListViewModel) -> Void
|
|
|
|
|
|
class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
|
|
@@ -32,7 +32,7 @@ class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
@IBOutlet weak var verticalTitleLabel: NSTextField!
|
|
|
@IBOutlet weak var showImageView: NSImageView!
|
|
|
|
|
|
- var url: URL? {
|
|
|
+ var model: KMHomeHistoryListViewModel? {
|
|
|
didSet {
|
|
|
self.reloadData()
|
|
|
}
|
|
@@ -45,7 +45,7 @@ class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
|
|
|
var showPathAction: KMHomeHistoryCollectionItemShowPathAction?
|
|
|
var removeAction: KMHomeHistoryCollectionItemRemovePathAction?
|
|
|
- var mouseMoveAction: KMHomeHistoryCollectionItemMouseMoveAction?
|
|
|
+// var mouseMoveAction: KMHomeHistoryCollectionItemMouseMoveAction?
|
|
|
var mouseDobleClickAction: KMHomeHistoryCollectionItemMouseDobleClickAction?
|
|
|
|
|
|
|
|
@@ -53,7 +53,7 @@ class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
super.viewDidLoad()
|
|
|
// Do view setup here.
|
|
|
// 使用自动布局设置 item 宽度等于父视图的宽度
|
|
|
- view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
+// view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
self.setup()
|
|
|
}
|
|
@@ -66,6 +66,31 @@ class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
self.view.wantsLayer = true;
|
|
|
self.view.layer?.cornerRadius = 4.0
|
|
|
|
|
|
+ self.iconImageView.backgroundColor(NSColor.red)
|
|
|
+
|
|
|
+ self.contentBox.border(NSColor.clear, 0, 8)
|
|
|
+ self.contentBox.moveCallback = { [unowned self] mouseEntered, mouseBox in
|
|
|
+ if mouseEntered {
|
|
|
+ if self.showType == .horizontal {
|
|
|
+ self.handleView.isHidden = false
|
|
|
+ self.contentBox.fillColor = KMAppearance.Status.hovColor()
|
|
|
+ } else {
|
|
|
+ self.showImageView.border(KMAppearance.Interactive.a0Color(), 1, 4)
|
|
|
+ self.verticalTitleLabel.backgroundColor(KMAppearance.Interactive.a0Color())
|
|
|
+ self.verticalTitleLabel.textColor = KMAppearance.Layout.w0Color()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if self.showType == .horizontal {
|
|
|
+ self.handleView.isHidden = true
|
|
|
+ self.contentBox.fillColor = NSColor.clear
|
|
|
+ } else {
|
|
|
+ self.showImageView.border(NSColor.clear, 1, 4)
|
|
|
+ self.verticalTitleLabel.backgroundColor(NSColor.clear)
|
|
|
+ self.verticalTitleLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
showPathButton.toolTip = NSLocalizedString("Show in Finder", comment: "")
|
|
|
removePathButton.toolTip = NSLocalizedString("Remove from Recents", comment: "")
|
|
|
handleView.isHidden = true;
|
|
@@ -97,9 +122,16 @@ class KMHomeHistoryCollectionItem: NSCollectionViewItem {
|
|
|
removePathButton.image = NSImage(named: "KMImageNameUXIconHomeRemoveNor")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ //vertical
|
|
|
+ self.showImageView.border(NSColor.clear, 1, 4)
|
|
|
+ self.verticalTitleLabel.backgroundColor(NSColor.clear)
|
|
|
+ self.verticalTitleLabel.textColor = KMAppearance.Layout.h0Color()
|
|
|
}
|
|
|
|
|
|
func reloadData() {
|
|
|
+ self.contentBox.updateTrackingAreas()
|
|
|
+
|
|
|
self.updateTypeData()
|
|
|
self.updateFilesData()
|
|
|
}
|
|
@@ -126,123 +158,22 @@ extension KMHomeHistoryCollectionItem {
|
|
|
}
|
|
|
|
|
|
func updateFilesData() -> Void {
|
|
|
- guard let fileUrl = url else { return }
|
|
|
+ guard let model = model else { return }
|
|
|
|
|
|
- let attrib = try? FileManager.default.attributesOfItem(atPath: fileUrl.path) as? Dictionary<FileAttributeKey , Any>
|
|
|
- if attrib != nil {
|
|
|
- let dateFormatter: DateFormatter = DateFormatter.init()
|
|
|
- let fileDate: Date = attrib![FileAttributeKey(rawValue: "NSFileModificationDate")] as! Date
|
|
|
- var fileTime: String = ""
|
|
|
- if fileDate.isToday() {
|
|
|
- dateFormatter.dateFormat = "HH:mm"
|
|
|
- } else if self.isDateInCurrentWeek(fileDate) {
|
|
|
- dateFormatter.dateFormat = "EEE, HH:mm"
|
|
|
- } else {
|
|
|
- dateFormatter.dateFormat = "MMM d, yyyy"
|
|
|
- }
|
|
|
+ horizontalTitleLabel.stringValue = model.fileName
|
|
|
+ verticalTitleLabel.stringValue = model.fileName
|
|
|
+ pathLabel.stringValue = model.showPath
|
|
|
|
|
|
- let fileName = fileUrl.lastPathComponent
|
|
|
-// let fileType = url.pathExtension.isEmpty ? "" : url.pathExtension
|
|
|
- let fileType = ""
|
|
|
- let sizeFloat: Float = attrib![FileAttributeKey(rawValue: "NSFileSize")] as? Float ?? 0.0
|
|
|
- let fileSize = fileSizeString(sizeFloat).isEmpty ? "" : fileSizeString(sizeFloat)
|
|
|
- let lastTime = dateFormatter.string(from: fileDate)
|
|
|
- if fileDate.isToday() {
|
|
|
- fileTime = String(format: "%@, %@", NSLocalizedString("Today", comment: ""), lastTime)
|
|
|
- } else if isDateInCurrentWeek(fileDate) {
|
|
|
- fileTime = lastTime
|
|
|
- } else {
|
|
|
- fileTime = lastTime
|
|
|
- }
|
|
|
-
|
|
|
- let paragraphStyle = NSMutableParagraphStyle()
|
|
|
- paragraphStyle.lineSpacing = 22.0
|
|
|
- horizontalTitleLabel.stringValue = fileName
|
|
|
- verticalTitleLabel.stringValue = fileName
|
|
|
-
|
|
|
- //path
|
|
|
- let array = fileUrl.path
|
|
|
- .deletingLastPathComponent
|
|
|
- .components(separatedBy: "/")
|
|
|
- let string = NSMutableString()
|
|
|
+ let paragraphStyle = NSMutableParagraphStyle()
|
|
|
+ paragraphStyle.lineSpacing = 22.0
|
|
|
+ sizeLabel.attributedStringValue = NSAttributedString(string: model.fileSize, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
+ timeLabel.attributedStringValue = NSAttributedString(string: model.modificationTime, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
|
|
|
- if array.count > 4 {
|
|
|
- string.append(".../")
|
|
|
- string.append(array[array.count - 3])
|
|
|
- string.append("/")
|
|
|
- string.append(array[array.count - 2])
|
|
|
- string.append("/")
|
|
|
- string.append(array[array.count - 1])
|
|
|
- } else {
|
|
|
- string.setString(fileUrl.path.deletingLastPathComponent)
|
|
|
- }
|
|
|
+
|
|
|
+ showImageView.image = model.iconImage
|
|
|
+
|
|
|
+ self.view.toolTip = model.fileName
|
|
|
|
|
|
- pathLabel.stringValue = string as String
|
|
|
-
|
|
|
- // documentName.attributedStringValue = NSAttributedString(string: fileName, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
-// documentType.attributedStringValue = NSAttributedString(string: fileType, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
- sizeLabel.attributedStringValue = NSAttributedString(string: fileSize, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
- timeLabel.attributedStringValue = NSAttributedString(string: fileTime, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
|
|
|
-// mainBox.toolTip = fileName
|
|
|
-//
|
|
|
-// if selectUrls.contains(fileUrl) {
|
|
|
-// isSelect = true
|
|
|
-// mainBox.fillColor = NSColor.km_init(hex: "#CED0D4", alpha: 0.6)
|
|
|
-// mainBox.borderWidth = 1.0
|
|
|
-// mainBox.borderColor = NSColor.km_init(hex: "#CED0D4")
|
|
|
-// mainBox.cornerRadius = 4.0
|
|
|
-// } else {
|
|
|
-// isSelect = false
|
|
|
-// mainBox.fillColor = .clear
|
|
|
-// mainBox.borderWidth = 0.0
|
|
|
-// mainBox.cornerRadius = 0.0
|
|
|
-// }
|
|
|
-// documentName.backgroundColor = .clear
|
|
|
-// documentName.textColor = NSColor.km_init(hex: "#252629")
|
|
|
-// documentName.font = NSFont(name: "SFProText-Regular", size: 14)
|
|
|
-// documentType.textColor = NSColor.km_init(hex: "#94989C")
|
|
|
-// documentSize.textColor = NSColor.km_init(hex: "#94989C")
|
|
|
-// documentName.backgroundColor = .clear
|
|
|
-// lastModificationTime.textColor = NSColor.km_init(hex: "#94989C")
|
|
|
-// lastModificationTime.backgroundColor = .clear
|
|
|
-// moreButton.image = NSImage(named: "KMHomeMoreTools")
|
|
|
- let image: NSImage = NSImage.previewForFile(path: fileUrl, ofSize: self.view.bounds.size, asIcon: true) ?? NSImage()
|
|
|
- showImageView.image = image
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func fileSizeString(_ fSize: Float) -> String {
|
|
|
- let fileSize = fSize / 1024
|
|
|
- let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
|
|
|
- let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
|
|
|
- return String(format: "%0.1f %@", size, unit)
|
|
|
- }
|
|
|
-
|
|
|
- func isSameWeek (withDate date: Date) -> Bool {
|
|
|
- let currentWeekOfYear = getWeekOfYear(date: Date.init())
|
|
|
- let targetWeekOfYear = getWeekOfYear(date: date)
|
|
|
- if targetWeekOfYear == currentWeekOfYear {
|
|
|
- return false
|
|
|
- } else {
|
|
|
- return true
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func isDateInCurrentWeek(_ date: Date) -> Bool {
|
|
|
- let calendar = Calendar.current
|
|
|
- // 获取当前日期的星期几
|
|
|
- let weekday = calendar.component(.weekday, from: Date())
|
|
|
- // 获取一周的第一天(周日)的日期
|
|
|
- let firstDayOfWeek = calendar.date(byAdding: .day, value: -weekday, to: Date())!
|
|
|
- // 获取一周的最后一天(下周的第一天)的日期
|
|
|
- let lastDayOfWeek = calendar.date(byAdding: .day, value: 7, to: firstDayOfWeek)!
|
|
|
- // 判断日期是否在当前周的范围内
|
|
|
- return date > firstDayOfWeek && date < lastDayOfWeek
|
|
|
- }
|
|
|
-
|
|
|
- func getWeekOfYear(date: Date) -> Int {
|
|
|
- let components = Calendar.current.dateComponents([Calendar.Component.weekOfYear], from: date)
|
|
|
- return components.weekOfYear ?? 0
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -250,34 +181,22 @@ extension KMHomeHistoryCollectionItem {
|
|
|
@objc @IBAction func showPathButtonAction(_ sender: Any) {
|
|
|
guard let callBack = showPathAction else { return }
|
|
|
|
|
|
- callBack(self)
|
|
|
+ callBack(self, model!)
|
|
|
}
|
|
|
|
|
|
@objc @IBAction func removePathButtonAction(_ sender: Any) {
|
|
|
guard let callBack = removeAction else { return }
|
|
|
|
|
|
- callBack(self)
|
|
|
+ callBack(self, model!)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
extension KMHomeHistoryCollectionItem {
|
|
|
- override func mouseEntered(with event: NSEvent) {
|
|
|
- guard let callBack = mouseMoveAction else { return }
|
|
|
-
|
|
|
- callBack(self, true)
|
|
|
- }
|
|
|
-
|
|
|
- override func mouseExited(with event: NSEvent) {
|
|
|
- guard let callBack = mouseMoveAction else { return }
|
|
|
-
|
|
|
- callBack(self, false)
|
|
|
- }
|
|
|
-
|
|
|
override func mouseDown(with event: NSEvent) {
|
|
|
if event.clickCount == 2 {
|
|
|
// 处理双击操作
|
|
|
guard let callBack = mouseDobleClickAction else { return }
|
|
|
- callBack(self, self.url!)
|
|
|
+ callBack(self, self.model!)
|
|
|
} else {
|
|
|
super.mouseDown(with: event)
|
|
|
}
|