diff --git a/src/functions.vala b/src/functions.vala new file mode 100644 index 0000000..545b0cf --- /dev/null +++ b/src/functions.vala @@ -0,0 +1,27 @@ +namespace SwayNotificatonCenter { + public class Functions { + public static void set_image_path (owned string path, Gtk.Image img) { + if (path.slice (0, 7) == "file://") { + try { + path = path.slice (7, path.length); + var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, 48, 48); + img.set_from_pixbuf (pixbuf); + } catch (Error e) { + stderr.printf (e.message + "\n"); + img.set_from_icon_name ("image-missing", Gtk.IconSize.DIALOG); + } + return; + } + img.set_from_icon_name (path, Gtk.IconSize.DIALOG); + } + + public static void set_image_data (Image_Data data, Gtk.Image img) { + // Rebuild and scale the image + var pixbuf = new Gdk.Pixbuf.with_unowned_data (data.data, Gdk.Colorspace.RGB, + data.has_alpha, data.bits_per_sample, + data.width, data.height, data.rowstride, null); + var scaled_pixbuf = pixbuf.scale_simple (64, 64, Gdk.InterpType.BILINEAR); + img.set_from_pixbuf (scaled_pixbuf); + } + } +} diff --git a/src/meson.build b/src/meson.build index 7de8e1f..f959122 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,6 +4,7 @@ sway_notificaton_center_sources = [ 'notification/notification.vala', 'controlCenter/controlCenter.vala', 'constants.vala', + 'functions.vala', ] sway_notificaton_center_deps = [ diff --git a/src/notification/notification.vala b/src/notification/notification.vala index f9f6b1a..eb26434 100644 --- a/src/notification/notification.vala +++ b/src/notification/notification.vala @@ -42,7 +42,7 @@ namespace SwayNotificatonCenter { close_button.clicked.connect (close_notification); - set_icon.begin(); + set_icon (); if (show) this.show (); } @@ -90,25 +90,27 @@ namespace SwayNotificatonCenter { } } - private async void set_icon () { + private void set_icon () { + img.set_pixel_size (48); if (param.image_data.is_initialized) { - var data = param.image_data; - // Rebuild and scale the image - var pixbuf = new Gdk.Pixbuf.with_unowned_data (data.data, Gdk.Colorspace.RGB, - data.has_alpha, data.bits_per_sample, - data.width, data.height, data.rowstride, null); - var scaled_pixbuf = pixbuf.scale_simple (64, 64, Gdk.InterpType.BILINEAR); - img.set_from_pixbuf (scaled_pixbuf); - } else if (param.app_icon != "") { - img.set_from_icon_name (param.app_icon, Gtk.IconSize.DIALOG); + Functions.set_image_data (param.image_data, img); + } else if (param.image_path != null) { + Functions.set_image_path (param.image_path, img); + } else if (param.app_icon != null) { + Functions.set_image_path (param.app_icon, img); + } else if (param.icon_data.is_initialized) { + Functions.set_image_data (param.icon_data, img); } else { // Get the app icon GLib.Icon ? icon = null; - foreach (var app in AppInfo.get_all ()) { - if (app.get_name ().down () == param.app_name.down ()) { - icon = app.get_icon (); - break; + if (param.desktop_entry == "") { + foreach (var app in AppInfo.get_all ()) { + if (app.get_name ().down () == param.app_name.down ()) { + icon = app.get_icon (); + break; + } } + } else { } if (icon != null) { img.set_from_gicon (icon, Gtk.IconSize.DIALOG);