<commit_before>diff --git a/modules/input/imquartz.c b/modules/input/imquartz.c
index 61ad914..720f039 100644
--- a/modules/input/imquartz.c
+++ b/modules/input/imquartz.c
@@ -211,9 +211,37 @@ quartz_filter_keypress (GtkIMContext *context,
 }
 
 static void
+discard_preedit (GtkIMContext *context, gboolean commit)
+{
+  GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context);
+
+  if (!qc->client_window)
+    return;
+
+  NSView *nsview = gdk_quartz_window_get_nsview (qc->client_window);
+  if (!nsview)
+    return;
+
+  /* reset any partial input for this NSView */
+  NSInputManager *currentInputManager = [NSInputManager currentInputManager];
+  [currentInputManager markedTextAbandoned:nsview];
+
+  if (qc->preedit_str)
+    {
+      if (commit)
+        g_signal_emit_by_name (context, "commit", qc->preedit_str);
+
+      g_free (qc->preedit_str);
+      qc->preedit_str = NULL;
+      g_signal_emit_by_name (context, "preedit_changed");
+    }
+}
+
+static void
 quartz_reset (GtkIMContext *context)
 {
   GTK_NOTE (MISC, g_print ("quartz_reset\n"));
+  discard_preedit (context, TRUE);
 }
 
 static void
@@ -242,6 +270,9 @@ quartz_focus_out (GtkIMContext *context)
 
   GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context);
   qc->focused = FALSE;
+
+  /* Clear any partially built strings or it'll mess up other GTK+ widgets in the window */
+  discard_preedit (context, FALSE);
 }
 
 static void<commit_msg>unmarkText from within discard_preedit.<commit_after>diff --git a/modules/input/imquartz.c b/modules/input/imquartz.c
index 61ad914..9e42d8f 100644
--- a/modules/input/imquartz.c
+++ b/modules/input/imquartz.c
@@ -211,9 +211,40 @@ quartz_filter_keypress (GtkIMContext *context,
 }
 
 static void
+discard_preedit (GtkIMContext *context)
+{
+  GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context);
+
+  if (!qc->client_window)
+    return;
+
+  NSView *nsview = gdk_quartz_window_get_nsview (qc->client_window);
+  if (!nsview)
+    return;
+
+  if (GDK_IS_WINDOW (nsview))
+    return;
+
+  /* reset any partial input for this NSView */
+  [(GdkQuartzView *)nsview unmarkText];
+  NSInputManager *currentInputManager = [NSInputManager currentInputManager];
+  [currentInputManager markedTextAbandoned:nsview];
+
+  if (qc->preedit_str)
+    {
+      g_signal_emit_by_name (context, "commit", qc->preedit_str);
+
+      g_free (qc->preedit_str);
+      qc->preedit_str = NULL;
+      g_signal_emit_by_name (context, "preedit_changed");
+    }
+}
+
+static void
 quartz_reset (GtkIMContext *context)
 {
   GTK_NOTE (MISC, g_print ("quartz_reset\n"));
+  discard_preedit (context);
 }
 
 static void
@@ -242,6 +273,9 @@ quartz_focus_out (GtkIMContext *context)
 
   GtkIMContextQuartz *qc = GTK_IM_CONTEXT_QUARTZ (context);
   qc->focused = FALSE;
+
+  /* Commit any partially built strings or it'll mess up other GTK+ widgets in the window */
+  discard_preedit (context);
 }
 
 static void
{'fraction_non_alphanumeric': 0.13625470729202327, 'fraction_numerical': 0.022595001711742554, 'mean_word_length': 4.341864716636198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027565', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1094, 'n_words': 219}