talkatu/talkatu

Merged in default (pull request #45)

2020-04-27, Gary Kramlich
9d6557bc7b0b
Merged in default (pull request #45)

Automatically scroll the history view on new messages

Approved-by: Elliott Sales de Andrade
--- a/talkatu/talkatuhistory.c Mon Apr 27 00:32:04 2020 +0000
+++ b/talkatu/talkatuhistory.c Mon Apr 27 00:32:14 2020 +0000
@@ -43,11 +43,67 @@
*/
struct _TalkatuHistory {
TalkatuView parent;
+
+ GtkAdjustment *vadjustment;
+ gboolean auto_scroll;
};
G_DEFINE_TYPE(TalkatuHistory, talkatu_history, TALKATU_TYPE_VIEW)
/******************************************************************************
+ * Callbacks
+ *****************************************************************************/
+static void
+talkatu_history_vadjustment_changed_cb(GtkAdjustment *adjustment,
+ gpointer data)
+{
+ TalkatuHistory *history = TALKATU_HISTORY(data);
+
+ if(history->auto_scroll) {
+ gdouble upper, pagesize;
+
+ upper = gtk_adjustment_get_upper(adjustment);
+ pagesize = gtk_adjustment_get_page_size(adjustment);
+
+ gtk_adjustment_set_value(adjustment, upper - pagesize);
+ }
+}
+
+static void
+talkatu_history_vadjustment_value_changed_cb(GtkAdjustment *adjustment,
+ gpointer data)
+{
+ TalkatuHistory *history = TALKATU_HISTORY(data);
+ gdouble current, upper, pagesize;
+
+ current = gtk_adjustment_get_value(adjustment);
+ upper = gtk_adjustment_get_upper(adjustment);
+ pagesize = gtk_adjustment_get_page_size(adjustment);
+
+ history->auto_scroll = (current + pagesize >= upper);
+}
+
+static void
+talkatu_history_vadjustment_notify_cb(GObject *obj, GParamSpec *pspec,
+ gpointer data)
+{
+ TalkatuHistory *history = TALKATU_HISTORY(obj);
+ GtkAdjustment *adjustment = NULL;
+
+ adjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(obj));
+ if(g_set_object(&history->vadjustment, adjustment)) {
+ history->auto_scroll = TRUE;
+
+ g_signal_connect(G_OBJECT(adjustment), "value-changed",
+ G_CALLBACK(talkatu_history_vadjustment_value_changed_cb),
+ history);
+ g_signal_connect(G_OBJECT(adjustment), "changed",
+ G_CALLBACK(talkatu_history_vadjustment_changed_cb),
+ history);
+ }
+}
+
+/******************************************************************************
* GtkTextViewClass overrides
*****************************************************************************/
static GtkTextBuffer *
@@ -62,12 +118,29 @@
talkatu_history_init(TalkatuHistory *history) {
gtk_text_view_set_editable(GTK_TEXT_VIEW(history), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(history), FALSE);
+
+ history->auto_scroll = TRUE;
+
+ g_signal_connect(G_OBJECT(history), "notify::vadjustment",
+ G_CALLBACK(talkatu_history_vadjustment_notify_cb), NULL);
+}
+
+static void
+talkatu_history_finalize(GObject *obj) {
+ TalkatuHistory *history = TALKATU_HISTORY(obj);
+
+ g_clear_object(&history->vadjustment);
+
+ G_OBJECT_CLASS(talkatu_history_parent_class)->finalize(obj);
}
static void
talkatu_history_class_init(TalkatuHistoryClass *klass) {
+ GObjectClass *obj_class = G_OBJECT_CLASS(klass);
GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS(klass);
+ obj_class->finalize = talkatu_history_finalize;
+
text_view_class->create_buffer = talkatu_history_create_buffer;
}
@@ -97,16 +170,15 @@
*/
void
talkatu_history_page_up(TalkatuHistory *history) {
- GtkAdjustment *adjustment = NULL;
- double value = 0.0;
-
g_return_if_fail(TALKATU_IS_HISTORY(history));
- adjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(history));
+ if(history->vadjustment != NULL) {
+ double value = 0.0;
- value = gtk_adjustment_get_value(adjustment);
- value -= gtk_adjustment_get_page_increment(adjustment);
- gtk_adjustment_set_value(adjustment, value);
+ value = gtk_adjustment_get_value(history->vadjustment);
+ value -= gtk_adjustment_get_page_increment(history->vadjustment);
+ gtk_adjustment_set_value(history->vadjustment, value);
+ }
}
/**
@@ -117,15 +189,14 @@
*/
void
talkatu_history_page_down(TalkatuHistory *history) {
- GtkAdjustment *adjustment = NULL;
- double value = 0.0;
-
g_return_if_fail(TALKATU_IS_HISTORY(history));
- adjustment = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(history));
+ if(history->vadjustment != NULL) {
+ double value = 0.0;
- value = gtk_adjustment_get_value(adjustment);
- value += gtk_adjustment_get_page_increment(adjustment);
- gtk_adjustment_set_value(adjustment, value);
+ value = gtk_adjustment_get_value(history->vadjustment);
+ value += gtk_adjustment_get_page_increment(history->vadjustment);
+ gtk_adjustment_set_value(history->vadjustment, value);
+ }
}