From 72ce74d3eae78a6b31538ea7ebe0c1fcf4a10f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Na=C3=AFm=20Favier?= Date: Mon, 20 Feb 2023 12:30:29 +0100 Subject: [PATCH] qt: auto-detect style package from name (#3692) * qt: auto-detect style package from name * qt: test package auto-detection --- modules/misc/qt.nix | 60 +++++++++++++++---- .../misc/qt/qt-platform-theme-gnome.nix | 5 +- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/modules/misc/qt.nix b/modules/misc/qt.nix index 313e79ee..4c289522 100644 --- a/modules/misc/qt.nix +++ b/modules/misc/qt.nix @@ -6,6 +6,24 @@ let cfg = config.qt; + # Maps known lowercase style names to style packages. Non-exhaustive. + stylePackages = with pkgs; { + bb10bright = libsForQt5.qtstyleplugins; + bb10dark = libsForQt5.qtstyleplugins; + cleanlooks = libsForQt5.qtstyleplugins; + gtk2 = libsForQt5.qtstyleplugins; + motif = libsForQt5.qtstyleplugins; + cde = libsForQt5.qtstyleplugins; + plastique = libsForQt5.qtstyleplugins; + + adwaita = adwaita-qt; + adwaita-dark = adwaita-qt; + adwaita-highcontrast = adwaita-qt; + adwaita-highcontrastinverse = adwaita-qt; + + breeze = libsForQt5.breeze-qt5; + }; + in { meta.maintainers = [ maintainers.rycee ]; @@ -26,7 +44,7 @@ in { relatedPackages = [ "qgnomeplatform" [ "libsForQt5" "qtstyleplugins" ] ]; description = '' - Selects the platform theme to use for Qt applications. + Platform theme to use for Qt applications. The options are @@ -52,17 +70,29 @@ in { example = "adwaita-dark"; relatedPackages = [ "adwaita-qt" [ "libsForQt5" "qtstyleplugins" ] ]; description = '' - Selects the style to use for Qt5 applications. - The options are + Style to use for Qt5 applications. Case-insensitive. + + Some examples are adwaita adwaita-dark - Use Adwaita Qt style with + adwaita-highcontrast + adwaita-highcontrastinverse + Use the Adwaita style from adwaita + breeze + Use the Breeze style from + breeze + + + + bb10bright + bb10dark + cde cleanlooks gtk2 motif @@ -79,7 +109,10 @@ in { type = types.nullOr types.package; default = null; example = literalExpression "pkgs.adwaita-qt"; - description = "Theme package to be used in Qt5 applications."; + description = '' + Theme package to be used in Qt5 applications. + Auto-detected from if possible. + ''; }; }; }; @@ -87,20 +120,23 @@ in { config = mkIf (cfg.enable && cfg.platformTheme != null) { assertions = [{ - assertion = (cfg.platformTheme == "gnome") - -> ((cfg.style.name != null) && (cfg.style.package != null)); + assertion = cfg.platformTheme == "gnome" -> cfg.style.name != null + && cfg.style.package != null; message = '' `qt.platformTheme` "gnome" must have `qt.style` set to a theme that - supports both Qt and Gtk, for example "adwaita" or "adwaita-dark". + supports both Qt and Gtk, for example "adwaita", "adwaita-dark", or "breeze". ''; }]; - # Necessary because home.sessionVariables is of types.attrs - home.sessionVariables = (filterAttrs (n: v: v != null) { + qt.style.package = mkIf (cfg.style.name != null) + (mkDefault (stylePackages.${toLower cfg.style.name} or null)); + + # Necessary because home.sessionVariables doesn't support mkIf + home.sessionVariables = filterAttrs (n: v: v != null) { QT_QPA_PLATFORMTHEME = if cfg.platformTheme == "gnome" then "gnome" else "gtk2"; QT_STYLE_OVERRIDE = cfg.style.name; - }); + }; home.packages = if cfg.platformTheme == "gnome" then [ pkgs.qgnomeplatform ] @@ -109,7 +145,7 @@ in { [ pkgs.libsForQt5.qtstyleplugins ]; xsession.importedVariables = [ "QT_QPA_PLATFORMTHEME" ] - ++ lib.optionals (cfg.style != null) [ "QT_STYLE_OVERRIDE" ]; + ++ lib.optionals (cfg.style.name != null) [ "QT_STYLE_OVERRIDE" ]; # Enable GTK+ style for Qt4 in either case. # It doesn’t support the platform theme packages. diff --git a/tests/modules/misc/qt/qt-platform-theme-gnome.nix b/tests/modules/misc/qt/qt-platform-theme-gnome.nix index 80baeae6..58b4cd7a 100644 --- a/tests/modules/misc/qt/qt-platform-theme-gnome.nix +++ b/tests/modules/misc/qt/qt-platform-theme-gnome.nix @@ -5,10 +5,7 @@ qt = { enable = true; platformTheme = "gnome"; - style = { - name = "adwaita"; - package = config.lib.test.mkStubPackage { }; - }; + style.name = "adwaita"; }; test.stubs.qgnomeplatform = { };