Skip to content

WPF - Use WpfImeKeyboardHandler for supported KeyboardLayoutIds #4439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

amaitland
Copy link
Member

@amaitland amaitland commented Mar 29, 2023

Fixes: #1262

Summary:

  • Determine which keyboard handler to use based on CurrentInputLanguage

Changes:

  • Setup default keyboard handler based on CurrentInputLanguage
  • Detect changes in input language

How Has This Been Tested?
Minimal Testing, requires user feedback

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Updated documentation

Checklist:

  • Tested the code(if applicable)
  • Commented my code
  • Changed the documentation(if applicable)
  • New files have a license disclaimer
  • The formatting is consistent with the project (project supports .editorconfig)

Summary by CodeRabbit

  • New Features
    • Improved keyboard handler to dynamically adapt to input language changes, providing better support for languages requiring IME (e.g., Korean, Japanese, Chinese).
  • Bug Fixes
    • Enhanced event management to ensure proper cleanup of input language event subscriptions, reducing potential issues during language switching.
  • Refactor
    • Streamlined logic for determining when to use IME keyboard handling based on keyboard layout.

@AppVeyorBot
Copy link

Copy link

coderabbitai bot commented Apr 26, 2025

"""

Walkthrough

The changes refactor and enhance the way keyboard handlers are managed in the WPF ChromiumWebBrowser component. A new virtual method, SetupWpfKeyboardHandler, is introduced to determine and initialize the appropriate keyboard handler based on the current input language's keyboard layout. The system now dynamically switches to an IME-capable handler when languages such as Korean, Japanese, or Chinese are detected. Event handlers are added and managed to respond to input language changes, and proper cleanup is ensured during disposal. Additionally, logic for determining IME usage is encapsulated in a new static method.

Changes

File(s) Change Summary
CefSharp.Wpf/ChromiumWebBrowser.cs Refactored keyboard handler initialization into a new virtual method; added dynamic switching for IME support; managed event subscriptions and disposal; imported System.Diagnostics for error tracing.
CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs Made PrimaryLangId static; added public static UseImeKeyboardHandler to determine IME support based on keyboard layout ID.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ChromiumWebBrowser
    participant WpfKeyboardHandler
    participant WpfImeKeyboardHandler
    participant InputLanguageManager

    User->>ChromiumWebBrowser: Instantiate
    ChromiumWebBrowser->>ChromiumWebBrowser: SetupWpfKeyboardHandler()
    alt IME language detected
        ChromiumWebBrowser->>WpfImeKeyboardHandler: Create instance
    else Non-IME language
        ChromiumWebBrowser->>WpfKeyboardHandler: Create instance
        ChromiumWebBrowser->>InputLanguageManager: Subscribe InputLanguageChanged
    end
    InputLanguageManager-->>ChromiumWebBrowser: InputLanguageChanged event
    ChromiumWebBrowser->>ChromiumWebBrowser: OnInputLangageChanged()
    alt Switch to IME required
        ChromiumWebBrowser->>WpfImeKeyboardHandler: Create new instance
        ChromiumWebBrowser->>WpfKeyboardHandler: Dispose old handler
        ChromiumWebBrowser->>InputLanguageManager: Unsubscribe InputLanguageChanged
    end
    User->>ChromiumWebBrowser: Dispose
    ChromiumWebBrowser->>ChromiumWebBrowser: UnsubsribeInputLanguageChanged()
Loading

Assessment against linked issues

Objective Addressed Explanation
Support IME input in WPF ChromiumWebBrowser for Windows 8.1/10/11 with transparent window (#1262)

Poem

A browser hops with nimble flair,
Switching keyboards with utmost care.
IME or not, it knows just when,
To swap its handler and listen again.
Languages change? No need to fear—
This code’s a rabbit, quick and clear!
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3a34926 and d67af50.

📒 Files selected for processing (2)
  • CefSharp.Wpf/ChromiumWebBrowser.cs (6 hunks)
  • CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs
  • CefSharp.Wpf/ChromiumWebBrowser.cs
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@amaitland amaitland marked this pull request as ready for review April 26, 2025 23:29
@amaitland
Copy link
Member Author

@coderabbitai review

Copy link

coderabbitai bot commented Apr 26, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@amaitland
Copy link
Member Author

@coderabbitai full review

Copy link

coderabbitai bot commented Apr 26, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs (1)

470-489: Well-designed IME detection method with clear documentation

This new method properly encapsulates the logic for determining whether to use the IME keyboard handler based on the keyboard layout ID. The method checks if the layout corresponds to Korean, Japanese, or Chinese languages which require IME support.

The XML documentation is clear and well-structured, though the <see cref="System.Globalization.CultureInfo.KeyboardLayoutId"/> reference should specify it's related to the parameter.

-/// <param name="keyboardLayoutId">Keyboard Layout Id (obtained from <see cref="System.Globalization.CultureInfo.KeyboardLayoutId"/></param>
+/// <param name="keyboardLayoutId">Keyboard Layout Id (obtained from <see cref="System.Globalization.CultureInfo.KeyboardLayoutId"/>)</param>
CefSharp.Wpf/ChromiumWebBrowser.cs (3)

780-816: Well-implemented keyboard handler setup with proper error handling

This new method determines the appropriate keyboard handler based on the current input language, using the WpfImeKeyboardHandler for supported languages (Korean, Japanese, Chinese) and the default handler for others. The error handling is appropriate, falling back to the default handler if an exception occurs.

However, there's an issue with the error message.

-                Trace.TraceError($"Error unsubscribing from InputLanguageChanged {ex.ToString()}");
+                Trace.TraceError($"Error initializing keyboard handler: {ex.ToString()}");

The error message incorrectly indicates it's unsubscribing from InputLanguageChanged when it's actually in the setup method.


1799-1817: Good implementation of input language change handler

This method appropriately handles input language changes by checking if the new language requires the IME keyboard handler and switching handlers as needed. It also properly disposes of the old handler and unsubscribes from the event when switching to the IME handler.

There's a minor typo in the method name that should be fixed.

-private void OnInputLangageChanged(object sender, InputLanguageEventArgs e)
+private void OnInputLanguageChanged(object sender, InputLanguageEventArgs e)

Make sure to update all references to this method name elsewhere in the code.


2928-2956: Good implementation of event unsubscription with UI thread safety

This method safely unsubscribes from the input language change event, properly handling the fact that it might be called from a non-UI thread. The error handling is appropriate, capturing and logging any exceptions that might occur during unsubscription.

There's a typo in the method name that should be fixed.

-private void UnsubsribeInputLanguageChanged()
+private void UnsubscribeInputLanguageChanged()

Make sure to update all calls to this method elsewhere in the code.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d5d0cb9 and 3a34926.

📒 Files selected for processing (2)
  • CefSharp.Wpf/ChromiumWebBrowser.cs (6 hunks)
  • CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs (1)
CefSharp.Wpf/Internals/ImeNative.cs (1)
  • ImeNative (10-133)
🔇 Additional comments (5)
CefSharp.Wpf/Experimental/WpfIMEKeyboardHandler.cs (1)

357-360: Good refactoring to make PrimaryLangId static

Making this method static is appropriate since it doesn't depend on instance state, improving reusability.

CefSharp.Wpf/ChromiumWebBrowser.cs (4)

6-6: Adding System.Diagnostics namespace for error tracing

Appropriate addition of System.Diagnostics namespace for using Trace.TraceError in the error handling code.


608-614: Improved keyboard handler initialization

The code now calls the new SetupWpfKeyboardHandler method first, then falls back to the default WpfKeyboardHandler only if needed. This allows for dynamic selection of the appropriate keyboard handler based on the current input language.


762-762: Good cleanup of language event handlers

Adding the call to UnsubsribeInputLanguageChanged during disposal ensures proper cleanup of event handlers, preventing memory leaks.


2942-2949: Good thread safety with UI thread dispatching

The code correctly uses UiThreadRunAsync to ensure the event handler is unsubscribed on the UI thread, which is necessary since InputLanguageManager.Current is thread-specific.

@AppVeyorBot
Copy link

Copy link

coderabbitai bot commented Apr 26, 2025

✅ Actions performed

Reviews resumed.

1 similar comment
Copy link

coderabbitai bot commented Apr 27, 2025

✅ Actions performed

Reviews resumed.

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.

2 participants