2024-01-30 11:29:07 -05:00
|
|
|
import Gtk from 'gi://Gtk?version=3.0';
|
|
|
|
const Hyprland = await Service.import('hyprland');
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2024-01-30 11:29:07 -05:00
|
|
|
const { Box, Overlay, register } = Widget;
|
|
|
|
|
|
|
|
const { timeout } = Utils;
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2024-01-13 11:15:08 -05:00
|
|
|
// Types
|
2024-01-29 18:54:07 -05:00
|
|
|
import { Window } from 'resource:///com/github/Aylur/ags/widgets/window.js';
|
|
|
|
import { Variable as Var } from 'types/variable';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CloseType,
|
|
|
|
BoxGeneric,
|
|
|
|
OverlayGeneric,
|
|
|
|
PopupChild,
|
|
|
|
PopupWindowProps,
|
|
|
|
} from 'global-types';
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-02 18:37:12 -05:00
|
|
|
// FIXME: deal with overlay children?
|
2024-01-29 18:54:07 -05:00
|
|
|
// TODO: make props changes affect the widget
|
|
|
|
|
|
|
|
|
|
|
|
export class PopupWindow<
|
2024-01-30 11:29:07 -05:00
|
|
|
Child extends Gtk.Widget,
|
2024-01-29 18:54:07 -05:00
|
|
|
Attr,
|
|
|
|
> extends Window<Child, Attr> {
|
|
|
|
static {
|
|
|
|
register(this, {
|
|
|
|
properties: {
|
|
|
|
content: ['widget', 'rw'],
|
|
|
|
},
|
2024-01-02 18:37:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-30 11:29:07 -05:00
|
|
|
#content: Var<Gtk.Widget>;
|
2024-01-29 18:54:07 -05:00
|
|
|
#antiClip: Var<boolean>;
|
|
|
|
#needsAnticlipping: boolean;
|
|
|
|
#close_on_unfocus: CloseType;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
get content() {
|
|
|
|
return this.#content.value;
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-30 11:29:07 -05:00
|
|
|
set content(value: Gtk.Widget) {
|
2024-02-11 02:18:59 -05:00
|
|
|
this.#content.setValue(value);
|
2024-01-29 18:54:07 -05:00
|
|
|
this.child.show_all();
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
get close_on_unfocus() {
|
|
|
|
return this.#close_on_unfocus;
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
set close_on_unfocus(value: 'none' | 'stay' | 'released' | 'clicked') {
|
|
|
|
this.#close_on_unfocus = value;
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
constructor({
|
|
|
|
transition = 'slide_down',
|
|
|
|
transition_duration = 800,
|
|
|
|
bezier = 'cubic-bezier(0.68, -0.4, 0.32, 1.4)',
|
|
|
|
on_open = () => {/**/},
|
|
|
|
on_close = () => {/**/},
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
// Window props
|
|
|
|
name,
|
|
|
|
visible = false,
|
|
|
|
anchor = [],
|
|
|
|
layer = 'overlay',
|
|
|
|
attribute,
|
|
|
|
content = Box(),
|
|
|
|
blur = false,
|
|
|
|
close_on_unfocus = 'released',
|
|
|
|
...rest
|
|
|
|
}: PopupWindowProps<Child, Attr>) {
|
|
|
|
const needsAnticlipping = bezier.match(/-[0-9]/) !== null &&
|
|
|
|
transition !== 'crossfade';
|
2024-01-30 11:29:07 -05:00
|
|
|
const contentVar = Variable(Box() as Gtk.Widget);
|
2024-01-29 18:54:07 -05:00
|
|
|
const antiClip = Variable(false);
|
|
|
|
|
|
|
|
if (content) {
|
2024-02-11 02:18:59 -05:00
|
|
|
contentVar.setValue(content);
|
2024-01-29 18:54:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
super({
|
|
|
|
...rest,
|
|
|
|
name,
|
|
|
|
visible,
|
|
|
|
anchor,
|
|
|
|
layer,
|
|
|
|
attribute,
|
|
|
|
setup: () => {
|
|
|
|
const id = App.connect('config-parsed', () => {
|
2024-02-05 10:37:56 -05:00
|
|
|
// Set close delay dynamically
|
|
|
|
App.closeWindowDelay[name] = transition_duration;
|
|
|
|
|
|
|
|
// Add way to make window open on startup
|
2024-01-29 18:54:07 -05:00
|
|
|
if (visible) {
|
|
|
|
App.openWindow(`${name}`);
|
2024-01-02 18:37:12 -05:00
|
|
|
}
|
2024-02-05 10:37:56 -05:00
|
|
|
|
|
|
|
// This connection should always run only once
|
2024-01-29 18:54:07 -05:00
|
|
|
App.disconnect(id);
|
|
|
|
});
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
if (blur) {
|
2024-02-11 02:18:59 -05:00
|
|
|
Hyprland.messageAsync('[[BATCH]] ' +
|
2024-01-29 18:54:07 -05:00
|
|
|
`keyword layerrule ignorealpha[0.97],${name}; ` +
|
|
|
|
`keyword layerrule blur,${name}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: Overlay({
|
|
|
|
overlays: [Box({
|
|
|
|
css: `
|
|
|
|
min-height: 1px;
|
|
|
|
min-width: 1px;
|
|
|
|
padding: 1px;
|
|
|
|
`,
|
|
|
|
setup: (self) => {
|
|
|
|
// Make sure child doesn't
|
|
|
|
// get bigger than it should
|
|
|
|
const MAX_ANCHORS = 4;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
self.hpack = 'center';
|
|
|
|
self.vpack = 'center';
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
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';
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
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';
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
if (anchor.length === MAX_ANCHORS) {
|
|
|
|
self.hpack = 'center';
|
|
|
|
self.vpack = 'center';
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
if (needsAnticlipping) {
|
|
|
|
const reorder_child = (position: number) => {
|
|
|
|
// 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 !== contentVar.value) {
|
|
|
|
self.reorder_child(ch, position);
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
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: contentVar.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];
|
|
|
|
}
|
|
|
|
}) as PopupChild,
|
|
|
|
})],
|
2023-11-21 01:29:46 -05:00
|
|
|
|
2023-12-17 00:01:58 -05:00
|
|
|
setup: (self) => {
|
2024-01-29 18:54:07 -05:00
|
|
|
self.on('get-child-position', (_, ch) => {
|
|
|
|
const overlay = contentVar.value
|
|
|
|
.get_parent() as OverlayGeneric;
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
if (ch === overlay) {
|
2024-01-02 18:37:12 -05:00
|
|
|
const alloc = overlay.get_allocation();
|
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
(self.child as BoxGeneric).css = `
|
|
|
|
min-height: ${alloc.height}px;
|
|
|
|
min-width: ${alloc.width}px;
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
child: Box({
|
|
|
|
css: `
|
|
|
|
min-height: 1px;
|
|
|
|
min-width: 1px;
|
|
|
|
padding: 1px;
|
|
|
|
`,
|
|
|
|
|
|
|
|
setup: (self) => {
|
|
|
|
let currentTimeout: number;
|
|
|
|
|
|
|
|
self.hook(App, (_, currentName, isOpen) => {
|
|
|
|
if (currentName === name) {
|
|
|
|
const overlay = contentVar.value
|
|
|
|
.get_parent() as OverlayGeneric;
|
|
|
|
|
|
|
|
const alloc = overlay.get_allocation();
|
|
|
|
const height = antiClip ?
|
|
|
|
alloc.height + 100 + 10 :
|
|
|
|
alloc.height + 10;
|
|
|
|
|
|
|
|
if (needsAnticlipping) {
|
2024-02-11 02:18:59 -05:00
|
|
|
antiClip.setValue(true);
|
2024-01-29 18:54:07 -05:00
|
|
|
|
|
|
|
const thisTimeout = timeout(
|
|
|
|
transition_duration,
|
|
|
|
() => {
|
|
|
|
// Only run the timeout if there isn't a newer timeout
|
|
|
|
if (thisTimeout ===
|
|
|
|
currentTimeout) {
|
2024-02-11 02:18:59 -05:00
|
|
|
antiClip.setValue(false);
|
2024-01-29 18:54:07 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
currentTimeout = thisTimeout;
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
let css = '';
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
if (isOpen) {
|
|
|
|
on_open(this);
|
|
|
|
|
|
|
|
// 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};
|
|
|
|
`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timeout(transition_duration, () => {
|
|
|
|
on_close(this);
|
|
|
|
});
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
overlay.css = `${css}
|
2024-01-02 18:37:12 -05:00
|
|
|
transition: margin
|
|
|
|
${transition_duration}ms ${bezier},
|
|
|
|
|
|
|
|
opacity
|
|
|
|
${transition_duration}ms ${bezier};
|
|
|
|
`;
|
2024-01-29 18:54:07 -05:00
|
|
|
}
|
2023-12-17 00:01:58 -05:00
|
|
|
}
|
2024-01-29 18:54:07 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
}),
|
2023-10-20 23:11:21 -04:00
|
|
|
}),
|
2024-01-29 18:54:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.#content = contentVar;
|
|
|
|
this.#close_on_unfocus = close_on_unfocus;
|
|
|
|
this.#needsAnticlipping = needsAnticlipping;
|
|
|
|
this.#antiClip = antiClip;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_x_pos(
|
2024-01-30 11:29:07 -05:00
|
|
|
alloc: Gtk.Allocation,
|
2024-01-29 18:54:07 -05:00
|
|
|
side = 'right' as 'left' | 'right',
|
|
|
|
) {
|
|
|
|
const width = this.get_display()
|
|
|
|
.get_monitor_at_point(alloc.x, alloc.y)
|
|
|
|
.get_geometry().width;
|
|
|
|
|
|
|
|
this.margins = [
|
|
|
|
this.margins[0],
|
|
|
|
|
|
|
|
side === 'right' ?
|
|
|
|
(width - alloc.x - alloc.width) :
|
|
|
|
this.margins[1],
|
|
|
|
|
|
|
|
this.margins[2],
|
|
|
|
|
|
|
|
side === 'right' ?
|
|
|
|
this.margins[3] :
|
|
|
|
(alloc.x - alloc.width),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2023-10-30 19:53:50 -04:00
|
|
|
|
2024-01-30 11:29:07 -05:00
|
|
|
export default <Child extends Gtk.Widget, Attr>(
|
2024-01-29 18:54:07 -05:00
|
|
|
props: PopupWindowProps<Child, Attr>,
|
|
|
|
) => new PopupWindow(props);
|