nixos-configs/modules/ags/v1/config/ts/bar/keyboard-layout.ts

55 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

const Hyprland = await Service.import('hyprland');
const { Icon, Label } = Widget;
import HoverRevealer from './hover-revealer.ts';
const DEFAULT_KB = 'at-translated-set-2-keyboard';
// Types
import { Keyboard, LabelGeneric } from 'global-types';
2024-01-13 11:15:08 -05:00
const getKbdLayout = (self: LabelGeneric, _: string, layout: string) => {
if (layout) {
if (layout === 'error') {
return;
}
const shortName = layout.match(/\(([A-Za-z]+)\)/);
self.label = shortName ? shortName[1] : layout;
}
else {
// At launch, kb layout is undefined
2024-02-11 02:18:59 -05:00
Hyprland.messageAsync('j/devices').then((obj) => {
2024-01-13 11:15:08 -05:00
const keyboards = Array.from(JSON.parse(obj)
.keyboards) as Keyboard[];
2024-01-13 11:15:08 -05:00
const kb = keyboards.find((v) => v.name === DEFAULT_KB);
2024-01-13 11:15:08 -05:00
if (kb) {
layout = kb.active_keymap;
2024-01-13 11:15:08 -05:00
const shortName = layout
.match(/\(([A-Za-z]+)\)/);
2024-01-13 11:15:08 -05:00
self.label = shortName ? shortName[1] : layout;
}
else {
self.label = 'None';
}
}).catch(print);
}
};
export default () => HoverRevealer({
class_name: 'keyboard',
spacing: 4,
icon: Icon({
icon: 'input-keyboard-symbolic',
size: 20,
}),
label: Label({ css: 'font-size: 20px;' })
.hook(Hyprland, getKbdLayout, 'keyboard-layout'),
});