summaryrefslogtreecommitdiff
path: root/examples/gtk3/js/popover/app.tsx
blob: 6358297d0693c43f688f02bb1fc2286c56340850 (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
import { App, Astal, Gdk, Gtk } from "astal/gtk3"

const { TOP, RIGHT, BOTTOM, LEFT } = Astal.WindowAnchor

type PopupProps = {
    child?: unknown
    marginBottom?: number
    marginTop?: number
    marginLeft?: number
    marginRight?: number
    halign?: Gtk.Align
    valign?: Gtk.Align
}

function Popup({
    child,
    marginBottom,
    marginTop,
    marginLeft,
    marginRight,
    halign = Gtk.Align.CENTER,
    valign = Gtk.Align.CENTER,
}: PopupProps) {
    return (
        <window
            visible={false}
            css="background-color: transparent"
            keymode={Astal.Keymode.EXCLUSIVE}
            anchor={TOP | RIGHT | BOTTOM | LEFT}
            exclusivity={Astal.Exclusivity.IGNORE}
            // close when click occurs otside of child
            onButtonPressEvent={(self, event) => {
                const [, _x, _y] = event.get_coords()
                const { x, y, width, height } = self
                    .get_child()!
                    .get_allocation()

                const xOut = _x < x || _x > x + width
                const yOut = _y < y || _y > y + height

                // clicked outside
                if (xOut || yOut) self.hide()
            }}
            // close when hitting Escape
            onKeyPressEvent={(self, event: Gdk.Event) => {
                if (event.get_keyval()[1] === Gdk.KEY_Escape) {
                    self.hide()
                }
            }}
        >
            <box
                className="Popup"
                onButtonPressEvent={() => true} // make sure click event does not bubble up
                // child can be positioned with `halign` `valign` and margins
                expand
                halign={halign}
                valign={valign}
                marginBottom={marginBottom}
                marginTop={marginTop}
                marginStart={marginLeft}
                marginEnd={marginRight}
            >
                {child}
            </box>
        </window>
    )
}

App.start({
    instanceName: "popup-example",
    css: `
        .Popup {
            background-color: @theme_bg_color;
            box-shadow: 2px 3px 7px 0 rgba(0,0,0,0.4);
            border-radius: 12px;
            padding: 12px;
        }
    `,
    main() {
        const popup = (
            <Popup
                marginTop={36}
                marginRight={60}
                valign={Gtk.Align.START}
                halign={Gtk.Align.END}
            >
                <button onClicked={() => popup.hide()}>
                    Click me to close the popup
                </button>
            </Popup>
        )

        return (
            <window
                anchor={TOP | LEFT | RIGHT}
                exclusivity={Astal.Exclusivity.EXCLUSIVE}
            >
                <button onClicked={() => popup.show()} halign={Gtk.Align.END}>
                    Click to open popup
                </button>
            </window>
        )
    },
})