Skip to content

Commit

Permalink
linux notification actions
Browse files Browse the repository at this point in the history
  • Loading branch information
4rneee committed Feb 22, 2025
1 parent f540be3 commit ad171a5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
57 changes: 56 additions & 1 deletion src/singletons/Toasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,47 @@ class AvatarDownloader : public QObject
void downloadComplete();
};

#ifdef CHATTERINO_WITH_LIBNOTIFY
void onAction(NotifyNotification *notif, const char *action, void *user_data)
{
auto *channelName = static_cast<QString *>(user_data);
auto toastReaction =
static_cast<ToastReaction>(getSettings()->openFromToast.getValue());

switch (toastReaction)
{
case ToastReaction::OpenInBrowser:
QDesktopServices::openUrl(
QUrl(u"https://www.twitch.tv/" % *channelName));
break;
case ToastReaction::OpenInPlayer:
QDesktopServices::openUrl(
QUrl(TWITCH_PLAYER_URL.arg(*channelName)));
break;
case ToastReaction::OpenInStreamlink: {
openStreamlinkForChannel(*channelName);
break;
}
case ToastReaction::DontOpen:
// nothing should happen
break;
}

notify_notification_close(notif, nullptr);
}

void onActionClosed(NotifyNotification *notif, void * /*user_data*/)
{
g_object_unref(notif);
}

void onActionDestroyed(void *data)
{
auto *channelNameHeap = static_cast<QString *>(data);
delete channelNameHeap;
}
#endif

} // namespace

namespace chatterino {
Expand Down Expand Up @@ -310,6 +351,19 @@ void Toasts::sendLibnotify(const QString &channelName,
NotifyNotification *notif = notify_notification_new(
str.toUtf8().constData(), channelTitle.toUtf8().constData(), nullptr);

auto toastReaction =
static_cast<ToastReaction>(getSettings()->openFromToast.getValue());

if (toastReaction != ToastReaction::DontOpen)
{
auto *channelNameHeap = new QString(channelName);

notify_notification_add_action(
notif, "default",
Toasts::findStringFromReaction(toastReaction).toUtf8().constData(),
(NotifyActionCallback)onAction, channelNameHeap, onActionDestroyed);
}

GdkPixbuf *img = gdk_pixbuf_new_from_file(
avatarFilePath(channelName).toUtf8().constData(), nullptr);
if (img == nullptr)
Expand All @@ -322,8 +376,9 @@ void Toasts::sendLibnotify(const QString &channelName,
g_object_unref(img);
}

g_signal_connect(notif, "closed", (GCallback)onActionClosed, nullptr);

notify_notification_show(notif, nullptr);
g_object_unref(notif);
}
#endif

Expand Down
3 changes: 1 addition & 2 deletions src/widgets/settingspages/NotificationPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ NotificationPage::NotificationPage()
#if defined(Q_OS_WIN) || defined(CHATTERINO_WITH_LIBNOTIFY)
settings.append(this->createCheckBox(
"Show notification", getSettings()->notificationToast));
#endif
#ifdef Q_OS_WIN

auto openIn = settings.emplace<QHBoxLayout>().withoutMargin();
{
openIn
Expand Down

0 comments on commit ad171a5

Please sign in to comment.