summaryrefslogtreecommitdiff
path: root/csrc/notification-area.c
blob: 2db53977b2a00c10c14cf8dcee5dacbbc755d776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "notification-area.h"
#include "widgets/notification-widget.h"
#include <gtk4-layer-shell.h>

//
// FoobarNotificationArea:
//
// A (usually invisible) window in the corner of the screen, displaying incoming notifications before they are either
// dismissed or a timeout has passed.
//

struct _FoobarNotificationArea
{
	GtkWindow                   parent_instance;
	GtkWidget*                  notification_list;
	FoobarNotificationService*  notification_service;
	FoobarConfigurationService* configuration_service;
	gint                        min_height;
	gint                        spacing;
	gint                        close_button_inset;
	gchar*                      time_format;
	gulong                      config_handler_id;
};

static void foobar_notification_area_class_init          ( FoobarNotificationAreaClass* klass );
static void foobar_notification_area_init                ( FoobarNotificationArea*      self );
static void foobar_notification_area_finalize            ( GObject*                     object );
static void foobar_notification_area_handle_config_change( GObject*                     object,
                                                           GParamSpec*                  pspec,
                                                           gpointer                     userdata );
static void foobar_notification_area_handle_item_setup   ( GtkListItemFactory*          factory,
                                                           GtkListItem*                 list_item,
                                                           gpointer                     userdata );

G_DEFINE_FINAL_TYPE( FoobarNotificationArea, foobar_notification_area, GTK_TYPE_WINDOW )

// ---------------------------------------------------------------------------------------------------------------------
// Window Implementation
// ---------------------------------------------------------------------------------------------------------------------

//
// Static initialization for the notification area.
//
void foobar_notification_area_class_init( FoobarNotificationAreaClass* klass )
{
	GObjectClass* object_klass = G_OBJECT_CLASS( klass );
	object_klass->finalize = foobar_notification_area_finalize;
}

//
// Instance initialization for the notification area.
//
void foobar_notification_area_init( FoobarNotificationArea* self )
{
	GtkListItemFactory* notification_factory = gtk_signal_list_item_factory_new( );
	g_signal_connect( notification_factory, "setup", G_CALLBACK( foobar_notification_area_handle_item_setup ), self );
	self->notification_list = gtk_list_view_new( NULL, notification_factory );

	gtk_window_set_child( GTK_WINDOW( self ), self->notification_list );
	gtk_window_set_title( GTK_WINDOW( self ), "Foobar Notification Area" );
	gtk_widget_add_css_class( GTK_WIDGET( self ), "notification-area" );
	gtk_layer_init_for_window( GTK_WINDOW( self ) );
	gtk_layer_set_namespace( GTK_WINDOW( self ), "foobar-notification-area" );
	gtk_layer_set_anchor( GTK_WINDOW( self ), GTK_LAYER_SHELL_EDGE_RIGHT, TRUE );
	gtk_layer_set_anchor( GTK_WINDOW( self ), GTK_LAYER_SHELL_EDGE_TOP, TRUE );
}

//
// Instance cleanup for the notification area.
//
void foobar_notification_area_finalize( GObject* object )
{
	FoobarNotificationArea* self = (FoobarNotificationArea*)object;

	g_clear_signal_handler( &self->config_handler_id, self->configuration_service );
	g_clear_object( &self->notification_service );
	g_clear_object( &self->configuration_service );
	g_clear_pointer( &self->time_format, g_free );

	G_OBJECT_CLASS( foobar_notification_area_parent_class )->finalize( object );
}

// ---------------------------------------------------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------------------------------------------------

