Pankaj Bansal
Pankaj Bansal

Reputation: 919

Get system UIScale factor using gtk3 APIs

I am working on a SDK which uses gtk3 for windowing and event handling. I am trying to find the system UIScale using the gtk3 APIs. The scale can be greater than 1.0 on HiDPI screens. I was hoping that gtk_window_get_scale_factor will return the correct scale factor, but it is always returning 1, even if I have set the scale to 2.0.

I know that gtk has knowledge about the scale factor as gtk native apps are getting scaled properly. I have ran gtk3-demo and it is getting scaled properly depending upon the system UIScale. Can someone please help me to get the scaling factor using gtk3 APIs?

PS: The scale is set using these steps. Go to "Setting->Displays->Scale" and make scale 2.0. I am testing this on fedora 32 and Ubuntu 20.04.

Upvotes: 2

Views: 851

Answers (1)

ntd
ntd

Reputation: 7434

If what you are looking for is the DPI scale (usually set by the GDK_DPI_SCALE environment variable), I would use a variant the following code snippet:


static gdouble get_dpi_scale(GdkScreen *screen)
{
    gdouble dpi = gdk_screen_get_resolution(screen);
    GtkSettings *settings = gtk_settings_get_default();
    gint xft_dpi = 0;

    g_object_get(settings, "gtk-xft-dpi", &xft_dpi, NULL);
    g_return_val_if_fail(xft_dpi > 0, 0);

    return dpi / xft_dpi;
}

Upvotes: 4

Related Questions