From cf910be1d771b615b76c77bc0b1510c7eac243aa Mon Sep 17 00:00:00 2001 From: Daemonratte Date: Sun, 3 May 2026 09:04:55 +0200 Subject: [PATCH] statusicon: honor X11 tray icon padding hint Read _NET_SYSTEM_TRAY_PADDING from the XEmbed tray manager and apply it as GtkStatusIcon image padding according to the tray orientation. Default padding remains zero, so behavior is unchanged for tray managers that do not provide the hint. Ported-from: Fedora gtk2 icon-padding.patch --- gtk/gtkstatusicon.c | 55 ++++++++++++++----- gtk/gtktrayicon-x11.c | 123 +++++++++++++++++++++++++++++++++++------- gtk/gtktrayicon.h | 3 +- 3 files changed, 146 insertions(+), 35 deletions(-) diff --git a/gtk/gtkstatusicon.c b/gtk/gtkstatusicon.c index d1d08a6b2..951f91d8d 100644 --- a/gtk/gtkstatusicon.c +++ b/gtk/gtkstatusicon.c @@ -171,8 +171,10 @@ static void gtk_status_icon_screen_changed (GtkStatusIcon *status_icon, GdkScreen *old_screen); static void gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon); static void gtk_status_icon_orientation_changed (GtkStatusIcon *status_icon); +static void gtk_status_icon_padding_changed (GtkStatusIcon *status_icon); +static void gtk_status_icon_update_padding (GtkStatusIcon *status_icon); static gboolean gtk_status_icon_scroll (GtkStatusIcon *status_icon, - GdkEventScroll *event); + GdkEventScroll *event); static gboolean gtk_status_icon_query_tooltip (GtkStatusIcon *status_icon, gint x, gint y, @@ -876,11 +878,13 @@ gtk_status_icon_init (GtkStatusIcon *status_icon) g_signal_connect_swapped (priv->tray_icon, "popup-menu", G_CALLBACK (gtk_status_icon_popup_menu), status_icon); g_signal_connect_swapped (priv->tray_icon, "notify::embedded", - G_CALLBACK (gtk_status_icon_embedded_changed), status_icon); + G_CALLBACK (gtk_status_icon_embedded_changed), status_icon); g_signal_connect_swapped (priv->tray_icon, "notify::orientation", - G_CALLBACK (gtk_status_icon_orientation_changed), status_icon); + G_CALLBACK (gtk_status_icon_orientation_changed), status_icon); + g_signal_connect_swapped (priv->tray_icon, "notify::padding", + G_CALLBACK (gtk_status_icon_padding_changed), status_icon); g_signal_connect_swapped (priv->tray_icon, "button-press-event", - G_CALLBACK (gtk_status_icon_button_press), status_icon); + G_CALLBACK (gtk_status_icon_button_press), status_icon); g_signal_connect_swapped (priv->tray_icon, "button-release-event", G_CALLBACK (gtk_status_icon_button_release), status_icon); g_signal_connect_swapped (priv->tray_icon, "scroll-event", @@ -894,6 +898,8 @@ gtk_status_icon_init (GtkStatusIcon *status_icon) gtk_container_add (GTK_CONTAINER (priv->tray_icon), priv->image); gtk_widget_show (priv->image); + gtk_status_icon_update_padding (status_icon); + g_signal_connect_swapped (priv->image, "size-allocate", G_CALLBACK (gtk_status_icon_size_allocate), status_icon); @@ -1009,21 +1015,19 @@ gtk_status_icon_finalize (GObject *object) #ifdef GDK_WINDOWING_X11 g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_key_press, status_icon); + gtk_status_icon_embedded_changed, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_popup_menu, status_icon); + gtk_status_icon_orientation_changed, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_embedded_changed, status_icon); + gtk_status_icon_padding_changed, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_orientation_changed, status_icon); + gtk_status_icon_button_press, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_button_press, status_icon); + gtk_status_icon_button_release, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_button_release, status_icon); + gtk_status_icon_scroll, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_scroll, status_icon); - g_signal_handlers_disconnect_by_func (priv->tray_icon, - gtk_status_icon_query_tooltip, status_icon); + gtk_status_icon_query_tooltip, status_icon); g_signal_handlers_disconnect_by_func (priv->tray_icon, gtk_status_icon_screen_changed, status_icon); gtk_widget_destroy (priv->image); @@ -1729,12 +1733,35 @@ gtk_status_icon_embedded_changed (GtkStatusIcon *status_icon) static void gtk_status_icon_orientation_changed (GtkStatusIcon *status_icon) { + gtk_status_icon_update_padding (status_icon); g_object_notify (G_OBJECT (status_icon), "orientation"); } +static void +gtk_status_icon_padding_changed (GtkStatusIcon *status_icon) +{ + gtk_status_icon_update_padding (status_icon); +} + +static void +gtk_status_icon_update_padding (GtkStatusIcon *status_icon) +{ + GtkStatusIconPrivate *priv = status_icon->priv; + GtkOrientation orientation; + gint padding; + + orientation = _gtk_tray_icon_get_orientation (GTK_TRAY_ICON (priv->tray_icon)); + padding = _gtk_tray_icon_get_padding (GTK_TRAY_ICON (priv->tray_icon)); + + if (orientation == GTK_ORIENTATION_HORIZONTAL) + gtk_misc_set_padding (GTK_MISC (priv->image), padding, 0); + else + gtk_misc_set_padding (GTK_MISC (priv->image), 0, padding); +} + static gboolean gtk_status_icon_key_press (GtkStatusIcon *status_icon, - GdkEventKey *event) + GdkEventKey *event) { guint state, keyval; diff --git a/gtk/gtktrayicon-x11.c b/gtk/gtktrayicon-x11.c index 11056b999..f7dbba4b4 100644 --- a/gtk/gtktrayicon-x11.c +++ b/gtk/gtktrayicon-x11.c @@ -43,23 +43,26 @@ enum { PROP_0, - PROP_ORIENTATION + PROP_ORIENTATION, + PROP_PADDING }; struct _GtkTrayIconPrivate { guint stamp; - + Atom selection_atom; Atom manager_atom; Atom system_tray_opcode_atom; Atom orientation_atom; + Atom padding_atom; Atom visual_atom; Window manager_window; GdkVisual *manager_visual; gboolean manager_visual_rgba; GtkOrientation orientation; + gint padding; }; static void gtk_tray_icon_constructed (GObject *object); @@ -105,13 +108,23 @@ gtk_tray_icon_class_init (GtkTrayIconClass *class) widget_class->expose_event = gtk_tray_icon_expose; g_object_class_install_property (gobject_class, - PROP_ORIENTATION, - g_param_spec_enum ("orientation", - P_("Orientation"), - P_("The orientation of the tray"), - GTK_TYPE_ORIENTATION, - GTK_ORIENTATION_HORIZONTAL, - GTK_PARAM_READABLE)); + PROP_ORIENTATION, + g_param_spec_enum ("orientation", + P_("Orientation"), + P_("The orientation of the tray"), + GTK_TYPE_ORIENTATION, + GTK_ORIENTATION_HORIZONTAL, + GTK_PARAM_READABLE)); + + g_object_class_install_property (gobject_class, + PROP_PADDING, + g_param_spec_int ("padding", + P_("Padding"), + P_("The padding that should be used around the tray icon"), + 0, + G_MAXINT, + 0, + GTK_PARAM_READABLE)); } static void @@ -122,6 +135,7 @@ gtk_tray_icon_init (GtkTrayIcon *icon) icon->priv->stamp = 1; icon->priv->orientation = GTK_ORIENTATION_HORIZONTAL; + icon->priv->padding = 0; gtk_widget_set_app_paintable (GTK_WIDGET (icon), TRUE); gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK); @@ -152,12 +166,16 @@ gtk_tray_icon_constructed (GObject *object) False); icon->priv->orientation_atom = XInternAtom (xdisplay, - "_NET_SYSTEM_TRAY_ORIENTATION", - False); + "_NET_SYSTEM_TRAY_ORIENTATION", + False); + + icon->priv->padding_atom = XInternAtom (xdisplay, + "_NET_SYSTEM_TRAY_PADDING", + False); icon->priv->visual_atom = XInternAtom (xdisplay, - "_NET_SYSTEM_TRAY_VISUAL", - False); + "_NET_SYSTEM_TRAY_VISUAL", + False); /* Add a root window filter so that we get changes on MANAGER */ gdk_window_add_filter (root_window, @@ -212,6 +230,9 @@ gtk_tray_icon_get_property (GObject *object, case PROP_ORIENTATION: g_value_set_enum (value, icon->priv->orientation); break; + case PROP_PADDING: + g_value_set_int (value, icon->priv->padding); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -378,6 +399,52 @@ gtk_tray_icon_get_visual_property (GtkTrayIcon *icon) XFree (prop.prop); } +static void +gtk_tray_icon_get_padding_property (GtkTrayIcon *icon) +{ + GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (icon)); + Display *xdisplay = GDK_DISPLAY_XDISPLAY (display); + Atom type; + int format; + union { + gulong *prop; + guchar *prop_ch; + } prop = { NULL }; + gulong nitems; + gulong bytes_after; + gint padding = 0; + int error, result; + + g_assert (icon->priv->manager_window != None); + + gdk_error_trap_push (); + type = None; + result = XGetWindowProperty (xdisplay, + icon->priv->manager_window, + icon->priv->padding_atom, + 0, G_MAXLONG, FALSE, + XA_CARDINAL, + &type, &format, &nitems, + &bytes_after, &(prop.prop_ch)); + error = gdk_error_trap_pop (); + + if (!error && result == Success && + type == XA_CARDINAL && nitems == 1 && format == 32) + padding = prop.prop[0]; + + if (type != None) + XFree (prop.prop); + + if (padding < 0) + padding = 0; + + if (icon->priv->padding != padding) + { + icon->priv->padding = padding; + g_object_notify (G_OBJECT (icon), "padding"); + } +} + static GdkFilterReturn gtk_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, @@ -398,13 +465,21 @@ gtk_tray_icon_manager_filter (GdkXEvent *xevent, else if (xev->xany.window == icon->priv->manager_window) { if (xev->xany.type == PropertyNotify && - xev->xproperty.atom == icon->priv->orientation_atom) - { + xev->xproperty.atom == icon->priv->orientation_atom) + { GTK_NOTE (PLUGSOCKET, - g_print ("GtkStatusIcon %p: got PropertyNotify on manager window for orientation atom\n", icon)); + g_print ("GtkStatusIcon %p: got PropertyNotify on manager window for orientation atom\n", icon)); - gtk_tray_icon_get_orientation_property (icon); - } + gtk_tray_icon_get_orientation_property (icon); + } + else if (xev->xany.type == PropertyNotify && + xev->xproperty.atom == icon->priv->padding_atom) + { + GTK_NOTE (PLUGSOCKET, + g_print ("GtkStatusIcon %p: got PropertyNotify on manager window for padding atom\n", icon)); + + gtk_tray_icon_get_padding_property (icon); + } else if (xev->xany.type == DestroyNotify) { GTK_NOTE (PLUGSOCKET, @@ -505,10 +580,11 @@ gtk_tray_icon_update_manager_window (GtkTrayIcon *icon) gdkwin = gdk_window_lookup_for_display (display, icon->priv->manager_window); - + gdk_window_add_filter (gdkwin, gtk_tray_icon_manager_filter, icon); gtk_tray_icon_get_orientation_property (icon); + gtk_tray_icon_get_padding_property (icon); gtk_tray_icon_get_visual_property (icon); if (gtk_widget_get_realized (GTK_WIDGET (icon))) @@ -748,7 +824,14 @@ _gtk_tray_icon_get_orientation (GtkTrayIcon *icon) return icon->priv->orientation; } +gint +_gtk_tray_icon_get_padding (GtkTrayIcon *icon) +{ + g_return_val_if_fail (GTK_IS_TRAY_ICON (icon), 0); + + return icon->priv->padding; +} + #define __GTK_TRAY_ICON_X11_C__ #include "gtkaliasdef.c" - diff --git a/gtk/gtktrayicon.h b/gtk/gtktrayicon.h index 4c1e18489..6dc98521f 100644 --- a/gtk/gtktrayicon.h +++ b/gtk/gtktrayicon.h @@ -69,7 +69,8 @@ void _gtk_tray_icon_cancel_message (GtkTrayIcon *icon, guint id); GtkOrientation _gtk_tray_icon_get_orientation (GtkTrayIcon *icon); - +gint _gtk_tray_icon_get_padding (GtkTrayIcon *icon); + G_END_DECLS #endif /* __GTK_TRAY_ICON_H__ */