Repository navigation
Expand file tree
/
Copy pathClipboardHistoryManager.cs
More file actions
133 lines (117 loc) · 3.75 KB
/
Copy pathClipboardHistoryManager.cs
File metadata and controls
133 lines (117 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Text.Json;
namespace NoteUI;
public class ClipboardHistoryEntry
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string ContentType { get; set; } = "text"; // "text" or "image"
public string? TextContent { get; set; }
public string? RtfContent { get; set; }
public byte[]? ImageData { get; set; }
public string? SourceExePath { get; set; }
public string? SourceTitle { get; set; }
public DateTime CapturedAt { get; set; } = DateTime.Now;
public bool IsPinned { get; set; }
public string Preview => ContentType == "image"
? $"[{Lang.T("clipboard_image")}]"
: string.IsNullOrEmpty(TextContent)
? ""
: TextContent.Length > 150 ? TextContent[..150] : TextContent;
}
public class ClipboardHistoryManager
{
private const int MaxEntries = 50;
private readonly List<ClipboardHistoryEntry> _entries = [];
private readonly string _savePath;
public IReadOnlyList<ClipboardHistoryEntry> Entries => _entries;
public ClipboardHistoryManager()
{
var dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"NoteUI");
_savePath = Path.Combine(dir, "clipboard_history.json");
}
public void Load()
{
try
{
if (!File.Exists(_savePath)) return;
var json = File.ReadAllText(_savePath);
var entries = JsonSerializer.Deserialize<List<ClipboardHistoryEntry>>(json);
if (entries != null)
{
_entries.Clear();
_entries.AddRange(entries);
}
}
catch { }
}
public void Save()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(_savePath)!);
var json = JsonSerializer.Serialize(_entries, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(_savePath, json);
}
catch { }
}
public void AddEntry(ClipboardHistoryEntry entry)
{
_entries.Insert(0, entry);
while (_entries.Count > MaxEntries)
{
var oldest = _entries.LastOrDefault(e => !e.IsPinned);
if (oldest != null)
_entries.Remove(oldest);
else
break;
}
Save();
}
public void RemoveEntry(string id)
{
_entries.RemoveAll(e => e.Id == id);
Save();
}
public void TogglePin(string id)
{
var entry = _entries.FirstOrDefault(e => e.Id == id);
if (entry != null)
{
entry.IsPinned = !entry.IsPinned;
Save();
}
}
public List<ClipboardHistoryEntry> GetSorted()
{
return _entries
.OrderByDescending(e => e.IsPinned)
.ThenByDescending(e => e.CapturedAt)
.ToList();
}
public void Clear()
{
_entries.RemoveAll(e => !e.IsPinned);
Save();
}
public bool IsDuplicate(string? text)
{
if (string.IsNullOrEmpty(text)) return false;
return _entries.Any(e =>
e.ContentType == "text" &&
string.Equals(e.TextContent, text, StringComparison.Ordinal));
}
public static string GetRelativeTime(DateTime dt)
{
var diff = DateTime.Now - dt;
if (diff.TotalMinutes < 1)
return Lang.T("clipboard_just_now");
if (diff.TotalMinutes < 60)
return Lang.T("clipboard_minutes_ago", (int)diff.TotalMinutes);
if (diff.TotalHours < 24)
return Lang.T("clipboard_hours_ago", (int)diff.TotalHours);
if (diff.TotalDays < 2)
return Lang.T("clipboard_yesterday");
return Lang.T("clipboard_days_ago", (int)diff.TotalDays);
}
}