[GTK3] Autoscroll the printer view

This commit is contained in:
Maximilian Mader 2021-01-07 15:18:50 +01:00
parent 6ce3016719
commit cc4be1f903
Signed by: Max
GPG Key ID: F71D56A3151C4FB3
5 changed files with 17 additions and 11 deletions

View File

@ -36,10 +36,10 @@ Author: Maximilian Mader
<template class="PrinterWindow">
<property name="can_focus">False</property>
<property name="resizable">False</property>
<property name="default_width">320</property>
<property name="default_height">432</property>
<property name="default_width">322</property>
<property name="default_height">434</property>
<child>
<object class="GtkScrolledWindow">
<object class="GtkScrolledWindow" id="scrolled_window">
<property name="min-content-height">432</property>
<property name="max-content-height">432</property>
<property name="hexpand">0</property>

View File

@ -143,7 +143,7 @@ void set_combo_box_row_separator_func(GtkContainer *container) {
g_list_free(children);
}
gboolean scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark) {
void text_view_scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark) {
GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
GtkTextIter iter;
@ -154,8 +154,11 @@ gboolean scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark) {
gtk_text_view_scroll_to_mark(textview, mark, 0.0, true, 0.0, 0.10);
gtk_text_buffer_delete_mark(buffer, mark);
}
return true;
void scrolled_window_scroll_to_bottom(GtkScrolledWindow *window) {
GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(window);
gtk_adjustment_set_value(adj, gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj));
}
gchar* format_scale_value_pct(GtkScale *scale, gdouble value) {

View File

@ -25,7 +25,8 @@ GtkWidget *menubar_to_menu(GtkMenuBar *menubar);
gboolean is_separator(GtkTreeModel *model, GtkTreeIter *iter, gpointer data);
void set_combo_box_row_separator_func(GtkContainer *container);
gboolean scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark);
void text_view_scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark);
void scrolled_window_scroll_to_bottom(GtkScrolledWindow *window);
gchar* format_scale_value_pct(GtkScale *scale, gdouble value);
gchar* format_scale_color_temperature(GtkScale *scale, gdouble value);

View File

@ -411,11 +411,11 @@ static gboolean console_window_draw(GtkWidget *widget, cairo_t *cr) {
}
if (scroll_main) {
scroll_to_bottom(self->output, gtk_text_buffer_create_mark(main_text_buf, NULL, &main_scroll_iter, true));
text_view_scroll_to_bottom(self->output, gtk_text_buffer_create_mark(main_text_buf, NULL, &main_scroll_iter, true));
}
if (scroll_sidebar) {
scroll_to_bottom(self->sidebar_output, gtk_text_buffer_create_mark(sidebar_text_buf, NULL, &sidebar_scroll_iter, true));
text_view_scroll_to_bottom(self->sidebar_output, gtk_text_buffer_create_mark(sidebar_text_buf, NULL, &sidebar_scroll_iter, true));
}
}

View File

@ -5,6 +5,7 @@
struct _PrinterWindow {
GtkWindowClass parent_class;
GtkScrolledWindow *scrolled_window;
GtkDrawingArea *printer_canvas;
GtkButton *printer_save_button;
GtkButton *printer_clear_button;
@ -73,6 +74,7 @@ static void printer_window_finalize(GObject *object) {
static void printer_window_class_init(PrinterWindowClass *class) {
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class), RESOURCE_PREFIX "ui/printer_window.ui");
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), PrinterWindow, scrolled_window);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), PrinterWindow, printer_canvas);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), PrinterWindow, printer_save_button);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), PrinterWindow, printer_clear_button);
@ -115,13 +117,13 @@ void printer_window_update(PrinterWindow *self, struct PrinterData *data) {
cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, 160)
);
// cairo_surface_finish(self->surface);
g_mutex_unlock(&self->surface_mutex);
gtk_widget_set_size_request(GTK_WIDGET(self->printer_canvas), 160 * 2, self->current_height * 2);
gtk_window_present_with_time(GTK_WINDOW(self), time(NULL));
// queue scrolling, so that it will execute after the size request
g_idle_add((GSourceFunc)scrolled_window_scroll_to_bottom, self->scrolled_window);
}
void printer_window_clear(PrinterWindow *self) {