Multi monitors panel layouts and background switcher

Multi monitors panel layouts and background switcher

Introduction:
If you use Ubuntu MATE with several monitors, you might have figured out it can be tricky sometimes.

  • When turning on additional monitors, they will be panel-free. You can add/customize panels and docks to it but as soon as you turn an additional monitor off, those will go to the remaining monitor(s) and of course, you don't want that.
  • Turning on more than one monitor during a session gives me corruption on the added monitor space (Windows leave a trail etc...). Reloading/changing the desktop background fixes immediately the issue for whatever reason.

This little script aims to provide a workaround for both problems by switching between panel layouts and desktop background(s) for each situation (monitor 1, monitor 1 + 2 etc...) using a keyboard shortcut.

1 - Preparing panel layouts.

A - For the first monitor.
Edit your panel for your single monitor usage.
Open mate-tweak
Go to the panel section, click save as and name it solo.

B - For the second monitor.
Add your panels on monitor 2 and customize it. To do so, add a new panel on monitor 1 by right clicking on an existing panel, select add a new panel, then go to its properties, untick "extend" and drag it on monitor 2 while maintaining the left alt key and fill it with whatever applets you want.
Save your new dual monitor layout in mate-tweak and name it dual.

C - For the third monitor.
Repeat step B on monitor 3 but name it triple this time.

D - The Plank dock (optional).
If you want your Plank dock on more than one monitor, do this:
In a terminal window:
cd ~/.config/plank & mkdir dock2
cp dock1/launchers dock2/
plank -n dock2
Dock2 should then launch on top of Dock1 with the same launchers icons.
With an additional monitor turned on, hold ctrl and right click on the dock, go to Preferences in order to select the monitor this second dock will be displayed on.
It should now display on the second monitor.

You can repeat this with as many docks you want. Just edit dock2 to dock3 for monitor 3 etc...

Notes:

Layouts are stored in /usr/share/mate-panel/layouts. Customs layouts have "-tweak" added at the end of the filename given in mate-tweak. solo layout will become solo-tweak for instance.

[IMPORTANT]: at the time of the Ubuntu MATE 19.10 Eoan Ermine release (10/17/2019), switching to custom layouts made for several monitors is currently broken. Panels will go on top of each other and sometimes be blank if you use the same applets on your panels.
To fix it:
Go to /usr/share/mate-panel/layouts
If your custom layout is named "multimon", you will find multimon.panel and multimon.layout files.
You need to edit the *.layoutfile as root.
a) In order to put the right panels on the right monitors:
If you use two monitors, you will see this section at the top of the file:

[Toplevel toplevel-0]
size=28
expand=true
orientation=top

Adding monitor=1 as shown below will allow panel 2 to be on monitor 2.

[Toplevel toplevel-0]
size=28
expand=true
orientation=top
monitor=1

If you use three monitors, I assume there will be a Toplevel toplevel-1 section for your third panel. Adding monitor=2 to it should put panel 3 on monitor 3.

b) In order to fix the missing applets:
You need to rename the objects so that they don't share the same name.
If you locate two [Object briskmenu] for instance, rename the second one [Object briskmenu2].
Save and exit.

Now the panels and applets should load correctly when switching between custom layouts.

2 - The switching script

A - Creating the script
Copy this script, paste it in a text editor, edit the path to your background(s), save it and gave it a name. Make it executable (chmod +x filename in a terminal or tick the case in the permissions tab of the file properties) . It's currently written for 3 monitors as triple monitor setups seem to be quite popular.

#!/bin/bash
########## MATE panel switcher ##########
l='mate-tweak --layout'
bg='gsettings set org.mate.background picture-filename'
bgo='gsettings set org.mate.background picture-options'
n='notify-send -i mate -t 2000'
p='plank -n'

function solo()
{
$n 'MATE panel switcher' 'Switching to one monitor layout'
$l solo-tweak
$bg '/path/to/solomonitorbackground.png'
$bgo 'zoom'
pkill plank
sleep 1
$p dock1
}

function dual()
{
$n 'MATE panel switcher' 'Switching to dual monitors layout'
$l dual-tweak
$bg '/path/to/dualmonitorsbackground.png'
$bgo 'spanned'
pkill plank
sleep 1
$p dock1
$p dock2
}

function triple()
{
$n 'MATE panel switcher' 'Switching to triple monitors layout'
$l triple-tweak
$bg '/path/to/triplemonitorsbackground.png'
$bgo 'spanned'
pkill plank
sleep 1
$p dock1
$p dock2
$p dock3
}

if [ $(xrandr | grep -sw 'connected' | wc -l) == "1" ] ; then
solo
elif [ $(xrandr | grep -sw 'connected' | wc -l) == "2" ] ; then
dual
elif [ $(xrandr | grep -sw 'connected' | wc -l) == "3" ] ; then
triple
else
exit
fi

B - Assigning a keyboard shortcut to the script
Open mate-keybinding-properties
Add a new shortcut.
As a command, choose the /path/to/the/scriptname and apply.
Click on "disabled" in the shortcut column next to it and give it the key combination you want. One you will remember. I chose Alt+C.

C - Usage

  • Just enter your keyboard shortcut every time you turn on or off an additional monitor.
  • Code contributor vkareh provided an interesting solution in the forums for automating stuff when plugging several monitors: autorandr. As mentioned here you can use postswitch files in order to execute commands every time you plug/unplug a monitor. Sadly, it would only work when I switch from one to several monitors and not the other way around and seemed to open more and more panels/plank processes in the meantime. So I chose to stick with the manual workaround at the moment.

Notes:

  • Feel free to comment/remove Plank dock related lines if you don't use it.
  • Add other similar functions if you use more than 3 monitors.
  • The "spanned" picture option allows you to use background pictures designed for multi monitors with resolutions like 3840x1080 (2x 1080p monitors) or 5760x1080 (3x 1080p monitors) and stretch them all over the space. Use "zoom" instead if you want the same image repeated on all monitors.
  • pkill plank is there in order to kill docks so that docks from additional monitors don't sit on other docks when turning them off. I couldn't find a command to kill a specific dock so I killed them all. docks for the remaining monitors will automatically be reloaded one second later.

Example screenshots:
Default behavior with two monitors:


Expected behavior with two monitors (using script):

Back to one monitor (without script):
4
Back to one monitor (using script):
3

Enjoy !

3 Likes

Well done! I run triple monitors and have the exact same problems when on occasion I need to disconnect one.

Thanks very much!

Cheers

1 Like