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-04-10 21:04:19 -04:00
|
|
|
/* Types */
|
2024-01-29 18:54:07 -05:00
|
|
|
import { Window } from 'resource:///com/github/Aylur/ags/widgets/window.js';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CloseType,
|
2024-04-07 14:29:12 -04:00
|
|
|
HyprTransition,
|
2024-01-29 18:54:07 -05:00
|
|
|
PopupWindowProps,
|
|
|
|
} from 'global-types';
|
2023-12-18 23:20:32 -05:00
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
|
|
|
|
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 {
|
2024-04-10 21:04:19 -04:00
|
|
|
Widget.register(this, {
|
2024-01-29 18:54:07 -05:00
|
|
|
properties: {
|
2024-04-10 21:04:19 -04:00
|
|
|
close_on_unfocus: ['string', 'rw'],
|
|
|
|
transition: ['string', 'rw'],
|
2024-01-29 18:54:07 -05:00
|
|
|
},
|
2024-01-02 18:37:12 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
#close_on_unfocus: CloseType;
|
2024-04-07 14:29:12 -04:00
|
|
|
#transition: HyprTransition;
|
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-04-07 14:29:12 -04:00
|
|
|
set close_on_unfocus(value: CloseType) {
|
2024-01-29 18:54:07 -05:00
|
|
|
this.#close_on_unfocus = value;
|
|
|
|
}
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-04-07 14:29:12 -04:00
|
|
|
get transition() {
|
|
|
|
return this.#transition;
|
|
|
|
}
|
|
|
|
|
|
|
|
set transition(t: HyprTransition) {
|
|
|
|
this.#transition = t;
|
|
|
|
Hyprland.messageAsync(`keyword layerrule animation ${t}, ${this.name}`);
|
|
|
|
}
|
|
|
|
|
2024-01-29 18:54:07 -05:00
|
|
|
constructor({
|
2024-04-07 14:29:12 -04:00
|
|
|
transition = 'slide top',
|
2024-01-29 18:54:07 -05:00
|
|
|
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',
|
|
|
|
close_on_unfocus = 'released',
|
|
|
|
...rest
|
|
|
|
}: PopupWindowProps<Child, Attr>) {
|
|
|
|
super({
|
|
|
|
...rest,
|
2024-04-07 14:29:12 -04:00
|
|
|
name: `win-${name}`,
|
2024-01-29 18:54:07 -05:00
|
|
|
visible,
|
|
|
|
anchor,
|
|
|
|
layer,
|
|
|
|
setup: () => {
|
|
|
|
const id = App.connect('config-parsed', () => {
|
2024-02-05 10:37:56 -05:00
|
|
|
// Add way to make window open on startup
|
2024-01-29 18:54:07 -05:00
|
|
|
if (visible) {
|
2024-04-07 14:29:12 -04:00
|
|
|
App.openWindow(`win-${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-04-07 14:29:12 -04:00
|
|
|
Hyprland.messageAsync(
|
|
|
|
`keyword layerrule animation ${transition}, win-${name}`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2024-01-02 18:37:12 -05:00
|
|
|
|
2024-04-07 14:29:12 -04:00
|
|
|
this.hook(App, (_, currentName, isOpen) => {
|
|
|
|
if (currentName === `win-${name}`) {
|
|
|
|
if (isOpen) {
|
|
|
|
on_open(this);
|
|
|
|
}
|
|
|
|
else {
|
2024-04-10 21:04:19 -04:00
|
|
|
on_close(this);
|
2024-04-07 14:29:12 -04:00
|
|
|
}
|
|
|
|
}
|
2024-01-29 18:54:07 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.#close_on_unfocus = close_on_unfocus;
|
2024-04-07 14:29:12 -04:00
|
|
|
this.#transition = transition;
|
2024-01-29 18:54:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|