-
Notifications
You must be signed in to change notification settings - Fork 1
/
CalenCore.cs
119 lines (106 loc) · 3.19 KB
/
CalenCore.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CalenTrack.Resources;
using CalenTrack.Helpers;
using System.Drawing;
using System.Windows.Forms;
namespace CalenTrack {
class CalenCore {
public static List<CalenHour> hours;
public static CalenHour latestHour;
public static DateTime startTime;
public static double lastTick = 0;
public static bool takeABreak = false;
public static double timeSinceBreak = 0;
public static int selectStart = -1;
public static int selectEnd = -1;
public static Config config;
public static void reset() {
if (!File.Exists("config.ini")) {
File.WriteAllText("config.ini", DefaultConfig.get());
}
config = new Config("config.ini");
CalenApp.reset();
hours = new List<CalenHour>();
var now = DateTime.Now;
var hour = new CalenHour();
hour.count = TimeTools.getTicksSinceHourStart(now);
hours.Add(hour);
latestHour = hour;
startTime = now;
lastTick = 0;
var timeView = MainForm.inst.timeView;
var colors = config.colors;
var colorThumbs = new ImageList();
foreach (var c in colors.auto) {
colorThumbs.Images.Add(c.ToString(), makeThumb(c.active));
}
foreach (var rule in config.rules) {
if (rule.color.IsEmpty) continue;
colorThumbs.Images.Add(rule.color.ToString(), makeThumb(rule.color));
}
foreach (var color in new Color[] { colors.unknown, colors.rest.active }) {
colorThumbs.Images.Add(color.ToString(), makeThumb(color));
}
timeView.SmallImageList = colorThumbs;
}
static Bitmap makeThumb(Color color) {
var bit = new Bitmap(16, 16);
using (var g = Graphics.FromImage(bit)) g.Clear(color);
return bit;
}
public static void init() {
reset();
}
static void add(uint appid) {
var ok = latestHour.add(appid);
if (!ok) {
var hour = new CalenHour();
latestHour = hour;
hours.Add(hour);
hour.add(appid);
}
}
public static bool overrideToMarker = false;
public static void timerTickPost(bool updateTimeView = true) {
var tickRate = CalenHour.tickRate;
lastTick += tickRate;
bool hasFocus;
var appid = Helpers.CalenSearch.GetCurrent(tickRate, out hasFocus);
if (overrideToMarker) {
overrideToMarker = false;
appid = CalenApp.appMarker.index;
}
add(appid);
if (hasFocus) {
CalenDraw.redraw(updateTimeView);
}
if (config.autosaveInterval > 0 && DateTime.Now.Subtract(CalenState.lastSaveAt).TotalSeconds > config.autosaveInterval) {
CalenState.lastSaveAt = DateTime.Now;
Console.WriteLine("Auto-saving...");
CalenState.save();
}
}
public static void timerTick(bool updateTimeView = true) {
var now = DateTime.Now.Subtract(startTime).TotalSeconds;
var diff = now - lastTick;
var tickRate = CalenHour.tickRate;
if (diff < tickRate) return;
var skipTime = 0;
while (diff >= tickRate * 2) {
add(CalenApp.appInactive.index);
CalenApp.appInactive.idleTime += tickRate;
CalenApp.appTotal.idleTime += tickRate;
diff -= tickRate;
lastTick += tickRate;
skipTime += tickRate;
}
if (skipTime >= config.timeTillBreak) timeSinceBreak = 0;
timerTickPost(updateTimeView);
}
}
}