scaling: Keep proportions of labels and icons when stretching layouts

Before this change, the labels and icons were stretched along with the
layout.

Part-of: <https://gitlab.gnome.org/World/Phosh/squeekboard/-/merge_requests/686>
This commit is contained in:
MoonlightWave-12
2024-09-25 15:03:43 +02:00
committed by Marge Bot
parent 96824b7c6e
commit 95be96d27e

View File

@ -56,13 +56,20 @@ render_outline (cairo_t *cr,
position.x, position.y, position.width, position.height);
}
float get_scale(cairo_t *cr) {
double get_scale_width(cairo_t *cr) {
double width = 1;
double height = 1;
cairo_user_to_device_distance (cr, &width, &height);
return width;
}
double get_scale_height(cairo_t *cr) {
double width = 1;
double height = 1;
cairo_user_to_device_distance (cr, &width, &height);
return height;
}
/// Rust interface
void eek_render_button_in_context(uint32_t scale_factor,
cairo_t *cr,
@ -79,11 +86,15 @@ void eek_render_button_in_context(uint32_t scale_factor,
/* render icon (if any) */
if (icon_name) {
int context_scale = ceil (get_scale (cr));
int context_scale = ceil (get_scale_width (cr));
double scale_x = get_scale_width (cr);
double scale_y = get_scale_height (cr);
cairo_surface_t *icon_surface =
eek_renderer_get_icon_surface (icon_name, 16, scale_factor * context_scale);
if (icon_surface) {
double width = cairo_image_surface_get_width (icon_surface);
double width = cairo_image_surface_get_width (icon_surface) * scale_y / scale_x ;
double height = cairo_image_surface_get_height (icon_surface);
cairo_save (cr);
@ -96,6 +107,9 @@ void eek_render_button_in_context(uint32_t scale_factor,
GdkRGBA color = {0};
gtk_style_context_get_color (ctx, GTK_STATE_FLAG_NORMAL, &color);
// Unstretch icons, when the layout is stretched.
cairo_scale (cr, scale_y / scale_x, 1.0);
cairo_set_source_rgba (cr, color.red,
color.green,
color.blue,
@ -190,6 +204,13 @@ render_button_label (cairo_t *cr,
GdkRGBA color = {0};
gtk_style_context_get_color (ctx, GTK_STATE_FLAG_NORMAL, &color);
double scale_x = get_scale_width (cr);
double scale_y = get_scale_height (cr);
// Unstretch labels, when the layout is stretched.
cairo_scale (cr, scale_y / scale_x, 1.0);
pango_cairo_update_layout (cr, layout);
cairo_set_source_rgba (cr,
color.red,
color.green,