From 2e601e3c6c27de6625c9b7d5d32177141e25acf6 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 1 Mar 2025 16:14:36 +0300 Subject: GTK: set the mouse cursor on the Scintilla view to signal business and on the popup entries * By default, use the "text" cursor - this is the default Scintilla cursor, but inhibited by the GtkEventBox I used to catch all input events. * When processing input events, the cursor is changed to "wait". This is done with a small delay in order to avoid flickering during normal typing. The cursor is only changed after 100ms of activity, i.e. only when executing long loops or external programs. * We use the raw GSource API since it's tricky to work with source ids if the source could be removed in the meantime. * The popup entries' cursor is also changed to "pointer" (hand) to give a hint that it can be clicked. --- src/interface-gtk/interface.c | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/interface-gtk/interface.c') diff --git a/src/interface-gtk/interface.c b/src/interface-gtk/interface.c index a41890d..5330bfa 100644 --- a/src/interface-gtk/interface.c +++ b/src/interface-gtk/interface.c @@ -60,6 +60,8 @@ //#define DEBUG +static gboolean teco_interface_busy_timeout_cb(gpointer user_data); +static void teco_interface_event_box_realized_cb(GtkWidget *widget, gpointer user_data); static void teco_interface_cmdline_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer user_data); @@ -312,6 +314,8 @@ teco_interface_init(void) gtk_box_pack_start(GTK_BOX(overlay_vbox), teco_interface.event_box_widget, TRUE, TRUE, 0); + g_signal_connect(teco_interface.event_box_widget, "realize", + G_CALLBACK(teco_interface_event_box_realized_cb), NULL); g_signal_connect(teco_interface.event_box_widget, "size-allocate", G_CALLBACK(teco_interface_size_allocate_cb), NULL); @@ -402,6 +406,17 @@ teco_interface_init(void) teco_interface_cmdline_update(&empty_cmdline); } +static void +teco_interface_set_cursor(GtkWidget *widget, const gchar *name) +{ + GdkWindow *window = gtk_widget_get_window(widget); + g_assert(window != NULL); + GdkDisplay *display = gdk_window_get_display(window); + + g_autoptr(GdkCursor) cursor = name ? gdk_cursor_new_from_name(display, name) : NULL; + gdk_window_set_cursor(window, cursor); +} + GOptionGroup * teco_interface_get_options(void) { @@ -1331,6 +1346,29 @@ teco_interface_cleanup(void) * GTK+ callbacks */ +/** + * Called some time after processing an input event in order to show + * business. + * + * The delay avoids cursor flickering during normal typing. + * + * @fixme It would be nicer to set the cursor for the entire window, + * but that would apparently require another GtkEventBox, spanning everything. + */ +static gboolean +teco_interface_busy_timeout_cb(gpointer user_data) +{ + teco_interface_set_cursor(teco_interface.event_box_widget, "wait"); + return G_SOURCE_REMOVE; +} + +static void +teco_interface_event_box_realized_cb(GtkWidget *widget, gpointer user_data) +{ + /* It's only now safe to get the GdkWindow. */ + teco_interface_set_cursor(widget, "text"); +} + /** * Called when the commandline widget is resized. * This should ensure that the caret jumps to the middle of the command line, @@ -1398,6 +1436,10 @@ teco_interface_input_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data) recursed = TRUE; + GSource *busy_timeout = g_timeout_source_new(500); /* ms */ + g_source_set_callback(busy_timeout, teco_interface_busy_timeout_cb, NULL, NULL); + g_source_attach(busy_timeout, NULL); + teco_memory_start_limiting(); g_queue_push_tail(teco_interface.event_queue, gdk_event_copy(event)); @@ -1471,6 +1513,10 @@ teco_interface_input_cb(GtkWidget *widget, GdkEvent *event, gpointer user_data) teco_memory_stop_limiting(); + g_source_destroy(busy_timeout); + g_source_unref(busy_timeout); + teco_interface_set_cursor(teco_interface.event_box_widget, "text"); + recursed = FALSE; return TRUE; } -- cgit v1.2.3