Watch
1
0
Fork
You've already forked dotfiles
0
dotfiles/.local/bin/night-light-toggle-schedule

102 lines
3.7 KiB
Text
Raw Normal View History

2026-08-03 14:30:55 +02:00
#!/bin/sh
# Triggered by <Control><Alt>n
# as defined in [org/gnome/settings-daemon/plugins/media-keys]
# in ~/.config/gnome-settings/key-bindings.ini
#
# Night Light control:
#
# - <Control><Alt>n => "when" toggle
# + "Force" the screen to be warm right now!
# (Turns Night Light on if it was off, and keeps it on all day)
# + "Un-force" the Night Light into the local evening schedule
# - <Alt><Shift>n => "if" toggle
# Plain on/off switch for the Night Light feature
#
# Neither key binding changes what the other controls, so they don't fight.
#
# Making the screen cooler or warmer with <Alt><Shift>Left/Right
# also forces "Night Light" on because adjusting this implies
# that the user wants to use it.
#
# State machine:
# TARGET STATE
# +--------------------------+--------------------------+
# CURRENT STATE | nl-toggle-schedule | nl-toggle-enabled |
# +--------------------------+--------------------------+--------------------------+
# | disabled, automatic | enabled, force | enabled, automatic (*) |
# | disabled, force | enabled, force | enabled, force |
# | enabled, automatic | enabled, force (*) | disabled, automatic (*) |
# | enabled, force | enabled, automatic (*) | disabled, force |
# +--------------------------+--------------------------+--------------------------+
# (*) There are four cases where a user does not perceive a change on the screen.
# Then, a notification is shown instead.
#
# See ~/.local/bin/night-light-toggle-enabled for the other half of the toggling logic
set -eu
notify() {
ID_FILE="${XDG_RUNTIME_DIR}/night-light-notification-id"
_id=$(cat "$ID_FILE" 2>/dev/null || true)
if [ -n "$_id" ]; then
notify-send -p -t 5000 --replace-id="$_id" "Night Light" "$1" > "$ID_FILE" || rm -f "$ID_FILE"
else
notify-send -p -t 5000 "Night Light" "$1" > "$ID_FILE" || rm -f "$ID_FILE"
fi
}
Schema=org.gnome.settings-daemon.plugins.color
active_before=$(gdbus call --session \
--dest org.gnome.SettingsDaemon.Color \
--object-path /org/gnome/SettingsDaemon/Color \
--method org.freedesktop.DBus.Properties.Get \
org.gnome.SettingsDaemon.Color NightLightActive)
automatic=$(gsettings get $Schema night-light-schedule-automatic)
enabled=$(gsettings get $Schema night-light-enabled)
if [ "$automatic" = "true" ] || [ "$enabled" = "false" ]; then
# Force "Night Light" enabled, enabling it if needed
gsettings set $Schema night-light-enabled true
gsettings set $Schema night-light-schedule-automatic false
gsettings set $Schema night-light-schedule-from 0.0
gsettings set $Schema night-light-schedule-to 24.0
if [ "$active_before" = "(<true>,)" ] && [ "$automatic" = "true" ]; then
notify "is now forced to stay on and no longer on an automatic schedule"
fi
else
# Back to the live location's schedule
gsettings set $Schema night-light-schedule-automatic true
gsettings set $Schema night-light-schedule-from 18.0
gsettings set $Schema night-light-schedule-to 6.0
if [ "$active_before" = "(<true>,)" ]; then
# Give the settings daemon a moment to recompute "NightLightActive"
sleep 0.1
active_after=$(gdbus call --session \
--dest org.gnome.SettingsDaemon.Color \
--object-path /org/gnome/SettingsDaemon/Color \
--method org.freedesktop.DBus.Properties.Get \
org.gnome.SettingsDaemon.Color NightLightActive)
if [ "$active_after" = "(<true>,)" ]; then
notify "is still on because of an automatic schedule"
fi
fi
fi