Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Maccy/Clipboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Clipboard {

private var onNewCopyHooks: [OnNewCopyHook] = []
var changeCount: Int
var lastCleanup: Date

private let pasteboard = NSPasteboard.general

Expand Down Expand Up @@ -37,6 +38,7 @@ class Clipboard {

init() {
changeCount = pasteboard.changeCount
lastCleanup = Date()
}

func onNewCopy(_ hook: @escaping OnNewCopyHook) {
Expand Down Expand Up @@ -148,6 +150,15 @@ class Clipboard {
@objc
@MainActor
func checkForChangesInPasteboard() { // swiftlint:disable:this cyclomatic_complexity

let cleanUpInterval = (Defaults[.time] * 3600) / 2

if (Date().timeIntervalSince(lastCleanup) > cleanUpInterval) {
// Remove items older than time limit in background
History.shared.removeOldestItem(to: Defaults[.time])
lastCleanup = Date()
}

guard pasteboard.changeCount != changeCount else {
return
}
Expand Down
1 change: 1 addition & 0 deletions Maccy/Extensions/Defaults.Keys+Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension Defaults.Keys {
static let showSpecialSymbols = Key<Bool>("showSpecialSymbols", default: true)
static let showTitle = Key<Bool>("showTitle", default: true)
static let size = Key<Int>("historySize", default: 200)
static let time = Key<Double>("timeLimit", default: 8)
static let sortBy = Key<Sorter.By>("sortBy", default: .lastCopiedAt)
static let suppressClearAlert = Key<Bool>("suppressClearAlert", default: false)
static let windowSize = Key<NSSize>("windowSize", default: NSSize(width: 450, height: 800))
Expand Down
28 changes: 24 additions & 4 deletions Maccy/Observables/History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length
let results = try Storage.shared.context.fetch(descriptor)
all = sorter.sort(results).map { HistoryItemDecorator($0) }
items = all


// check and remove older items
removeOldestItem(to: Defaults[.time])

limitHistorySize(to: Defaults[.size])

updateShortcuts()
Expand All @@ -116,6 +119,23 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length
AppState.shared.popup.needsResize = true
}
}

@MainActor
func removeOldestItem(to timeInHours: Double) {
if (timeInHours == 0) {
return
}
if (all.isEmpty) {
return
}
let timeInSeconds = timeInHours * 3600
let oldest = all.filter{
$0.isUnpinned && (Date().timeIntervalSince($0.item.lastCopiedAt) > timeInSeconds)
};
if oldest.count != 0 {
oldest.forEach(delete)
}
}

@MainActor
private func limitHistorySize(to maxSize: Int) {
Expand Down Expand Up @@ -170,7 +190,9 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length
// Remove exceeding items. Do this after the item is added to avoid removing something
// if a duplicate was found as then the size already stayed the same.
limitHistorySize(to: Defaults[.size] - 1)


removeOldestItem(to: Defaults[.time])

sessionLog[Clipboard.shared.changeCount] = item

var itemDecorator: HistoryItemDecorator
Expand Down Expand Up @@ -473,7 +495,6 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length

return item
}

updateUnpinnedShortcuts()
}

Expand All @@ -483,7 +504,6 @@ class History: ItemsContainer { // swiftlint:disable:this type_body_length
item.shortcuts = KeyShortcut.create(character: pin)
}
}

updateUnpinnedShortcuts()
}

Expand Down
15 changes: 15 additions & 0 deletions Maccy/Settings/StorageSettingsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct StorageSettingsPane: View {
}

@Default(.size) private var size
@Default(.time) private var time
@Default(.sortBy) private var sortBy

@State private var viewModel = ViewModel()
Expand Down Expand Up @@ -108,6 +109,20 @@ struct StorageSettingsPane: View {
}
}
}

Settings.Section(label: { Text("Time", tableName: "StorageSettings") }) {
HStack {
TextField("", value: $time, formatter: sizeFormatter)
.frame(width: 80)
.help(Text("TimeToolTip", tableName: "StorageSettings"))
Stepper("", value: $time, in: 0...48)
.labelsHidden()
Text(time != 1 ? "hours" : "hour")
.controlSize(.small)
.foregroundStyle(.gray)
.help(Text("TimeToolTip", tableName: "StorageSettings"))
}
}

Settings.Section(label: { Text("SortBy", tableName: "StorageSettings") }) {
Picker("", selection: $sortBy) {
Expand Down
2 changes: 2 additions & 0 deletions Maccy/Settings/en.lproj/StorageSettings.strings
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"Size" = "Size:";
"SizeTooltip" = "Number of history items to keep.\nDefault: 200.";
"CurrentSizeTooltip" = "Current size on disk.";
"Time" = "Keep for:";
"TimeToolTip" = "How long to keep items in history.\nDefault: 8 hours.";
"SortBy" = "Sort by:";
"LastCopiedAt" = "Time of last copy";
"FirstCopiedAt" = "Time of first copy";
Expand Down