|
1 | | -#include <giomm/desktopappinfo.h> |
2 | 1 | #include <gtkmm/button.h> |
3 | 2 | #include <gtkmm/box.h> |
4 | 3 | #include <gtkmm/icontheme.h> |
|
12 | 11 | #include "toplevel.hpp" |
13 | 12 | #include "toplevel-icon.hpp" |
14 | 13 | #include "gtk-utils.hpp" |
15 | | -#include <iostream> |
16 | | -#include <sstream> |
17 | 14 | #include <cassert> |
18 | 15 | #include "wf-option-wrap.hpp" |
19 | 16 |
|
20 | | -namespace IconProvider |
21 | | -{ |
22 | | -void set_image_from_icon(Gtk::Image& image, |
23 | | - std::string app_id_list, int size, int scale); |
24 | | -} |
25 | 17 |
|
26 | 18 | class WfToplevelIcon::impl |
27 | 19 | { |
@@ -88,10 +80,8 @@ class WfToplevelIcon::impl |
88 | 80 | } |
89 | 81 |
|
90 | 82 | this->app_id = app_id; |
91 | | - IconProvider::set_image_from_icon(image, |
92 | | - app_id, |
93 | | - icon_height, |
94 | | - button.get_scale_factor()); |
| 83 | + IconProvider::image_set_icon(image, |
| 84 | + app_id); |
95 | 85 | } |
96 | 86 |
|
97 | 87 | void send_rectangle_hint() |
@@ -210,177 +200,3 @@ void WfToplevelIcon::close() |
210 | 200 | { |
211 | 201 | return pimpl->close(); |
212 | 202 | } |
213 | | - |
214 | | -/* Icon loading functions */ |
215 | | -namespace IconProvider |
216 | | -{ |
217 | | -using Icon = Glib::RefPtr<Gio::Icon>; |
218 | | - |
219 | | -namespace |
220 | | -{ |
221 | | -std::string tolower(std::string str) |
222 | | -{ |
223 | | - for (auto& c : str) |
224 | | - { |
225 | | - c = std::tolower(c); |
226 | | - } |
227 | | - |
228 | | - return str; |
229 | | -} |
230 | | - |
231 | | -std::map<std::string, std::string> custom_icons; |
232 | | -} |
233 | | - |
234 | | -void load_custom_icons() |
235 | | -{ |
236 | | - static const std::string prefix = "icon_mapping_"; |
237 | | - auto section = WayfireShellApp::get().config.get_section("dock"); |
238 | | - |
239 | | - for (auto option : section->get_registered_options()) |
240 | | - { |
241 | | - if (option->get_name().compare(0, prefix.length(), prefix) != 0) |
242 | | - { |
243 | | - continue; |
244 | | - } |
245 | | - |
246 | | - auto app_id = option->get_name().substr(prefix.length()); |
247 | | - custom_icons[app_id] = option->get_value_str(); |
248 | | - } |
249 | | -} |
250 | | - |
251 | | -bool set_custom_icon(Gtk::Image& image, std::string app_id, int size, int scale) |
252 | | -{ |
253 | | - if (!custom_icons.count(app_id)) |
254 | | - { |
255 | | - return false; |
256 | | - } |
257 | | - |
258 | | - image_set_icon(&image, custom_icons[app_id]); |
259 | | - return true; |
260 | | -} |
261 | | - |
262 | | -/* Gio::DesktopAppInfo |
263 | | - * |
264 | | - * Usually knowing the app_id, we can get a desktop app info from Gio |
265 | | - * The filename is either the app_id + ".desktop" or lower_app_id + ".desktop" */ |
266 | | -Icon get_from_desktop_app_info(std::string app_id) |
267 | | -{ |
268 | | - Glib::RefPtr<Gio::DesktopAppInfo> app_info; |
269 | | - |
270 | | - std::vector<std::string> prefixes = { |
271 | | - "", |
272 | | - "/usr/share/applications/", |
273 | | - "/usr/share/applications/kde/", |
274 | | - "/usr/share/applications/org.kde.", |
275 | | - "/usr/share/applications/org.gnome.", |
276 | | - "/usr/local/share/applications/", |
277 | | - "/usr/local/share/applications/org.kde.", |
278 | | - "/usr/local/share/applications/org.gnome.", |
279 | | - }; |
280 | | - |
281 | | - std::vector<std::string> app_id_variations = { |
282 | | - app_id, |
283 | | - tolower(app_id), |
284 | | - tolower(app_id), |
285 | | - }; |
286 | | - // e.g. org.gnome.Evince.desktop |
287 | | - app_id_variations[2][0] = std::toupper(app_id_variations[2][0]); |
288 | | - |
289 | | - std::vector<std::string> suffixes = { |
290 | | - "", |
291 | | - ".desktop" |
292 | | - }; |
293 | | - |
294 | | - for (auto& prefix : prefixes) |
295 | | - { |
296 | | - for (auto& id : app_id_variations) |
297 | | - { |
298 | | - for (auto& suffix : suffixes) |
299 | | - { |
300 | | - if (!app_info) |
301 | | - { |
302 | | - app_info = Gio::DesktopAppInfo |
303 | | - ::create_from_filename(prefix + id + suffix); |
304 | | - } |
305 | | - } |
306 | | - } |
307 | | - } |
308 | | - |
309 | | - if (!app_info) |
310 | | - { |
311 | | - // special treatment for snap apps |
312 | | - std::string prefix = "/var/lib/snapd/desktop/applications/"; |
313 | | - auto& id = app_id_variations[1]; // seems to be lower case |
314 | | - for (auto& suffix : suffixes) |
315 | | - { |
316 | | - app_info = Gio::DesktopAppInfo::create_from_filename( |
317 | | - prefix + id + "_" + id + suffix); |
318 | | - if (app_info) |
319 | | - { |
320 | | - break; |
321 | | - } |
322 | | - } |
323 | | - } |
324 | | - |
325 | | - if (app_info) // success |
326 | | - { |
327 | | - return app_info->get_icon(); |
328 | | - } |
329 | | - |
330 | | - return Icon{}; |
331 | | -} |
332 | | - |
333 | | -void set_image_from_icon(Gtk::Image& image, |
334 | | - std::string app_id_list, int size, int scale) |
335 | | -{ |
336 | | - std::string app_id; |
337 | | - std::istringstream stream(app_id_list); |
338 | | - |
339 | | - bool found_icon = false; |
340 | | - |
341 | | - /* Wayfire sends a list of app-id's in space separated format, other compositors |
342 | | - * send a single app-id, but in any case this works fine */ |
343 | | - auto display = image.get_display(); |
344 | | - while (stream >> app_id) |
345 | | - { |
346 | | - /* Try first method: custom icon file provided by the user */ |
347 | | - if (set_custom_icon(image, app_id, size, scale)) |
348 | | - { |
349 | | - found_icon = true; |
350 | | - break; |
351 | | - } |
352 | | - |
353 | | - /* Then try to load the DesktopAppInfo */ |
354 | | - auto icon = get_from_desktop_app_info(app_id); |
355 | | - std::string icon_name = "unknown"; |
356 | | - |
357 | | - if (!icon) |
358 | | - { |
359 | | - /* Finally try directly looking up the icon, if it exists */ |
360 | | - if (Gtk::IconTheme::get_for_display(display)->lookup_icon(app_id, 24)) |
361 | | - { |
362 | | - icon_name = app_id; |
363 | | - } |
364 | | - } else |
365 | | - { |
366 | | - icon_name = icon->to_string(); |
367 | | - } |
368 | | - |
369 | | - WfIconLoadOptions options; |
370 | | - options.user_scale = scale; |
371 | | - image_set_icon(&image, icon_name); |
372 | | - |
373 | | - /* finally found some icon */ |
374 | | - if (icon_name != "unknown") |
375 | | - { |
376 | | - found_icon = true; |
377 | | - break; |
378 | | - } |
379 | | - } |
380 | | - |
381 | | - if (!found_icon) |
382 | | - { |
383 | | - std::cout << "Failed to load icon for any of " << app_id_list << std::endl; |
384 | | - } |
385 | | -} |
386 | | -} |
0 commit comments