Files
custum-hyprpanel/modules/notifications/index.ts
Rubin Bhandari b88644833d feat: on hover only show actions if available on notifications (#396)
* feat: on hover only show actions if available on notifications

* feat: on hover only show actions if available on notifications

* fix: make the change configurable

* fix: remove unneeded op
2024-11-04 22:47:26 -08:00

106 lines
4.8 KiB
TypeScript

const notifs = await Service.import('notifications');
import options from 'options';
import { notifHasImg } from '../menus/notifications/utils.js';
import { Image } from './image/index.js';
import { Action } from './actions/index.js';
import { Header } from './header/index.js';
import { Body } from './body/index.js';
import { CloseButton } from './close/index.js';
import { getPosition } from 'lib/utils.js';
import { filterNotifications } from 'lib/shared/notifications.js';
import { Notification } from 'types/service/notifications.js';
import Window from 'types/widgets/window.js';
import Box from 'types/widgets/box.js';
import { Attribute, Child } from 'lib/types/widget.js';
const hyprland = await Service.import('hyprland');
const { position, timeout, cache_actions, monitor, active_monitor, displayedTotal, ignore, showActionsOnHover } =
options.notifications;
const curMonitor = Variable(monitor.value);
hyprland.active.connect('changed', () => {
curMonitor.value = hyprland.active.monitor.id;
});
export default (): Window<Box<Child, Attribute>, unknown> => {
Utils.merge([timeout.bind('value'), cache_actions.bind('value')], (timeout, doCaching) => {
notifs.popupTimeout = timeout;
notifs.cacheActions = doCaching;
});
return Widget.Window({
name: 'notifications-window',
class_name: 'notifications-window',
monitor: Utils.merge(
[curMonitor.bind('value'), monitor.bind('value'), active_monitor.bind('value')],
(curMon, mon, activeMonitor) => {
if (activeMonitor === true) {
return curMon;
}
return mon;
},
),
layer: options.tear.bind('value').as((tear) => (tear ? 'top' : 'overlay')),
anchor: position.bind('value').as((v) => getPosition(v)),
exclusivity: 'normal',
child: Widget.Box({
class_name: 'notification-card-container',
vertical: true,
hexpand: true,
setup: (self) => {
Utils.merge(
[notifs.bind('popups'), ignore.bind('value'), showActionsOnHover.bind('value')],
(notifications: Notification[], ignoredNotifs: string[], showActions: boolean) => {
const filteredNotifications = filterNotifications(notifications, ignoredNotifs);
return (self.children = filteredNotifications.slice(0, displayedTotal.value).map((notif) => {
const actionsbox =
notif.actions.length > 0
? Widget.Revealer({
transition: 'slide_down',
reveal_child: showActions ? false : true,
child: Widget.EventBox({
child: Action(notif, notifs),
}),
})
: null;
return Widget.EventBox({
on_secondary_click: () => {
notifs.CloseNotification(notif.id);
},
on_hover() {
if (actionsbox && showActions) actionsbox.reveal_child = true;
},
on_hover_lost() {
if (actionsbox && showActions) actionsbox.reveal_child = false;
},
child: Widget.Box({
class_name: 'notification-card',
vpack: 'start',
hexpand: true,
children: [
Image(notif),
Widget.Box({
vpack: 'start',
vertical: true,
hexpand: true,
class_name: `notification-card-content ${!notifHasImg(notif) ? 'noimg' : ''}`,
children: actionsbox
? [Header(notif), Body(notif), actionsbox]
: [Header(notif), Body(notif)],
}),
CloseButton(notif, notifs),
],
}),
});
}));
},
);
},
}),
});
};