Move the wallpaper and panel scripts to the same startup script (#187)

This commit is contained in:
magnouvean 2024-06-04 21:11:19 +02:00 committed by GitHub
parent 82da66c417
commit 0b3f430fe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 115 additions and 110 deletions

43
lib/panel.nix Normal file
View File

@ -0,0 +1,43 @@
{ lib, config, ... }:
let
widgets = (import ../modules/widgets { inherit lib; });
panelToLayout = panel:
let
inherit (widgets.lib) addWidgetStmts stringIfNotNull;
inherit (lib) boolToString optionalString;
inherit (builtins) toString;
plasma6OnlyCmd = cmd: ''
if (isPlasma6) {
${cmd}
}
'';
in
''
{
const panel = new Panel();
panel.height = ${toString panel.height};
panel.floating = ${boolToString panel.floating};
${stringIfNotNull panel.alignment ''panel.alignment = "${panel.alignment}";''}
${stringIfNotNull panel.hiding ''panel.hiding = "${panel.hiding}";''}
${stringIfNotNull panel.location ''panel.location = "${panel.location}";''}
${stringIfNotNull panel.lengthMode (plasma6OnlyCmd ''panel.lengthMode = "${panel.lengthMode}";'')}
${stringIfNotNull panel.maxLength "panel.maximumLength = ${toString panel.maxLength};"}
${stringIfNotNull panel.minLength "panel.minimumLength = ${toString panel.minLength};"}
${stringIfNotNull panel.offset "panel.offset = ${toString panel.offset};"}
${optionalString (panel.screen != 0) ''panel.writeConfig("lastScreen[$i]", ${toString panel.screen});''}
${addWidgetStmts "panel" "panelWidgets" panel.widgets}
${stringIfNotNull panel.extraSettings panel.extraSettings}
}
'';
in
''
// Removes all existing panels
panels().forEach((panel) => panel.remove());
const isPlasma6 = applicationVersion.split(".")[0] == 6;
// Adds the panels
${lib.concatMapStringsSep "\n" panelToLayout config.programs.plasma.panels}
''

View File

@ -1,10 +1,7 @@
{ config
, lib
{ lib
, ...
} @ args:
let
cfg = config.programs.plasma;
widgets = import ./widgets args;
panelType = lib.types.submodule ({ config, ... }: {
@ -116,52 +113,6 @@ let
};
};
});
# list of panels -> bool
# Checks if any panels have non-default screens. If any of them do we need
# some hacky tricks to place them on their screens.
anyNonDefaultScreens = builtins.any (panel: panel.screen != 0);
panelToLayout = panel:
let
inherit (widgets.lib) addWidgetStmts stringIfNotNull;
inherit (lib) boolToString optionalString;
inherit (builtins) toString;
plasma6OnlyCmd = cmd: ''
if (isPlasma6) {
${cmd}
}
'';
in
''
{
const panel = new Panel();
panel.height = ${toString panel.height};
panel.floating = ${boolToString panel.floating};
${stringIfNotNull panel.alignment ''panel.alignment = "${panel.alignment}";''}
${stringIfNotNull panel.hiding ''panel.hiding = "${panel.hiding}";''}
${stringIfNotNull panel.location ''panel.location = "${panel.location}";''}
${stringIfNotNull panel.lengthMode (plasma6OnlyCmd ''panel.lengthMode = "${panel.lengthMode}";'')}
${stringIfNotNull panel.maxLength "panel.maximumLength = ${toString panel.maxLength};"}
${stringIfNotNull panel.minLength "panel.minimumLength = ${toString panel.minLength};"}
${stringIfNotNull panel.offset "panel.offset = ${toString panel.offset};"}
${optionalString (panel.screen != 0) ''panel.writeConfig("lastScreen[$i]", ${toString panel.screen});''}
${addWidgetStmts "panel" "panelWidgets" panel.widgets}
${stringIfNotNull panel.extraSettings panel.extraSettings}
}
'';
layout = ''
// Removes all existing panels
panels().forEach((panel) => panel.remove());
const isPlasma6 = applicationVersion.split(".")[0] == 6;
// Adds the panels
${lib.concatMapStringsSep "\n" panelToLayout config.programs.plasma.panels}
'';
in
{
options.programs.plasma.panels = lib.mkOption {
@ -169,24 +120,7 @@ in
default = [ ];
};
config = lib.mkIf (cfg.enable && (lib.length cfg.panels) > 0) {
programs.plasma.startup.desktopScript."apply_panels" = {
preCommands = ''
# We delete plasma-org.kde.plasma.desktop-appletsrc to hinder it
# growing indefinitely. See:
# https://github.com/pjones/plasma-manager/issues/76
[ -f ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc ] && rm ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
'';
text = layout;
postCommands = lib.mkIf (anyNonDefaultScreens cfg.panels) ''
if [ -f ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc ]; then
sed -i 's/^lastScreen\\x5b$i\\x5d=/lastScreen[$i]=/' ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
# We sleep a second in order to prevent some bugs (like the incorrect height being set)
sleep 1; nohup plasmashell --replace &
fi
'';
priority = 2;
};
};
# The config-part of this module is located in workspace due to the need for
# having wallpaper and panels in the same script.
}

View File

