commit d533c72c5729ae7715b464f88e169b7993e77eb7
parent 8ef26852ac4da54e6784e738452346c4f9873c02
Author: TheJackiMonster <thejackimonster@gmail.com>
Date: Fri, 11 Feb 2022 00:12:25 +0100
Added search filter to chats listing
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat:
4 files changed, 77 insertions(+), 16 deletions(-)
diff --git a/resources/ui/messenger.ui b/resources/ui/messenger.ui
@@ -747,10 +747,6 @@ Author: Tobias Frisch
</packing>
</child>
</object>
- <packing>
- <property name="name">page0</property>
- <property name="title" translatable="yes">page0</property>
- </packing>
</child>
</object>
<packing>
diff --git a/src/event.c b/src/event.c
@@ -91,15 +91,9 @@ _add_new_chat_entry(MESSENGER_Application *app,
gtk_container_add(GTK_CONTAINER(ui->chats_listbox), entry->entry_box);
GNUNET_CHAT_context_set_user_pointer(context, entry);
- char context_id [9];
- g_snprintf(context_id, sizeof(context_id), "%08lx", (gulong) context);
-
- gtk_widget_set_name(entry->entry_box, context_id);
-
- gtk_stack_add_named(
- ui->chats_stack,
- entry->chat->chat_box,
- context_id
+ gtk_container_add(
+ GTK_CONTAINER(ui->chats_stack),
+ entry->chat->chat_box
);
g_hash_table_insert(
@@ -114,7 +108,15 @@ _add_new_chat_entry(MESSENGER_Application *app,
gtk_widget_get_parent(entry->entry_box)
);
+ g_hash_table_insert(
+ app->ui.bindings,
+ row,
+ entry
+ );
+
gtk_list_box_select_row(ui->chats_listbox, row);
+ gtk_list_box_invalidate_filter(ui->chats_listbox);
+
gtk_widget_activate(GTK_WIDGET(row));
}
diff --git a/src/ui/messenger.c b/src/ui/messenger.c
@@ -161,6 +161,16 @@ handle_chats_listbox_row_activated(UNUSED GtkListBox* listbox,
{
UI_MESSENGER_Handle *handle = (UI_MESSENGER_Handle*) user_data;
+ if (!gtk_list_box_row_get_selectable(row))
+ return;
+
+ UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) g_hash_table_lookup(
+ handle->bindings, row
+ );
+
+ if ((!entry) || (!(entry->chat)) || (!(entry->chat->chat_box)))
+ return;
+
GtkStack *stack = handle->chats_stack;
HdyLeaflet *leaflet = handle->leaflet_chat;
@@ -170,11 +180,48 @@ handle_chats_listbox_row_activated(UNUSED GtkListBox* listbox,
hdy_leaflet_set_visible_child(leaflet, GTK_WIDGET(children->next->data));
}
- GtkWidget *entry = GTK_WIDGET(
- gtk_container_get_children(GTK_CONTAINER(row))->data
+ gtk_stack_set_visible_child(stack, entry->chat->chat_box);
+}
+
+static gboolean
+handle_chats_listbox_filter_func(GtkListBoxRow *row,
+ gpointer user_data)
+{
+ UI_MESSENGER_Handle *handle = (UI_MESSENGER_Handle*) user_data;
+
+ if ((!gtk_list_box_row_get_selectable(row)) ||
+ (gtk_list_box_row_is_selected(row)))
+ return TRUE;
+
+ const gchar *filter = gtk_entry_get_text(
+ GTK_ENTRY(handle->chats_search)
);
- gtk_stack_set_visible_child_name(stack, gtk_widget_get_name(entry));
+ if (!filter)
+ return TRUE;
+
+ UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) g_hash_table_lookup(
+ handle->bindings, row
+ );
+
+ if (!entry)
+ return FALSE;
+
+ const gchar *title = gtk_label_get_text(entry->title_label);
+
+ if (!title)
+ return FALSE;
+
+ return g_str_match_string(filter, title, TRUE);
+}
+
+static void
+handle_chats_search_changed(UNUSED GtkSearchEntry *search,
+ gpointer user_data)
+{
+ GtkListBox *listbox = GTK_LIST_BOX(user_data);
+
+ gtk_list_box_invalidate_filter(listbox);
}
static void
@@ -193,6 +240,7 @@ ui_messenger_init(MESSENGER_Application *app,
UI_MESSENGER_Handle *handle)
{
handle->chat_entries = NULL;
+ handle->bindings = app->ui.bindings;
handle->builder = gtk_builder_new_from_resource(
application_get_resource_path(app, "ui/messenger.ui")
@@ -377,6 +425,20 @@ ui_messenger_init(MESSENGER_Application *app,
gtk_builder_get_object(handle->builder, "chats_listbox")
);
+ gtk_list_box_set_filter_func(
+ handle->chats_listbox,
+ handle_chats_listbox_filter_func,
+ handle,
+ NULL
+ );
+
+ g_signal_connect(
+ handle->chats_search,
+ "search-changed",
+ G_CALLBACK(handle_chats_search_changed),
+ handle->chats_listbox
+ );
+
g_signal_connect(
handle->chats_listbox,
"row-activated",
diff --git a/src/ui/messenger.h b/src/ui/messenger.h
@@ -36,6 +36,7 @@ typedef struct MESSENGER_Application MESSENGER_Application;
typedef struct UI_MESSENGER_Handle
{
GList *chat_entries;
+ GHashTable *bindings;
GtkBuilder *builder;
GtkApplicationWindow *main_window;