aboutsummaryrefslogtreecommitdiff
path: root/.config/ags/widget/quicksettings/ToggleButton.ts
blob: 62a2e67b73745513e971c941f4943c54878ede45 (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
import { type Props as IconProps } from "types/widgets/icon"
import { type Props as LabelProps } from "types/widgets/label"
import type GObject from "gi://GObject?version=2.0"
import type Gtk from "gi://Gtk?version=3.0"
import icons from "lib/icons"

export const opened = Variable("")
App.connect("window-toggled", (_, name: string, visible: boolean) => {
    if (name === "quicksettings" && !visible)
        Utils.timeout(500, () => opened.value = "")
})

export const Arrow = (name: string, activate?: false | (() => void)) => {
    let deg = 0
    let iconOpened = false
    const icon = Widget.Icon(icons.ui.arrow.right).hook(opened, () => {
        if (opened.value === name && !iconOpened || opened.value !== name && iconOpened) {
            const step = opened.value === name ? 10 : -10
            iconOpened = !iconOpened
            for (let i = 0; i < 9; ++i) {
                Utils.timeout(15 * i, () => {
                    deg += step
                    icon.setCss(`-gtk-icon-transform: rotate(${deg}deg);`)
                })
            }
        }
    })
    return Widget.Button({
        child: icon,
        class_name: "arrow",
        on_clicked: () => {
            opened.value = opened.value === name ? "" : name
            if (typeof activate === "function")
                activate()
        },
    })
}

type ArrowToggleButtonProps = {
    name: string
    icon: IconProps["icon"]
    label: LabelProps["label"]
    activate: () => void
    deactivate: () => void
    activateOnArrow?: boolean
    connection: [GObject.Object, () => boolean]
}
export const ArrowToggleButton = ({
    name,
    icon,
    label,
    activate,
    deactivate,
    activateOnArrow = true,
    connection: [service, condition],
}: ArrowToggleButtonProps) => Widget.Box({
    class_name: "toggle-button",
    setup: self => self.hook(service, () => {
        self.toggleClassName("active", condition())
    }),
    children: [
        Widget.Button({
            child: Widget.Box({
                hexpand: true,
                children: [
                    Widget.Icon({
                        class_name: "icon",
                        icon,
                    }),
                    Widget.Label({
                        class_name: "label",
                        max_width_chars: 10,
                        truncate: "end",
                        label,
                    }),
                ],
            }),
            on_clicked: () => {
                if (condition()) {
                    deactivate()
                    if (opened.value === name)
                        opened.value = ""
                } else {
                    activate()
                }
            },
        }),
        Arrow(name, activateOnArrow && activate),
    ],
})

type MenuProps = {
    name: string
    icon: IconProps["icon"]
    title: LabelProps["label"]
    content: Gtk.Widget[]
}
export const Menu = ({ name, icon, title, content }: MenuProps) => Widget.Revealer({
    transition: "slide_down",
    reveal_child: opened.bind().as(v => v === name),
    child: Widget.Box({
        class_names: ["menu", name],
        vertical: true,
        children: [
            Widget.Box({
                class_name: "title-box",
                children: [
                    Widget.Icon({
                        class_name: "icon",
                        icon,
                    }),
                    Widget.Label({
                        class_name: "title",
                        truncate: "end",
                        label: title,
                    }),
                ],
            }),
            Widget.Separator(),
            Widget.Box({
                vertical: true,
                class_name: "content vertical",
                children: content,
            }),
        ],
    }),
})

type SimpleToggleButtonProps = {
    icon: IconProps["icon"]
    label: LabelProps["label"]
    toggle: () => void
    connection: [GObject.Object, () => boolean]
}
export const SimpleToggleButton = ({
    icon,
    label,
    toggle,
    connection: [service, condition],
}: SimpleToggleButtonProps) => Widget.Button({
    on_clicked: toggle,
    class_name: "simple-toggle",
    setup: self => self.hook(service, () => {
        self.toggleClassName("active", condition())
    }),
    child: Widget.Box([
        Widget.Icon({ icon }),
        Widget.Label({
            max_width_chars: 10,
            truncate: "end",
            label,
        }),
    ]),
})