How to set scaling in a gtk4-rs / relm4 app?

I want to change the scale of the app. I found that the setting in GTK Inspector Global > Settings > Font > Scale is the perfect setting for me.

According to the source code (gtk/gtk/inspector/visual.c at 15edfe4ffa7a800ab67717e6a6be81d0b37c1813 · GNOME/gtk · GitHub), this is the line that I want, but in C:

g_object_set (vis->settings, "gtk-xft-dpi", (int)(factor * get_dpi_ratio (vis)), NULL);

Where the 3rd argument basically is a const.

vis->settings is defined here gtk/inspector/visual.c

void
gtk_inspector_visual_set_display (GtkInspectorVisual *vis,
                                  GdkDisplay         *display)
{
  vis->display = display;
  vis->settings = gtk_settings_get_for_display (display);

And the gtk_settings_get_for_display() functions is defined in gtk/gtk/gtksettings.c.

I found bindings for the g_object_set() function in the source and I tried things like

    let new_dpi: f64 = 2. * (96.0 * 1024.0);
    gtk_app.set_properties(&[("gtk-xft-dpi", &new_dpi)]);

(Panics with Can't find property 'gtk-xft-dpi' for type 'GtkApplication')

And I tried other things too with no success. I see there are a struct Settings, as well as Display in gtk4, but I don’t know how to access them. I guess they probably are defined in some private structure inside GTK::Application or something like that.

For instance, the gtk4::Settings has the gtk-xft-dpi property, but I can’t figure out how to access the current settings or setup a new one.

I’m using both gtk4-rs (in one of the attempts) and relm4 (with my second attempt to port the Python code).

Not a minute after posting this, I found the documentation part that I need :man_facepalming: .

On documentation for Struct gtk4::Settings:

Applications can override system-wide settings by setting the property of the Settings object with g_object_set(). This should be restricted to special cases though; Settings are not meant as an application configuration facility.

There is one Settings instance per display. It can be obtained with for_display(), but in many cases, it is more convenient to use WidgetExt::settings().

PoC of what I wanted:

    fn init_root() -> Self::Root {
        let win = gtk::Window::builder().title("uChoose").build();
        let settings = win.settings();
        let curr_dpi: i32 = settings.gtk_xft_dpi();
        println!("gtk-xft-dpi: {:#?} ({}*1024)", curr_dpi, curr_dpi / 1024);
        settings.set_gtk_xft_dpi(2 * curr_dpi);
        win
    }

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.