-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCPUProcessTreeView.vala
More file actions
246 lines (200 loc) · 8.65 KB
/
CPUProcessTreeView.vala
File metadata and controls
246 lines (200 loc) · 8.65 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
public class Monitor.CPUProcessTreeView : Gtk.TreeView {
private new TreeViewModel model;
private Gtk.TreeViewColumn name_column;
private Gtk.TreeViewColumn pid_column;
private Gtk.TreeViewColumn cpu_column;
private Gtk.TreeViewColumn memory_column;
private Regex ? regex;
public signal void process_selected (Process process);
public CPUProcessTreeView (TreeViewModel model) {
this.model = model;
/* *INDENT-OFF* */
regex = /(?i:^.*\.(xpm|png)$)/; // vala-lint=space-before-paren,
/* *INDENT-ON* */
// setup name column
name_column = new Gtk.TreeViewColumn ();
name_column.title = _("Process Name");
name_column.expand = true;
name_column.min_width = 250;
name_column.set_sort_column_id (Column.NAME);
var icon_cell = new Gtk.CellRendererPixbuf ();
name_column.pack_start (icon_cell, false);
// name_column.add_attribute (icon_cell, "icon_name", Column.ICON);
name_column.set_cell_data_func (icon_cell, icon_cell_layout);
var name_cell = new Gtk.CellRendererText ();
name_cell.ellipsize = Pango.EllipsizeMode.END;
name_cell.set_fixed_height_from_font (1);
name_column.pack_start (name_cell, false);
name_column.add_attribute (name_cell, "text", Column.NAME);
insert_column (name_column, -1);
// setup cpu column
var cpu_cell = new Gtk.CellRendererText ();
cpu_cell.xalign = 0.5f;
cpu_column = new Gtk.TreeViewColumn.with_attributes (_("CPU"), cpu_cell);
cpu_column.expand = false;
cpu_column.set_cell_data_func (cpu_cell, cpu_usage_cell_layout);
cpu_column.alignment = 0.5f;
cpu_column.set_sort_column_id (Column.CPU);
insert_column (cpu_column, -1);
// setup memory column
var memory_cell = new Gtk.CellRendererText ();
memory_cell.xalign = 0.5f;
memory_column = new Gtk.TreeViewColumn.with_attributes (_("Memory"), memory_cell);
memory_column.expand = false;
memory_column.set_cell_data_func (memory_cell, memory_usage_cell_layout);
memory_column.alignment = 0.5f;
memory_column.set_sort_column_id (Column.MEMORY);
insert_column (memory_column, -1);
// setup PID column
var pid_cell = new Gtk.CellRendererText ();
pid_cell.xalign = 0.5f;
pid_column = new Gtk.TreeViewColumn.with_attributes (_("PID"), pid_cell);
pid_column.set_cell_data_func (pid_cell, pid_cell_layout);
pid_column.expand = false;
pid_column.alignment = 0.5f;
pid_column.set_sort_column_id (Column.PID);
pid_column.add_attribute (pid_cell, "text", Column.PID);
insert_column (pid_column, -1);
// resize all of the columns
columns_autosize ();
set_model (model);
model.added_first_row.connect (() => {
focus_on_first_row ();
});
cursor_changed.connect (_cursor_changed);
// model.process_manager.updated.connect (_cursor_changed);
}
public void icon_cell_layout (Gtk.CellLayout cell_layout, Gtk.CellRenderer icon_cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
Value icon_name;
model.get_value (iter, Column.ICON, out icon_name);
string path = ((string) icon_name);
if (regex.match (path)) {
try {
Gdk.Pixbuf icon = new Gdk.Pixbuf.from_file_at_size (path, 16, -1);
((Gtk.CellRendererPixbuf)icon_cell).pixbuf = icon;
} catch (Error e) {
warning (e.message);
}
} else {
((Gtk.CellRendererPixbuf)icon_cell).icon_name = path;
}
}
public void cpu_usage_cell_layout (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
// grab the value that was store in the model and convert it down to a usable format
Value cpu_usage_value;
model.get_value (iter, Column.CPU, out cpu_usage_value);
double cpu_usage = cpu_usage_value.get_double ();
// format the double into a string
if (cpu_usage < 0.0)
((Gtk.CellRendererText)cell).text = Utils.NO_DATA;
else
((Gtk.CellRendererText)cell).text = "%.0f%%".printf (cpu_usage);
}
public void memory_usage_cell_layout (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
// grab the value that was store in the model and convert it down to a usable format
Value memory_usage_value;
model.get_value (iter, Column.MEMORY, out memory_usage_value);
int64 memory_usage = memory_usage_value.get_int64 ();
double memory_usage_double = (double) memory_usage;
string units = _("KiB");
// convert to MiB if needed
if (memory_usage_double > 1024.0) {
memory_usage_double /= 1024.0;
units = _("MiB");
}
// convert to GiB if needed
if (memory_usage_double > 1024.0) {
memory_usage_double /= 1024.0;
units = _("GiB");
}
// format the double into a string
if (memory_usage == 0)
((Gtk.CellRendererText)cell).text = Utils.NO_DATA;
else
((Gtk.CellRendererText)cell).text = "%.1f %s".printf (memory_usage_double, units);
}
private void pid_cell_layout (Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
Value pid_value;
model.get_value (iter, Column.PID, out pid_value);
int pid = pid_value.get_int ();
// format the double into a string
if (pid == 0) {
((Gtk.CellRendererText)cell).text = Utils.NO_DATA;
}
}
public void focus_on_first_row () {
Gtk.TreePath tree_path = new Gtk.TreePath.from_indices (0);
this.set_cursor (tree_path, null, false);
grab_focus ();
}
public void focus_on_child_row () {
Gtk.TreePath tree_path = new Gtk.TreePath.from_indices (0, 0);
this.set_cursor (tree_path, null, false);
grab_focus ();
}
public int get_pid_of_selected () {
Gtk.TreeIter iter;
Gtk.TreeModel model;
int pid = 0;
var selection = this.get_selection ().get_selected_rows (out model).nth_data (0);
model.get_iter (out iter, selection);
model.get (iter, Column.PID, out pid);
return pid;
}
// How about GtkTreeSelection ?
public void expanded () {
Gtk.TreeModel model;
var selection = this.get_selection ().get_selected_rows (out model).nth_data (0);
this.expand_row (selection, false);
}
public void collapse () {
Gtk.TreeModel model;
var selection = this.get_selection ().get_selected_rows (out model).nth_data (0);
this.collapse_row (selection);
}
public void kill_process () {
int pid = get_pid_of_selected ();
model.kill_process (pid);
}
public void end_process () {
int pid = get_pid_of_selected ();
model.end_process (pid);
}
// when row is selected send signal to update process_info_view
public void _cursor_changed () {
Gtk.TreeModel tree_model;
Gtk.TreeIter iter;
int pid = 0;
var selection = get_selection ().get_selected_rows (out tree_model).nth_data (0);
if (selection != null) {
tree_model.get_iter (out iter, selection);
tree_model.get (iter, Column.PID, out pid);
Process process = model.process_manager.get_process (pid);
process_selected (process);
// debug ("cursor changed");
}
}
public string get_sorting_state () {
string sorting_state = "";
foreach (var column in this.get_columns ()) {
int sort_indicator = column.sort_indicator ? 1 : 0;
sorting_state += "%d%d".printf (sort_indicator, column.get_sort_order ());
}
debug ("Get sorting state: " + sorting_state);
return sorting_state;
}
public void set_sorting_state (string sorting_state) {
debug ("== Going to set sorting state: " + sorting_state);
var columns = this.get_columns ();
int index = 0;
foreach (var column in columns) {
column.sort_indicator = (sorting_state[index] == '1');
var sort_type = sorting_state[index + 1] == '1' ? Gtk.SortType.ASCENDING : Gtk.SortType.DESCENDING;
column.set_sort_order (sort_type);
debug ("== " + (sorting_state[index] == '1').to_string () + " " + sort_type.to_string ());
index += 2;
}
get_sorting_state ();
set_model (model);
}
}