From 412c7d4f210096c7cde549c48e3e2a2262724d00 Mon Sep 17 00:00:00 2001 From: TheJackiMonster Date: Tue, 21 Dec 2021 22:31:34 +0100 Subject: Added automatic scrolling to chats Signed-off-by: TheJackiMonster --- resources/ui/chat.ui | 18 ++++-------------- resources/ui/message_content.ui | 2 +- src/chat/messenger.c | 1 + src/event.c | 4 +++- src/ui/chat.c | 35 +++++++++++++++++++++++++++++++++++ src/ui/chat.h | 3 +++ src/ui/message.c | 33 +++++++++++++++++++++++++++++++++ src/ui/message.h | 8 ++++++++ 8 files changed, 88 insertions(+), 16 deletions(-) diff --git a/resources/ui/chat.ui b/resources/ui/chat.ui index 8d9cb02..c70eb40 100644 --- a/resources/ui/chat.ui +++ b/resources/ui/chat.ui @@ -145,7 +145,7 @@ Author: Tobias Frisch - + True True @@ -153,21 +153,11 @@ Author: Tobias Frisch True False - + True False - - - True - False - none - False - - - page0 - page0 - - + none + False diff --git a/resources/ui/message_content.ui b/resources/ui/message_content.ui index bca5165..a59c59e 100644 --- a/resources/ui/message_content.ui +++ b/resources/ui/message_content.ui @@ -71,7 +71,7 @@ Author: Tobias Frisch - + True False diff --git a/src/chat/messenger.c b/src/chat/messenger.c index c7eb033..ea8b450 100644 --- a/src/chat/messenger.c +++ b/src/chat/messenger.c @@ -146,6 +146,7 @@ _chat_messenger_message(void *cls, break; } case GNUNET_CHAT_KIND_TEXT: + case GNUNET_CHAT_KIND_FILE: { application_call_message_event( app, diff --git a/src/event.c b/src/event.c index 2280785..a99f13d 100644 --- a/src/event.c +++ b/src/event.c @@ -304,11 +304,13 @@ event_receive_message(UNUSED MESSENGER_Application *app, if (!handle) return; + struct GNUNET_CHAT_File *file = GNUNET_CHAT_message_get_file(msg); + const int sent = GNUNET_CHAT_message_is_sent(msg); UI_MESSAGE_Handle *message = ui_message_new( GNUNET_YES == sent? UI_MESSAGE_SENT : UI_MESSAGE_DEFAULT, - UI_MESSAGE_CONTENT_TEXT + file? UI_MESSAGE_CONTENT_FILE : UI_MESSAGE_CONTENT_TEXT ); const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( diff --git a/src/ui/chat.c b/src/ui/chat.c index 42ccb9e..5dd11f6 100644 --- a/src/ui/chat.c +++ b/src/ui/chat.c @@ -94,6 +94,29 @@ handle_chat_contacts_listbox_row_activated(GtkListBox *listbox, GNUNET_CHAT_context_request(context); } +static void +handle_chat_messages_listbox_size_allocate(UNUSED GtkWidget *widget, + UNUSED GdkRectangle *allocation, + gpointer user_data) +{ + UI_CHAT_Handle *handle = (UI_CHAT_Handle*) user_data; + + GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment( + handle->chat_scrolled_window + ); + + const gdouble value = gtk_adjustment_get_value(adjustment); + const gdouble upper = gtk_adjustment_get_upper(adjustment); + const gdouble page_size = gtk_adjustment_get_page_size(adjustment); + + const gdouble edge_value = upper - page_size; + + if (value >= handle->edge_value) + gtk_adjustment_set_value(adjustment, edge_value); + + handle->edge_value = upper - page_size; +} + static void handle_back_button_click(UNUSED GtkButton *button, gpointer user_data) @@ -254,6 +277,7 @@ ui_chat_new(MESSENGER_Application *app) UI_MESSENGER_Handle *messenger = &(app->ui.messenger); handle->messages = NULL; + handle->edge_value = 0; handle->builder = gtk_builder_new_from_file( "resources/ui/chat.ui" @@ -324,6 +348,10 @@ ui_chat_new(MESSENGER_Application *app) gtk_builder_get_object(handle->builder, "chat_details_contacts_box") ); + handle->chat_scrolled_window = GTK_SCROLLED_WINDOW( + gtk_builder_get_object(handle->builder, "chat_scrolled_window") + ); + handle->chat_contacts_listbox = GTK_LIST_BOX( gtk_builder_get_object(handle->builder, "chat_contacts_listbox") ); @@ -339,6 +367,13 @@ ui_chat_new(MESSENGER_Application *app) gtk_builder_get_object(handle->builder, "messages_listbox") ); + g_signal_connect( + handle->messages_listbox, + "size-allocate", + G_CALLBACK(handle_chat_messages_listbox_size_allocate), + handle + ); + handle->attach_file_button = GTK_BUTTON( gtk_builder_get_object(handle->builder, "attach_file_button") ); diff --git a/src/ui/chat.h b/src/ui/chat.h index f065f61..6619506 100644 --- a/src/ui/chat.h +++ b/src/ui/chat.h @@ -37,6 +37,7 @@ typedef struct UI_PICKER_Handle UI_PICKER_Handle; typedef struct UI_CHAT_Handle { GList *messages; + gdouble edge_value; GtkBuilder *builder; GtkWidget *chat_box; @@ -53,6 +54,8 @@ typedef struct UI_CHAT_Handle GtkButton *hide_chat_details_button; GtkBox *chat_details_contacts_box; + GtkScrolledWindow *chat_scrolled_window; + GtkListBox *chat_contacts_listbox; GtkListBox *messages_listbox; diff --git a/src/ui/message.c b/src/ui/message.c index 4c89edb..6f379d7 100644 --- a/src/ui/message.c +++ b/src/ui/message.c @@ -106,6 +106,10 @@ ui_message_new(UI_MESSAGE_Type type, gtk_builder_get_object(builder, "read_receipt_image") ); + handle->content_stack = GTK_STACK( + gtk_builder_get_object(builder, "content_stack") + ); + handle->text_label = GTK_LABEL( gtk_builder_get_object(builder, "text_label") ); @@ -129,9 +133,26 @@ ui_message_new(UI_MESSAGE_Type type, switch (content_type) { + case UI_MESSAGE_CONTENT_TEXT: + gtk_stack_set_visible_child( + handle->content_stack, + GTK_WIDGET(handle->text_label) + ); + break; case UI_MESSAGE_CONTENT_FILE: + gtk_stack_set_visible_child( + handle->content_stack, + GTK_WIDGET(handle->file_revealer) + ); + gtk_revealer_set_reveal_child(handle->file_revealer, TRUE); break; + case UI_MESSAGE_CONTENT_PREVIEW: + gtk_stack_set_visible_child( + handle->content_stack, + GTK_WIDGET(handle->preview_drawing_area) + ); + break; default: break; } @@ -144,6 +165,18 @@ ui_message_new(UI_MESSAGE_Type type, return handle; } +void +ui_message_update(UI_MESSAGE_Handle *handle, + struct GNUNET_CHAT_Message *msg) +{ + struct GNUNET_CHAT_File *file = GNUNET_CHAT_message_get_file(msg); + + if (!file) + return; + + // TODO +} + void ui_message_delete(UI_MESSAGE_Handle *handle) { diff --git a/src/ui/message.h b/src/ui/message.h index b380960..704c33f 100644 --- a/src/ui/message.h +++ b/src/ui/message.h @@ -30,6 +30,8 @@ #include #include +#include + typedef struct MESSENGER_Application MESSENGER_Application; typedef enum UI_MESSAGE_Type @@ -65,6 +67,8 @@ typedef struct UI_MESSAGE_Handle GtkLabel *timestamp_label; GtkImage *read_receipt_image; + GtkStack *content_stack; + GtkLabel *text_label; GtkRevealer *file_revealer; GtkDrawingArea *preview_drawing_area; @@ -74,6 +78,10 @@ UI_MESSAGE_Handle* ui_message_new(UI_MESSAGE_Type type, UI_MESSAGE_ContentType content_type); +void +ui_message_update(UI_MESSAGE_Handle *handle, + struct GNUNET_CHAT_Message *message); + void ui_message_delete(UI_MESSAGE_Handle *handle); -- cgit v1.2.3