2023-12-28 00:19:57 -05:00
|
|
|
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
|
|
|
|
|
|
|
|
import { Icon, Label } from 'resource:///com/github/Aylur/ags/widget.js';
|
|
|
|
|
2024-01-13 23:38:31 -05:00
|
|
|
import HoverRevealer from './hover-revealer.ts';
|
2023-12-28 00:19:57 -05:00
|
|
|
|
|
|
|
const DEFAULT_KB = 'at-translated-set-2-keyboard';
|
|
|
|
|
2024-01-13 23:38:31 -05:00
|
|
|
import AgsLabel from 'types/widgets/label.ts';
|
2024-01-13 11:15:08 -05:00
|
|
|
type Keyboard = {
|
|
|
|
address: string;
|
|
|
|
name: string;
|
|
|
|
rules: string;
|
|
|
|
model: string;
|
|
|
|
layout: string;
|
|
|
|
variant: string;
|
|
|
|
options: string;
|
|
|
|
active_keymap: string;
|
|
|
|
main: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-12-28 00:19:57 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
const getKbdLayout = (self: AgsLabel, _: string, layout: string) => {
|
2023-12-28 00:19:57 -05:00
|
|
|
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
|
|
|
|
Hyprland.sendMessage('j/devices').then((obj) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const keyboards = Array.from(JSON.parse(obj)
|
|
|
|
.keyboards) as Array<Keyboard>;
|
|
|
|
const kb = keyboards.find((v) => v.name === DEFAULT_KB);
|
2023-12-28 00:19:57 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
if (kb) {
|
|
|
|
layout = kb.active_keymap;
|
2023-12-28 00:19:57 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
const shortName = layout
|
|
|
|
.match(/\(([A-Za-z]+)\)/);
|
2023-12-28 00:19:57 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
self.label = shortName ? shortName[1] : layout;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.label = 'None';
|
|
|
|
}
|
2023-12-28 00:19:57 -05:00
|
|
|
}).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'),
|
|
|
|
});
|