Skip to content

editor: Stabilize IME candidate box position during pre-edit on Wayland - #28429

Merged
JunkuiZhang merged 5 commits into
zed-industries:mainfrom
WantenMN:fix_editor
Jun 4, 2025
Merged

editor: Stabilize IME candidate box position during pre-edit on Wayland#28429
JunkuiZhang merged 5 commits into
zed-industries:mainfrom
WantenMN:fix_editor

Conversation

@WantenMN

@WantenMN WantenMN commented Apr 9, 2025

Copy link
Copy Markdown
Contributor

Modify the replace_and_mark_text_in_range method in the Editor to keep the cursor at the start of the preedit range during IME composition. Previously, the cursor would move to the end of the preedit text with each update, causing the IME candidate box to shift (e.g., when typing pinyin with Fcitx5 on Wayland). This change ensures the cursor and candidate box remain fixed until the composition is committed, improving the IME experience.

Closes #21004

Release Notes:

  • N/A

Modify the `replace_and_mark_text_in_range` method in the `Editor` to
keep the cursor at the start of the preedit range during IME
composition. Previously, the cursor would move to the end of the preedit
text with each update, causing the IME candidate box to shift (e.g.,
when typing pinyin with Fcitx5 on Wayland). This change ensures the
cursor and candidate box remain fixed until the composition is
committed, improving the IME experience.

Fixes #21004
@cla-bot

cla-bot Bot commented Apr 9, 2025

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @WantenMN on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@WantenMN

WantenMN commented Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot

cla-bot Bot commented Apr 9, 2025

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @WantenMN on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@cla-bot

cla-bot Bot commented Apr 9, 2025

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@maxdeviant maxdeviant changed the title fix(editor): prevent cursor movement during IME preedit composition editor: Prevent cursor movement during IME pre-edit composition Apr 9, 2025
@WantenMN

WantenMN commented Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 9, 2025
@cla-bot

cla-bot Bot commented Apr 9, 2025

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@JunkuiZhang

Copy link
Copy Markdown
Contributor

I think the code changes were made in the wrong place. While everything works fine on Windows and macOS, the issue appears to be specifically with the Linux implementation. Your current changes affect behavior across all platforms when they should likely target Linux specifically.

Windows:

2025-04-09.235340.mp4

@WantenMN

Copy link
Copy Markdown
Contributor Author

I think the code changes were made in the wrong place. While everything works fine on Windows and macOS, the issue appears to be specifically with the Linux implementation. Your current changes affect behavior across all platforms when they should likely target Linux specifically.

Windows:
2025-04-09.235340.mp4

You're right, I'll revert the changes and work on a better approach to fix the issue.

@WantenMN
WantenMN marked this pull request as draft April 10, 2025 12:31
@WantenMN

Copy link
Copy Markdown
Contributor Author

I believe that the cursor moving continuously while typing is expected behavior and shouldn’t be addressed with a hack. The issue of the IME candidate box shifting with the cursor is a problem with the input method itself, not the editor. Instead of modifying the editor’s code, it would be more appropriate to submit a pull request to the IME to ensure the candidate box remains fixed at its initial position.

@WantenMN WantenMN closed this Apr 10, 2025
…on Wayland

Replace `selected_text_range` with `marked_text_range` in
`WaylandWindowStatePtr` to fix the IME candidate box movement issue on
Linux Wayland. The previous implementation caused the candidate box to
follow the cursor due to `selected_text_range` always returning the
cursor's current position. Using `marked_text_range` ensures the
candidate box remains fixed at the initial pre-edit position, improving
usability for input methods like Fcitx5.

Resolves #21004
@WantenMN WantenMN reopened this Apr 10, 2025
@WantenMN

Copy link
Copy Markdown
Contributor Author

I’ve updated the fix for this issue in my latest commit (d6ccb78).

I realized the issue was with selected_text_range—it’s the function causing the candidate box to follow the cursor on Wayland. Switching to marked_text_range ensures it stays fixed at the pre-edit start position

One thing I noticed: selected_text_range always returns a range where start and end are the same (the cursor position), so I’m not sure what selection.reversed is supposed to do in this case—or how reversed even gets triggered.

It’s unclear to me why get_ime_area relied on selected_text_range in the first place. I think marked_text_range is a much better fit here, and this change should fully resolve the moving candidate box issue on Linux Wayland.

Here are some screenshots:

  • Editor
    Screenshot from 2025-04-11 03-20-39

  • AI Assistant Panel
    Screenshot from 2025-04-11 03-22-11

  • Command Palette
    Screenshot from 2025-04-11 03-23-08

  • Project Panel creating a new file
    Screenshot from 2025-04-11 03-21-16

@WantenMN
WantenMN marked this pull request as ready for review April 10, 2025 19:30
@WantenMN WantenMN changed the title editor: Prevent cursor movement during IME pre-edit composition editor: Stabilize IME candidate box position during pre-edit on Wayland Apr 10, 2025

@mikayla-maki mikayla-maki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

selection.reversed

This is to model the case when the user selects from bottom to top, e.g.:

-------<|---------|----------

If the user starts their selection at | and then moves to <|, then the selection will be marked as "reversed", but the range will still be in order relative to the buffer contents. Therefore, the correct side to query should be the "start", whereas in this case:

-------|---------|>----------

If the user starts their selection at | and then moves to |>, then the selection will not be marked as "reversed", the correct side to query should be the "end"

Otherwise, this change seems pretty good!

Comment thread crates/gpui/src/platform/linux/wayland/window.rs Outdated
@mikayla-maki
mikayla-maki enabled auto-merge (squash) May 30, 2025 22:50
@JunkuiZhang

Copy link
Copy Markdown
Contributor

@WantenMN Hello, looks like this PR requires a rebase on main

@imumesh18

Copy link
Copy Markdown
Contributor

Hey @JunkuiZhang last pr merge broke the ci. I have a pr open: #32049. I believe this pr will be fixed once we have this on main.

@JunkuiZhang

Copy link
Copy Markdown
Contributor

@imumesh18 Thanks for the fix!

@JunkuiZhang

JunkuiZhang commented Jun 4, 2025

Copy link
Copy Markdown
Contributor

Still requires a rebase I think, tests failed even that pr was merged

auto-merge was automatically disabled June 4, 2025 08:09

Head branch was pushed to by a user without write access

@JunkuiZhang
JunkuiZhang enabled auto-merge (squash) June 4, 2025 09:06
@JunkuiZhang
JunkuiZhang merged commit 5d0c968 into zed-industries:main Jun 4, 2025
@JunkuiZhang

Copy link
Copy Markdown
Contributor

Thanks!

@WantenMN
WantenMN deleted the fix_editor branch June 4, 2025 18:40
WantenMN added a commit to WantenMN/zed that referenced this pull request Jan 27, 2026
WantenMN added a commit to WantenMN/zed that referenced this pull request May 6, 2026
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…nd (zed-industries#28429)

Modify the `replace_and_mark_text_in_range` method in the `Editor` to
keep the cursor at the start of the preedit range during IME
composition. Previously, the cursor would move to the end of the preedit
text with each update, causing the IME candidate box to shift (e.g.,
when typing pinyin with Fcitx5 on Wayland). This change ensures the
cursor and candidate box remain fixed until the composition is
committed, improving the IME experience.

Closes zed-industries#21004

Release Notes:

- N/A

---------

Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com>
Co-authored-by: 张小白 <364772080@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with Chinese Input Candidate Box Movement

4 participants