Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

gtk - 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.

question from:https://stackoverflow.com/questions/65934974/get-system-uiscale-factor-using-gtk3-apis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...