aboutsummaryrefslogtreecommitdiff
path: root/programs/cwal/components/gtk.sh
blob: c823dfe01c93d678c649d73cb8f700fa2ed907b9 (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
#!/usr/bin/env bash
# Copyright (C) 2026 deltegui
# SPDX-License-Identifier: GPL-3.0-or-later

set -euo pipefail

component_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
source "$component_dir/common.sh"

CWAL_COMPONENT=gtk
export CWAL_COMPONENT

template="$CWAL_TEMPLATES_DIR/gtk.css"
icon_template="$CWAL_TEMPLATES_DIR/icon-theme.txt"
destination="$HOME/.config/gtk-3.0/generated-cwal.css"
settings="$HOME/.config/gtk-3.0/settings.ini"

# The generated colors are Breeze's named colors: other GTK themes ignore them.
warn_if_not_breeze()
{
    local selected=

    if ! gtk_theme_available Breeze; then
        printf 'apply-theme: the Breeze GTK theme is not installed, GTK colors will have no effect\n' >&2
        return 0
    fi
    if [[ -r $settings ]]; then
        selected=$(sed -n \
            's/^[[:space:]]*gtk-theme-name[[:space:]]*=[[:space:]]*\(.*[^[:space:]]\)[[:space:]]*$/\1/p' \
            "$settings" | tail -n 1)
        if [[ -n $selected && $selected != Breeze* ]]; then
            printf 'apply-theme: GTK theme is %s, not Breeze: GTK colors will have no effect\n' \
                "$selected" >&2
        fi
    fi
}

prepare_icon_settings()
{
    local temporary
    local mode=644
    local theme_name=${CWAL_ICON_THEME:-}

    [[ ${CWAL_ICON_THEME_INSTALLED:-0} == 1 ]] || return 0
    temporary=$(component_temp settings "$settings")
    if [[ -f $settings ]]; then
        require_readable 'GTK settings file' "$settings"
        awk -v theme_name="$theme_name" '
function emit_icon_theme() {
    print "gtk-icon-theme-name=" theme_name
    found = 1
}
BEGIN {
    in_settings = 0
    has_settings = 0
}
{
    if ($0 ~ /^[[:space:]]*\[Settings\][[:space:]]*$/) {
        in_settings = 1
        has_settings = 1
        print
        next
    }
    if ($0 ~ /^[[:space:]]*\[/) {
        if (in_settings && !found)
            emit_icon_theme()
        in_settings = 0
    }
    if (in_settings &&
        $0 ~ /^[[:space:]]*gtk-icon-theme-name[[:space:]]*=/) {
        if (!found)
            emit_icon_theme()
        next
    }
    print
}
END {
    if (in_settings && !found)
        emit_icon_theme()
    else if (!has_settings) {
        print ""
        print "[Settings]"
        emit_icon_theme()
    }
}' "$settings" > "$temporary"
        mode=$(stat -c '%a' "$settings" 2>/dev/null || printf '644')
    else
        printf '[Settings]\ngtk-icon-theme-name=%s\n' "$theme_name" \
            > "$temporary"
    fi
    chmod "$mode" "$temporary"
    component_set_status icon-applied 1
}

case ${1:-} in
detect)
    component_available thunar "$destination"
    ;;
prepare)
    require_readable 'GTK template' "$template"
    require_readable 'icon theme template' "$icon_template"
    require_destination_directory GTK "$destination"
    warn_if_not_breeze
    prepare_icon_settings
    ;;
generate)
    component_render theme "$destination" "$template"
    ;;
validate)
    ;;
install)
    component_install theme "$destination"
    if [[ -r $(component_state_path settings) ]]; then
        component_install settings "$settings"
    fi
    ;;
reload)
    icon_status=$(component_get_status icon-applied)
    runtime_status=0
    if [[ $icon_status == 1 ]] && command -v gsettings >/dev/null 2>&1; then
        if gsettings set org.gnome.desktop.interface icon-theme \
            "${CWAL_ICON_THEME:-}" >/dev/null 2>&1; then
            runtime_status=1
        else
            printf 'apply-theme: could not update the live GTK icon theme\n' >&2
        fi
    fi
    component_set_status icon-runtime "$runtime_status"
    ;;
summary)
    printf '  GTK/Thunar: %s (used by new windows)\n' "$destination"
    if [[ ${CWAL_ICON_THEME:-} != '' ]]; then
        if [[ ${CWAL_ICON_THEME_INSTALLED:-0} == 1 ]]; then
            printf '  GTK icons: %s (used by new windows)\n' "$CWAL_ICON_THEME"
            if [[ $(component_get_status icon-runtime) == 1 ]]; then
                printf '              live GTK setting updated\n'
            fi
        else
            printf '  GTK icons: icon theme was not changed\n'
        fi
    else
        printf '  GTK icons: unchanged (system default)\n'
    fi
    ;;
*)
    printf 'Usage: %s {detect|prepare|generate|validate|install|reload|summary}\n' \
        "${0##*/}" >&2
    exit 2
    ;;
esac