2023-11-19 15:00:29 -05:00
|
|
|
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
|
2024-01-13 23:38:31 -05:00
|
|
|
import Brightness from '../../services/brightness.ts';
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-11-19 15:00:29 -05:00
|
|
|
import { Box, EventBox, Label } from 'resource:///com/github/Aylur/ags/widget.js';
|
|
|
|
import { execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
|
|
|
|
|
2024-01-11 10:25:53 -05:00
|
|
|
const { Gdk, Gtk } = imports.gi;
|
|
|
|
const display = Gdk.Display.get_default();
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2024-01-13 23:38:31 -05:00
|
|
|
import Separator from '../misc/separator.ts';
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
// Keep track of when a non modifier key
|
|
|
|
// is clicked to release all modifiers
|
|
|
|
const NormalClick = Variable(false);
|
|
|
|
|
|
|
|
// Keep track of modifier statuses
|
|
|
|
const Super = Variable(false);
|
|
|
|
const LAlt = Variable(false);
|
|
|
|
const LCtrl = Variable(false);
|
|
|
|
const AltGr = Variable(false);
|
|
|
|
const RCtrl = Variable(false);
|
|
|
|
|
|
|
|
const Caps = Variable(false);
|
2023-11-21 01:29:46 -05:00
|
|
|
|
|
|
|
Brightness.connect('caps', (_, state) => {
|
|
|
|
Caps.value = state;
|
|
|
|
});
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
// Assume both shifts are the same for key.labelShift
|
|
|
|
const LShift = Variable(false);
|
|
|
|
const RShift = Variable(false);
|
|
|
|
|
|
|
|
const Shift = Variable(false);
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
LShift.connect('changed', () => {
|
|
|
|
Shift.value = LShift.value || RShift.value;
|
|
|
|
});
|
|
|
|
RShift.connect('changed', () => {
|
|
|
|
Shift.value = LShift.value || RShift.value;
|
|
|
|
});
|
|
|
|
|
|
|
|
const SPACING = 4;
|
|
|
|
const LSHIFT_CODE = 42;
|
|
|
|
const LALT_CODE = 56;
|
|
|
|
const LCTRL_CODE = 29;
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
// Types
|
2024-01-13 23:38:31 -05:00
|
|
|
import { Variable as Var } from 'types/variable.ts';
|
2024-01-13 11:15:08 -05:00
|
|
|
type Key = {
|
|
|
|
keytype: 'normal',
|
|
|
|
label: string,
|
|
|
|
labelShift?: string,
|
|
|
|
labelAltGr?: string,
|
|
|
|
shape: 'normal' | 'modkey',
|
|
|
|
keycode: number
|
|
|
|
};
|
|
|
|
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
const ModKey = (key: Key) => {
|
|
|
|
let Mod: Var<any>;
|
2023-11-21 01:29:46 -05:00
|
|
|
|
|
|
|
if (key.label === 'Super') {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = Super;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
// Differentiate left and right mods
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'Shift' && key.keycode === LSHIFT_CODE) {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = LShift;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'Alt' && key.keycode === LALT_CODE) {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = LAlt;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'Ctrl' && key.keycode === LCTRL_CODE) {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = LCtrl;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'Shift') {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = RShift;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'AltGr') {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = AltGr;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
else if (key.label === 'Ctrl') {
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod = RCtrl;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2024-01-13 11:15:08 -05:00
|
|
|
const label = Label({
|
|
|
|
class_name: `mod ${key.label}`,
|
|
|
|
label: key.label,
|
|
|
|
});
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
const button = EventBox({
|
|
|
|
class_name: 'key',
|
2023-12-24 00:26:44 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
on_primary_click_release: () => {
|
2023-11-19 15:00:29 -05:00
|
|
|
console.log('mod toggled');
|
|
|
|
|
|
|
|
execAsync(`ydotool key ${key.keycode}:${Mod.value ? 0 : 1}`);
|
2023-12-21 01:25:59 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
label.toggleClassName('active', !Mod.value);
|
2023-11-19 15:00:29 -05:00
|
|
|
Mod.value = !Mod.value;
|
|
|
|
},
|
2023-12-24 00:26:44 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
2024-01-11 10:25:53 -05:00
|
|
|
self
|
|
|
|
.hook(NormalClick, () => {
|
|
|
|
Mod.value = false;
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
label.toggleClassName('active', false);
|
2024-01-11 10:25:53 -05:00
|
|
|
execAsync(`ydotool key ${key.keycode}:0`);
|
|
|
|
})
|
|
|
|
|
|
|
|
// OnHover
|
|
|
|
.on('enter-notify-event', () => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(
|
|
|
|
display,
|
|
|
|
'pointer',
|
|
|
|
));
|
|
|
|
self.toggleClassName('hover', true);
|
|
|
|
})
|
|
|
|
|
|
|
|
// OnHoverLost
|
|
|
|
.on('leave-notify-event', () => {
|
|
|
|
self.window.set_cursor(null);
|
|
|
|
self.toggleClassName('hover', false);
|
|
|
|
});
|
2023-12-17 00:01:58 -05:00
|
|
|
},
|
2024-01-13 11:15:08 -05:00
|
|
|
child: label,
|
2023-11-19 15:00:29 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return Box({
|
|
|
|
children: [
|
|
|
|
button,
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-11-19 15:00:29 -05:00
|
|
|
],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
const RegularKey = (key: Key) => {
|
2023-11-19 15:00:29 -05:00
|
|
|
const widget = EventBox({
|
|
|
|
class_name: 'key',
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-11-19 15:00:29 -05:00
|
|
|
child: Label({
|
|
|
|
class_name: `normal ${key.label}`,
|
|
|
|
label: key.label,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
|
|
|
self
|
|
|
|
.hook(Shift, () => {
|
|
|
|
if (!key.labelShift) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.label = Shift.value ? key.labelShift : key.label;
|
|
|
|
})
|
|
|
|
.hook(Caps, () => {
|
|
|
|
if (key.label === 'Caps') {
|
|
|
|
self.toggleClassName('active', Caps.value);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key.labelShift) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.label.match(/[A-Za-z]/)) {
|
|
|
|
self.label = Caps.value ?
|
|
|
|
key.labelShift :
|
|
|
|
key.label;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.hook(AltGr, () => {
|
|
|
|
if (!key.labelAltGr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.toggleClassName('altgr', AltGr.value);
|
|
|
|
self.label = AltGr.value ? key.labelAltGr : key.label;
|
2024-01-11 10:25:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// OnHover
|
|
|
|
.on('enter-notify-event', () => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(
|
|
|
|
display,
|
|
|
|
'pointer',
|
|
|
|
));
|
|
|
|
self.toggleClassName('hover', true);
|
|
|
|
})
|
|
|
|
|
|
|
|
// OnHoverLost
|
|
|
|
.on('leave-notify-event', () => {
|
|
|
|
self.window.set_cursor(null);
|
|
|
|
self.toggleClassName('hover', false);
|
2023-12-17 00:01:58 -05:00
|
|
|
});
|
|
|
|
},
|
2023-11-19 15:00:29 -05:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
const gesture = Gtk.GestureLongPress.new(widget);
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-11-19 15:00:29 -05:00
|
|
|
gesture.delay_factor = 1.0;
|
|
|
|
|
|
|
|
// Long press
|
2023-12-17 00:01:58 -05:00
|
|
|
widget.hook(gesture, () => {
|
2023-11-19 15:00:29 -05:00
|
|
|
const pointer = gesture.get_point(null);
|
|
|
|
const x = pointer[1];
|
|
|
|
const y = pointer[2];
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if ((!x || !y) || (x === 0 && y === 0)) {
|
2023-11-19 15:00:29 -05:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
console.log('Not implemented yet');
|
|
|
|
|
|
|
|
// TODO: popup menu for accents
|
|
|
|
}, 'pressed');
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
// OnPrimaryClickRelease
|
2023-12-17 00:01:58 -05:00
|
|
|
widget.hook(gesture, () => {
|
2023-11-19 15:00:29 -05:00
|
|
|
const pointer = gesture.get_point(null);
|
|
|
|
const x = pointer[1];
|
|
|
|
const y = pointer[2];
|
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
if ((!x || !y) || (x === 0 && y === 0)) {
|
2023-11-19 15:00:29 -05:00
|
|
|
return;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-11-19 15:00:29 -05:00
|
|
|
|
|
|
|
console.log('key clicked');
|
|
|
|
|
|
|
|
execAsync(`ydotool key ${key.keycode}:1`);
|
|
|
|
execAsync(`ydotool key ${key.keycode}:0`);
|
|
|
|
NormalClick.value = true;
|
|
|
|
}, 'cancelled');
|
|
|
|
|
|
|
|
return Box({
|
|
|
|
children: [
|
|
|
|
widget,
|
2023-11-21 01:29:46 -05:00
|
|
|
Separator(SPACING),
|
2023-11-19 15:00:29 -05:00
|
|
|
],
|
|
|
|
});
|
|
|
|
};
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
export default (key: Key) => key.keytype === 'normal' ?
|
2023-11-21 01:29:46 -05:00
|
|
|
RegularKey(key) :
|
|
|
|
ModKey(key);
|