text: Fix disabling of history

Keep a separate boolean for enable-undo, and
disable the history if it is false, or the entry
is not using visible text, or isn't editable.

Related to: #5622
This commit is contained in:
Matthias Clasen 2023-02-27 19:17:08 -05:00
parent 0db2d0bc07
commit 61e4eadbce

View File

@ -247,6 +247,7 @@ struct _GtkTextPrivate
guint populate_all : 1; guint populate_all : 1;
guint propagate_text_width : 1; guint propagate_text_width : 1;
guint text_handles_enabled : 1; guint text_handles_enabled : 1;
guint enable_undo : 1;
}; };
struct _GtkTextPasswordHint struct _GtkTextPasswordHint
@ -397,6 +398,9 @@ static void gtk_text_set_max_width_chars (GtkText *self,
static void gtk_text_set_alignment (GtkText *self, static void gtk_text_set_alignment (GtkText *self,
float xalign); float xalign);
static void gtk_text_set_enable_undo (GtkText *self,
gboolean enable_undo);
/* Default signal handlers /* Default signal handlers
*/ */
static GMenuModel *gtk_text_get_menu_model (GtkText *self); static GMenuModel *gtk_text_get_menu_model (GtkText *self);
@ -561,6 +565,7 @@ static void begin_change (GtkText *self);
static void end_change (GtkText *self); static void end_change (GtkText *self);
static void emit_changed (GtkText *self); static void emit_changed (GtkText *self);
static void gtk_text_update_history (GtkText *self);
static void gtk_text_update_clipboard_actions (GtkText *self); static void gtk_text_update_clipboard_actions (GtkText *self);
static void gtk_text_update_emoji_action (GtkText *self); static void gtk_text_update_emoji_action (GtkText *self);
static void gtk_text_update_handles (GtkText *self); static void gtk_text_update_handles (GtkText *self);
@ -1602,11 +1607,7 @@ gtk_text_set_property (GObject *object,
break; break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_ENABLE_UNDO: case NUM_PROPERTIES + GTK_EDITABLE_PROP_ENABLE_UNDO:
if (g_value_get_boolean (value) != gtk_text_history_get_enabled (priv->history)) gtk_text_set_enable_undo (self, g_value_get_boolean (value));
{
gtk_text_history_set_enabled (priv->history, g_value_get_boolean (value));
g_object_notify_by_pspec (object, pspec);
}
break; break;
/* GtkText properties */ /* GtkText properties */
@ -1732,7 +1733,7 @@ gtk_text_get_property (GObject *object,
break; break;
case NUM_PROPERTIES + GTK_EDITABLE_PROP_ENABLE_UNDO: case NUM_PROPERTIES + GTK_EDITABLE_PROP_ENABLE_UNDO:
g_value_set_boolean (value, gtk_text_history_get_enabled (priv->history)); g_value_set_boolean (value, priv->enable_undo);
break; break;
/* GtkText properties */ /* GtkText properties */
@ -1858,6 +1859,7 @@ gtk_text_init (GtkText *self)
priv->cursor_alpha = 1.0; priv->cursor_alpha = 1.0;
priv->invisible_char = 0; priv->invisible_char = 0;
priv->history = gtk_text_history_new (&history_funcs, self); priv->history = gtk_text_history_new (&history_funcs, self);
priv->enable_undo = TRUE;
gtk_text_history_set_max_undo_levels (priv->history, DEFAULT_MAX_UNDO); gtk_text_history_set_max_undo_levels (priv->history, DEFAULT_MAX_UNDO);
@ -5525,6 +5527,7 @@ gtk_text_set_editable (GtkText *self,
gtk_event_controller_key_set_im_context (GTK_EVENT_CONTROLLER_KEY (priv->key_controller), gtk_event_controller_key_set_im_context (GTK_EVENT_CONTROLLER_KEY (priv->key_controller),
is_editable ? priv->im_context : NULL); is_editable ? priv->im_context : NULL);
gtk_text_update_history (self);
gtk_text_update_clipboard_actions (self); gtk_text_update_clipboard_actions (self);
gtk_text_update_emoji_action (self); gtk_text_update_emoji_action (self);
@ -5605,7 +5608,7 @@ gtk_text_set_visibility (GtkText *self,
gtk_text_recompute (self); gtk_text_recompute (self);
/* disable undo when invisible text is used */ /* disable undo when invisible text is used */
gtk_text_history_set_enabled (priv->history, visible); gtk_text_update_history (self);
gtk_text_update_clipboard_actions (self); gtk_text_update_clipboard_actions (self);
} }
@ -7284,3 +7287,28 @@ gtk_text_history_select_cb (gpointer funcs_data,
selection_insert, selection_insert,
selection_bound); selection_bound);
} }
static void
gtk_text_set_enable_undo (GtkText *self,
gboolean enable_undo)
{
GtkTextPrivate *priv = gtk_text_get_instance_private (self);
if (priv->enable_undo == enable_undo)
return;
priv->enable_undo = enable_undo;
gtk_text_update_history (self);
g_object_notify (G_OBJECT (self), "enable-undo");
}
static void
gtk_text_update_history (GtkText *self)
{
GtkTextPrivate *priv = gtk_text_get_instance_private (self);
gtk_text_history_set_enabled (priv->history,
priv->enable_undo &&
priv->visible &&
priv->editable);
}