nixos-configs/devices/wim/config/ags/js/bar/buttons/keyboard-layout.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
import { Box, EventBox, Icon, Label, Revealer } from 'resource:///com/github/Aylur/ags/widget.js';
import Separator from '../../misc/separator.js';
const DEFAULT_KB = 'at-translated-set-2-keyboard';
2023-12-08 02:22:38 -05:00
const SPACING = 4;
2023-12-18 18:00:30 -05:00
/**
* @param {import('types/widgets/label').default} self
2023-12-18 18:00:30 -05:00
* @param {string} layout
* @param {string} _
2023-12-18 18:00:30 -05:00
*/
const getKbdLayout = (self, _, layout) => {
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) => {
const kb = Array.from(JSON.parse(obj).keyboards)
.find((v) => v.name === DEFAULT_KB);
layout = kb['active_keymap'];
const shortName = layout
.match(/\(([A-Za-z]+)\)/);
self.label = shortName ? shortName[1] : layout;
}).catch(print);
}
};
export default () => {
2023-12-18 18:00:30 -05:00
const hoverRevLabel = Revealer({
transition: 'slide_right',
2023-12-18 18:00:30 -05:00
child: Box({
2023-12-18 18:00:30 -05:00
children: [
2023-12-08 02:22:38 -05:00
Separator(SPACING),
2023-12-18 18:00:30 -05:00
Label({ css: 'font-size: 20px;' })
.hook(Hyprland, getKbdLayout, 'keyboard-layout'),
],
}),
});
const widget = EventBox({
2023-12-18 18:00:30 -05:00
on_hover: () => {
hoverRevLabel.reveal_child = true;
},
2023-12-18 18:00:30 -05:00
on_hover_lost: () => {
hoverRevLabel.reveal_child = false;
},
2023-12-18 18:00:30 -05:00
child: Box({
css: 'padding: 0 10px; margin-right: -10px;',
2023-12-18 18:00:30 -05:00
children: [
Icon({
icon: 'input-keyboard-symbolic',
size: 20,
}),
2023-12-18 18:00:30 -05:00
hoverRevLabel,
],
}),
});
return widget;
};