summaryrefslogtreecommitdiff
path: root/common/modules/window_managers/scripts.nix
blob: 4a9392816dd9dd42764b92068e7b699cc54acf52 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
{ pkgs, config }:

rec {
  terminal = pkgs.writeShellScript "terminal" ''
    TERM="${config.beeMods.terminal.default}"
    SESSION="_main"

    if pgrep -x "$TERM" >/dev/null; then
        if tmux ls &>/dev/null; then
            tmux neww
            exit 0
        else
            exec $TERM -e tmux new -s $SESSION
        fi
    else 
        if tmux ls &>/dev/null; then
            exec $TERM -e tmux attach
        else
          exec $TERM -e tmux new -s $SESSION
        fi
    fi
  '';
  dotfiles = pkgs.writeShellScriptBin "dotfiles" ''
    case "$1" in
        cd)
            cd "$HOME/.config/beeSystems"
        ;;
        edit)
            "$EDITOR" "$HOME/.config/beeSystems"
        ;;
        flake)
            case "$2" in
            update)
                (
                cd "$HOME/.config/beeSystems" || exit 1
                nix flake update
                )
            ;;
            esac
        ;;
    esac
  '';
  update_gtk_theme = pkgs.writeShellScript "update_gtk_theme" ''
    if [ "$1" = "dark" ]; then
        dconf write /org/gnome/desktop/interface/color-scheme "'prefer-light'"
    else
        dconf write /org/gnome/desktop/interface/color-scheme "'prefer-dark'"
    fi

    dconf write /org/gnome/desktop/interface/color-scheme "'prefer-$1'"
    systemctl --user restart xdg-desktop-portal-gtk
  '';
  volume = pkgs.writeShellScript "volume" ''
    TAG="audio-volume"
    case $1 in
        up)   wpctl set-volume -l 1.5 @DEFAULT_AUDIO_SINK@ 5%+ ;;
        down) wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%- ;;
        toggle) wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle ;;
    esac

    status=$(wpctl get-volume @DEFAULT_AUDIO_SINK@)
    vol=$(awk '{print int($2 * 100)}' <<< "$status")

    case "$status" in
        *MUTED*)
            dunstify -a "Volume" -u low \
                -h string:x-canonical-private-synchronous:$TAG \
                "Volume: Muted"
            ;;
        *)
            dunstify -a "Volume" -u low \
                -h string:x-canonical-private-synchronous:$TAG \
                -h int:value:"$vol" \
                "Volume: $vol%"
            ;;
    esac
  '';
  micToggle = pkgs.writeShellScript "micToggle" ''
    TAG="audio-mic"

    wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle

    status=$(wpctl get-volume @DEFAULT_AUDIO_SOURCE@)

    if [[ "$status" == *MUTED* ]]; then
        dunstify -a "Microphone" -u low \
            -h string:x-canonical-private-synchronous:$TAG \
            "Microphone: Muted"
    else
        dunstify -a "Microphone" -u low \
            -h string:x-canonical-private-synchronous:$TAG \
            "Microphone: On"
    fi
  '';
  brightness = pkgs.writeShellScript "brightness" ''
    TAG="screen-brightness"

    case "$1" in
        up)   brightnessctl set +5% > /dev/null ;;
        down) brightnessctl set 5%- > /dev/null ;;
    esac

    current=$(brightnessctl -m | cut -d, -f4 | tr -d '%')

    dunstify -a "Brightness" -u low \
        -h string:x-canonical-private-synchronous:$TAG \
        -h int:value:"$current" \
        "Brightness: $current%"
  '';
  screenshot = pkgs.writeShellScript "screenshot" ''
    case $1 in
      full)
        ${pkgs.grim}/bin/grim - | wl-copy
        ;;
      selection)
        GEOM=$(${pkgs.slurp}/bin/slurp) || exit
        ${pkgs.grim}/bin/grim -g "$GEOM" - | wl-copy
        ;;
      window)
        ${pkgs.grim}/bin/grim -g "$(mmsg -x | awk '
          / x / {x=$3}
          / y / {y=$3}
          / width / {w=$3}
          / height / {h=$3}
          END {print x "," y " " w "x" h}
          ')" - | wl-copy
        ;;
      esac
  '';
  dostuff = pkgs.writeShellScript "dostuff" ''
    exec 2>/dev/null
    : "''${CONFIG_FILE:=$HOME/.config/beestuff/conf}"
    : "''${WALL_DIR:=$HOME/Pictures/Wallpapers}"
    : "''${THUMB_DIR:=$HOME/.cache/beestuff/thumbnails}"

    init_settings() {
        [ -f "$CONFIG_FILE" ] && return 0

        local config_dir
        config_dir=$(dirname -- "$CONFIG_FILE")

        mkdir -p -- "$config_dir"

        cat > "$CONFIG_FILE" <<EOF
    theme=dark
    nightlight=off
    wallpaper=$WALL_DIR/default.jpg
    color_type=scheme-tonal-spot
    EOF
    }

    get_val() {
        grep "^$1=" "$CONFIG_FILE" | cut -d= -f2-
    }

    set_val() {
        sed -i "s|^$1=.*|$1=$2|" "$CONFIG_FILE"
    }

    set_layout() {
        mmsg dispatch setlayout,"$1"
    }

    hash_path() {
        printf '%s' "$1" | md5sum | cut -d' ' -f1
    }

    ensure_thumb() {
        local img="$1"
        local hash thumb candidate

        hash=$(hash_path "$img")
        thumb="$THUMB_DIR/$hash.jpg"

        [ -f "$thumb" ] && {
            printf '%s' "$thumb"
            return 0
        }

        mkdir -p -- "$THUMB_DIR"

        if command -v magick >/dev/null 2>&1; then
            magick "$img" \
                -resize 400x300^ \
                -gravity center \
                -extent 400x300 \
                "$thumb" 

        elif command -v convert >/dev/null 2>&1; then
            convert "$img" \
                -resize 400x300^ \
                -gravity center \
                -extent 400x300 \
                "$thumb" 

        elif command -v ffmpeg >/dev/null 2>&1; then
            ffmpeg -hide_banner -loglevel error \
                -i "$img" \
                -vf "scale=400:300:force_original_aspect_ratio=cover,crop=400:300" \
                -frames:v 1 \
                "$thumb"

        else
            printf '%s' "$img"
            return 0
        fi

        [ -f "$thumb" ] || thumb="$img"

        printf '%s' "$thumb"
    }

    list_walls() {
        local wall

        for wall in "$WALL_DIR"/*.{jpg,jpeg,png,webp}; do
            [ -f "$wall" ] || continue

            printf '%s\0icon\x1f%s\n' \
                "''${wall##*/}" \
                "$(ensure_thumb "$wall")"
        done 
    }

    apply_all() {
        local theme=$(get_val "theme")
        local wall=$(get_val "wallpaper")
        local color_type=$(get_val "color_type")
        local mode_flag="dark"

        mode_flag="dark"
        [ "$theme" = "light" ] && mode_flag="light"

        matugen image "$wall" \
            -r lanczos3 \
            --continue-on-error \
            -m "$mode_flag" \
            -t "$color_type" \
            --source-color-index 0
            

        "${update_gtk_theme}" "$mode_flag"

        if [ -z "$WAYLAND_DISPLAY" ] && [ -n "$DISPLAY" ]; then
            xrdb -merge "$HOME/.Xresources"
        fi

        local seq_file="$HOME/.local/share/bee/sequences"
        [ -f "$seq_file" ] && sh "$seq_file"
    }

    cmd_wall() {
        local selected full_path

        selected=$(
            list_walls |
            rofi -dmenu -i -p "Wallpaper" \
                -show-icons \
                -theme-str 'element { orientation: vertical; padding: 15px; }' \
                -theme-str 'element-icon { size: 10em; horizontal-align: 0.5; }' \
                -theme-str 'listview { columns: 4; lines: 2; }' \
                -theme-str 'window { width: 50%; }'
        )

        [ -n "$selected" ] || return 0

        full_path="$WALL_DIR/$selected"
        set_val "wallpaper" "$full_path"

        if [ -n "$WAYLAND_DISPLAY" ]; then
            if command -v awww >/dev/null 2>&1; then
                awww img "$full_path" \
                    --transition-type grow \
                    --transition-fps 120 \
                    --transition-step 60 \
                    --transition-duration 1.5
            else
                pkill swaybg 
                swaybg -i "$full_path" -m fill &
            fi
        else
            feh --no-fehbg --bg-fill "$full_path"
        fi

        apply_all
    }

    clr_type() {
        local schemes selection

        schemes=(
            "scheme-tonal-spot"
            "scheme-content"
            "scheme-expressive"
            "scheme-fidelity"
            "scheme-fruit-salad"
            "scheme-monochrome"
            "scheme-neutral"
            "scheme-rainbow"
            "scheme-vibrant"
        )

        selection=$(
            printf '%s\n' "''${schemes[@]}" |
            rofi -dmenu -i -p "Colorscheme Types"
        )

        [ -n "$selection" ] || return 0

        set_val "color_type" "$selection"
        apply_all
    }

    cmd_layout() {
        local layouts selection

        layouts=(
            tile
            monocle
            scroller
            fair
            dwindle
            grid
            deck
            center_tile
            right_tile
            vertical_scroller
            vertical_tile
            vertical_grid
            vertical_fair
            vertical_deck
        )

        selection=$(
            printf '%s\n' "''${layouts[@]}" |
            rofi -dmenu -i -p "Layout"
        )

        [ -n "$selection" ] || return 0

        set_layout "$selection"
    }

    cmd_settings() {
        local theme nl ct choice

        theme=$(get_val "theme")
        nl=$(get_val "nightlight")
        ct=$(get_val "color_type")

        choice=$(
            printf '%s\n' \
                "Mode: $theme" \
                "Night Light: $nl" \
                "Wallpaper" \
                "Colorscheme Type: $ct" \
                "Layouts" |
            rofi -dmenu -p "Settings" -i
        )

        case "$choice" in
            "Mode: "*)
                if [ "$theme" = "dark" ]; then
                    set_val "theme" "light"
                else
                    set_val "theme" "dark"
                fi
                apply_all
                ;;

            "Night Light: "*)
                if [ "$nl" = "on" ]; then
                    set_val "nightlight" "off"
                    pkill wlsunset 
                else
                    set_val "nightlight" "on"
                    exec wlsunset -T 4500
                fi
                ;;

            "Wallpaper")
                cmd_wall
                ;;

            "Colorscheme Type:"*)
                clr_type
                ;;

            "Layouts")
                cmd_layout
                ;;
        esac
    }

    init_settings

    case "$1" in
        wall)        cmd_wall ;;
        settings)    cmd_settings ;;
        layout)      cmd_layout ;;
        apply)       apply_all ;;
        *)
            printf '%s\n' "Usage: bee {wall|settings|layout|apply}"
            ;;
    esac
  '';

}