summaryrefslogtreecommitdiff
path: root/common/modules/window_managers/dwm/scripts.nix
blob: 25ecc19f3394bce0e4b03f32d20541b3a7776ccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
homeManager:
{ pkgs, lib, ... }:
let
  sync_dms = pkgs.writeShellScriptBin "sync_dms" ''
    jq=${pkgs.jq}/bin/jq
    SETTINGS_FILE="$HOME/.config/noctalia/settings.json"
    WALLPAPER_FILE="$HOME/.cache/noctalia/wallpapers.json"

    # --- Helpers ---

    get_setting() {
      $jq -r "$1" "$SETTINGS_FILE"
    }

    toggle_nightlight_state() {
      local tmp=$(mktemp)
      # Flips the boolean cleanly without changing it to a string
      $jq '.nightLight.enabled = (.nightLight.enabled | not)' "$SETTINGS_FILE" > "$tmp" && mv "$tmp" "$SETTINGS_FILE"
    }

    # --- Modules ---

    apply_wallpaper() {
      # Read dark mode and monitor preferences from settings.json
      local is_dark=$(get_setting '.colorSchemes.darkMode')
      local monitor=$(get_setting '.colorSchemes.monitorForColors')
      
      # Default to eDP-1 if monitor setting is missing
      if [[ "$monitor" == "null" || -z "$monitor" ]]; then
        monitor="eDP-1"
      fi

      local mode="light"
      if [[ "$is_dark" == "true" ]]; then
        mode="dark"
      fi

      # Use jq variables to safely parse the nested monitor and mode keys
      local wall=$($jq -r --arg mon "$monitor" --arg mode "$mode" '.wallpapers[$mon][$mode]' "$WALLPAPER_FILE")

      if [[ "$wall" != "null" && -n "$wall" ]]; then
        ${pkgs.xwallpaper}/bin/xwallpaper --zoom "$wall"
      fi
    }

    apply_nightlight() {
      local is_night_mode=$(get_setting '.nightLight.enabled')
      local night_temp=$(get_setting '.nightLight.nightTemp')

      if [[ "$is_night_mode" == "true" ]]; then
        echo "bitch $is_night_mode"
        # -P resets prior adjustments, -O applies one-shot manual temperature
        ${pkgs.redshift}/bin/redshift -P -O "$night_temp"
      else
        # -x clears all redshift adjustments
        ${pkgs.redshift}/bin/redshift -x
      fi
    }

    sync_all() {
      apply_wallpaper
      apply_nightlight
    }

    dmenu_settings() {
      local is_night_mode=$(get_setting '.nightLight.enabled')
      local options="Toggle NightMode (Currently $is_night_mode)\nSync Now"

      local choice=$(echo -e "$options" | dmenu -p "Settings:")

      case "$choice" in
        *"NightMode"*)
          toggle_nightlight_state
          # Immediately apply the new state after toggling
          apply_nightlight 
          ;;
        "Sync Now")
          sync_all
          ;;
      esac
    }

    # --- Main ---

    case "$1" in
      settings) dmenu_settings ;;
      *)        sync_all ;;
    esac
  '';
  logout_dmenu = pkgs.writeShellScriptBin "logout_dmenu" ''
    options="Lock\nLogout\nReboot\nShutdown"

    choice=$(echo -e "$options" | dmenu -p "System:")

    case "$choice" in
      Lock)     slock ;;
      Logout)   pkill dwm ;;
      Reboot)   reboot ;;
      Shutdown) poweroff ;;
      *)        exit 1 ;;
    esac
  '';
in
{
  config = lib.mkIf homeManager (
    lib.optionalAttrs homeManager {
      home.packages = with pkgs; [
        sync_dms
        logout_dmenu
      ];
    }
  );
}