-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
SearchItem.vala
167 lines (139 loc) · 5.46 KB
/
SearchItem.vala
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
/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
* 2011-2012 Giulio Collura
*
* 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, see <http://www.gnu.org/licenses/>.
*/
public class Slingshot.Widgets.SearchItem : Gtk.ListBoxRow {
private const int ICON_SIZE = 32;
public signal bool launch_app ();
public Backend.App app { get; construct; }
public string search_term { get; construct; }
public ResultType result_type { public get; construct; }
public Gtk.Image icon { public get; private set; }
public string? app_uri { get; private set; }
private Slingshot.AppContextMenu menu;
private Gtk.Label name_label;
private Cancellable? cancellable = null;
public SearchItem (Backend.App app, string search_term = "", ResultType result_type = ResultType.UNKNOWN) {
Object (
app: app,
search_term: search_term,
result_type: result_type
);
}
construct {
string markup;
if (result_type == ResultType.TEXT) {
markup = app.match.title;
} else if (result_type == ResultType.APP_ACTIONS) {
markup = markup_string_with_search (app.match.title, search_term);
} else {
markup = markup_string_with_search (app.name, search_term);
}
name_label = new Gtk.Label (markup);
name_label.set_ellipsize (Pango.EllipsizeMode.END);
name_label.use_markup = true;
name_label.xalign = 0;
icon = new Gtk.Image ();
icon.gicon = app.icon;
icon.pixel_size = ICON_SIZE;
tooltip_markup = app.description;
if (app.match != null && app.match.icon_name.has_prefix (Path.DIR_SEPARATOR_S)) {
var pixbuf = Backend.SynapseSearch.get_pathicon_for_match (app.match, ICON_SIZE);
if (pixbuf != null) {
icon.set_from_pixbuf (pixbuf);
}
}
var grid = new Gtk.Grid ();
grid.column_spacing = 12;
grid.add (icon);
grid.add (name_label);
grid.margin = 6;
grid.margin_start = 18;
add (grid);
if (result_type != ResultType.APP_ACTIONS) {
launch_app.connect (app.launch);
}
app_uri = null;
var app_match = app.match as Synapse.ApplicationMatch;
if (app_match != null && app_match.filename != null) {
app_uri = File.new_for_path (app_match.filename).get_uri ();
}
}
private static string markup_string_with_search (string text, string pattern) {
const string MARKUP = "%s";
if (pattern == "") {
return MARKUP.printf (Markup.escape_text (text));
}
// if no text found, use pattern
if (text == "") {
return MARKUP.printf (Markup.escape_text (pattern));
}
var matchers = Synapse.Query.get_matchers_for_query (
pattern,
0,
RegexCompileFlags.OPTIMIZE | RegexCompileFlags.CASELESS
);
string? highlighted = null;
foreach (var matcher in matchers) {
MatchInfo mi;
if (matcher.key.match (text, 0, out mi)) {
int start_pos;
int end_pos;
int last_pos = 0;
int cnt = mi.get_match_count ();
StringBuilder res = new StringBuilder ();
for (int i = 1; i < cnt; i++) {
mi.fetch_pos (i, out start_pos, out end_pos);
warn_if_fail (start_pos >= 0 && end_pos >= 0);
res.append (Markup.escape_text (text.substring (last_pos, start_pos - last_pos)));
last_pos = end_pos;
res.append (Markup.printf_escaped ("<b>%s</b>", mi.fetch (i)));
if (i == cnt - 1) {
res.append (Markup.escape_text (text.substring (last_pos)));
}
}
highlighted = res.str;
break;
}
}
if (highlighted != null) {
return MARKUP.printf (highlighted);
} else {
return MARKUP.printf (Markup.escape_text (text));
}
}
public override void destroy () {
base.destroy ();
if (cancellable != null)
cancellable.cancel ();
}
public bool create_context_menu (Gdk.Event e) {
if (result_type != APPLICATION) {
return Gdk.EVENT_PROPAGATE;
}
menu = new Slingshot.AppContextMenu (app.desktop_id, app.desktop_path);
if (menu.get_children () != null) {
if (e.type == Gdk.EventType.KEY_PRESS) {
menu.popup_at_widget (this, Gdk.Gravity.EAST, Gdk.Gravity.CENTER, e);
return Gdk.EVENT_STOP;
} else if (e.type == Gdk.EventType.BUTTON_PRESS) {
menu.popup_at_pointer (e);
return Gdk.EVENT_STOP;
}
}
return Gdk.EVENT_PROPAGATE;
}
}