TextBuffer can_undo and can_redo always false in some callbacks, like end_user_action
TextBuffer can_undo
and can_redo
are always false
in some callbacks, like end_user_action
.
Steps to reproduce
- create a TextView with a buffer attached to it
- Set
enable_undo
to true in the TextBuffer. - connect to the TextBuffer
end_user_action
signal a function that prints the buffer'scan_undo
property. - You can type in stuff in the TextView, do Ctrl-Z or Ctrl-Y,
can_undo
will always be false. - Connect the function that prints
can_undo
to the click of a button. When you click the button, the value will be correct.
Here are two pieces of C code do demonstrate it:
The first code with the bug
#include <gtk/gtk.h>
static void action(GtkTextBuffer* buf, gpointer) {
g_print("%u\n", gtk_text_buffer_get_can_undo(buf));
}
static void activate(GtkApplication* app, gpointer) {
GtkWidget* window = gtk_application_window_new(app);
GtkTextBuffer* buf = gtk_text_buffer_new(NULL);
GtkWidget* view = gtk_text_view_new_with_buffer(buf);
gtk_text_buffer_set_enable_undo(buf, TRUE);
g_signal_connect(buf, "end-user-action", G_CALLBACK(action), NULL);
gtk_window_set_child(GTK_WINDOW(window), view);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char** argv) {
g_autoptr(GtkApplication) app = gtk_application_new("arpa.sp1rit.gsvbug", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
The second one whitout:
#include <gtk/gtk.h>
static void clicked(GtkButton*, GtkTextBuffer* buf) {
g_print("%u\n", gtk_text_buffer_get_can_undo(buf));
}
static void activate(GtkApplication* app, gpointer) {
GtkWidget* window = gtk_application_window_new(app);
GtkWidget* container = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
GtkTextBuffer* buf = gtk_text_buffer_new(NULL);
GtkWidget* view = gtk_text_view_new_with_buffer(buf);
gtk_text_buffer_set_enable_undo(buf, TRUE);
gtk_box_append(GTK_BOX(container), view);
GtkWidget* btn = gtk_button_new_with_label("Can undo?");
g_signal_connect(btn, "clicked", G_CALLBACK(clicked), buf);
gtk_box_append(GTK_BOX(container), btn);
gtk_window_set_child(GTK_WINDOW(window), container);
gtk_window_present(GTK_WINDOW(window));
}
int main(int argc, char** argv) {
g_autoptr(GtkApplication) app = gtk_application_new("arpa.sp1rit.gsvbug", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
return g_application_run(G_APPLICATION(app), argc, argv);
}
Version information
I'm using fedora workstation 38 with gtk 4.10.4
Edited by mibi88