calls-in-app-notification: migrate to AdwToastOverlay

Part-of: <https://gitlab.gnome.org/GNOME/calls/-/merge_requests/714>
This commit is contained in:
Anton Lazarev
2024-01-08 11:46:14 -08:00
parent 915b855ec8
commit 76697050e8
10 changed files with 20 additions and 238 deletions

View File

@@ -26,166 +26,14 @@
#define DEFAULT_TIMEOUT_SECONDS 3
struct _CallsInAppNotification {
GtkWidget parent_instance;
GtkRevealer *revealer;
GtkLabel *label;
guint timeout;
guint timeout_id;
};
G_DEFINE_TYPE (CallsInAppNotification, calls_in_app_notification, GTK_TYPE_WIDGET)
enum {
PROP_0,
PROP_TIMEOUT,
PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];
static gboolean
timeout_cb (CallsInAppNotification *self)
{
calls_in_app_notification_hide (self);
return G_SOURCE_REMOVE;
}
static void
calls_in_app_notification_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
CallsInAppNotification *self = CALLS_IN_APP_NOTIFICATION (object);
switch (property_id) {
case PROP_TIMEOUT:
g_value_set_uint (value, self->timeout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
calls_in_app_notification_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
CallsInAppNotification *self = CALLS_IN_APP_NOTIFICATION (object);
switch (property_id) {
case PROP_TIMEOUT:
self->timeout = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
calls_in_app_notification_dispose (GObject *object)
{
CallsInAppNotification *self = CALLS_IN_APP_NOTIFICATION (object);
GtkWidget *revealer = GTK_WIDGET (self->revealer);
g_clear_pointer (&revealer, gtk_widget_unparent);
G_OBJECT_CLASS (calls_in_app_notification_parent_class)->dispose (object);
}
static void
calls_in_app_notification_finalize (GObject *object)
{
CallsInAppNotification *self = CALLS_IN_APP_NOTIFICATION (object);
g_clear_handle_id (&self->timeout_id, g_source_remove);
G_OBJECT_CLASS (calls_in_app_notification_parent_class)->finalize (object);
}
static void
calls_in_app_notification_class_init (CallsInAppNotificationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->get_property = calls_in_app_notification_get_property;
object_class->set_property = calls_in_app_notification_set_property;
object_class->dispose = calls_in_app_notification_dispose;
object_class->finalize = calls_in_app_notification_finalize;
props[PROP_TIMEOUT] = g_param_spec_uint ("timeout",
"Timeout",
"The time the in-app notifaction should be shown",
1,
G_MAXUINT,
DEFAULT_TIMEOUT_SECONDS,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Calls/ui/in-app-notification.ui");
gtk_widget_class_bind_template_child (widget_class, CallsInAppNotification, revealer);
gtk_widget_class_bind_template_child (widget_class, CallsInAppNotification, label);
gtk_widget_class_bind_template_callback (widget_class, calls_in_app_notification_hide);
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT);
}
static void
calls_in_app_notification_init (CallsInAppNotification *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}
CallsInAppNotification *
calls_in_app_notification_new (void)
{
return g_object_new (CALLS_TYPE_IN_APP_NOTIFICATION, NULL);
}
void
calls_in_app_notification_show (CallsInAppNotification *self, const gchar *message)
calls_in_app_notification_show (AdwToastOverlay *overlay, const gchar *message)
{
g_return_if_fail (CALLS_IS_IN_APP_NOTIFICATION (self));
AdwToast* toast;
gtk_label_set_text (self->label, message);
g_return_if_fail (ADW_IS_TOAST_OVERLAY (overlay));
if (self->timeout_id)
g_source_remove (self->timeout_id);
gtk_revealer_set_reveal_child (self->revealer, TRUE);
self->timeout_id = g_timeout_add_seconds (self->timeout, (GSourceFunc) timeout_cb, self);
}
void
calls_in_app_notification_hide (CallsInAppNotification *self)
{
g_return_if_fail (CALLS_IS_IN_APP_NOTIFICATION (self));
g_clear_handle_id (&self->timeout_id, g_source_remove);
gtk_revealer_set_reveal_child (self->revealer, FALSE);
toast = adw_toast_new (message);
adw_toast_set_timeout (toast, DEFAULT_TIMEOUT_SECONDS);
adw_toast_overlay_add_toast (overlay, toast);
}