nixos-configs/modules/ags/config/widgets/bar/items/tray.tsx

81 lines
2.4 KiB
TypeScript
Raw Permalink Normal View History

import { App, Gtk, Widget } from 'astal/gtk3';
import { bind, idle } from 'astal';
2024-10-12 04:03:22 -04:00
import AstalTray from 'gi://AstalTray';
const SKIP_ITEMS = ['.spotify-wrapped'];
const TrayItem = (item: AstalTray.TrayItem) => {
if (item.iconThemePath) {
App.add_icons(item.iconThemePath);
}
return (
<revealer
transitionType={Gtk.RevealerTransitionType.SLIDE_RIGHT}
revealChild={false}
>
<menubutton
2024-10-12 04:03:22 -04:00
className="tray-item"
cursor="pointer"
usePopover={false}
// @ts-expect-error types are wrong
2024-10-12 04:03:22 -04:00
tooltipMarkup={bind(item, 'tooltipMarkup')}
// @ts-expect-error types are wrong
actionGroup={bind(item, 'actionGroup').as((ag) => ['dbusmenu', ag])}
// @ts-expect-error types are wrong
menuModel={bind(item, 'menuModel')}
2024-10-12 04:03:22 -04:00
>
<icon gIcon={bind(item, 'gicon')} />
</menubutton>
2024-10-12 04:03:22 -04:00
</revealer>
);
};
export default () => {
const tray = AstalTray.get_default();
2024-10-12 04:03:22 -04:00
const itemMap = new Map<string, Widget.Revealer>();
return (
<box
className="bar-item system-tray"
visible={bind(tray, 'items').as((items) => items.length !== 0)}
2024-10-12 04:03:22 -04:00
setup={(self) => {
self
.hook(tray, 'item-added', (_, item: string) => {
if (itemMap.has(item) || SKIP_ITEMS.includes(tray.get_item(item).title)) {
2024-10-12 04:03:22 -04:00
return;
}
const widget = TrayItem(tray.get_item(item)) as Widget.Revealer;
2024-10-12 04:03:22 -04:00
itemMap.set(item, widget);
self.add(widget);
idle(() => {
widget.set_reveal_child(true);
});
})
.hook(tray, 'item-removed', (_, item: string) => {
2024-10-12 04:03:22 -04:00
if (!itemMap.has(item)) {
return;
}
const widget = itemMap.get(item);
widget?.set_reveal_child(false);
setTimeout(() => {
widget?.destroy();
}, 1000);
});
}}
/>
);
};