Feature Request: Auto-change day/night theme

Hi Lubuntu,

I use Lubuntu 22.04.1. I was wondering if we could add the functionality to add auto changing a day/night theme on Lubuntu. This would mean defining two palettes, a day one and a night one, and setting a cron job to check the time and switch between the two. I already asked upstream, and they don’t want to implement it because they think it will cause problems with other Qt apps. But I think even for the default Lubuntu apps, this would be nice. We only need to extract the StyleConfig::applyStyle() from the lxqt-config-appearance github code and implement it in isolation. What do people think?

Best,

thedoctar

I don’t understand that upstream, as I understand it, is not really eager to implement such a feature request.

Some days ago some other Lubuntu user had a question about changing the colour of the desktop (with no image projected onto it) to a specific colour in function of the day of the week. I advised him to use sed, and I advise you to do the same. See file: ~/.config/lxqt/lxqt.conf.

For you the quickest possible solution. Let me know if you need help.

I have written a script to check the time of day and change the theme. One could run this hourly as a cron job for auomatic changing. I attach it here. If anyone is interested in integrating this into Lubuntu in a better way, please feel free. I have no experience in software development.

#!/usr/bin/bash

# In order for the changes to the qterminal colour scheme to be persistent, please run this
# outside of qterminal, as qterminal saves the settings defined by the UI upon exiting

date=$(date)
hour=${date[@]:15:3}
if (($hour>=7 && $hour<=18)); then
	qtermColSchm="BlackOnWhite"
	qtPalette="Default"
	qtIconTheme="Papirus-Light"
	qtPanelTheme="system"
	gtkTheme="Adwaita"
	fPadDark="false"
else
	qtermColSchm="BreezeModified"
	qtPalette="Dark"
	qtIconTheme="Papirus-Dark"
	qtPanelTheme="dark"
	gtkTheme="Adwaita-dark"
	fPadDark="true"
fi

$HOME=$(echo ~)

# Change qterminal colour scheme
sed -i.bak -E "s/^(colorScheme=).*$/\1${qtermColSchm}/" \
$HOME/.config/qterminal.org/qterminal.ini

# Back up lxqt.conf
qtConfFile=$HOME/.config/lxqt/lxqt.conf
cp $qtConfFile "${qtConfFile}.bak"

# Change icon theme
sed -i -E "s/^(icon_theme=).*$/\1${qtIconTheme}/" $qtConfFile

# Change panel theme
sed -i -E "s/^(theme=).*$/\1${qtPanelTheme}/" $qtConfFile

# Change Qt palette
palette=$(cat $HOME/.local/share/lxqt/palettes/${qtPalette})
sed -i -e "s/^\[Palette\]$.*\n\n/${palette}\n/" $qtConfFile

# Restart lxqt-panel
kill $(pidof lxqt-panel)
lxqt-panel &

# Change gtk-3.0 theme
gtk3ConfFile="$HOME/.config/gtk-3.0/settings.ini"
sed -i.bak -E "s/^(gtk-theme-name = ).*$/\1${gtkTheme}/" $gtk3ConfFile

# Change featherpad theme
fpadConf="$HOME/.config/featherpad/fp.conf"
sed -i.bak -E "s/^(darkColorScheme=).*$/\1${fPadDark}/" $fpadConf
1 Like

Here are some more ideas:

  • Run the day/night theme changer before login as part of xdg autostart (then you don’t have to restart lxqt-panel or any other app).
  • Have a separate day and night theme switcher script. Then run they day-theme switcher at sunrise and night one at sunset via cron.
1 Like

Sorry, there was another typo. I wish I could edit my posts.

#!/usr/bin/bash

# In order for the changes to the qterminal colour scheme to be persistent, please run this
# outside of qterminal, as qterminal saves the settings defined by the UI upon exiting

date=$(date)
hour=${date[@]:15:3}
if (($hour>=7 && $hour<=16)); then
	qtermColSchm="BlackOnWhite"
	qtPalette="Default"
	qtIconTheme="Papirus-Light"
	qtPanelTheme="system"
	gtkTheme="Adwaita"
	fPadDark="false"
	joplinTheme="1"
