commit 412c7d4f210096c7cde549c48e3e2a2262724d00
parent bd545ea846e668112e5a54b702acc9c9f9231aea
Author: TheJackiMonster <thejackimonster@gmail.com>
Date: Tue, 21 Dec 2021 22:31:34 +0100
Added automatic scrolling to chats
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
Diffstat:
8 files changed, 88 insertions(+), 16 deletions(-)
diff --git a/resources/ui/chat.ui b/resources/ui/chat.ui
@@ -145,7 +145,7 @@ Author: Tobias Frisch
</packing>
</child>
<child>
- <object class="GtkScrolledWindow">
+ <object class="GtkScrolledWindow" id="chat_scrolled_window">
<property name="visible">True</property>
<property name="can-focus">True</property>
<child>
@@ -153,21 +153,11 @@ Author: Tobias Frisch
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
- <object class="GtkStack">
+ <object class="GtkListBox" id="messages_listbox">
<property name="visible">True</property>
<property name="can-focus">False</property>
- <child>
- <object class="GtkListBox" id="messages_listbox">
- <property name="visible">True</property>
- <property name="can-focus">False</property>
- <property name="selection-mode">none</property>
- <property name="activate-on-single-click">False</property>
- </object>
- <packing>
- <property name="name">page0</property>
- <property name="title" translatable="yes">page0</property>
- </packing>
- </child>
+ <property name="selection-mode">none</property>
+ <property name="activate-on-single-click">False</property>
</object>
</child>
</object>
diff --git a/resources/ui/message_content.ui b/resources/ui/message_content.ui
@@ -71,7 +71,7 @@ Author: Tobias Frisch
</packing>
</child>
<child>
- <object class="GtkStack">
+ <object class="GtkStack" id="content_stack">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
diff --git 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
@@ -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
@@ -95,6 +95,29 @@ handle_chat_contacts_listbox_row_activated(GtkListBox *listbox,
}
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
@@ -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
@@ -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;
}
@@ -145,6 +166,18 @@ ui_message_new(UI_MESSAGE_Type type,
}
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)
{
g_object_unref(handle->builder);
diff --git a/src/ui/message.h b/src/ui/message.h
@@ -30,6 +30,8 @@
#include <gtk-3.0/gtk/gtk.h>
#include <libhandy-1/handy.h>
+#include <gnunet/gnunet_chat_lib.h>
+
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;
@@ -75,6 +79,10 @@ 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);
#endif /* UI_MESSAGE_H_ */