summaryrefslogtreecommitdiff
path: root/examples/js/applauncher/widget/Applauncher.tsx
blob: d92b5e3ddeff0064c7a2997314292e3f05a2b0e2 (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
import Apps from "gi://AstalApps"
import { App, Astal, Gdk, Gtk } from "astal/gtk3"
import { Variable } from "astal"

const MAX_ITEMS = 8

function AppButton({ app }: { app: Apps.Application }) {
    return <button className="AppButton" onClicked={() => app.launch()}>
        <box>
            <icon icon={app.iconName} />
            <box valign={Gtk.Align.CENTER} vertical>
                <label
                    className="name"
                    truncate
                    xalign={0}
                    label={app.name}
                />
                {app.description && <label
                    className="description"
                    wrap
                    xalign={0}
                    label={app.description}
                />}
            </box>
        </box>
    </button>
}

export default function Applauncher() {
    const apps = new Apps.Apps()
    const list = Variable(apps.get_list().slice(0, MAX_ITEMS))
    const hide = () => App.get_window("launcher")!.hide()

    function search(text: string) {
        list.set(apps.fuzzy_query(text).slice(0, MAX_ITEMS))
    }

    return <window
        name="launcher"
        anchor={Astal.WindowAnchor.TOP | Astal.WindowAnchor.BOTTOM}
        exclusivity={Astal.Exclusivity.IGNORE}
        keymode={Astal.Keymode.ON_DEMAND}
        application={App}
        onShow={() => list.set(apps.get_list().slice(0, MAX_ITEMS))}
        onKeyPressEvent={function (self, event: Gdk.Event) {
            if (event.get_keyval()[1] === Gdk.KEY_Escape)
                self.hide()
        }}>
        <box>
            <eventbox widthRequest={4000} expand onClick={hide} />
            <box hexpand={false} vertical>
                <eventbox heightRequest={100} onClick={hide} />
                <box widthRequest={500} className="Applauncher" vertical>
                    <entry
                        placeholderText="Search"
                        onChanged={({ text }) => search(text)}
                    />
                    <box spacing={6} vertical>
                        {list(list => list.map(app => (
                            <AppButton app={app} />
                        )))}
                    </box>
                    <box visible={list(l => l.length === 0)}>
                        <icon icon="system-search-symbolic" />
                        No match found
                    </box>
                </box>
                <eventbox expand onClick={hide} />
            </box>
            <eventbox widthRequest={4000} expand onClick={hide} />
        </box>
    </window>
}