forked from changbowen/DesktopNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setting.cs
287 lines (256 loc) · 9.94 KB
/
Setting.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Xml.Linq;
namespace DesktopNote
{
public class Setting : INotifyPropertyChanged
{
internal MainWindow MainWin;
internal const string SettingBeginMark = "-----SETTINGS_BEGIN-----";
internal const string ContentBeginMark = "-----CONTENTS_BEGIN-----";
internal const string SettingEndMark = "-----SETTINGS_END-----";
internal const string ContentEndMark = "-----CONTENTS_END-----";
private const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
public event PropertyChangedEventHandler PropertyChanged;
internal readonly NoteFlag Flags;
[Flags]
internal enum NoteFlag
{
CreateNew,
Existing,
IgnoreSettingsFromFile,
}
//mapping to app settings for convenience
internal static StringCollection NoteList => Properties.Settings.Default.NoteList;
internal static bool UpgradeFlag
{
get => Properties.Settings.Default.UpgradeFlag;
set => Properties.Settings.Default.UpgradeFlag = value;
}
#region Per-note setting properties
private string doc_location = App.AppRootDir + "DesktopNoteContent";
internal string Doc_Location
{
get => doc_location;
set {
if (string.IsNullOrWhiteSpace(Path.GetFileName(value))) return;
if (doc_location == value) return;
//convert relative path to absolute path and check again
value = Path.GetFullPath(value);
if (doc_location == value) return;
if (MainWin != null && MainWin.IsLoaded) {
if (File.Exists(doc_location) && !File.Exists(value) &&
File.Exists(doc_location + ".txt") && !File.Exists(value + ".txt")) {
File.Move(doc_location, value);
File.Move(doc_location + ".txt", value + ".txt");
doc_location = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Doc_Location)));
}
}
else {
doc_location = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Doc_Location)));
}
}
}
internal string Bak_Location => Doc_Location + ".txt";
internal string Doc_FileName => Path.GetFileName(Doc_Location);
private Size win_size;
[NoteSetting]
internal Size Win_Size
{
get => win_size;
set {
if (win_size == value) return;
win_size = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Win_Size)));
}
}
private Point win_pos;
[NoteSetting]
internal Point Win_Pos
{
get => win_pos;
set {
if (win_pos == value) return;
win_pos = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Win_Pos)));
}
}
private bool autodock;
[NoteSetting]
internal bool AutoDock
{
get => autodock;
set {
if (autodock == value) return;
autodock = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AutoDock)));
}
}
private int dockedto;
[NoteSetting]
internal int DockedTo
{
get => dockedto;
set {
if (dockedto == value) return;
dockedto = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DockedTo)));
}
}
private string font;
[NoteSetting]
internal string Font
{
get => font;
set {
if (font == value) return;
font = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Font)));
}
}
private Color fontcolor;
[NoteSetting]
internal Color FontColor
{
get => fontcolor;
set {
if (fontcolor == value) return;
fontcolor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FontColor)));
}
}
private Color backcolor;
[NoteSetting]
internal Color BackColor
{
get => backcolor;
set {
if (backcolor == value) return;
backcolor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BackColor)));
}
}
private Color papercolor;
[NoteSetting]
internal Color PaperColor
{
get => papercolor;
set {
if (papercolor == value) return;
papercolor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PaperColor)));
}
}
#endregion
/// <summary>
/// Create new instance of Setting. If refSetting is null, default values will be used. Otherwise values from refSetting will be used.
/// Specify path to override default Doc_Location which is DesktopNoteContent_CurrentDateTime at application root.
/// </summary>
internal Setting(NoteFlag flags, Setting refSetting = null, string path = null)
{
Flags = flags;
Doc_Location = path ?? $"{App.AppRootDir}DesktopNoteContent_{DateTime.Now.ToString("yyyyMMddHHmmss")}";
if (refSetting != null) {
Win_Size = refSetting.Win_Size;
Win_Pos = refSetting.Win_Pos;
AutoDock = refSetting.AutoDock;
DockedTo = refSetting.DockedTo;
Font = refSetting.Font;
FontColor = refSetting.FontColor;
BackColor = refSetting.BackColor;
PaperColor = refSetting.PaperColor;
}
else Reset();
}
internal static void Upgrade()
{
if (UpgradeFlag) {
Properties.Settings.Default.Upgrade();
UpgradeFlag = false;
Properties.Settings.Default.Save();
}
}
//Shortcut to Properties.Settings.Default.Save().
internal static void Save()
{
Properties.Settings.Default.Save();
}
internal void Reset()
{
Win_Size = new Size(300, 350);
Win_Pos = new Point(0, 0);
AutoDock = true;
DockedTo = 0;
Font = "Segoe Print";
FontColor = (Color)ColorConverter.ConvertFromString("#FF000000");
BackColor = (Color)ColorConverter.ConvertFromString("#00FFFFFF");
PaperColor = (Color)ColorConverter.ConvertFromString("#FFFFF7C5");
}
internal string Serialize()
{
var root = new XElement("Setting");
foreach (var info in typeof(Setting).GetProperties(flags).Where(p => Attribute.IsDefined(p, typeof(NoteSettingAttribute)))) {
var ele = new XElement(info.Name, info.GetValue(this).ToString());
root.Add(ele);
}
return root.ToString(SaveOptions.DisableFormatting);
}
internal void Parse(string content)
{
var root = XElement.Parse(content);
foreach (var ele in root.Elements()) {
var info = typeof(Setting).GetProperty(ele.Name.LocalName, flags);
if (info == null) continue;
switch (ele.Name.LocalName) {
case nameof(Win_Size):
info.SetValue(this, Size.Parse(ele.Value));
break;
case nameof(Win_Pos):
info.SetValue(this, Point.Parse(ele.Value));
break;
case nameof(AutoDock):
info.SetValue(this, bool.Parse(ele.Value));
break;
case nameof(DockedTo):
info.SetValue(this, int.Parse(ele.Value));
break;
case nameof(FontColor):
case nameof(BackColor):
case nameof(PaperColor):
info.SetValue(this, ColorConverter.ConvertFromString(ele.Value));
break;
case nameof(Font):
info.SetValue(this, ele.Value);
break;
}
}
}
// /// <summary>
// /// Leaving win to null will not save per-note settings.
// /// Returns status message from SaveNote if win is passed. Otherwise null.
// /// </summary>
//public static string Save(MainWindow win = null)
// {
// ////update note list (moved to Window.Closing event)
// //Properties.Settings.Default.NoteList.Clear();
// //Properties.Settings.Default.NoteList.AddRange(App.MainWindows.Select(w => w.CurrentSetting.Doc_Location).ToArray());
// //save settings
// Properties.Settings.Default.Save();
// //save per-note settings
// if (win != null) return Helpers.SaveNote(win);
// return null;
// }
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
sealed class NoteSettingAttribute : Attribute
{
}
}
}