summaryrefslogtreecommitdiff
path: root/pip.fish
blob: 08fda6d25c864a66e24ed4ffb6f12660288b63ac (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
#!/usr/bin/env fish

argparse -n 'caelestia-pip' -X 0 \
    'h/help' \
    'd/daemon' \
    -- $argv
or exit

if set -q _flag_h
    echo 'Usage:'
    echo '    caelestia pip ( -h | --help )'
    echo '    caelestia pip [ -d | --daemon ]'
    echo
    echo 'Options:'
    echo '    -h, --help        Print this help message and exit'
    echo '    -d, --daemon      Run this script in daemon mode'
    echo
    echo 'Normal mode (no args):'
    echo '    Move and resize the active window to picture in picture default geometry.'
    echo
    echo 'Daemon mode:'
    echo '    Set all picture in picture window initial geometry to default.'

    exit
end

. (dirname (status filename))/util.fish

function handle-window -a address workspace
    set -l monitor_id (hyprctl workspaces -j | jq '.[] | select(.name == "'$workspace'").monitorID')
    set -l monitor_size (hyprctl monitors -j | jq -r '.[] | select(.id == '$monitor_id') | "\(.width)\n\(.height)"')
    set -l window_size (hyprctl clients -j | jq '.[] | select(.address == "'$address'").size[]')
    set -l scale_factor (math $monitor_size[2] / 4 / $window_size[2])
    set -l scaled_window_size (math -s 0 $window_size[1] x $scale_factor) (math -s 0 $window_size[2] x $scale_factor)

    hyprctl dispatch "resizewindowpixel exact $scaled_window_size,address:$address" > /dev/null
    hyprctl dispatch "movewindowpixel exact $(math -s 0 $monitor_size[1] x 0.98 - $scaled_window_size[1]) $(math -s 0 $monitor_size[2] x 0.97 - $scaled_window_size[2]),address:$address" > /dev/null
    log "Handled window at address $address"
end

if set -q _flag_d
    log 'Daemon started'
    socat -U - UNIX-CONNECT:$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock | while read line
        switch $line
            case 'openwindow*'
                set -l window (string sub -s 13 $line | string split ',')
                if string match -qr '^(Picture(-| )in(-| )[Pp]icture)$' $window[4]
                    handle-window 0x$window[1] $window[2]
                end
        end
    end

    exit
end

set -l active_window (hyprctl activewindow -j | jq -r '"\(.address)\n\(.workspace.name)\n\(.floating)"')
if test $active_window[3] = true
    handle-window $active_window
else
    warn 'Focused window is not floating, ignoring'
end