1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:47:46 +00:00

Notification: Use RefPtr for connection

With this RefPtr, we can initialize the connection to the
NotificationServer upon showing the notification. With this, we can
prevent double shows and updates or closes before showing.
This commit is contained in:
Nick Johnson 2021-03-12 11:53:51 -06:00 committed by Andreas Kling
parent e17752fc91
commit c7f00717c3
2 changed files with 11 additions and 47 deletions

View file

@ -54,7 +54,6 @@ private:
};
Notification::Notification()
: m_connection(NotificationServerConnection::construct())
{
}
@ -64,73 +63,40 @@ Notification::~Notification()
void Notification::show()
{
VERIFY(!m_showing);
VERIFY(!m_disposed);
if (!m_connection->is_connected()) {
// This would imply that the NotificationServer crashed before we could send it any data.
VERIFY_NOT_REACHED();
}
VERIFY(!m_connection);
auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
m_connection = NotificationServerConnection::construct();
m_connection->send_sync<Messages::NotificationServer::ShowNotification>(m_text, m_title, icon);
m_showing = true;
}
void Notification::close()
{
VERIFY(m_showing);
if (m_connection->is_connected()) {
m_connection->send_sync<Messages::NotificationServer::CloseNotification>();
VERIFY(m_connection);
if (!m_connection->is_connected()) {
return;
}
m_showing = false;
m_disposed = true;
m_connection->send_sync<Messages::NotificationServer::CloseNotification>();
}
bool Notification::update()
{
VERIFY(m_showing);
VERIFY(!m_disposed);
VERIFY(m_connection);
if (!m_connection->is_connected()) {
m_showing = false;
m_disposed = true;
return false;
}
bool is_checked = false;
if (m_text_dirty || m_title_dirty) {
auto response = m_connection->send_sync<Messages::NotificationServer::UpdateNotificationText>(m_text, m_title);
m_connection->send_sync<Messages::NotificationServer::UpdateNotificationText>(m_text, m_title);
m_text_dirty = false;
m_title_dirty = false;
is_checked = true;
if (!response->still_showing()) {
m_showing = false;
m_disposed = true;
return false;
}
}
if (m_icon_dirty) {
auto response = m_connection->send_sync<Messages::NotificationServer::UpdateNotificationIcon>(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
m_connection->send_sync<Messages::NotificationServer::UpdateNotificationIcon>(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
m_icon_dirty = false;
is_checked = true;
if (!response->still_showing()) {
m_showing = false;
m_disposed = true;
return false;
}
}
if (!is_checked) {
auto response = m_connection->send_sync<Messages::NotificationServer::IsShowing>();
m_showing = response->still_showing();
if (!m_showing) {
m_disposed = true;
}
}
return m_showing;
return true;
}
}