-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
210 lines (185 loc) · 5.88 KB
/
MainForm.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
using CalenTrack.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CalenTrack {
public partial class MainForm : Form {
public static MainForm inst;
public string origTitle;
public Icon origIcon;
public Icon breakIcon;
public MainForm() {
inst = this;
InitializeComponent();
Win32.SetDoubleBuffering(timeView, true);
DoubleBuffered = true;
}
private void MainForm_Load(object sender, EventArgs e) {
var ListView_doubleBuffered = typeof(ListView).GetProperty("DoubleBuffered");
if (ListView_doubleBuffered != null) ListView_doubleBuffered.SetValue(timeView, true);
origTitle = Text;
origIcon = Icon;
var breakIconPath = "breakTime.ico";
if (System.IO.File.Exists(breakIconPath)) {
breakIcon = new Icon(breakIconPath);
} else breakIcon = origIcon;
timeView.Columns.Add("Time", 50, HorizontalAlignment.Left);
timeView.Columns.Add("Idle", 50, HorizontalAlignment.Left);
var nameCol = timeView.Columns.Add("Name", -2, HorizontalAlignment.Left);
timeView.Columns.Remove(nameCol);
timeView.Columns.Insert(0, nameCol);
timeView.AllowColumnReorder = true;
CalenCore.init();
CalenDraw.init();
tickTimer.Interval = (int)(CalenHour.tickRate * 0.7);
tickTimer.Start();
if (File.Exists("state.json")) CalenState.load();
}
private void tickTimer_Tick(object sender, EventArgs e) {
CalenCore.timerTick(!isSelecting);
}
private void btNew_Click(object sender, EventArgs e) {
if (CalenCore.lastTick > 15 * 60) {
switch (MessageBox.Show("Save the current session before starting a new one?", Text, MessageBoxButtons.YesNoCancel)) {
case DialogResult.Yes:
if (!Directory.Exists("sessions")) Directory.CreateDirectory("sessions");
CalenState.save("sessions/" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));
break;
case DialogResult.No:
break;
default: return;
}
}
CalenCore.reset();
CalenDraw.redraw();
}
bool isSelecting = false;
int selectFrom, selectTo;
void syncSelect(bool finish) {
var oldStart = CalenCore.selectStart;
var oldEnd = CalenCore.selectEnd;
if (selectFrom >= 0 && selectTo >= 0 && (!finish || selectFrom != selectTo)) {
if (selectTo >= selectFrom) {
CalenCore.selectStart = selectFrom;
CalenCore.selectEnd = selectTo;
} else {
CalenCore.selectStart = selectTo;
CalenCore.selectEnd = selectFrom;
}
} else {
CalenCore.selectStart = -1;
CalenCore.selectEnd = -1;
}
if (finish || CalenCore.selectStart != oldStart || CalenCore.selectEnd != oldEnd) {
CalenDraw.redraw(finish);
}
}
private void canvas_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
var ctrl = (ModifierKeys & Keys.Control) != 0;
var alt = (ModifierKeys & Keys.Alt) != 0;
if (!ctrl) CalenApp.clearHighlight();
//
var at = CalenDraw.pointToOffset(e.X, e.Y);
if (at >= 0 && CalenDraw.offsetToHour(at, out var hour, out var index)) {
var app = CalenAppId.getApp(hour.appIds[index]);
if (app != null) {
if (ctrl) {
if (app.isHighlighted) {
app.isHighlighted = false;
CalenApp.highlighted.Remove(app);
} else {
app.isHighlighted = true;
CalenApp.highlighted.Add(app);
}
} else {
app.isHighlighted = true;
CalenApp.highlighted.Add(app);
}
}
}
CalenDraw.redraw();
} else if (e.Button == MouseButtons.Right) {
selectTo = selectFrom = CalenDraw.pointToOffset(e.X, e.Y);
isSelecting = selectFrom >= 0;
syncSelect(false);
}
}
private void canvas_MouseMove(object sender, MouseEventArgs e) {
var at = CalenDraw.pointToOffset(e.X, e.Y);
if (isSelecting) {
selectTo = at;
if (selectTo >= 0) syncSelect(false);
} else {
var app = CalenDraw.offsetToApp(at);
if (app != null) {
mouseLabel.Text = app.name;
} else mouseLabel.Text = "";
}
}
private void canvas_MouseUp(object sender, MouseEventArgs e) {
if (isSelecting) {
syncSelect(true);
isSelecting = false;
}
}
private void timeView_SelectedIndexChanged(object sender, EventArgs e) {
CalenApp.clearHighlight();
foreach (ListViewItem item in timeView.SelectedItems) {
var appid = int.Parse(item.SubItems[3].Text);
var app = CalenApp.list[appid];
app.isHighlighted = true;
CalenApp.highlighted.Add(app);
}
CalenDraw.redraw(false);
}
private void btAddMarker_Click(object sender, EventArgs e) {
CalenCore.overrideToMarker = true;
}
private void btRefresh_Click(object sender, EventArgs e) {
CalenDraw.redraw();
}
private void btEditConfig_Click(object sender, EventArgs e) {
Process.Start("config.ini");
}
private void btReloadConfig_Click(object sender, EventArgs e) {
CalenState.save();
CalenCore.reset();
CalenState.load();
}
private void btEndBreak_Click(object sender, EventArgs e) {
CalenCore.timeSinceBreak = 0;
if (CalenCore.takeABreak) {
CalenCore.takeABreak = false;
TaskbarHelper.SetBreakState(MainForm.inst, 0, -1);
}
}
private void openSessionDialog_FileOk(object sender, CancelEventArgs e) {
tickTimer.Enabled = false;
var path = openSessionDialog.FileName;
var dot = path.LastIndexOf(".");
path = path.Substring(0, dot);
CalenState.load(path);
CalenDraw.redraw();
}
private void toolStripButton1_Click(object sender, EventArgs e) {
openSessionDialog.ShowDialog();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
if (tickTimer.Enabled) CalenState.save();
}
private void firstUpdate_Tick(object sender, EventArgs e) {
if (CalenCore.config == null) return;
CalenCore.timerTickPost();
firstUpdate.Enabled = false;
}
}
}