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
|
/******************************************************************************
* Copyright (C) 2011-2013 Michael Hofmann <mh21@mh21.de> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
******************************************************************************/
public class Indicator : Object {
private uint currenticonindex;
private uint lasticonwidth;
private TimeoutSource timeout;
private Gtk.MenuItem[] menuitems;
private IndicatorView indicatorview;
// TODO some of these don't need to be properties
public string icondirectory { get; construct; }
public Providers providers { get; construct; }
public Gtk.Menu menu { get; construct; }
public MenuModel menumodel { get; construct; }
public MenuModel descriptionmodel { get; construct; }
public int indicator_index { get; set; }
public uint height { get; set; }
public uint width { get; set; }
public uint speed { get; set; }
public Gdk.RGBA background_rgba { get; set; }
public GraphModels graphmodels { get; set; }
public signal void providers_updated();
public Indicator(string icondirectory, Providers providers, Gtk.Menu menu,
bool trayicon) {
Object(icondirectory: icondirectory,
providers: providers,
menu: menu,
menumodel: new MenuModel(providers),
descriptionmodel: new MenuModel(providers));
DirUtils.create(this.icondirectory, 0777);
this.iconwritedummy();
this.notify["speed"].connect(() => {
if (this.timeout != null)
this.timeout.destroy();
if (this.speed == 0) {
this.timeout = null;
return;
}
if (this.speed % 1000 == 0)
this.timeout = new TimeoutSource.seconds(this.speed / 1000);
else
this.timeout = new TimeoutSource(this.speed);
this.timeout.attach(null);
this.timeout.set_callback(() => {
this.updateall();
return true;
});
});
if (trayicon)
this.indicatorview = new TrayIndicatorView(this.icondirectory, this.menu);
else
this.indicatorview = new AppIndicatorView(this.icondirectory, this.menu);
}
~Indicator() {
FileUtils.remove(this.iconpath(0));
FileUtils.remove(this.iconpath(1));
DirUtils.remove(this.icondirectory);
}
// Needs to be called before destruction to break the reference cycle from the timeout source
public void destroy() {
if (this.timeout == null)
return;
this.timeout.destroy();
this.timeout = null;
}
public void updateall() {
this.providers.update();
this.providers_updated();
this.updatemodels();
this.updateviews();
}
private void updatemodels() {
this.menumodel.update();
this.graphmodels.update(this.width);
this.descriptionmodel.update();
}
private void updateviews() {
this.updatemenuview();
this.updategraphsview();
// needs to after updategraphsview
}
private void updatemenuview() {
// start after system monitor and separator
uint menu_position = 2;
var length = this.menumodel.expressions.length;
for (uint j = 0; j < length; ++j) {
Gtk.MenuItem item;
if (j < this.menuitems.length) {
item = this.menuitems[j];
} else {
item = new Gtk.MenuItem.with_label("");
item.visible = true;
this.menu.insert(item, (int)menu_position);
this.menuitems += item;
}
item.label = this.menumodel.expression(j).label();
++menu_position;
}
if (length != this.menuitems.length) {
for (uint j = length, jsize = this.menuitems.length; j < jsize; ++j)
menuitems[j].destroy();
this.menuitems = this.menuitems[0:length];
}
}
private void updategraphsview() {
this.iconwrite();
var found = false;
// fix icon size if using a GtkStatusIcon
foreach (var toplevel in Gtk.Window.list_toplevels()) {
if (toplevel.get_type().name() != "GtkTrayIcon" || !(toplevel is Gtk.Container))
continue;
((Gtk.Container) toplevel).foreach((w) => {
if (!(w is Gtk.Image))
return;
((Gtk.Image) w).set_from_file(this.iconpath(this.currenticonindex));
((Gtk.Image) w).pixel_size = (int) uint.max(this.lasticonwidth, this.height);
found = true;
});
}
if (!found) {
this.indicatorview.icon = this.lasticonwidth > 0 ?
this.iconname(this.currenticonindex) : "";
}
this.indicatorview.description = this.descriptionmodel.expression(0).label();
}
private string iconname(uint index) {
return @"indicator-multiload-graphs-$index";
}
private string iconpath(uint index) {
return Path.build_filename(this.icondirectory, this.iconname(index) + ".png");
}
// Create dummy icons because the availability of icons is cached per theme
private void iconwritedummy() {
var surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 1, 1);
surface.write_to_png(this.iconpath(0));
surface.write_to_png(this.iconpath(1));
}
private void iconwrite() {
this.lasticonwidth = 0;
if (this.graphmodels == null)
return;
uint count = 0;
foreach (var graphmodel in this.graphmodels.graphmodels)
if (graphmodel.enabled)
++count;
if (count == 0)
return;
this.lasticonwidth = (int) (count * (this._width + 2)) - 2;
var surface = new Cairo.ImageSurface(Cairo.Format.ARGB32,
(int) this.lasticonwidth, (int) this.height);
var ctx = new Cairo.Context(surface);
ctx.set_antialias(Cairo.Antialias.NONE);
ctx.set_line_width(1);
uint offset = 0;
foreach (var graphmodel in this.graphmodels.graphmodels) {
if (!graphmodel.enabled)
continue;
Gdk.cairo_set_source_rgba(ctx, this.background_rgba);
ctx.rectangle(offset, 0, this._width, this.height);
ctx.fill();
var tracemodels = graphmodel.tracemodels;
var values = new double[tracemodels.length, this._width];
var scale = graphmodel.scale;
for (uint j = 0, jsize = values.length[0]; j < jsize; ++j) {
var enabled = tracemodels[j].enabled;
unowned double[] tracedata = tracemodels[j].values;
for (uint i = 0, isize = values.length[1]; i < isize; ++i)
values[j, i] = (j > 0 ? values[j - 1, i] : 0) + (enabled ? tracedata[i] : 0) / scale;
}
for (int j = values.length[0] - 1; j >= 0; --j) {
Gdk.cairo_set_source_rgba(ctx, graphmodel.tracemodels[j].rgba);
for (uint i = 0, isize = values.length[1]; i < isize; ++i) {
// the baseline is outside the canvas
ctx.move_to(0.5 + offset + i, this.height + 0.5);
ctx.line_to(0.5 + offset + i,
this.height + 0.5 - this.height * values[j, i]);
}
ctx.stroke();
}
offset += this._width + 2;
}
this.currenticonindex = 1 - this.currenticonindex;
surface.write_to_png(this.iconpath(this.currenticonindex));
}
}
|