Skip to content

Commit 9fb4c8f

Browse files
committed
ProcessView: move model filter and sort here
1 parent 26dad9e commit 9fb4c8f

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

src/MainWindow.vala

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,14 @@ public class Monitor.MainWindow : Hdy.ApplicationWindow {
132132
dbusserver.indicator_state (MonitorApp.settings.get_boolean ("indicator-state"));
133133
stack.visible_child_name = MonitorApp.settings.get_string ("opened-view");
134134

135-
var filter_model = new Gtk.TreeModelFilter (process_view.treeview_model, null);
136-
filter_model.set_visible_func (filter_func);
137-
138-
var sort_model = new Gtk.TreeModelSort.with_model (filter_model);
139-
140-
process_view.process_tree_view.set_model (sort_model);
141-
142135
search_entry.search_changed.connect (() => {
143136
// collapse tree only when search is focused and changed
144137
if (search_entry.is_focus) {
145138
process_view.process_tree_view.collapse_all ();
146139
}
147140

148-
filter_model.refilter ();
141+
process_view.needle = search_entry.text;
142+
process_view.filter_model.refilter ();
149143

150144
// focus on child row to avoid the app crashes by clicking "Kill/End Process" buttons in headerbar
151145
process_view.process_tree_view.focus_on_child_row ();
@@ -178,47 +172,4 @@ public class Monitor.MainWindow : Hdy.ApplicationWindow {
178172
this.maximize ();
179173
}
180174
}
181-
182-
private bool filter_func (Gtk.TreeModel model, Gtk.TreeIter iter) {
183-
string name_haystack;
184-
int pid_haystack;
185-
string cmd_haystack;
186-
bool found = false;
187-
var needle = search_entry.text;
188-
189-
// should help with assertion errors, donno
190-
// if (needle == null) return true;
191-
192-
if (needle.length == 0) {
193-
return true;
194-
}
195-
196-
model.get (iter, Column.NAME, out name_haystack, -1);
197-
model.get (iter, Column.PID, out pid_haystack, -1);
198-
model.get (iter, Column.CMD, out cmd_haystack, -1);
199-
200-
// sometimes name_haystack is null
201-
if (name_haystack != null) {
202-
bool name_found = name_haystack.casefold ().contains (needle.casefold ()) || false;
203-
bool pid_found = pid_haystack.to_string ().casefold ().contains (needle.casefold ()) || false;
204-
bool cmd_found = cmd_haystack.casefold ().contains (needle.casefold ()) || false;
205-
found = name_found || pid_found || cmd_found;
206-
}
207-
208-
209-
Gtk.TreeIter child_iter;
210-
bool child_found = false;
211-
212-
if (model.iter_children (out child_iter, iter)) {
213-
do {
214-
child_found = filter_func (model, child_iter);
215-
} while (model.iter_next (ref child_iter) && !child_found);
216-
}
217-
218-
if (child_found && needle.length > 0) {
219-
process_view.process_tree_view.expand_all ();
220-
}
221-
222-
return found || child_found;
223-
}
224175
}

src/Views/ProcessView/ProcessView.vala

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
*/
55

66
public class Monitor.ProcessView : Gtk.Box {
7-
public TreeViewModel treeview_model { get; private set; }
7+
public string needle = "";
8+
9+
public Gtk.TreeModelFilter filter_model { get; private set; }
810
public CPUProcessTreeView process_tree_view { get; private set; }
911

1012
private ProcessInfoView process_info_view;
13+
private TreeViewModel treeview_model;
1114

1215
construct {
1316
treeview_model = new TreeViewModel ();
1417

18+
filter_model = new Gtk.TreeModelFilter (treeview_model, null);
19+
filter_model.set_visible_func (filter_func);
20+
21+
var sort_model = new Gtk.TreeModelSort.with_model (filter_model);
22+
1523
process_tree_view = new CPUProcessTreeView (treeview_model);
1624
process_tree_view.process_selected.connect ((process) => on_process_selected (process));
25+
process_tree_view.set_model (sort_model);
1726

1827
var process_tree_view_scrolled = new Gtk.ScrolledWindow (null, null) {
1928
child = process_tree_view
@@ -51,4 +60,46 @@ public class Monitor.ProcessView : Gtk.Box {
5160
});
5261

5362
}
63+
64+
private bool filter_func (Gtk.TreeModel model, Gtk.TreeIter iter) {
65+
string name_haystack;
66+
int pid_haystack;
67+
string cmd_haystack;
68+
bool found = false;
69+
70+
// should help with assertion errors, donno
71+
// if (needle == null) return true;
72+
73+
if (needle.length == 0) {
74+
return true;
75+
}
76+
77+
model.get (iter, Column.NAME, out name_haystack, -1);
78+
model.get (iter, Column.PID, out pid_haystack, -1);
79+
model.get (iter, Column.CMD, out cmd_haystack, -1);
80+
81+
// sometimes name_haystack is null
82+
if (name_haystack != null) {
83+
bool name_found = name_haystack.casefold ().contains (needle.casefold ()) || false;
84+
bool pid_found = pid_haystack.to_string ().casefold ().contains (needle.casefold ()) || false;
85+
bool cmd_found = cmd_haystack.casefold ().contains (needle.casefold ()) || false;
86+
found = name_found || pid_found || cmd_found;
87+
}
88+
89+
90+
Gtk.TreeIter child_iter;
91+
bool child_found = false;
92+
93+
if (model.iter_children (out child_iter, iter)) {
94+
do {
95+
child_found = filter_func (model, child_iter);
96+
} while (model.iter_next (ref child_iter) && !child_found);
97+
}
98+
99+
if (child_found && needle.length > 0) {
100+
process_tree_view.expand_all ();
101+
}
102+
103+
return found || child_found;
104+
}
54105
}

0 commit comments

Comments
 (0)