From 531111755eb17a152186cddbdb9804ecfca38b72 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Fri, 18 Oct 2024 09:09:34 -0400 Subject: [PATCH 1/3] gtkui: Avoid using float as counter --- src/gtkui/ui_infoarea.cc | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/gtkui/ui_infoarea.cc b/src/gtkui/ui_infoarea.cc index 1fd4e2021..cd72159d5 100644 --- a/src/gtkui/ui_infoarea.cc +++ b/src/gtkui/ui_infoarea.cc @@ -30,6 +30,9 @@ #include "ui_infoarea.h" +#define ALPHA_STEPS 10 +static inline float TO_ALPHA (int steps) { return (float) steps / ALPHA_STEPS; } + #define VIS_BANDS 12 #define VIS_DELAY 2 /* delay before falloff in frames */ #define VIS_FALLOFF 2 /* falloff in decibels per frame */ @@ -56,7 +59,7 @@ typedef struct { String title, artist, album; String last_title, last_artist, last_album; AudguiPixbuf pb, last_pb; - float alpha, last_alpha; + int alpha_steps, last_alpha_steps; bool show_art; bool stopped; @@ -207,7 +210,7 @@ static void draw_album_art (cairo_t * cr) int left = SPACING + (ICON_SIZE - area->pb.width ()) / 2; int top = SPACING + (ICON_SIZE - area->pb.height ()) / 2; gdk_cairo_set_source_pixbuf (cr, area->pb.get (), left, top); - cairo_paint_with_alpha (cr, area->alpha); + cairo_paint_with_alpha (cr, TO_ALPHA (area->alpha_steps)); } if (area->last_pb) @@ -215,7 +218,7 @@ static void draw_album_art (cairo_t * cr) int left = SPACING + (ICON_SIZE - area->last_pb.width ()) / 2; int top = SPACING + (ICON_SIZE - area->last_pb.height ()) / 2; gdk_cairo_set_source_pixbuf (cr, area->last_pb.get (), left, top); - cairo_paint_with_alpha (cr, area->last_alpha); + cairo_paint_with_alpha (cr, TO_ALPHA (area->last_alpha_steps)); } } @@ -230,25 +233,27 @@ static void draw_title (cairo_t * cr) int y_offset1 = ICON_SIZE / 2; int y_offset2 = ICON_SIZE * 3 / 4; int width = alloc.width - x; + float alpha = TO_ALPHA (area->alpha_steps); + float last_alpha = TO_ALPHA (area->last_alpha_steps); if (area->title) - draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, area->alpha, + draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, alpha, "18", area->title); if (area->last_title) - draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, area->last_alpha, + draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, last_alpha, "18", area->last_title); if (area->artist) draw_text (area->main, cr, x, SPACING + y_offset1, width, 1, 1, 1, - area->alpha, "9", area->artist); + alpha, "9", area->artist); if (area->last_artist) draw_text (area->main, cr, x, SPACING + y_offset1, width, 1, 1, 1, - area->last_alpha, "9", area->last_artist); + last_alpha, "9", area->last_artist); if (area->album) draw_text (area->main, cr, x, SPACING + y_offset2, width, 0.7, - 0.7, 0.7, area->alpha, "9", area->album); + 0.7, 0.7, alpha, "9", area->album); if (area->last_album) draw_text (area->main, cr, x, SPACING + y_offset2, width, 0.7, - 0.7, 0.7, area->last_alpha, "9", area->last_album); + 0.7, 0.7, last_alpha, "9", area->last_album); } #ifdef USE_GTK3 @@ -276,15 +281,15 @@ static void ui_infoarea_do_fade (void *) g_return_if_fail (area); bool done = true; - if (aud_drct_get_playing () && area->alpha < 1) + if (aud_drct_get_playing () && area->alpha_steps < ALPHA_STEPS) { - area->alpha += 0.1; + area->alpha_steps ++; done = false; } - if (area->last_alpha > 0) + if (area->last_alpha_steps > 0) { - area->last_alpha -= 0.1; + area->last_alpha_steps --; done = false; } @@ -340,8 +345,8 @@ static void infoarea_next () area->last_album = std::move (area->album); area->last_pb = std::move (area->pb); - area->last_alpha = area->alpha; - area->alpha = 0; + area->last_alpha_steps = area->alpha_steps; + area->alpha_steps = 0; gtk_widget_queue_draw (area->main); } @@ -457,7 +462,7 @@ GtkWidget * ui_infoarea_new () set_album_art (); /* skip fade-in */ - area->alpha = 1; + area->alpha_steps = ALPHA_STEPS; } GtkWidget * frame = gtk_frame_new (nullptr); From 098b77a7928af2cbd4afcbce99caba3f89bd4bf2 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 21 Oct 2024 18:09:33 +0200 Subject: [PATCH 2/3] gtkui: Simplify text handling for info area Setting the font size and text directly is cleaner and more efficient. --- src/gtkui/ui_infoarea.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/gtkui/ui_infoarea.cc b/src/gtkui/ui_infoarea.cc index cd72159d5..109291312 100644 --- a/src/gtkui/ui_infoarea.cc +++ b/src/gtkui/ui_infoarea.cc @@ -141,15 +141,14 @@ G_GNUC_END_IGNORE_DEPRECATIONS } static void draw_text (GtkWidget * widget, cairo_t * cr, int x, int y, int - width, float r, float g, float b, float a, const char * font, - const char * text) + width, float r, float g, float b, float a, int font_size, const char * text) { cairo_move_to (cr, x, y); cairo_set_source_rgba (cr, r, g, b, a); - PangoFontDescription * desc = pango_font_description_from_string (font); - PangoLayout * pl = gtk_widget_create_pango_layout (widget, nullptr); - pango_layout_set_text (pl, text, -1); + PangoLayout * pl = gtk_widget_create_pango_layout (widget, text); + PangoFontDescription * desc = pango_font_description_new (); + pango_font_description_set_size (desc, font_size * PANGO_SCALE); pango_layout_set_font_description (pl, desc); pango_font_description_free (desc); pango_layout_set_width (pl, width * PANGO_SCALE); @@ -238,22 +237,22 @@ static void draw_title (cairo_t * cr) if (area->title) draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, alpha, - "18", area->title); + 18, area->title); if (area->last_title) draw_text (area->main, cr, x, SPACING, width, 1, 1, 1, last_alpha, - "18", area->last_title); + 18, area->last_title); if (area->artist) draw_text (area->main, cr, x, SPACING + y_offset1, width, 1, 1, 1, - alpha, "9", area->artist); + alpha, 9, area->artist); if (area->last_artist) draw_text (area->main, cr, x, SPACING + y_offset1, width, 1, 1, 1, - last_alpha, "9", area->last_artist); + last_alpha, 9, area->last_artist); if (area->album) draw_text (area->main, cr, x, SPACING + y_offset2, width, 0.7, - 0.7, 0.7, alpha, "9", area->album); + 0.7, 0.7, alpha, 9, area->album); if (area->last_album) draw_text (area->main, cr, x, SPACING + y_offset2, width, 0.7, - 0.7, 0.7, last_alpha, "9", area->last_album); + 0.7, 0.7, last_alpha, 9, area->last_album); } #ifdef USE_GTK3 From e90351e4138fa50add53a4a86e8a0488a79ae005 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Mon, 21 Oct 2024 18:33:47 +0200 Subject: [PATCH 3/3] gtkui: Hide info area text containing emoji properly. Closes: #1491 Fading out text with emoji characters can lead to them still being displayed above the text of a new track. Ignoring zero alpha channels avoids this. Reproduced on Ubuntu 24.04. Arch Linux requires "noto-fonts-emoji". --- src/gtkui/ui_infoarea.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/gtkui/ui_infoarea.cc b/src/gtkui/ui_infoarea.cc index 109291312..25da5d4f3 100644 --- a/src/gtkui/ui_infoarea.cc +++ b/src/gtkui/ui_infoarea.cc @@ -143,6 +143,12 @@ G_GNUC_END_IGNORE_DEPRECATIONS static void draw_text (GtkWidget * widget, cairo_t * cr, int x, int y, int width, float r, float g, float b, float a, int font_size, const char * text) { + /* The visibility of Unicode characters like emoji is not affected by the + * alpha channel. Make sure to hide them when fading-out track information. + * See also: #1491 */ + if (a <= 0) + return; + cairo_move_to (cr, x, y); cairo_set_source_rgba (cr, r, g, b, a);