Gnome Font Scaling Script

Posted by Ryan Himmelwright on Mon, Feb 28, 2022
Tags linux, fedora, gnome, shell
Surf City, NC

Previously, I was using the Pantheon desktop environment on Fedora 35 for my work laptop. I liked how well Pantheon handled font scaling with my 4k monitor, and I figured out how to get the accessability menubar item working in Fedora. This allowed me to easily toggle the scaling factor as I connected and reconnected to different monitors. With that said, I have recently starting using Gnome on the laptop, and miss that feature of Pantheon.

Background

Unscaled 4k display

4k Display without scaling

I have been using Fedora Gnome on my work computer for the last month or so. The font scaling works well enough, and it seems to be a little less resource hungry/buggy than my cobbled-together Fedora Pantheon setup.

Most of the day, my laptop is connected to a 32" 4k monitor, which I need at least 125% scaling, and usually prefer to use 150% scaling. However, when I disconnect the laptop and want to work elsewhere, the 14" 1080p screen doesn’t have enough space at 150%, requiring me to tune down the scaling. I wanted a way to once again easily toggle though different scaling factors, without needing to navigate through the tweaks settings menus (which can also be difficult to do when connecting to a monitor that isn’t properly scaling).

So, I figured out how to script it.

Creating the Script

Changing Font Scaling Setting

First, I had to learn out how to change the scaling factor using the command line in Gnome. It turns out this is an item in gsettings, so it is easy to get the current value, and set a new one. I used these to commands to start my script:

SCALE=$(gsettings get org.gnome.desktop.interface text-scaling-factor)
gsettings set org.gnome.desktop.interface text-scaling-factor $SCALE_SWITCH

The first line grabs the current scaling factor and saves it to the SCALE variable (for use later). The second line sets the scaling factor to the value of a SCALE_SWITCH variable (not defined yet).

Simple Logic

I wanted to be able to toggle between my desktop monitor and laptop setups using a keybinding, but realized I ideally wanted 3 options. So, I used some simple logic to flip through the 3 options in order, which would work just as well:

# Set what to toggle to
if [ $SCALE == '1.0' ]; then
    SCALE_SWITCH=1.25
elif [ $SCALE == '1.25' ]; then
    SCALE_SWITCH=1.5
else [ $SCALE == '1.5' ]
    SCALE_SWITCH=1.0
fi

This code uses the $SCALE value we grabbed in the section above, and matches it to determine what the next value (SCALE_SWITCH) should be.

Notify

Notification message
Notification message

Working in the CLI on the script, I liked having a debug message print out so I could see the what scale it was switching to and from. After adding the script to a key-binding, I still wanted that feedback. To accomplish this, I added a notify-send line in the script to launch a Gnome notification when it runs.

This has the potential to be a bit annoying… but it can always commented out if I really don’t like it anymore:

# (Optional) Message intentions to CLI and GNOME Notifications
echo -e "Previous Font Scale: $SCALE, Switched to $SCALE_SWITCH"
notify-send "Previous Font Scale: $SCALE, Switched to $SCALE_SWITCH"

These two lines print out a message to both the command line (first line) and a gnome notification (second line) about what the previous scale was and what it is being switched to.

With the press of a key

Add shortcut to run script
Adding shortcut to run the script.

Lastly, I wanted the ability to execute the script by hitting a key binding. First, I made a symbolic link to the script location from /usr/bin/toggle-font-scale:

sudo ln -s /home/ryan/dotfiles/scripts/toggle-gnome-font-scale /usr/bin/toggle-font-scale

Next, I added a custom Gnome keyboard shortcut using gnome settings (for me, CTRL+SHIFT+M) to run the script, which toggles the scaling factor.

Conclusion

After some cleanup, I had my completed script:

#!/bin/bash

# Get Current Scailing factor
SCALE=$(gsettings get org.gnome.desktop.interface text-scaling-factor)

# Set what to toggle to
if [ $SCALE == '1.0' ]; then
    SCALE_SWITCH=1.25
elif [ $SCALE == '1.25' ]; then
    SCALE_SWITCH=1.5
else [ $SCALE == '1.5' ]
    SCALE_SWITCH=1.0
fi

# (Optional) Message intentions to CLI and GNOME Notifications
echo -e "Previous Font Scale: $SCALE, Switched to $SCALE_SWITCH"
notify-send "Previous Font Scale: $SCALE, Switched to $SCALE_SWITCH"

# Run switch command
gsettings set org.gnome.desktop.interface text-scaling-factor $SCALE_SWITCH

It’s a simple solution and I’m very happy with it. I use it all the time,and at this point, it feels like it is just a feature baked into Gnome. I’m glad I took the 20 minutes to figure it out!

Next Post:
Prev Post:

Building a 34 Key Keyboard Layout Intro to Python Type Hinting