-
Notifications
You must be signed in to change notification settings - Fork 1
/
CalenTimeView.cs
94 lines (90 loc) · 3.04 KB
/
CalenTimeView.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CalenTrack {
/// <summary>
/// Manages mainForm's timeView
/// </summary>
class CalenTimeView {
public static void update(MainForm form = null) {
if (form == null) form = MainForm.inst;
var config = CalenCore.config;
var sorted = CalenApp.sorted;
var selectStart = CalenCore.selectStart;
if (selectStart != -1) {
foreach (var app in CalenApp.list) {
app.selectedActiveTime = 0;
app.selectedIdleTime = 0;
}
var selectEnd = CalenCore.selectEnd;
var hourStart = 0;
var hourEnd = CalenHour.ticksPerHour;
var appTotal = CalenApp.appTotal;
foreach (var hour in CalenCore.hours) {
var selhStart = Math.Max(hourStart, selectStart) - hourStart;
var selhEnd = Math.Min(hourEnd, selectEnd + 1) - hourStart;
for (var i = selhStart; i < selhEnd; i++) {
var appid = hour.appIds[i];
var app = CalenApp.list[CalenAppId.getIndex(appid)];
if (CalenAppId.isIdle(appid)) {
app.selectedIdleTime += CalenHour.tickRate;
appTotal.selectedIdleTime += CalenHour.tickRate;
} else {
app.selectedActiveTime += CalenHour.tickRate;
appTotal.selectedActiveTime += CalenHour.tickRate;
}
}
hourStart += CalenHour.ticksPerHour;
hourEnd += CalenHour.ticksPerHour;
}
sorted.Sort((a, b) => {
var at = a.selectedActiveTime;
var bt = b.selectedActiveTime;
return at < bt ? 1 : (at > bt ? -1 : a.name.CompareTo(b.name));
});
} else sorted.Sort((a, b) => {
var at = a.activeTime;
var bt = b.activeTime;
return at < bt ? 1 : (at > bt ? -1 : a.name.CompareTo(b.name));
});
var n = Math.Min((int)form.maxAppRows.Value, sorted.Count);
var items = new ListViewItem[n + 2];
var selectedItems = new List<ListViewItem>();
var rest = config.colors.rest.ToString();
var selectedIndices = new List<int>();
for (int i = 0; i <= n + 1; i++) {
var app = i < n ? sorted[i] : i == n ? CalenApp.appInactive : CalenApp.appTotal;
if (app.isHighlighted) selectedIndices.Add(i);
//
var item = new ListViewItem(app.name);
if (app.index >= 3) {
if (app.color.active != Color.Empty) {
item.ImageKey = app.colorKey;
} else item.ImageKey = rest;
}
double activeTime, idleTime;
if (selectStart != -1) {
activeTime = app.selectedActiveTime;
idleTime = app.selectedIdleTime;
} else {
activeTime = app.activeTime;
idleTime = app.idleTime;
}
item.SubItems.Add(Helpers.TimeTools.toString(activeTime));
item.SubItems.Add(Helpers.TimeTools.toString(idleTime));
item.SubItems.Add("" + app.index);
//if (activeApps.ContainsKey(app.index)) selectedItems.Add(item);
items[i] = item;
}
var timeView = form.timeView;
timeView.Items.Clear();
timeView.Items.AddRange(items);
timeView.SelectedIndices.Clear();
foreach (var i in selectedIndices) timeView.SelectedIndices.Add(i);
}
}
}