@ -188,47 +188,75 @@ in
])
);
})
(lib.mkIf (cfg.workspace.wallpaper != null) {
# We need to set the wallpaper after the panels are created in order for
# this not to be reset when specifying the screens for panels. See:
# https://github.com/pjones/plasma-manager/issues/116.
programs.plasma.startup.startupScript."set_wallpaper" = {
text = ''
plasma-apply-wallpaperimage ${cfg.workspace.wallpaper}
'';
priority = 3;
};
})
(lib.mkIf (cfg.workspace.wallpaperSlideShow != null) {
programs.plasma.startup.desktopScript."set_wallpaper_slideshow" = {
text = ''
let allDesktops = desktops();
for (var desktopIndex = 0; desktopIndex < allDesktops.length; desktopIndex++) {
var desktop = allDesktops[desktopIndex];
desktop.wallpaperPlugin = "org.kde.slideshow";
desktop.currentConfigGroup = Array("Wallpaper", "org.kde.slideshow", "General");
desktop.writeConfig("SlidePaths", ${if (builtins.isPath cfg.workspace.wallpaperSlideShow.path) then
"\"" + cfg.workspace.wallpaperSlideShow.path + "\"" else
"[" + (builtins.concatStringsSep "," (map (s: "\"" + s + "\"") cfg.workspace.wallpaperSlideShow.path)) + "]"});
desktop.writeConfig("SlideInterval", "${builtins.toString cfg.workspace.wallpaperSlideShow.interval}");
# Wallpaper and panels are in the same script since the resetting of the
# panels in the panels-script also has a tendency to reset the wallpaper, so
# these should run at the same time.
(lib.mkIf
((cfg.workspace.wallpaper != null) ||
(cfg.workspace.wallpaperSlideShow != null) ||
(cfg.workspace.wallpaperPictureOfTheDay != null) ||
((builtins.length cfg.panels) > 0))
{
programs.plasma.startup.desktopScript."panels_and_wallpaper" = (
let
anyPanels = ((builtins.length cfg.panels) > 0);
anyNonDefaultScreens = ((builtins.any (panel: panel.screen != 0)) cfg.panels);
panelPreCMD = (if anyPanels then ''
# We delete plasma-org.kde.plasma.desktop-appletsrc to hinder it
# growing indefinitely. See:
# https://github.com/pjones/plasma-manager/issues/76
[ -f ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc ] && rm ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
'' else "");
panelLayoutStr = (if anyPanels then (import ../lib/panel.nix { inherit lib; inherit config; }) else "");
panelPostCMD = (if anyNonDefaultScreens then ''
if [ -f ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc ]; then
sed -i 's/^lastScreen\\x5b$i\\x5d=/lastScreen[$i]=/' ${config.xdg.configHome}/plasma-org.kde.plasma.desktop-appletsrc
# We sleep a second in order to prevent some bugs (like the incorrect height being set)
sleep 1; nohup plasmashell --replace &
fi
'' else "");
# This meaningless comment inserts the URL into the desktop-script
# which means that when the wallpaper is updated, the sha256 hash
# changes and the script will be re-run.
wallpaperDesktopScript = (if (cfg.workspace.wallpaper != null) then ''
// Wallpaper to set later: ${cfg.workspace.wallpaper}
'' else "");
wallpaperPostCMD = (if (cfg.workspace.wallpaper != null) then ''
plasma-apply-wallpaperimage ${cfg.workspace.wallpaper}
'' else "");
wallpaperSlideShow = (if (cfg.workspace.wallpaperSlideShow != null) then ''
// Wallpaper slideshow
let allDesktops = desktops();
for (var desktopIndex = 0; desktopIndex < allDesktops.length; desktopIndex++) {
var desktop = allDesktops[desktopIndex];
desktop.wallpaperPlugin = "org.kde.slideshow";
desktop.currentConfigGroup = Array("Wallpaper", "org.kde.slideshow", "General");
desktop.writeConfig("SlidePaths", ${if (builtins.isPath cfg.workspace.wallpaperSlideShow.path) then
"\"" + cfg.workspace.wallpaperSlideShow.path + "\"" else
"[" + (builtins.concatStringsSep "," (map (s: "\"" + s + "\"") cfg.workspace.wallpaperSlideShow.path)) + "]"});
desktop.writeConfig("SlideInterval", "${builtins.toString cfg.workspace.wallpaperSlideShow.interval}");
}
'' else "");
wallpaperPOTD = (if (cfg.workspace.wallpaperPictureOfTheDay != null) then ''
// Wallpaper POTD
let allDesktops = desktops();
for (const desktop of allDesktops) {
desktop.wallpaperPlugin = "org.kde.potd";
desktop.currentConfigGroup = ["Wallpaper", "org.kde.potd", "General"];
desktop.writeConfig("Provider", "${cfg.workspace.wallpaperPictureOfTheDay.provider}");
desktop.writeConfig("UpdateOverMeteredConnection", "${if (cfg.workspace.wallpaperPictureOfTheDay.updateOverMeteredConnection) then "1" else "0"}");
}
'' else ""
);
in
{
preCommands = panelPreCMD;
text = panelLayoutStr + wallpaperDesktopScript + wallpaperSlideShow + wallpaperPOTD;
postCommands = panelPostCMD + wallpaperPostCMD;
priority = 2;
}
'';
priority = 3;
};
})
(lib.mkIf (cfg.workspace.wallpaperPictureOfTheDay != null) {
programs.plasma.startup.desktopScript."set_wallpaper_potd" = {
text = ''
let allDesktops = desktops();
for (const desktop of allDesktops) {
desktop.wallpaperPlugin = "org.kde.potd";
desktop.currentConfigGroup = ["Wallpaper", "org.kde.potd", "General"];
desktop.writeConfig("Provider", "${cfg.workspace.wallpaperPictureOfTheDay.provider}");
desktop.writeConfig("UpdateOverMeteredConnection", "${if (cfg.workspace.wallpaperPictureOfTheDay.updateOverMeteredConnection) then "1" else "0"}");
}
'';
priority = 3;
};
})
);
}
)
]));
}