Skip to content

Commit

Permalink
Better solution for fcitx
Browse files Browse the repository at this point in the history
  • Loading branch information
tsayao committed Dec 15, 2024
1 parent be66041 commit 0c0c23b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class WindowContextBase: public WindowContext {
bool enabled;
bool on_preedit;
bool send_keypress;
bool on_key_event;
} im_ctx;

size_t events_processing_cnt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static void on_preedit_changed(GtkIMContext *im_context, gpointer user_data) {

gtk_im_context_get_preedit_string(im_context, &preedit_text, &attrList, &cursor_pos);
ctx->updateCaretPos();
ctx->setOnPreEdit(true);

jstring jstr = mainEnv->NewStringUTF(preedit_text);
EXCEPTION_OCCURED(mainEnv);
Expand Down Expand Up @@ -97,7 +98,7 @@ static gboolean on_retrieve_surrounding(GtkIMContext* self, gpointer user_data)
}

void WindowContextBase::commitIME(gchar *str) {
if (im_ctx.on_preedit) {
if (im_ctx.on_preedit || !im_ctx.on_key_event) {
jstring jstr = mainEnv->NewStringUTF(str);
EXCEPTION_OCCURED(mainEnv);
jsize slen = mainEnv->GetStringLength(jstr);
Expand All @@ -123,13 +124,15 @@ bool WindowContextBase::filterIME(GdkEvent *event) {
return false;
}

im_ctx.on_key_event = true;
bool filtered = gtk_im_context_filter_keypress(im_ctx.ctx, &event->key);

if (filtered && im_ctx.send_keypress) {
process_key(&event->key);
im_ctx.send_keypress = false;
return false;
}

im_ctx.on_key_event = false;
return filtered;
}

Expand Down Expand Up @@ -166,6 +169,7 @@ void WindowContextBase::enableOrResetIME() {
if (!im_ctx.enabled) {
im_ctx.ctx = gtk_im_multicontext_new();
gtk_im_context_set_client_window(GTK_IM_CONTEXT(im_ctx.ctx), gdk_window);
gtk_im_context_set_use_preedit(GTK_IM_CONTEXT(im_ctx.ctx), true);
g_signal_connect(im_ctx.ctx, "preedit-start", G_CALLBACK(on_preedit_start), this);
g_signal_connect(im_ctx.ctx, "preedit-changed", G_CALLBACK(on_preedit_changed), this);
g_signal_connect(im_ctx.ctx, "preedit-end", G_CALLBACK(on_preedit_end), this);
Expand Down

0 comments on commit 0c0c23b

Please sign in to comment.