nixos-configs/nixosModules/ags/v2/widgets/bar/items/current-client.tsx

79 lines
2.1 KiB
TypeScript
Raw Normal View History

import { bind, Variable } from 'astal';
2024-09-27 16:35:32 -04:00
2024-10-22 13:09:39 -04:00
import AstalApps from 'gi://AstalApps';
2024-09-27 16:35:32 -04:00
const Applications = AstalApps.Apps.new();
2024-10-22 13:09:39 -04:00
import AstalHyprland from 'gi://AstalHyprland';
2024-09-27 16:35:32 -04:00
const Hyprland = AstalHyprland.get_default();
import Separator from '../../misc/separator';
import { hyprMessage } from '../../../lib';
2024-09-27 16:35:32 -04:00
export default () => {
const visibleIcon = Variable<boolean>(false);
const focusedIcon = Variable<string>('');
const focusedTitle = Variable<string>('');
let lastFocused: string | undefined;
const updateVars = (
2024-10-12 04:03:22 -04:00
client: AstalHyprland.Client | null = Hyprland.get_focused_client(),
) => {
lastFocused = client?.get_address();
const app = Applications.fuzzy_query(
client?.get_class() ?? '',
)[0];
const icon = app?.iconName;
if (icon) {
visibleIcon.set(true);
focusedIcon.set(icon);
}
else {
visibleIcon.set(false);
}
focusedTitle.set(client?.get_title() ?? '');
const id = client?.connect('notify::title', (c) => {
if (c.get_address() !== lastFocused) {
c.disconnect(id);
}
focusedTitle.set(c.get_title());
});
};
updateVars();
Hyprland.connect('notify::focused-client', () => updateVars());
Hyprland.connect('client-removed', () => updateVars());
Hyprland.connect('client-added', async() => {
2024-10-16 22:33:15 -04:00
try {
updateVars(Hyprland.get_client(JSON.parse(await hyprMessage('j/activewindow')).address));
}
catch (e) {
console.log(e);
}
});
2024-09-27 16:35:32 -04:00
return (
<box
className="bar-item current-window"
visible={bind(focusedTitle).as((title) => title !== '')}
2024-09-27 16:35:32 -04:00
>
<icon
css="font-size: 32px;"
icon={bind(focusedIcon)}
visible={bind(visibleIcon)}
2024-09-27 16:35:32 -04:00
/>
<Separator size={8} />
2024-09-27 16:35:32 -04:00
<label
label={bind(focusedTitle)}
truncate
/>
2024-09-27 16:35:32 -04:00
</box>
);
};