//
// Create a new notification area instance.
//
FoobarNotificationArea* foobar_notification_area_new(
	FoobarNotificationService*  notification_service,
	FoobarConfigurationService* configuration_service )
{
	g_return_val_if_fail( FOOBAR_IS_NOTIFICATION_SERVICE( notification_service ), NULL );
	g_return_val_if_fail( FOOBAR_IS_CONFIGURATION_SERVICE( configuration_service ), NULL );

	FoobarNotificationArea* self = g_object_new( FOOBAR_TYPE_NOTIFICATION_AREA, NULL );
	self->notification_service = g_object_ref( notification_service );
	self->configuration_service = g_object_ref( configuration_service );

	// Apply the configuration and subscribe to changes.

	FoobarConfiguration const* config = foobar_configuration_service_get_current( self->configuration_service );
	foobar_notification_area_apply_configuration( self, foobar_configuration_get_notifications( config ) );
	self->config_handler_id = g_signal_connect(
		self->configuration_service,
		"notify::current",
		G_CALLBACK( foobar_notification_area_handle_config_change ),
		self );

	// Set up the notifications list view.

	GListModel* source_model = foobar_notification_service_get_popup_notifications( self->notification_service );
	GtkNoSelection* selection_model = gtk_no_selection_new( g_object_ref( source_model ) );
	gtk_list_view_set_model( GTK_LIST_VIEW( self->notification_list ), GTK_SELECTION_MODEL( selection_model ) );

	return self;
}

//
// Apply the notification configuration provided by the configuration service.
//
void foobar_notification_area_apply_configuration(
	FoobarNotificationArea*                self,
	FoobarNotificationConfiguration const* config )
{
	g_return_if_fail( FOOBAR_IS_NOTIFICATION_AREA( self ) );
	g_return_if_fail( config != NULL );

	// Copy configuration into member variables.

	gint width = foobar_notification_configuration_get_width( config );
	self->min_height = foobar_notification_configuration_get_min_height( config );
	self->spacing = foobar_notification_configuration_get_spacing( config );
	self->close_button_inset = foobar_notification_configuration_get_close_button_inset( config );
	g_clear_pointer( &self->time_format, g_free );
	self->time_format = g_strdup( foobar_notification_configuration_get_time_format( config ) );

	// Recreate list items by resetting the factory.

	GtkListItemFactory* factory = gtk_list_view_get_factory( GTK_LIST_VIEW( self->notification_list ) );
	g_object_ref( factory );
	gtk_list_view_set_factory( GTK_LIST_VIEW( self->notification_list ), NULL );
	gtk_list_view_set_factory( GTK_LIST_VIEW( self->notification_list ), factory );
	g_object_unref( factory );

	// We need to use height 2, otherwise it crashes for some reason.

	gtk_window_set_default_size( GTK_WINDOW( self ), width + self->spacing, 2 );
}

// ---------------------------------------------------------------------------------------------------------------------
// Signal Handlers
// ---------------------------------------------------------------------------------------------------------------------

//
// Signal handler called when the global configuration file has changed.
//
void foobar_notification_area_handle_config_change(
	GObject*    object,
	GParamSpec* pspec,
	gpointer    userdata )
{
	(void)object;
	(void)pspec;
	FoobarNotificationArea* self = (FoobarNotificationArea*)userdata;

	FoobarConfiguration const* config = foobar_configuration_service_get_current( self->configuration_service );
	foobar_notification_area_apply_configuration( self, foobar_configuration_get_notifications( config ) );
}

//
// Called by the notification list view to create a widget for displaying a notification.
//
void foobar_notification_area_handle_item_setup(
	GtkListItemFactory* factory,
	GtkListItem*        list_item,
	gpointer            userdata )
{
	(void)factory;
	FoobarNotificationArea* self = (FoobarNotificationArea*)userdata;

	GtkWidget* widget = foobar_notification_widget_new( );
	foobar_notification_widget_set_close_action( FOOBAR_NOTIFICATION_WIDGET( widget ), FOOBAR_TYPE_NOTIFICATION_CLOSE_ACTION_DISMISS );
	foobar_notification_widget_set_time_format( FOOBAR_NOTIFICATION_WIDGET( widget ), self->time_format );
	foobar_notification_widget_set_min_height( FOOBAR_NOTIFICATION_WIDGET( widget ), self->min_height );
	foobar_notification_widget_set_close_button_inset( FOOBAR_NOTIFICATION_WIDGET( widget ), self->close_button_inset );
	foobar_notification_widget_set_inset_end( FOOBAR_NOTIFICATION_WIDGET( widget ), self->spacing );
	foobar_notification_widget_set_inset_top( FOOBAR_NOTIFICATION_WIDGET( widget ), self->spacing );
	gtk_list_item_set_child( list_item, widget );

	{
		GtkExpression* item_expr = gtk_property_expression_new( GTK_TYPE_LIST_ITEM, NULL, "item" );
		gtk_expression_bind( item_expr, widget, "notification", list_item );
	}
}