Skip to content

Remove Timer from WindowsKeyboardSwitchingAdapter and add IME diag#1495

Open
jasonleenaylor wants to merge 1 commit intomasterfrom
bugfix/imeKeyboardGlitches
Open

Remove Timer from WindowsKeyboardSwitchingAdapter and add IME diag#1495
jasonleenaylor wants to merge 1 commit intomasterfrom
bugfix/imeKeyboardGlitches

Conversation

@jasonleenaylor
Copy link
Copy Markdown
Contributor

@jasonleenaylor jasonleenaylor commented Mar 27, 2026

Replace the Timer-based deferred IME conversion status restore with synchronous restore. The Timer caused jittery, unreliable IME switching for Chinese Pinyin and other TSF-based IMEs. The synchronous approach ensures ImmSetConversionStatus is applied before any subsequent SaveImeConversionStatus can capture stale values.

Add Trace.WriteLine diagnostic logging throughout keyboard switching, IME save/restore, and post-switch state verification to aid debugging of intermittent IME composition issues.


Open with Devin

This change is Reviewable

…tics

Replace the Timer-based deferred IME conversion status restore with
synchronous restore. The Timer caused jittery, unreliable IME switching
for Chinese Pinyin and other TSF-based IMEs. The synchronous approach
ensures ImmSetConversionStatus is applied before any subsequent
SaveImeConversionStatus can capture stale values.

Add Trace.WriteLine diagnostic logging throughout keyboard switching,
IME save/restore, and post-switch state verification to aid debugging
of intermittent IME composition issues.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 3 potential issues.

View 2 additional findings in Devin Review.

Open in Devin Review

/// This class handles switching for normal Windows keyboards, Windows IME keyboards, and Keyman 10 keyboards
/// </summary>
internal class WindowsKeyboardSwitchingAdapter : IKeyboardSwitchingAdaptor, IDisposable
internal class WindowsKeyboardSwitchingAdapter : IKeyboardSwitchingAdaptor
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Missing CHANGELOG.md update required by AGENTS.md

The AGENTS.md rule file mandates: "If the suggested code changes functionality, fixes a bug, or adds a feature, you must generate an update for CHANGELOG.md." This PR clearly changes functionality — it removes the Timer-based IME conversion status retry mechanism from WindowsKeyboardSwitchingAdapter, removes IDisposable from the class, and adds diagnostic tracing plus new Win32 P/Invoke declarations (ImmGetOpenStatus, ImmSetOpenStatus, GetClassName, GetKeyboardLayout). However, CHANGELOG.md was not updated with any entry under the [Unreleased] section. The ### Changed subsection should describe the removal of the timer-based workaround and behavioral change to IME switching.

Prompt for agents
Add a CHANGELOG.md entry under the ## [Unreleased] section. Under the ### Changed subsection (create it if it doesn't exist at the top of [Unreleased]), add an entry such as:

- **[SIL.Windows.Forms.Keyboarding]** Removed Timer-based IME conversion status retry from WindowsKeyboardSwitchingAdapter; added diagnostic tracing for IME keyboard switching.

The entry should go in CHANGELOG.md under the ## [Unreleased] section.
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +67 to +68
RestoreImeConversionStatus(keyboard);
TraceImeState("PostSwitch", focusAfter, keyboard);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚩 Removal of Timer-based IME retry may regress intermittent IME activation issues

The old code had a Timer (500ms interval) that would call RestoreImeConversionStatus a second time after keyboard switching, with the comment "Restore it even though sometimes windows will ignore us" and "Start the timer for restoring IME status for when windows ignores us." This was a deliberate workaround for an intermittent Windows behavior where the OS would silently reset IME conversion status shortly after it was set. The new code calls RestoreImeConversionStatus only once (line 67) and relies on the added diagnostic tracing to investigate the root cause instead. This is a reasonable approach for diagnosis, but the behavioral change means IME users (particularly Chinese/Japanese/Korean input) may see the intermittent mode-reset issue return until a more robust fix is implemented.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines 38 to 48
private bool SwitchKeyboard(KeyboardDescription winKeyboard)
{
var keyboard = winKeyboard as WinKeyboardDescription;
if (IsSwitchingKeyboards)
return true;

IsSwitchingKeyboards = true;
try
{
if (keyboard?.InputLanguage?.Culture == null || keyboard.InputProcessorProfile.LangId == 0)
{
return false;
}

_expectedKeyboard = keyboard;
return Platform.IsMono || SwitchByProfile(keyboard);
}
finally
if (keyboard?.InputLanguage?.Culture == null || keyboard.InputProcessorProfile.LangId == 0)
{
IsSwitchingKeyboards = false;
Trace.WriteLine($"[KbdSwitch] SwitchKeyboard: invalid keyboard description (culture={keyboard?.InputLanguage?.Culture}, langId=0x{keyboard?.InputProcessorProfile.LangId:X4})");
return false;
}

return Platform.IsMono || SwitchByProfile(keyboard);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🚩 Removal of IsSwitchingKeyboards re-entrancy guard

The old code had a IsSwitchingKeyboards flag that prevented re-entrant calls to SwitchKeyboard. This was relevant because SwitchByProfile makes COM calls (ChangeCurrentLanguage, ActivateProfile) that could theoretically pump Windows messages on the UI thread, potentially triggering another keyboard activation. The new code removes this guard entirely. The ActivateKeyboard method at line 29 does check KeyboardController.Instance.ActiveKeyboard == keyboard as a short-circuit, but during switching ActiveKeyboard is set to NullKeyboard (see KeyboardDescription.cs:64), so this wouldn't prevent re-entrant activation of the same keyboard. In practice, re-entrancy through COM message pumping during these specific calls is unlikely but not impossible.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@github-actions
Copy link
Copy Markdown

Palaso Tests

     4 files  ±0       4 suites  ±0   9m 55s ⏱️ -16s
 5 095 tests ±0   4 862 ✅ ±0  233 💤 ±0  0 ❌ ±0 
16 597 runs  ±0  15 878 ✅ ±0  719 💤 ±0  0 ❌ ±0 

Results for commit c509789. ± Comparison against base commit ce83cef.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant