2023-10-31 08:32:40 -04:00
|
|
|
import App from 'resource:///com/github/Aylur/ags/app.js';
|
2023-12-08 00:01:43 -05:00
|
|
|
import Hyprland from 'resource:///com/github/Aylur/ags/service/hyprland.js';
|
2024-01-02 18:37:12 -05:00
|
|
|
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
import { Box, Overlay, Window } from 'resource:///com/github/Aylur/ags/widget.js';
|
2023-11-28 00:50:58 -05:00
|
|
|
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
// Types
|
|
|
|
type Allocation = typeof imports.gi.Gtk.Allocation;
|
|
|
|
type Widget = typeof imports.gi.Gtk.Widget;
|
|
|
|
import { RevealerProps } from 'types/widgets/revealer';
|
|
|
|
import { WindowProps } from 'types/widgets/window';
|
|
|
|
import AgsWindow from 'types/widgets/window';
|
|
|
|
import AgsBox from 'types/widgets/box';
|
|
|
|
import AgsOverlay from 'types/widgets/overlay';
|
|
|
|
import { Binding } from 'types/service';
|
|
|
|
type PopupWindow = WindowProps & {
|
|
|
|
transition?: RevealerProps['transition']
|
|
|
|
transition_duration?: number
|
|
|
|
bezier?: string
|
|
|
|
on_open?(self: AgsWindow): void
|
|
|
|
on_close?(self: AgsWindow): void
|
|
|
|
blur?: boolean
|
|
|
|
close_on_unfocus?: 'none' | 'stay' | 'released' | 'clicked'
|
|
|
|
anchor?: Array<string>
|
|
|
|
name: string
|
|
|
|
};
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
// FIXME: deal with overlay children?
|
|
|
|
// TODO: make this a new class to be able to edit props
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2023-10-17 13:47:02 -04:00
|
|
|
export default ({
|
2023-10-30 19:53:50 -04:00
|
|
|
transition = 'slide_down',
|
2023-12-30 19:49:08 -05:00
|
|
|
transition_duration = 800,
|
2024-01-02 18:37:12 -05:00
|
|
|
bezier = 'cubic-bezier(0.68, -0.4, 0.32, 1.4)',
|
2023-12-31 03:08:48 -05:00
|
|
|
on_open = () => {/**/},
|
|
|
|
on_close = () => {/**/},
|
2023-10-30 19:53:50 -04:00
|
|
|
|
|
|
|
// Window props
|
2023-10-17 13:47:02 -04:00
|
|
|
name,
|
2024-01-02 18:37:12 -05:00
|
|
|
child = Box(),
|
2023-10-30 19:53:50 -04:00
|
|
|
visible = false,
|
2024-01-02 18:37:12 -05:00
|
|
|
anchor = [],
|
2023-10-30 19:53:50 -04:00
|
|
|
layer = 'overlay',
|
2023-12-18 23:20:32 -05:00
|
|
|
blur = false,
|
|
|
|
close_on_unfocus = 'released',
|
2023-10-20 23:11:21 -04:00
|
|
|
...props
|
2024-01-13 11:15:08 -05:00
|
|
|
}: PopupWindow) => {
|
2024-01-02 18:37:12 -05:00
|
|
|
const Child = Variable(child);
|
|
|
|
const AntiClip = Variable(false);
|
2023-09-22 01:34:36 -04:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
const needsAnticlipping = bezier.match(/-[0-9]/) !== null &&
|
|
|
|
transition !== 'crossfade';
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
const attribute = {
|
|
|
|
set_x_pos: (
|
2024-01-13 11:15:08 -05:00
|
|
|
alloc: Allocation,
|
|
|
|
side = 'right' as 'left' | 'right',
|
2024-01-02 18:37:12 -05:00
|
|
|
) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const window = App.getWindow(name) as AgsWindow;
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
const width = window.get_display()
|
|
|
|
.get_monitor_at_point(alloc.x, alloc.y)
|
|
|
|
.get_geometry().width;
|
|
|
|
|
|
|
|
window.margins = [
|
|
|
|
window.margins[0],
|
|
|
|
|
|
|
|
side === 'right' ?
|
|
|
|
(width - alloc.x - alloc.width) :
|
|
|
|
window.margins[1],
|
|
|
|
|
|
|
|
window.margins[2],
|
|
|
|
|
|
|
|
side === 'right' ?
|
|
|
|
window.margins[3] :
|
|
|
|
(alloc.x - alloc.width),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
|
|
|
get_child: () => Child.value,
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
set_child: (new_child: Widget) => {
|
2024-01-02 18:37:12 -05:00
|
|
|
Child.value = new_child;
|
|
|
|
App.getWindow(name)?.child.show_all();
|
2023-12-18 23:20:32 -05:00
|
|
|
},
|
|
|
|
|
2024-01-13 23:38:31 -05:00
|
|
|
// This is for my custom pointers.ts
|
2024-01-02 18:37:12 -05:00
|
|
|
close_on_unfocus,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (transition === 'none') {
|
|
|
|
return Window({
|
|
|
|
name,
|
|
|
|
layer,
|
|
|
|
anchor,
|
|
|
|
visible: false,
|
|
|
|
...props,
|
|
|
|
attribute,
|
|
|
|
child: Child.bind(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const window = Window({
|
|
|
|
name,
|
|
|
|
layer,
|
|
|
|
anchor,
|
|
|
|
visible: false,
|
|
|
|
...props,
|
|
|
|
|
|
|
|
attribute,
|
|
|
|
|
2023-10-30 19:53:50 -04:00
|
|
|
setup: () => {
|
2023-12-08 00:01:43 -05:00
|
|
|
// Add way to make window open on startup
|
2023-10-30 19:53:50 -04:00
|
|
|
const id = App.connect('config-parsed', () => {
|
2023-11-21 01:29:46 -05:00
|
|
|
if (visible) {
|
2024-01-02 18:37:12 -05:00
|
|
|
App.openWindow(`${name}`);
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-10-30 19:53:50 -04:00
|
|
|
App.disconnect(id);
|
|
|
|
});
|
2023-12-08 00:01:43 -05:00
|
|
|
|
|
|
|
if (blur) {
|
|
|
|
Hyprland.sendMessage('[[BATCH]] ' +
|
|
|
|
`keyword layerrule ignorealpha[0.97],${name}; ` +
|
|
|
|
`keyword layerrule blur,${name}`);
|
|
|
|
}
|
2023-10-30 19:53:50 -04:00
|
|
|
},
|
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
child: Overlay({
|
|
|
|
overlays: [Box({
|
|
|
|
setup: (self) => {
|
|
|
|
// Make sure child doesn't
|
|
|
|
// get bigger than it should
|
|
|
|
const MAX_ANCHORS = 4;
|
|
|
|
|
|
|
|
self.hpack = 'center';
|
|
|
|
self.vpack = 'center';
|
|
|
|
|
|
|
|
if (anchor.includes('top') &&
|
|
|
|
anchor.includes('bottom')) {
|
|
|
|
self.vpack = 'center';
|
|
|
|
}
|
|
|
|
else if (anchor.includes('top')) {
|
|
|
|
self.vpack = 'start';
|
|
|
|
}
|
|
|
|
else if (anchor.includes('bottom')) {
|
|
|
|
self.vpack = 'end';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anchor.includes('left') &&
|
|
|
|
anchor.includes('right')) {
|
|
|
|
self.hpack = 'center';
|
|
|
|
}
|
|
|
|
else if (anchor.includes('left')) {
|
|
|
|
self.hpack = 'start';
|
|
|
|
}
|
|
|
|
else if (anchor.includes('right')) {
|
|
|
|
self.hpack = 'end';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anchor.length === MAX_ANCHORS) {
|
|
|
|
self.hpack = 'center';
|
|
|
|
self.vpack = 'center';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsAnticlipping) {
|
2024-01-13 11:15:08 -05:00
|
|
|
const reorder_child = (position: number) => {
|
2024-01-02 18:37:12 -05:00
|
|
|
// If unanchored, we have another anticlip widget
|
|
|
|
// so we can't change the order
|
|
|
|
if (anchor.length !== 0) {
|
|
|
|
for (const ch of self.children) {
|
|
|
|
if (ch !== Child.value) {
|
|
|
|
self.reorder_child(ch, position);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.hook(AntiClip, () => {
|
|
|
|
if (transition === 'slide_down') {
|
|
|
|
self.vertical = true;
|
|
|
|
reorder_child(-1);
|
|
|
|
}
|
|
|
|
else if (transition === 'slide_up') {
|
|
|
|
self.vertical = true;
|
|
|
|
reorder_child(0);
|
|
|
|
}
|
|
|
|
else if (transition === 'slide_right') {
|
|
|
|
self.vertical = false;
|
|
|
|
reorder_child(-1);
|
|
|
|
}
|
|
|
|
else if (transition === 'slide_left') {
|
|
|
|
self.vertical = false;
|
|
|
|
reorder_child(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
children: Child.bind().transform((v) => {
|
|
|
|
if (needsAnticlipping) {
|
|
|
|
return [
|
|
|
|
// Add an anticlip widget when unanchored
|
|
|
|
// to not have a weird animation
|
|
|
|
anchor.length === 0 && Box({
|
|
|
|
css: `
|
|
|
|
min-height: 100px;
|
|
|
|
min-width: 100px;
|
|
|
|
padding: 2px;
|
|
|
|
`,
|
|
|
|
visible: AntiClip.bind(),
|
|
|
|
}),
|
|
|
|
v,
|
|
|
|
Box({
|
|
|
|
css: `
|
|
|
|
min-height: 100px;
|
|
|
|
min-width: 100px;
|
|
|
|
padding: 2px;
|
|
|
|
`,
|
|
|
|
visible: AntiClip.bind(),
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return [v];
|
|
|
|
}
|
2024-01-13 11:15:08 -05:00
|
|
|
}) as Binding<any, any, Widget[]>,
|
2024-01-02 18:37:12 -05:00
|
|
|
})],
|
|
|
|
|
|
|
|
setup: (self) => {
|
|
|
|
self.on('get-child-position', (_, ch) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
const overlay = (Child.value as Widget)
|
|
|
|
.get_parent() as AgsOverlay;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
|
|
|
if (ch === overlay) {
|
|
|
|
const alloc = overlay.get_allocation();
|
2024-01-13 11:15:08 -05:00
|
|
|
const setAlloc = (v: number) => v - 2 < 0 ? 1 : v;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
(self.child as AgsBox).css = `
|
2024-01-02 18:37:12 -05:00
|
|
|
min-height: ${setAlloc(alloc.height - 2)}px;
|
|
|
|
min-width: ${setAlloc(alloc.width - 2)}px;
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
child: Box({
|
|
|
|
css: `
|
|
|
|
min-height: 1px;
|
|
|
|
min-width: 1px;
|
|
|
|
padding: 1px;
|
|
|
|
`,
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
2024-01-13 11:15:08 -05:00
|
|
|
let currentTimeout: number;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
self.hook(App, (_, currentName, isOpen) => {
|
|
|
|
if (currentName === name) {
|
2024-01-13 11:15:08 -05:00
|
|
|
const overlay = (Child.value as Widget)
|
|
|
|
.get_parent() as AgsOverlay;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
|
|
|
const alloc = overlay.get_allocation();
|
|
|
|
const height = needsAnticlipping ?
|
|
|
|
alloc.height + 100 + 10 :
|
|
|
|
alloc.height + 10;
|
|
|
|
|
|
|
|
if (needsAnticlipping) {
|
|
|
|
AntiClip.value = true;
|
|
|
|
|
|
|
|
const thisTimeout = timeout(
|
|
|
|
transition_duration,
|
|
|
|
() => {
|
|
|
|
// Only run the timeout if there isn't a newer timeout
|
|
|
|
if (thisTimeout === currentTimeout) {
|
|
|
|
AntiClip.value = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
currentTimeout = thisTimeout;
|
|
|
|
}
|
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
let css = '';
|
2024-01-02 18:37:12 -05:00
|
|
|
|
|
|
|
/* Margin: top | right | bottom | left */
|
|
|
|
switch (transition) {
|
|
|
|
case 'slide_down':
|
|
|
|
css = `margin:
|
|
|
|
-${height}px
|
|
|
|
0
|
|
|
|
${height}px
|
|
|
|
0
|
|
|
|
;`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'slide_up':
|
|
|
|
css = `margin:
|
|
|
|
${height}px
|
|
|
|
0
|
|
|
|
-${height}px
|
|
|
|
0
|
|
|
|
;`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'slide_left':
|
|
|
|
css = `margin:
|
|
|
|
0
|
|
|
|
-${height}px
|
|
|
|
0
|
|
|
|
${height}px
|
|
|
|
;`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'slide_right':
|
|
|
|
css = `margin:
|
|
|
|
0
|
|
|
|
${height}px
|
|
|
|
0
|
|
|
|
-${height}px
|
|
|
|
;`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'crossfade':
|
|
|
|
css = `
|
|
|
|
opacity: 0;
|
|
|
|
min-height: 1px;
|
|
|
|
min-width: 1px;
|
|
|
|
`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-11-14 09:36:39 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
if (isOpen) {
|
2023-12-31 03:08:48 -05:00
|
|
|
on_open(window);
|
2024-01-02 18:37:12 -05:00
|
|
|
|
|
|
|
// To get the animation, we need to set the css
|
|
|
|
// to hide the widget and then timeout to have
|
|
|
|
// the animation
|
|
|
|
overlay.css = css;
|
|
|
|
timeout(10, () => {
|
|
|
|
overlay.css = `
|
|
|
|
transition: margin
|
|
|
|
${transition_duration}ms ${bezier},
|
|
|
|
|
|
|
|
opacity
|
|
|
|
${transition_duration}ms ${bezier};
|
|
|
|
`;
|
|
|
|
});
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
|
|
|
else {
|
2024-01-02 18:37:12 -05:00
|
|
|
timeout(transition_duration, () => {
|
2023-12-31 03:08:48 -05:00
|
|
|
on_close(window);
|
2023-12-17 00:01:58 -05:00
|
|
|
});
|
2024-01-02 18:37:12 -05:00
|
|
|
|
|
|
|
overlay.css = `${css}
|
|
|
|
transition: margin
|
|
|
|
${transition_duration}ms ${bezier},
|
|
|
|
|
|
|
|
opacity
|
|
|
|
${transition_duration}ms ${bezier};
|
|
|
|
`;
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
2023-11-21 01:29:46 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
});
|
|
|
|
},
|
2023-10-20 23:11:21 -04:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
});
|
2023-10-30 19:53:50 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
return window;
|
|
|
|
};
|