Hindsight (Ubunutu 20.04)

Troy Williams

2021-05-30

Hindsight

Hindsight is a simple tool that will record the position and size of all the active windows on your Ubuntu system. It can restore them to the same position at a later time.

Problem

The problem in Ubuntu 20.04 (at least), Gnome has trouble handling the window positions when you have multiple monitors, each set up with very different resolutions. It is particularly pronounced when locking the system. Suppose one monitor wakes up faster than the other monitor. In that case, the system assumes it is gone and proceeds to place all the windows onto one monitor in a random mess. I currently have one monitor set to 1920 x 1080 and another to 3840 x 2160 (4k). My primary is the 4k monitor, and it is the slow one to wake up. I have found this very annoying when you have dozens of windows open across multiple virtual desktops!

The idea is to launch a script when you log in to monitor the system for a lock event. When the lock event is detected, the script calls hindsight and records the positions and sizes of the windows. When the script sees the unlock event, it calls hindsight, restoring the windows to their original position (even to the original virtual desktop).

It is a pretty neat app and will save some time and frustration. Usually, I have 20 or so windows scattered over several virtual desktops. It is tedious to reposition and resize the windows by hand every time I return to my computer.

Here is the project on Github, and you can also access it on PyPI.

NOTE: It only records information about windows that are currently open. If you record the position of the windows and open a new one, the new window will not be affected. If a window is closed, nothing will happen. It is ignored.

Versions and Requirements

You will need Python installed (the latest will probably work fine, but you need at a minimum v3.9). You will also need a tool called wmctrl.

Installation

Install wmctrl:

$ sudo apt install wmctrl

From PYPI

You can install the package from PyPi:

$ pip install hindsight-ubuntu

Verify it is installed correctly:

$ hindsight --version

From GitHub

You can install it from the git repo:

$ mkdir -p ~/repositories/hindsight

$ cd ~/repositories/hindsight

$ git clone https://github.com/TroyWilliams3687/hindsight.git

Create the virtual environment:

$ make

Activate the virtual environment with make:

$ make shell

Or manually:

$ . .venv/bin/activate

Usage

Save the current window sizes and positions:

$ hindsight save

Restore the current window sizes and positions:

$ hindsight restore

NOTE: You can also install it from the Github repo. The README contains the installation instructions.

.bashrc alias

I find it helpful to have bash aliases set up to initiate the save/restore. In some cases, I have found that the monitor didn’t wake up fast enough, and the windows were not correctly positioned. In those cases, simply executing $ hindsight restore did the trick (the positions were correctly recorded when the system locked).

alias hsr="/path/to/hindsight restore"
alias hss="/path/to/hindsight save"

Startup Script

I am also quite lazy and would forget to record the positions. I use a script that can detect when the system locks and unlocks. Most likely, you will need to modify the path to the repository to save/restore the window positions. You will need to change the path to hindsight in the script:

EXE=~/repositories/projects/hindsight/.venv/bin/hindsight

NOTE: You can determine the path to hindsight with the following command $ type hindsight.

NOTE: Sometimes, the script doesn’t work all of the time. The system may not move some of the windows to the correct virtual desktop. I find simply running $ hindsight restore or $ hsr manually fixes things. I think what is happening is that I log back in too quick before the monitor is fully restored from sleep and Ubuntu doesn’t see it.

NOTE: You add the script to the startup applications to launch when you first log in to the system, and it will continue to run until you log out.

Configuration File

If you find that the windows are not being positioned quite right, you can modify the configuration file and provide adjustments for specific classes of windows. The configuration file is located in the sample folder. It can be placed in ~/.config/bluebill.net/ hindsight/settings.yaml. This will will be used for fine tuning the window placement. It is structured as below:


# Hindsight
# ---------
#
# https://github.com/TroyWilliams3687/hindsight

# The configuration file allows you to customize Hindsight for your system. It
# allows you to set the scale and specific positions for specific windows that
# don't seem to follow the rules.

# Location - ~/.config/bluebill.net/hindsight/settings.toml

# ---------
# Scale

# Adjust the scale ratio if you have monitors at different resolution. Find the
# min and max values for the width and height and divide them to get a proper
# aspect ratio. For example, I have two monitors with the following resolutions:

#         w  x h
# m1 = [1920, 1080]
# m2 = [3840, 2160]

# scale_x = m1[0] / m2[0] = 0.5
# scale_y = m1[1] / m2[1] = 0.5

scale_x = 0.5 # Default 1.0
scale_y = 0.5 # Default 1.0

# ---------
# Window Fine Tuning

# Some windows will simply not play nice with the system. You can specify part
# of the text that is in the window title (that is how the system knows which
# window to use). You also can specify the x and y position of the upper left
# corner of the window. If one coordinate does not require an adjustment, set it
# to 0.

# NOTE: Default is empty

# NOTE: The text is case-sensitive

# https://toml.io/en/v1.0.0#array-of-tables

[[window_adjustments]]
title_text = "Firefox"
x = -7
y = -8

[[window_adjustments]]
title_text = "Discord"
x = -10
y = 0

NOTE: The idea is to tweak windows that don’t quite restore to the same position. This can be for a variety reason, including the use of different windowing systems. They shouldn’t be out by much,S

NOTE: Be default no scale or position adjustments will be applied.It will simply attempt to restore the windows to the locations that have been recorded.