2023-11-06 20:10:12 -05:00
|
|
|
import Notifications from 'resource:///com/github/Aylur/ags/service/notifications.js';
|
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';
|
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
import Gtk from 'gi://Gtk';
|
|
|
|
import Gdk from 'gi://Gdk';
|
2023-09-11 13:58:24 -04:00
|
|
|
const display = Gdk.Display.get_default();
|
|
|
|
|
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-11-06 20:10:12 -05:00
|
|
|
maxOffset = 200,
|
2023-10-20 23:11:21 -04:00
|
|
|
startMargin = 0,
|
|
|
|
endMargin = 300,
|
|
|
|
command = () => {},
|
|
|
|
...props
|
2023-09-11 13:58:24 -04:00
|
|
|
}) => {
|
2023-10-20 23:11:21 -04:00
|
|
|
const widget = EventBox({
|
|
|
|
...props,
|
|
|
|
onHover: self => {
|
|
|
|
self.window.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
2023-11-06 20:10:12 -05:00
|
|
|
if (!self._hovered)
|
|
|
|
self._hovered = true;
|
2023-10-20 23:11:21 -04:00
|
|
|
},
|
|
|
|
onHoverLost: self => {
|
|
|
|
self.window.set_cursor(null);
|
2023-11-06 20:10:12 -05:00
|
|
|
if (self._hovered)
|
|
|
|
self._hovered = false;
|
2023-10-20 23:11:21 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
// Properties
|
|
|
|
widget._dragging = false;
|
|
|
|
widget._hovered = false;
|
|
|
|
widget._id = id;
|
|
|
|
widget.ready = false;
|
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
const gesture = Gtk.GestureDrag.new(widget);
|
|
|
|
|
2023-11-01 15:29:47 -04: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(maxOffset + endMargin)}px;
|
|
|
|
margin-right: ${Number(maxOffset + endMargin)}px;
|
|
|
|
`;
|
|
|
|
const MAX_RIGHT = `
|
|
|
|
margin-left: ${Number(maxOffset + endMargin)}px;
|
|
|
|
margin-right: -${Number(maxOffset + endMargin)}px;
|
|
|
|
`;
|
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
const slideLeft = `${TRANSITION} ${MAX_LEFT} margin-top: 0px; margin-bottom: 0px; opacity: 0;`;
|
2023-11-01 15:29:47 -04:00
|
|
|
const squeezeLeft = `${TRANSITION} ${MAX_LEFT} ${SQUEEZED} opacity: 0;`;
|
2023-11-06 20:10:12 -05:00
|
|
|
const slideRight = `${TRANSITION} ${MAX_RIGHT} margin-top: 0px; margin-bottom: 0px; opacity: 0;`;
|
2023-11-01 15:29:47 -04:00
|
|
|
const squeezeRight = `${TRANSITION} ${MAX_RIGHT} ${SQUEEZED} opacity: 0;`;
|
2023-11-06 20:10:12 -05:00
|
|
|
const defaultStyle = `${TRANSITION} margin: unset; opacity: 1;`;
|
|
|
|
|
|
|
|
// Notif methods
|
|
|
|
widget.slideAway = side => {
|
|
|
|
// Slide away
|
|
|
|
widget.child.setCss(side === 'Left' ? slideLeft : slideRight);
|
|
|
|
|
|
|
|
// Makie it uninteractable
|
|
|
|
widget.sensitive = false;
|
|
|
|
|
|
|
|
timeout(400, () => {
|
|
|
|
// Reduce height after sliding away
|
|
|
|
widget.child?.setCss(side === 'Left' ? squeezeLeft : squeezeRight);
|
|
|
|
|
|
|
|
timeout(500, () => {
|
|
|
|
// Kill notif and update HasNotifs after anim is done
|
|
|
|
command();
|
|
|
|
HasNotifs.value = Notifications.notifications.length > 0;
|
|
|
|
widget.get_parent()?.remove(widget);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2023-10-20 23:11:21 -04:00
|
|
|
|
|
|
|
widget.add(Box({
|
2023-11-06 20:10:12 -05:00
|
|
|
css: squeezeLeft,
|
2023-10-20 23:11:21 -04:00
|
|
|
connections: [
|
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
// When dragging
|
2023-10-20 23:11:21 -04:00
|
|
|
[gesture, self => {
|
|
|
|
var offset = gesture.get_offset()[1];
|
|
|
|
|
2023-11-01 15:29:47 -04:00
|
|
|
// Slide right
|
2023-10-20 23:11:21 -04:00
|
|
|
if (offset >= 0) {
|
2023-11-06 20:10:12 -05:00
|
|
|
self.setCss(`
|
|
|
|
margin-top: 0px; margin-bottom: 0px; opacity: 1; transition: none;
|
2023-11-01 15:29:47 -04:00
|
|
|
margin-left: ${Number(offset + startMargin)}px;
|
|
|
|
margin-right: -${Number(offset + startMargin)}px;
|
|
|
|
`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-11-01 15:29:47 -04:00
|
|
|
|
|
|
|
// Slide left
|
2023-10-20 23:11:21 -04:00
|
|
|
else {
|
|
|
|
offset = Math.abs(offset);
|
2023-11-06 20:10:12 -05:00
|
|
|
self.setCss(`
|
|
|
|
margin-top: 0px; margin-bottom: 0px; opacity: 1; transition: none;
|
2023-11-01 15:29:47 -04:00
|
|
|
margin-right: ${Number(offset + startMargin)}px;
|
|
|
|
margin-left: -${Number(offset + startMargin)}px;
|
|
|
|
`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-09-15 15:49:43 -04:00
|
|
|
|
2023-11-03 15:28:08 -04:00
|
|
|
// Put a threshold on if a click is actually dragging
|
2023-11-06 20:10:12 -05:00
|
|
|
widget._dragging = Math.abs(offset) > 10;
|
|
|
|
widget.window?.set_cursor(Gdk.Cursor.new_from_name(display, 'grabbing'));
|
2023-10-20 23:11:21 -04:00
|
|
|
}, 'drag-update'],
|
2023-09-11 13:58:24 -04:00
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
|
|
|
|
// On drag end
|
2023-10-20 23:11:21 -04:00
|
|
|
[gesture, self => {
|
2023-11-03 15:28:08 -04:00
|
|
|
// Make it slide in on init
|
2023-11-06 20:10:12 -05:00
|
|
|
if (!widget.ready) {
|
|
|
|
// Reverse of slideAway, so it started at squeeze, then we go to slide
|
|
|
|
self.setCss(slideIn === 'Left' ? slideLeft : slideRight);
|
|
|
|
|
|
|
|
timeout(500, () => {
|
|
|
|
// Then we got to center
|
|
|
|
self.setCss(defaultStyle);
|
|
|
|
timeout(500, () => widget.ready = true);
|
|
|
|
});
|
2023-10-20 23:11:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const offset = gesture.get_offset()[1];
|
|
|
|
|
2023-11-01 15:29:47 -04:00
|
|
|
// If crosses threshold after letting go, slide away
|
2023-10-20 23:11:21 -04:00
|
|
|
if (Math.abs(offset) > maxOffset) {
|
2023-11-06 20:10:12 -05:00
|
|
|
if (offset > 0)
|
|
|
|
widget.slideAway('Right');
|
|
|
|
else
|
|
|
|
widget.slideAway('Left');
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
else {
|
2023-11-06 20:10:12 -05:00
|
|
|
self.setCss(defaultStyle);
|
|
|
|
widget.window?.set_cursor(Gdk.Cursor.new_from_name(display, 'grab'));
|
2023-10-17 13:47:02 -04:00
|
|
|
|
2023-11-06 20:10:12 -05:00
|
|
|
widget._dragging = false;
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
}, 'drag-end'],
|
2023-09-11 19:28:22 -04:00
|
|
|
|
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
|
|
|
};
|