2023-11-06 20:10:12 -05:00
|
|
|
import Notifications from 'resource:///com/github/Aylur/ags/service/notifications.js';
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
import { Box, EventBox } from 'resource:///com/github/Aylur/ags/widget.js';
|
|
|
|
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
import { HasNotifs } from './base.js';
|
|
|
|
|
2024-01-11 10:25:53 -05:00
|
|
|
const { Gdk, Gtk } = imports.gi;
|
|
|
|
const display = Gdk.Display.get_default();
|
2023-09-11 13:58:24 -04:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
const MAX_OFFSET = 200;
|
|
|
|
const OFFSCREEN = 300;
|
|
|
|
const ANIM_DURATION = 500;
|
|
|
|
const SLIDE_MIN_THRESHOLD = 10;
|
2023-12-20 17:14:07 -05:00
|
|
|
const TRANSITION = 'transition: margin 0.5s ease, opacity 0.5s ease;';
|
|
|
|
const SQUEEZED = 'margin-bottom: -70px; margin-top: -70px;';
|
|
|
|
const MAX_LEFT = `
|
|
|
|
margin-left: -${Number(MAX_OFFSET + OFFSCREEN)}px;
|
|
|
|
margin-right: ${Number(MAX_OFFSET + OFFSCREEN)}px;
|
|
|
|
`;
|
|
|
|
const MAX_RIGHT = `
|
|
|
|
margin-left: ${Number(MAX_OFFSET + OFFSCREEN)}px;
|
|
|
|
margin-right: -${Number(MAX_OFFSET + OFFSCREEN)}px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const slideLeft = `${TRANSITION} ${MAX_LEFT}
|
|
|
|
margin-top: 0px;
|
|
|
|
margin-bottom: 0px;
|
|
|
|
opacity: 0;`;
|
|
|
|
const squeezeLeft = `${TRANSITION} ${MAX_LEFT} ${SQUEEZED} opacity: 0;`;
|
|
|
|
|
|
|
|
const slideRight = `${TRANSITION} ${MAX_RIGHT}
|
|
|
|
margin-top: 0px;
|
|
|
|
margin-bottom: 0px;
|
|
|
|
opacity: 0;`;
|
|
|
|
const squeezeRight = `${TRANSITION} ${MAX_RIGHT} ${SQUEEZED} opacity: 0;`;
|
|
|
|
|
|
|
|
const defaultStyle = `${TRANSITION} margin: unset; opacity: 1;`;
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-10-16 18:37:22 -04:00
|
|
|
export default ({
|
2023-11-06 20:10:12 -05:00
|
|
|
id,
|
2023-11-03 15:28:08 -04:00
|
|
|
slideIn = 'Left',
|
2023-12-20 17:14:07 -05:00
|
|
|
command = () => {/**/},
|
2023-10-20 23:11:21 -04:00
|
|
|
...props
|
2023-09-11 13:58:24 -04:00
|
|
|
}) => {
|
2023-10-20 23:11:21 -04:00
|
|
|
const widget = EventBox({
|
|
|
|
...props,
|
2023-12-22 16:58:12 -05:00
|
|
|
|
|
|
|
setup: (self) => {
|
|
|
|
self
|
2024-01-11 10:25:53 -05:00
|
|
|
// OnClick
|
|
|
|
.on('button-press-event', () => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(
|
|
|
|
display,
|
|
|
|
'grabbing',
|
|
|
|
));
|
2023-12-22 16:58:12 -05:00
|
|
|
})
|
2024-01-11 10:25:53 -05:00
|
|
|
|
|
|
|
// OnRelease
|
|
|
|
.on('button-release-event', () => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(
|
|
|
|
display,
|
|
|
|
'grab',
|
|
|
|
));
|
|
|
|
})
|
|
|
|
|
|
|
|
// OnHover
|
2023-12-22 16:58:12 -05:00
|
|
|
.on('enter-notify-event', () => {
|
2024-01-11 10:25:53 -05:00
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(
|
|
|
|
display,
|
|
|
|
'grab',
|
|
|
|
));
|
|
|
|
self.toggleClassName('hover', true);
|
2023-12-22 16:58:12 -05:00
|
|
|
if (!self.attribute.hovered) {
|
|
|
|
self.attribute.hovered = true;
|
|
|
|
}
|
2024-01-11 10:25:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
// OnHoverLost
|
|
|
|
.on('leave-notify-event', () => {
|
|
|
|
self.window.set_cursor(null);
|
|
|
|
self.toggleClassName('hover', false);
|
|
|
|
|
|
|
|
if (self.attribute.hovered) {
|
|
|
|
self.attribute.hovered = false;
|
|
|
|
}
|
2023-12-22 16:58:12 -05:00
|
|
|
});
|
2023-10-20 23:11:21 -04:00
|
|
|
},
|
|
|
|
|
2023-12-20 17:14:07 -05:00
|
|
|
attribute: {
|
|
|
|
dragging: false,
|
|
|
|
hovered: false,
|
|
|
|
ready: false,
|
|
|
|
id,
|
|
|
|
|
|
|
|
/** @param {'Left'|'Right'} side */
|
|
|
|
slideAway: (side) => {
|
|
|
|
// Slide away
|
|
|
|
// @ts-expect-error
|
|
|
|
widget.child.setCss(side === 'Left' ? slideLeft : slideRight);
|
|
|
|
|
|
|
|
// Make it uninteractable
|
|
|
|
widget.sensitive = false;
|
|
|
|
|
|
|
|
timeout(ANIM_DURATION - 100, () => {
|
|
|
|
// Reduce height after sliding away
|
|
|
|
// @ts-expect-error
|
|
|
|
widget.child?.setCss(side === 'Left' ?
|
|
|
|
squeezeLeft :
|
|
|
|
squeezeRight);
|
|
|
|
|
|
|
|
timeout(ANIM_DURATION, () => {
|
|
|
|
// Kill notif and update HasNotifs after anim is done
|
|
|
|
command();
|
|
|
|
|
|
|
|
HasNotifs.value = Notifications
|
|
|
|
.notifications.length > 0;
|
|
|
|
|
|
|
|
// @ts-expect-error
|
2023-12-23 16:23:30 -05:00
|
|
|
widget.get_parent()?.remove(widget);
|
2023-12-20 17:14:07 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2023-11-06 20:10:12 -05:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
const gesture = Gtk.GestureDrag.new(widget);
|
|
|
|
|
|
|
|
widget.add(Box({
|
2023-11-06 20:10:12 -05:00
|
|
|
css: squeezeLeft,
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
|
|
|
self
|
|
|
|
// When dragging
|
|
|
|
.hook(gesture, () => {
|
|
|
|
let offset = gesture.get_offset()[1];
|
|
|
|
|
|
|
|
if (offset === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// Slide right
|
|
|
|
if (offset > 0) {
|
|
|
|
self.setCss(`
|
2023-11-21 01:29:46 -05:00
|
|
|
margin-top: 0px; margin-bottom: 0px;
|
|
|
|
opacity: 1; transition: none;
|
|
|
|
margin-left: ${offset}px;
|
|
|
|
margin-right: -${offset}px;
|
2023-11-01 15:29:47 -04:00
|
|
|
`);
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
2023-11-01 15:29:47 -04:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// Slide left
|
|
|
|
else {
|
|
|
|
offset = Math.abs(offset);
|
|
|
|
self.setCss(`
|
2023-11-21 01:29:46 -05:00
|
|
|
margin-top: 0px; margin-bottom: 0px;
|
|
|
|
opacity: 1; transition: none;
|
|
|
|
margin-right: ${offset}px;
|
|
|
|
margin-left: -${offset}px;
|
2023-11-01 15:29:47 -04:00
|
|
|
`);
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
2023-09-11 13:58:24 -04:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// Put a threshold on if a click is actually dragging
|
2023-12-20 17:14:07 -05:00
|
|
|
widget.attribute.dragging =
|
|
|
|
Math.abs(offset) > SLIDE_MIN_THRESHOLD;
|
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
widget.cursor = 'grabbing';
|
|
|
|
}, 'drag-update')
|
2023-11-06 20:10:12 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// On drag end
|
|
|
|
.hook(gesture, () => {
|
|
|
|
// Make it slide in on init
|
2023-12-20 17:14:07 -05:00
|
|
|
if (!widget.attribute.ready) {
|
2023-12-17 00:01:58 -05:00
|
|
|
// Reverse of slideAway, so it started at squeeze, then we go to slide
|
|
|
|
self.setCss(slideIn === 'Left' ?
|
|
|
|
slideLeft :
|
|
|
|
slideRight);
|
2023-11-06 20:10:12 -05:00
|
|
|
|
2023-11-21 01:29:46 -05:00
|
|
|
timeout(ANIM_DURATION, () => {
|
2023-12-17 00:01:58 -05:00
|
|
|
// Then we go to center
|
|
|
|
self.setCss(defaultStyle);
|
|
|
|
timeout(ANIM_DURATION, () => {
|
2023-12-20 17:14:07 -05:00
|
|
|
widget.attribute.ready = true;
|
2023-12-17 00:01:58 -05:00
|
|
|
});
|
2023-11-21 01:29:46 -05:00
|
|
|
});
|
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
return;
|
|
|
|
}
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
const offset = gesture.get_offset()[1];
|
2023-10-20 23:11:21 -04:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
// If crosses threshold after letting go, slide away
|
|
|
|
if (Math.abs(offset) > MAX_OFFSET) {
|
|
|
|
if (offset > 0) {
|
2023-12-20 17:14:07 -05:00
|
|
|
widget.attribute.slideAway('Right');
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
|
|
|
else {
|
2023-12-20 17:14:07 -05:00
|
|
|
widget.attribute.slideAway('Left');
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
|
|
|
else {
|
2023-12-17 00:01:58 -05:00
|
|
|
self.setCss(defaultStyle);
|
|
|
|
widget.cursor = 'grab';
|
2023-12-20 17:14:07 -05:00
|
|
|
widget.attribute.dragging = false;
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
}, 'drag-end');
|
|
|
|
},
|
2023-10-20 23:11:21 -04:00
|
|
|
}));
|
2023-09-11 13:58:24 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
return widget;
|
2023-09-11 13:58:24 -04:00
|
|
|
};
|