-
Notifications
You must be signed in to change notification settings - Fork 1
/
TmpChangerForm.cs
executable file
·233 lines (207 loc) · 7.3 KB
/
TmpChangerForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Menora
{
public partial class TmpChangerForm : Form
{
private TempConfig currentConfig;
private static readonly string[] presetConfigs =
{
"[\n {\"time\": \"6:00\", \"temp\": 9500},\n {\"time\": \"10:00\", \"temp\": 6500},\n {\"time\": \"14:00\", \"temp\": 6500},\n {\"time\": \"22:00\", \"temp\": 3000},\n]",
"[\n {\"time\": \"12:00\", \"temp\": 6500},\n {\"time\": \"02:00\", \"temp\": 2000},\n]",
};
private bool shouldStartMinimized, isEnabled;
public TmpChangerForm(bool minimized)
{
InitializeComponent();
// Fill up presets
cboPreset.Items.Add("From tempConfig.json file");
cboPreset.Items.Add("From the source box (on the left)");
for (int i = 1; i <= presetConfigs.Length; i++)
cboPreset.Items.Add("Preset " + i);
// Loads the default config
cboPreset.SelectedIndex = 0;
tbTestDay.Value = 360;
if (currentConfig != null)
{
radioUse.Checked = true;
}
else
{
// Select a preset entry by default
radioTestTemp.Checked = true;
cboPreset.SelectedIndex = 2;
}
shouldStartMinimized = minimized;
}
#region Event handling
private void TmpChangerForm_Load(object sender, EventArgs e)
{
if (shouldStartMinimized && currentConfig != null)
this.WindowState = FormWindowState.Minimized;
}
private void cboPreset_SelectedIndexChanged(object sender, EventArgs e)
{
if (cboPreset.SelectedIndex == 0) // From file
LoadConfigFromFile();
else if (cboPreset.SelectedIndex == 1) // From source
LoadConfigFromTextBox();
else
{
SetCodeText(presetConfigs[cboPreset.SelectedIndex - 2]);
LoadConfigFromTextBox();
}
}
private void txtCode_Leave(object sender, EventArgs e)
{
// Select "from source"
cboPreset.SelectedIndex = 1;
LoadConfigFromTextBox();
}
private void radioTestTemp_CheckedChanged(object sender, EventArgs e)
{
UpdateStatus();
}
private void radioTestDay_CheckedChanged(object sender, EventArgs e)
{
UpdateStatus();
}
private void radioUse_CheckedChanged(object sender, EventArgs e)
{
UpdateStatus();
}
private void tempTestTrackbar_Scroll(object sender, EventArgs e)
{
radioTestTemp.Checked = true;
UpdateStatus();
}
private void dayTrackbar_Scroll(object sender, EventArgs e)
{
radioTestDay.Checked = true;
UpdateStatus();
}
private void btnSaveCode_Click(object sender, EventArgs e)
{
System.IO.File.WriteAllText(@"tempConfig.json", txtCode.Text);
}
private void tmrMinute_Tick(object sender, EventArgs e)
{
UpdateStatus();
}
private void TmpChangerForm_FormClosed(object sender, FormClosedEventArgs e)
{
// Restore on exit
TempUtils.ApplyGamma(6500);
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void TmpChangerForm_Resize(object sender, EventArgs e)
{
ShowSystrayIcon(this.WindowState == FormWindowState.Minimized);
}
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
#endregion
private void EnableDayControls(bool enable, Exception error)
{
radioTestDay.Enabled = tbTestDay.Enabled = radioUse.Enabled = lblCurrentTime.Enabled = btnSaveCode.Enabled = enable;
isEnabled = enable;
if (!enable)
{
lblStatus.Text = error != null ? error.Message : "Error";
}
else
{
lblStatus.Text = "JSON is valid.";
}
}
private void LoadConfigFromFile()
{
try
{
SetCodeText(ReadConfigFile());
EnableDayControls(true, null);
LoadConfigFromTextBox();
UpdateStatus();
}
catch (Exception e)
{
EnableDayControls(false, e);
MessageBox.Show("Unable to load the configuration properly. Loading preset config -- do not forget to click on the Save code button.\n" + e.Message);
}
}
private void LoadConfigFromTextBox()
{
try
{
currentConfig = new TempConfig(txtCode.Text);
EnableDayControls(true, null);
UpdateStatus();
}
catch (Exception e)
{
EnableDayControls(false, e);
}
}
private string ReadConfigFile()
{
return System.IO.File.ReadAllText(@"tempConfig.json");
}
private void SetCodeText(string code)
{
txtCode.Text = code.Replace("\n", Environment.NewLine);
UpdateStatus();
}
private void ShowSystrayIcon(bool showOnSysTray)
{
if (showOnSysTray)
{
this.ShowInTaskbar = false;
this.trayIcon.Visible = true;
}
else
{
this.ShowInTaskbar = true;
this.trayIcon.Visible = false;
}
}
private void UpdateStatus()
{
tmrMinute.Enabled = radioUse.Checked;
if (radioTestTemp.Checked)
{
TempUtils.ApplyGamma(tbTestTemp.Value);
lblTemperature.Text = tbTestTemp.Value + "K";
}
else if (radioTestDay.Checked)
{
int minutes = tbTestDay.Value;
int temp = currentConfig.TemperatureAt(minutes);
lblCurrentTime.Text = currentConfig.PrettyTime(minutes);
TempUtils.ApplyGamma(temp);
}
else if (radioUse.Checked)
{
DateTime now = DateTime.Now;
int temp = currentConfig.TemperatureAt(now.Hour * 60 + now.Minute - currentConfig.DayStartMinutes);
TempUtils.ApplyGamma(temp);
}
}
}
}