else
	qtermColSchm="BreezeModified"
	qtPalette="Dark"
	qtIconTheme="Papirus-Dark"
	qtPanelTheme="dark"
	gtkTheme="Adwaita-dark"
	fPadDark="true"
	joplinTheme="22"
fi

HOME=$(echo ~)

# Change qterminal colour scheme
sed -i.bak -E "s/^(colorScheme=).*$/\1${qtermColSchm}/" \
$HOME/.config/qterminal.org/qterminal.ini

# Back up lxqt.conf
qtConfFile=$HOME/.config/lxqt/lxqt.conf
cp $qtConfFile "${qtConfFile}.bak"

# Change icon theme
sed -i -E "s/^(icon_theme=).*$/\1${qtIconTheme}/" $qtConfFile

# Change panel theme
sed -i -E "s/^(theme=).*$/\1${qtPanelTheme}/" $qtConfFile

# Change Qt palette
palette=$(cat $HOME/.local/share/lxqt/palettes/${qtPalette})
perl -0777 -i -pe "s/\[Palette\]\n(.*\n)*\n/${palette}\n\n/" $qtConfFile

# Restart lxqt-panel
#kill $(pidof lxqt-panel)
#lxqt-panel &

## Change gtk-3.0 theme
gtk3ConfFile="$HOME/.config/gtk-3.0/settings.ini"
sed -i.bak -E "s/^(gtk-theme-name = ).*$/\1${gtkTheme}/" $gtk3ConfFile

# Change featherpad theme
fpadConf="$HOME/.config/featherpad/fp.conf"
sed -i.bak -E "s/^(darkColorScheme=).*$/\1${fPadDark}/" $fpadConf

# Change joplin theme
joplinConf="$HOME/.config/joplin-desktop/settings.json"
sed -i -E "s/^(\t\"theme\": ).*/\1${joplinTheme},/" $joplinConf

Here is my crontab. I hard-coded my sunrise/sunset times. But you could have another cron job that gets the sunrise/sunset times from a server and then auto-edits the daily cron jobs.

# m h  dom mon dow   command
30 7 * * * day-theme-change.sh
30 17 * * * night-theme-change.sh

I did not test your scripts yet, but I am sure it will work :slight_smile:

You gave me a great idea. I will implement some ‘hot button’ to switch themes. Would be nice to be able to switch between optimal screen visibility for outside and inside use of my laptop.

1 Like

Here is how to automatically change the wallpaper. For the autostart script via xdg, which is run before login, you can just use

# Change wallpaper
wallpaper=$(echo $wallpaper | sed -e "s/\//\\\\\//g")
wallpaperConfFile="$HOME/.config/pcmanfm-qt/lxqt/settings.conf"
sed -i.bak -E "s/^(Wallpaper=).*/\1${wallpaper}/" $wallpaperConfFile

where $wallpaper is the location of the wallpaper image.

To change after login, we have to manually restart pcmanfm-qt --desktop.

# First kill pcmanfm-qt --desktop
command="/usr/bin/pcmanfm-qt --desktop --profile=lxqt"
kill $(ps aux | grep "$command" | awk 'NR==1{print $2}')
# Change wallpaper
sleep 1
wallpaper=$(echo $wallpaper | sed -e "s/\//\\\\\//g")
wallpaperConfFile="$HOME/.config/pcmanfm-qt/lxqt/settings.conf"
sed -i.bak -E "s/^(Wallpaper=).*/\1${wallpaper}/" $wallpaperConfFile
# Restart pcmanfm-qt --desktop
sleep 1
$command &
1 Like

I found out that it’s possible to change the wallpaper by the simple command pcmanfm-qt -w $wallpaper, however this doesn’t work via cron. I posted a bug report, let’s see how it goes.

3 Likes

So it turns out that leading zeros mean that bash interprets the number as an octal number. So we need to strip them:

date=$(date)
hour=${date[@]:16:2}
min=${date[@]:19:2}
# Leading zero must be removed, otherwise interpreted as an octal number
hour=${hour##0}
min=${min##0}

Also, in order for pcmanfm-qt -w $wallpaper to work, we need to manually export all the environment variables. They seem to be set inside the QT programs, which is bad practice IMHO. You can find them using printenv | grep XDG or printenv | grep QT and then manually copy paste, and use the export command. Or put them in a text file and source it.