-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteData.cs
More file actions
138 lines (123 loc) · 4.76 KB
/
NoteData.cs
File metadata and controls
138 lines (123 loc) · 4.76 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
134
135
136
137
138
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace StickyNote
{
public class NoteData
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Title { get; set; } = "";
public string Content { get; set; } = ""; // RTF 格式
public double Left { get; set; }
public double Top { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public bool IsTopmost { get; set; }
public string ColorHex { get; set; } = "#E3C887";
public double Opacity { get; set; } = 1.0;
public float FontSize { get; set; } = 13f;
public bool IsBold { get; set; }
public bool IsItalic { get; set; }
public bool IsUnderline { get; set; }
public bool IsStrikethrough { get; set; }
public string Alignment { get; set; } = "Left";
public bool IsCustomTitle { get; set; } = false;
// P1: 纯文本预览缓存(不序列化,运行时生成)
[JsonIgnore]
public string PlainPreview { get; set; } = "";
}
/// <summary>
/// 应用级设置(窗口位置、分隔条宽度等)— 保存在独立 JSON 中
/// </summary>
public class AppSettings
{
public int WindowLeft { get; set; } = -1;
public int WindowTop { get; set; } = -1;
public int WindowWidth { get; set; } = 520;
public int WindowHeight { get; set; } = 340;
public int SplitterWidth { get; set; } = 110;
}
public static class NoteManager
{
private static readonly string DataDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"StickyNote");
private static readonly string FilePath = Path.Combine(DataDir, "StickyNotes_Data.json");
private static readonly string SettingsPath = Path.Combine(DataDir, "AppSettings.json");
private static List<NoteData> _notes = new();
private static NoteData? _lastDeleted;
private static AppSettings _settings = new();
public static AppSettings Settings => _settings;
public static void AddNote(NoteData note)
{
_notes.Add(note);
SaveNotes();
}
public static void RemoveNote(NoteData note)
{
var target = _notes.FirstOrDefault(n => n.Id == note.Id);
if (target != null)
{
_lastDeleted = target;
_notes.Remove(target);
}
SaveNotes();
}
public static void SaveNotes()
{
try
{
Directory.CreateDirectory(DataDir);
string json = JsonSerializer.Serialize(_notes, new JsonSerializerOptions { WriteIndented = false });
string temp = FilePath + ".tmp";
string bak = FilePath + ".bak";
File.WriteAllText(temp, json);
if (File.Exists(FilePath)) File.Replace(temp, FilePath, bak);
else File.Move(temp, FilePath);
}
catch { }
}
public static List<NoteData> LoadNotes()
{
if (!File.Exists(FilePath)) { _notes = new(); return _notes; }
try
{
string json = File.ReadAllText(FilePath);
_notes = JsonSerializer.Deserialize<List<NoteData>>(json) ?? new();
_notes = _notes.GroupBy(n => n.Id).Select(g => g.Last()).ToList();
return _notes;
}
catch { _notes = new(); return _notes; }
}
public static IReadOnlyList<NoteData> GetAll() => _notes;
public static bool RestoreLastDeleted()
{
if (_lastDeleted == null) return false;
_notes.Add(_lastDeleted);
_lastDeleted = null;
SaveNotes();
return true;
}
// ── 应用设置 ────────────────────────────────────────────────
public static void LoadSettings()
{
try
{
if (File.Exists(SettingsPath))
_settings = JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(SettingsPath)) ?? new();
}
catch { _settings = new(); }
}
public static void SaveSettings()
{
try
{
Directory.CreateDirectory(DataDir);
File.WriteAllText(SettingsPath, JsonSerializer.Serialize(_settings, new JsonSerializerOptions { WriteIndented = false }));
}
catch { }
}
}
}