]> git.mdlowis.com Git - proto/labwc.git/commitdiff
theme: draw hover overlay in the correct scale
authortokyo4j <hrak1529@gmail.com>
Thu, 3 Oct 2024 00:23:50 +0000 (09:23 +0900)
committerJohan Malm <johanmalm@users.noreply.github.com>
Thu, 3 Oct 2024 20:24:19 +0000 (21:24 +0100)
The hover icon buffer on which the hover overlay is draw can be larger
than the button size when a large non-hover icon is provided by user. In
this case, we should scale up all the coorinates that are used to draw
the hover overlay. Before this commit, corner radius and padding.width
are not scaled correctly.

This commit fixes it by scaling the cairo context with cairo_scale()
when drawing the hover overlay.

src/theme.c

index b3c864d878335dbfbd5e3387cb56880c311b8a14..f3b328b0059b8ac2fb401780396980427904fd25 100644 (file)
@@ -135,34 +135,37 @@ create_hover_fallback(struct theme *theme, const char *icon_name,
        int width = theme->window_button_width;
        int height = theme->title_height;
 
-       if (width && height) {
-               /*
-                * Proportionately increase size of hover_buffer if the
-                * non-hover 'donor' buffer is larger than the allocated space.
-                * It will get scaled down again by wlroots when rendered and as
-                * required by the current output scale.
-                *
-                * This ensures that icons > width or > height keep their aspect
-                * ratio and are rendered the same as without the hover overlay.
-                */
-               double scale = MAX((double)icon_width / width,
-                               (double)icon_height / height);
-               if (scale > 1.0f) {
-                       width = (double)width * scale;
-                       height = (double)height * scale;
-               }
+       /*
+        * Proportionately increase size of hover_buffer if the non-hover
+        * 'donor' buffer is larger than the allocated space. It will get
+        * scaled down again by wlroots when rendered and as required by the
+        * current output scale.
+        *
+        * This ensures that icons > width or > height keep their aspect ratio
+        * and are rendered the same as without the hover overlay.
+        */
+       double scale = (width && height) ?
+               MAX((double)icon_width / width, (double)icon_height / height) : 1.0;
+       if (scale < 1.0) {
+               scale = 1.0;
        }
+       int buffer_width = (double)width * scale;
+       int buffer_height = (double)height * scale;
 
-       *hover_buffer = buffer_create_cairo(width, height, 1.0, true);
+       *hover_buffer = buffer_create_cairo(buffer_width, buffer_height, 1.0, true);
 
        cairo_t *cairo = (*hover_buffer)->cairo;
        cairo_surface_t *surf = cairo_get_target(cairo);
 
        /* Background */
        cairo_set_source_surface(cairo, icon.surface,
-               (width - icon_width) / 2, (height - icon_height) / 2);
+               (buffer_width - icon_width) / 2, (buffer_height - icon_height) / 2);
        cairo_paint(cairo);
 
+       /* Switch from buffer scale to theme scale */
+       cairo_save(cairo);
+       cairo_scale(cairo, scale, scale);
+
        /* Overlay (pre-multiplied alpha) */
        float overlay_color[4] = { 0.15f, 0.15f, 0.15f, 0.3f};
        int radius = MIN(width, height) / 2;
@@ -212,6 +215,7 @@ create_hover_fallback(struct theme *theme, const char *icon_name,
                break;
        }
        cairo_surface_flush(surf);
+       cairo_restore(cairo);
 
        if (icon.is_duplicate) {
                cairo_surface_destroy(icon.surface);