Skip to content

Commit

Permalink
Add app icon above the regular icon
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Dec 15, 2023
1 parent b07a296 commit f7f1236
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
6 changes: 6 additions & 0 deletions data/style/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ $notification-shadow: 0 0 0 1px rgba(0, 0, 0, 0.3),
border-radius: 100px; /* Size in px */
margin: 4px;
}
.app-icon {
/* Notification app icon (only visible when the primary image is set) */
-gtk-icon-effect: none;
-gtk-icon-shadow: 0 1px 4px black;
margin: 6px;
}

.text-box {
margin: 0 8px;
Expand Down
4 changes: 2 additions & 2 deletions src/configSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
},
"notification-icon-size": {
"type": "integer",
"description": "The notification icon size (in pixels)",
"description": "The notification icon size (in pixels). The app icon size is 1/3",
"default": 64,
"minimum": 16
},
Expand Down Expand Up @@ -157,7 +157,7 @@
},
"image-visibility": {
"type": "string",
"description": "An explanation about the purpose of this instance.",
"description": "The notification image visibility when no icon is available.",
"default": "when-available",
"enum": ["always", "when-available", "never"]
},
Expand Down
24 changes: 12 additions & 12 deletions src/functions.vala
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ namespace SwayNotificationCenter {
theme.add_resource_path ("/org/erikreider/swaync/icons");
}

public static void set_image_path (owned string path,
Gtk.Image img,
int icon_size,
int radius,
bool file_exists) {
if ((path.length > 6 && path.slice (0, 7) == "file://") || file_exists) {
public static void set_image_uri (owned string uri,
Gtk.Image img,
int icon_size,
int radius,
bool file_exists) {
const string uri_prefix = "file://";
const uint prefix_size = 7;
bool is_uri = (uri.length >= prefix_size
&& uri.slice (0, prefix_size) == uri_prefix);
if (is_uri || file_exists) {
// Try as a URI (file:// is the only URI schema supported right now)
try {
if (!file_exists) path = path.slice (7, path.length);
if (is_uri) uri = uri.slice (prefix_size, uri.length);

var pixbuf = new Gdk.Pixbuf.from_file (path);
var pixbuf = new Gdk.Pixbuf.from_file (uri);
var surface = scale_round_pixbuf (pixbuf,
icon_size,
icon_size,
img.scale_factor,
radius);
img.set_from_surface (surface);
return;
} catch (Error e) {
stderr.printf (e.message + "\n");
}
} else if (Gtk.IconTheme.get_default ().has_icon (path)) {
// Try as a freedesktop.org-compliant icon theme
img.set_from_icon_name (path, Notification.icon_size);
}
}

Expand Down
34 changes: 27 additions & 7 deletions src/notification/notification.ui
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,35 @@
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkImage" id="img">
<object class="GtkOverlay">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">center</property>
<property name="margin-end">12</property>
<property name="icon_size">6</property>
<style>
<class name="image"/>
</style>
<child>
<object class="GtkImage" id="img">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="valign">center</property>
<property name="icon_size">6</property>
<style>
<class name="image"/>
</style>
</object>
<packing>
<property name="index">-1</property>
</packing>
</child>
<child type="overlay">
<object class="GtkImage" id="img_app_icon">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">end</property>
<property name="valign">end</property>
<property name="icon_size">6</property>
<style>
<class name="app-icon"/>
</style>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
Expand Down
52 changes: 39 additions & 13 deletions src/notification/notification.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace SwayNotificationCenter {
[GtkChild]
unowned Gtk.Image img;
[GtkChild]
unowned Gtk.Image img_app_icon;
[GtkChild]
unowned Gtk.Image body_image;

// Inline Reply
Expand Down Expand Up @@ -652,21 +654,36 @@ namespace SwayNotificationCenter {

private void set_icon () {
img.clear ();
img.set_visible (true);
img_app_icon.clear ();
img_app_icon.set_visible (true);

Icon ? app_icon_name = null;
string ? app_icon_uri = null;
if (param.desktop_app_info != null) {
app_icon_name = param.desktop_app_info.get_icon ();
}
if (param.app_icon != null && param.app_icon != "") {
app_icon_uri = param.app_icon;
}

var image_visibility = ConfigModel.instance.image_visibility;
if (image_visibility == ImageVisibility.NEVER) {
img.set_visible (false);
img_app_icon.set_visible (false);
return;
}

img.set_pixel_size (notification_icon_size);
img.height_request = notification_icon_size;
img.width_request = notification_icon_size;
int app_icon_size = notification_icon_size / 3;
img_app_icon.set_pixel_size (app_icon_size);

var img_path_exists = File.new_for_path (
var img_path_exists = File.new_for_uri (
param.image_path ?? "").query_exists ();
var app_icon_exists = File.new_for_path (
param.app_icon ?? "").query_exists ();
var app_icon_exists = File.new_for_uri (
app_icon_uri ?? "").query_exists ();

// Get the image CSS corner radius in pixels
int radius = 0;
Expand All @@ -677,38 +694,47 @@ namespace SwayNotificationCenter {
radius = value.get_int ();
}

// Set the main image to the provided image
if (param.image_data.is_initialized) {
Functions.set_image_data (param.image_data, img,
notification_icon_size, radius);
} else if (param.image_path != null &&
param.image_path != "" &&
img_path_exists) {
Functions.set_image_path (param.image_path, img,
Functions.set_image_uri (param.image_path, img,
notification_icon_size,
radius,
img_path_exists);
} else if (param.app_icon != null && param.app_icon != "") {
Functions.set_image_path (param.app_icon, img,
notification_icon_size,
radius,
app_icon_exists);
} else if (param.icon_data.is_initialized) {
Functions.set_image_data (param.icon_data, img,
notification_icon_size, radius);
}

if (img.storage_type == Gtk.ImageType.EMPTY) {
// Get the app icon
Icon ? icon = null;
if (param.desktop_app_info != null
&& (icon = param.desktop_app_info.get_icon ()) != null) {
img.set_from_gicon (icon, icon_size);
if (app_icon_uri != null) {
Functions.set_image_uri (app_icon_uri, img,
notification_icon_size,
radius,
app_icon_exists);
} else if (app_icon_name != null) {
img.set_from_gicon (app_icon_name, icon_size);
} else if (image_visibility == ImageVisibility.ALWAYS) {
// Default icon
img.set_from_icon_name ("image-missing", icon_size);
} else {
img.set_visible (false);
}
} else {
// We only set the app icon if the main image is set
if (app_icon_uri != null) {
Functions.set_image_uri (app_icon_uri, img_app_icon,
app_icon_size,
0,
app_icon_exists);
} else if (app_icon_name != null) {
img_app_icon.set_from_gicon (app_icon_name, Gtk.IconSize.INVALID);
}
}
}

Expand Down

0 comments on commit f7f1236

Please sign in to comment.