-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGlobalSettingsManager.cs
154 lines (131 loc) · 5.43 KB
/
GlobalSettingsManager.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
using System;
using System.IO;
using System.Windows.Forms;
using Desktoptale.Messages;
using Desktoptale.Messaging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Desktoptale
{
public class GlobalSettingsManager
{
public GlobalSettings GlobalSettings;
private string path;
private ISerializer serializer;
private IDeserializer deserializer;
private bool disableSaving;
public GlobalSettingsManager(string path)
{
this.path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
serializer = new SerializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();
deserializer = new DeserializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();
MessageBus.Subscribe<ClickThroughChangedMessage>(OnClickThroughChangedMessage);
MessageBus.Subscribe<InteractionButtonChangedMessage>(OnInteractionButtonChangedMessage);
MessageBus.Subscribe<SetDistractionLevelMessage>(OnSetDistractionLevelMessage);
MessageBus.Subscribe<SetDistractionScaleMessage>(OnSetDistractionScaleMessage);
MessageBus.Subscribe<SetDistractionTrackedWindowMessage>(OnSetDistractionTrackedWindowMessage);
}
public bool DoesGlobalSettingsFileExist()
{
return File.Exists(path);
}
public void SendMessages()
{
disableSaving = true;
MessageBus.Send(new ClickThroughChangedMessage() { Enabled = GlobalSettings.ClickThroughMode });
MessageBus.Send(new InteractionButtonChangedMessage() { Enabled = GlobalSettings.EnableInteractionButton });
MessageBus.Send(new SetDistractionLevelMessage() { Level = GlobalSettings.DistractionLevel });
MessageBus.Send(new SetDistractionScaleMessage() { Scale = GlobalSettings.DistractionScale });
if (!string.IsNullOrWhiteSpace(GlobalSettings.DistractionWindow))
{
WindowInfo target = WindowsUtils.GetWindowByName(GlobalSettings.DistractionWindow);
if (target != null)
{
MessageBus.Send(new SetDistractionTrackedWindowMessage() { Window = target });
}
}
disableSaving = false;
}
public void LoadGlobalSettings()
{
try
{
string serialized = File.ReadAllText(path);
GlobalSettings = deserializer.Deserialize<GlobalSettings>(serialized);
}
catch (Exception e)
{
WindowsUtils.ShowMessageBox($"Failed to load global settings: {e.Message}", ProgramInfo.NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void SaveGlobalSettings()
{
if (disableSaving)
{
return;
}
FileStream fileStream = null;
try
{
if (File.Exists(path))
{
fileStream = File.Open(path, FileMode.Truncate);
}
else
{
fileStream = File.Create(path);
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
}
string serialized = serializer.Serialize(GlobalSettings);
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.Write(serialized);
}
}
catch (Exception e)
{
disableSaving = true;
WindowsUtils.ShowMessageBox(
$"Failed to save global settings to file {Path.GetFullPath(path)}\n\nYou can still use the program, but your global settings will not be saved.",
ProgramInfo.NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
fileStream?.Dispose();
}
}
private void OnClickThroughChangedMessage(ClickThroughChangedMessage message)
{
GlobalSettings.ClickThroughMode = message.Enabled;
SaveGlobalSettings();
}
private void OnInteractionButtonChangedMessage(InteractionButtonChangedMessage message)
{
GlobalSettings.EnableInteractionButton = message.Enabled;
SaveGlobalSettings();
}
private void OnSetDistractionLevelMessage(SetDistractionLevelMessage message)
{
if (message.Level >= 0 && message.Level <= DistractionManager.MaxDistractionLevel)
{
GlobalSettings.DistractionLevel = message.Level;
SaveGlobalSettings();
}
}
private void OnSetDistractionScaleMessage(SetDistractionScaleMessage message)
{
GlobalSettings.DistractionScale = message.Scale;
SaveGlobalSettings();
}
private void OnSetDistractionTrackedWindowMessage(SetDistractionTrackedWindowMessage message)
{
GlobalSettings.DistractionWindow = message.Window?.ProcessName ?? null;
SaveGlobalSettings();
}
}
}