nixos-configs/devices/wim/config/ags/ts/on-screen-keyboard/gesture.ts

156 lines
4.6 KiB
TypeScript
Raw Normal View History

import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
import { execAsync, timeout } from 'resource:///com/github/Aylur/ags/utils.js';
2023-12-21 01:25:59 -05:00
const { Gtk } = imports.gi;
import Tablet from '../../services/tablet.ts';
const KEY_N = 249;
const HIDDEN_MARGIN = 340;
const ANIM_DURATION = 700;
2024-01-13 11:15:08 -05:00
// Types
import AgsWindow from 'types/widgets/window.ts';
import AgsBox from 'types/widgets/box.ts';
2024-01-13 11:15:08 -05:00
const releaseAllKeys = () => {
const keycodes = Array.from(Array(KEY_N).keys());
execAsync([
'ydotool', 'key',
...keycodes.map((keycode) => `${keycode}:0`),
]).catch(print);
};
2024-01-13 11:15:08 -05:00
export default (window: AgsWindow) => {
const gesture = Gtk.GestureDrag.new(window);
2024-01-13 11:15:08 -05:00
const child = window.child as AgsBox;
child.setCss(`margin-bottom: -${HIDDEN_MARGIN}px;`);
2024-01-13 11:15:08 -05:00
let signals = [] as Array<number>;
2023-12-21 01:25:59 -05:00
window.attribute = {
2024-01-13 11:15:08 -05:00
setVisible: (state: boolean) => {
2023-12-21 01:25:59 -05:00
if (state) {
window.visible = true;
window.attribute.setSlideDown();
2024-01-13 11:15:08 -05:00
child.setCss(`
2023-12-21 01:25:59 -05:00
transition: margin-bottom 0.7s
cubic-bezier(0.36, 0, 0.66, -0.56);
margin-bottom: 0px;
`);
}
else {
timeout(ANIM_DURATION + 10, () => {
if (!Tablet.tabletMode) {
window.visible = false;
}
});
2023-12-21 01:25:59 -05:00
releaseAllKeys();
window.attribute.setSlideUp();
2024-01-13 11:15:08 -05:00
child.setCss(`
2023-12-21 01:25:59 -05:00
transition: margin-bottom 0.7s
cubic-bezier(0.36, 0, 0.66, -0.56);
margin-bottom: -${HIDDEN_MARGIN}px;
`);
2023-12-21 01:25:59 -05:00
}
},
2023-12-21 01:25:59 -05:00
killGestureSigs: () => {
signals.forEach((id) => {
gesture.disconnect(id);
});
signals = [];
},
setSlideUp: () => {
window.attribute.killGestureSigs();
// Begin drag
signals.push(
gesture.connect('drag-begin', () => {
Hyprland.sendMessage('j/cursorpos').then((out) => {
gesture.startY = JSON.parse(out).y;
});
}),
);
// Update drag
signals.push(
gesture.connect('drag-update', () => {
Hyprland.sendMessage('j/cursorpos').then((out) => {
const currentY = JSON.parse(out).y;
const offset = gesture.startY - currentY;
if (offset < 0) {
return;
}
2024-01-13 11:15:08 -05:00
(window.child as AgsBox).setCss(`
2023-12-21 01:25:59 -05:00
margin-bottom: ${offset - HIDDEN_MARGIN}px;
`);
});
}),
);
// End drag
signals.push(
gesture.connect('drag-end', () => {
2024-01-13 11:15:08 -05:00
(window.child as AgsBox).setCss(`
2023-12-21 01:25:59 -05:00
transition: margin-bottom 0.5s ease-in-out;
margin-bottom: -${HIDDEN_MARGIN}px;
`);
2023-12-21 01:25:59 -05:00
}),
);
},
setSlideDown: () => {
window.attribute.killGestureSigs();
// Begin drag
signals.push(
gesture.connect('drag-begin', () => {
Hyprland.sendMessage('j/cursorpos').then((out) => {
gesture.startY = JSON.parse(out).y;
});
}),
);
// Update drag
signals.push(
gesture.connect('drag-update', () => {
Hyprland.sendMessage('j/cursorpos').then((out) => {
const currentY = JSON.parse(out).y;
const offset = gesture.startY - currentY;
if (offset > 0) {
return;
}
2024-01-13 11:15:08 -05:00
(window.child as AgsBox).setCss(`
2023-12-21 01:25:59 -05:00
margin-bottom: ${offset}px;
`);
});
}),
);
// End drag
signals.push(
gesture.connect('drag-end', () => {
2024-01-13 11:15:08 -05:00
(window.child as AgsBox).setCss(`
2023-12-21 01:25:59 -05:00
transition: margin-bottom 0.5s ease-in-out;
margin-bottom: 0px;
`);
}),
);
},
};
return window;
};