-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathTreeViewModel.vala
More file actions
210 lines (174 loc) · 6.49 KB
/
TreeViewModel.vala
File metadata and controls
210 lines (174 loc) · 6.49 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
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
*/
public enum Monitor.Column {
ICON,
NAME,
CPU,
MEMORY,
PID,
CMD
}
public class Monitor.ProcessRowData : GLib.Object {
public Icon icon { get; set; }
public string name { get; set; }
public int cpu { get; set; }
public uint64 memory { get; set; }
public int pid { get; set; }
public string cmd { get; set; }
}
public class Monitor.TreeViewFilter : GLib.Object {
private string _needle;
public string needle {
get {
return _needle;
}
set {
name_filter.search = value;
cmd_filter.search = value;
_needle = value;
}
}
public Gtk.FilterListModel model_out;
private Gtk.AnyFilter any_filter;
public Gtk.StringFilter name_filter;
public Gtk.StringFilter cmd_filter;
public Gtk.CustomFilter pid_filter;
public TreeViewFilter (GLib.ListModel? model) {
name_filter = build_str_filter ("name");
cmd_filter = build_str_filter ("cmd");
// since the pid property is an int, we need to use a custom filter to convert it to a string
pid_filter = new Gtk.CustomFilter ((obj) => {
var item = (ProcessRowData) obj;
bool pid_found = item.pid.to_string ().contains (needle.casefold ()) || false;
return pid_found;
});
any_filter = new Gtk.AnyFilter ();
any_filter.append (name_filter);
any_filter.append (cmd_filter);
any_filter.append (pid_filter);
model_out = new Gtk.FilterListModel (model, any_filter);
}
private Gtk.StringFilter build_str_filter (string column_name) {
var expression = new Gtk.PropertyExpression (typeof (ProcessRowData), null, column_name);
return new Gtk.StringFilter (expression) {
ignore_case = true,
match_mode = SUBSTRING,
search = needle
};
}
}
public class Monitor.TreeViewModel : GLib.Object {
public ProcessManager process_manager;
public TreeViewFilter filtered;
public Gtk.SingleSelection selection_model;
public signal void added_first_row ();
public signal void process_selected (Process process);
public Gtk.Sorter sorter {
get { return sorted.sorter; }
set {
sorted.sorter = value;
}
}
private GLib.ListStore store;
private Gtk.SortListModel sorted;
private Gee.Map<int, ProcessRowData ?> process_rows;
construct {
process_rows = new Gee.HashMap<int, ProcessRowData ?> ();
store = new GLib.ListStore (typeof (ProcessRowData));
sorted = new Gtk.SortListModel (store, null);
filtered = new TreeViewFilter (sorted);
selection_model = new Gtk.SingleSelection (filtered.model_out) {
autoselect = true
};
selection_model.notify["selected-item"].connect ((sender, property) => {
var row_data = selection_model.get_selected_item () as ProcessRowData;
Process process = process_manager.get_process (row_data.pid);
process_selected (process);
});
process_manager = ProcessManager.get_default ();
process_manager.process_added.connect ((process) => add_process (process));
process_manager.process_removed.connect ((pid) => remove_process (pid));
process_manager.updated.connect (update_model);
Idle.add (() => { add_running_processes (); return false; });
}
public Gtk.StringSorter str_sorter (string column_name) {
return new Gtk.StringSorter (new Gtk.PropertyExpression (typeof (ProcessRowData), null, column_name));
}
public Gtk.NumericSorter num_sorter (string column_name) {
return new Gtk.NumericSorter (new Gtk.PropertyExpression (typeof (ProcessRowData), null, column_name)) {
sort_order = Gtk.SortType.DESCENDING
};
}
private void add_running_processes () {
debug ("add_running_processes");
var running_processes = process_manager.get_process_list ();
foreach (var process in running_processes.values) {
add_process (process);
}
}
private bool add_process (Process process) {
if (process != null && !process_rows.has_key (process.stat.pid)) {
debug ("Add process %d Parent PID: %d", process.stat.pid, process.stat.ppid);
// add the process to the model
var row = new ProcessRowData () {
icon = process.icon,
name = process.application_name,
cpu = (int) process.cpu_percentage,
memory = process.mem_usage,
pid = process.stat.pid,
cmd = process.command
};
store.append (row);
if (process_rows.size < 1) {
added_first_row ();
}
// add the process to our cache of process_rows
process_rows.set (process.stat.pid, row);
return true;
}
return false;
}
private void update_model () {
foreach (int pid in process_rows.keys) {
Process process = process_manager.get_process (pid);
var process_row = process_rows.get (pid);
uint pos;
if (store.find (process_row, out pos)) {
var item = store.get_item (pos) as ProcessRowData;
item.cpu = (int) process.cpu_percentage;
item.memory = process.mem_usage;
store.items_changed (pos, 1, 1);
} else {
debug ("Failed to find process row for pid %d", pid);
}
}
}
private void remove_process (int pid) {
debug ("remove process %d from model".printf (pid));
// if process rows has pid
if (process_rows.has_key (pid)) {
uint pos;
var process_row = process_rows.get (pid);
if (store.find (process_row, out pos)) {
store.remove (pos);
}
process_rows.unset (pid);
}
}
public void kill_process (int pid) {
if (pid > 0) {
var process = process_manager.get_process (pid);
process.kill ();
info ("Kill:%d", process.stat.pid);
}
}
public void end_process (int pid) {
if (pid > 0) {
var process = process_manager.get_process (pid);
process.end ();
info ("End:%d", process.stat.pid);
}
}
}