feat: add Linux support for Live Notifications toasts w/ libnotify (#5881)

This commit is contained in:
cmp
2025-02-08 13:06:48 -06:00
committed by GitHub
parent e4ae092b6f
commit 2656fd0d6b
15 changed files with 95 additions and 16 deletions
+7
View File
@@ -1062,6 +1062,13 @@ if (UNIX)
message(STATUS "Linking with CMake DL libs: '${CMAKE_DL_LIBS}'")
target_link_libraries(${LIBRARY_PROJECT} PUBLIC ${CMAKE_DL_LIBS})
endif ()
if (NOT APPLE AND BUILD_WITH_LIBNOTIFY)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBNOTIFY REQUIRED IMPORTED_TARGET libnotify)
target_link_libraries(${LIBRARY_PROJECT} PRIVATE PkgConfig::LIBNOTIFY)
target_compile_definitions(${LIBRARY_PROJECT} PUBLIC CHATTERINO_WITH_LIBNOTIFY)
endif ()
endif ()
if (WIN32)
@@ -5,6 +5,7 @@
#include "common/Env.hpp"
#include "common/Literals.hpp"
#include "controllers/commands/CommandContext.hpp"
#include "controllers/notifications/NotificationController.hpp"
#include "messages/Image.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
@@ -13,6 +14,7 @@
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Theme.hpp"
#include "singletons/Toasts.hpp"
#include "util/PostToThread.hpp"
#include <QApplication>
@@ -180,6 +182,15 @@ QString debugTest(const CommandContext &ctx)
.arg(nowMillis);
getApp()->getTwitch()->addFakeMessage(ircText);
}
else if (command == "desktop-notify")
{
auto title = ctx.twitchChannel->accessStreamStatus()->title;
getApp()->getToasts()->sendChannelNotification(
ctx.twitchChannel->getName(), title, Platform::Twitch);
ctx.channel->addSystemMessage(
QString("debug-test sent desktop notification"));
}
else
{
ctx.channel->addSystemMessage(
+51 -6
View File
@@ -16,6 +16,8 @@
#ifdef Q_OS_WIN
# include <wintoastlib.h>
#elif defined(CHATTERINO_WITH_LIBNOTIFY)
# include <libnotify/notify.h>
#endif
#include <QDesktopServices>
@@ -77,18 +79,25 @@ Toasts::~Toasts()
{
WinToast::instance()->clear();
}
#elif defined(CHATTERINO_WITH_LIBNOTIFY)
if (this->initialized_)
{
notify_uninit();
}
#endif
}
bool Toasts::isEnabled()
{
auto enabled = getSettings()->notificationToast &&
!(getApp()->getStreamerMode()->isEnabled() &&
getSettings()->streamerModeSuppressLiveNotifications);
#ifdef Q_OS_WIN
return WinToast::isCompatible() && getSettings()->notificationToast &&
!(getApp()->getStreamerMode()->isEnabled() &&
getSettings()->streamerModeSuppressLiveNotifications);
#else
return false;
enabled = enabled && WinToast::isCompatible();
#endif
return enabled;
}
QString Toasts::findStringFromReaction(const ToastReaction &reaction)
@@ -123,10 +132,14 @@ void Toasts::sendChannelNotification(const QString &channelName,
auto sendChannelNotification = [this, channelName, channelTitle, p] {
this->sendWindowsNotification(channelName, channelTitle, p);
};
#elif defined(CHATTERINO_WITH_LIBNOTIFY)
auto sendChannelNotification = [this, channelName, channelTitle] {
this->sendLibnotify(channelName, channelTitle);
};
#else
(void)channelTitle;
auto sendChannelNotification = [] {
// Unimplemented for macOS and Linux
// Unimplemented for macOS
};
#endif
// Fetch user profile avatar
@@ -282,6 +295,38 @@ void Toasts::sendWindowsNotification(const QString &channelName,
}
}
#elif defined(CHATTERINO_WITH_LIBNOTIFY)
void Toasts::ensureInitialized()
{
if (this->initialized_)
{
return;
}
auto result = notify_init("chatterino2");
if (result == 0)
{
qCWarning(chatterinoNotification) << "Failed to initialize libnotify";
}
this->initialized_ = true;
}
void Toasts::sendLibnotify(const QString &channelName,
const QString &channelTitle)
{
this->ensureInitialized();
qCDebug(chatterinoNotification) << "sending to libnotify";
QString str = channelName % u" is live!";
NotifyNotification *notif = notify_notification_new(
str.toUtf8().constData(), channelTitle.toUtf8().constData(), nullptr);
notify_notification_show(notif, nullptr);
g_object_unref(notif);
}
#endif
} // namespace chatterino
+5
View File
@@ -35,6 +35,11 @@ private:
void sendWindowsNotification(const QString &channelName,
const QString &channelTitle, Platform p);
bool initialized_ = false;
#elif defined(CHATTERINO_WITH_LIBNOTIFY)
void ensureInitialized();
void sendLibnotify(const QString &channelName, const QString &channelTitle);
bool initialized_ = false;
#endif
};
@@ -46,9 +46,11 @@ NotificationPage::NotificationPage()
settings.append(this->createCheckBox(
"Suppress live notifications on startup",
getSettings()->suppressInitialLiveNotification));
#ifdef Q_OS_WIN
#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