Run a function when the user stops typing

I am writing a small GUI application using the Haskell gtk2hs library, and I am currently working with multi-line text fields . I have a function that I want to run when the user makes changes to the text in the text box, but I do not want them to click a button to activate it.

In addition, since this is a rather intrusive and intensive processing (it draws graphics, uploads files, etc.), I would like it to work not every time the user makes any changes (which, probably, could done with bufferChanged in a text buffer, I guess?), but when they stop for a few seconds between changes.

Basically, I am wondering if there is something in gtk that is similar to how range widgets can set their update policy continuous or pending , but for text fields

+6
source share
1 answer

I don't know anything about Haskell bindings, but in simple C it is pretty easy to implement using the GSource timeout.

#include <gtk/gtk.h> static guint source_id = 0; static gboolean do_stuff(gpointer user_data) { g_print("doing stuff...\n"); return FALSE; } static void postpone(void) { if (source_id > 0) g_source_remove(source_id); source_id = g_timeout_add(1000, do_stuff, NULL); } int main(int argc, char **argv) { GtkWidget *window, *text_view; GtkTextBuffer *text_buffer; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL); text_view = gtk_text_view_new(); gtk_container_add(GTK_CONTAINER(window), text_view); text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)); g_signal_connect(text_buffer, "changed", G_CALLBACK(postpone), NULL); gtk_widget_show_all(window); gtk_main(); return 0; } 

The question of exiting TextView before the timeout expires is still open.

+1
source

All Articles