Page 1 of 1

IME for Mac CEF3 off screen rendering

PostPosted: Thu Feb 21, 2013 11:02 am
by mihait
After applying the CEF3 mac off screen rendering patch, I've noticed IME input is not available.
After some research, I've found a few possible ways of adding this feature which I'd like to discuss before jumping into an implementation.

In general, enabling IME for Mac implies having a NSView in the responder hierarchy that conforms to the NSTextInputClient protocol. For example, IME is enabled on both
Chromium and CEF3 on screen rendering through a NSView owned by the RenderWidgetHostViewMac class called RenderWidgetHostViewCocoa which implements the NSTextInputClient protocol.

In other words, for enabling IME, we have to port the RenderWidgetHostViewCocoa implementation in one of the views included in the off screen rendering responder chain.
After doing some investigation, these are the options I've found:

The first option is to add another NSView in libcef, owned by the CefRenderWidgetHostViewOSR class, which would implement the NSTextInputClient protocol.
A major advantage is the fact that any client application will have IME enabled without any additional development effort. Unfortunately, this solution implies adding a NSView in an off screen rendering model, which perhaps isn't such a good idea.

Another potential solution implies implementing the NSTextInputClient protocol in CefClient, in the OSRTest_mac view. This comes with the advantage of not adding another NSView in the off screen rendering model, however, it implies that every embedding application should implement this protocol and copy most of the code in the CefClient view. Also, another problem consists of the API's that the content module should expose for compositing and editing text, API's which should be available to the NSTextInputClient protocol methods.

The final solution I've found is to implement the NSTextInputClient corresponding methods in CefRenderWidgetHostViewOSR as C++ methods. Then, each embedding application, including CefClient would add the protocol methods in the NSView used for displaying the rendered content and call the corresponding CefRenderWidgetHostViewOSR methods. For example, in the NSView - (BOOL)hasMarkedText selector, the embedder would call the bool CefRenderWidgetHostViewOSR::hasMarkedText method. This method has the advantage of not exposing any extra content methods, however, it still implies that the embedder should do some work for enabling IME.

I probably think the third option is the best we have considering the circumstances, but I'd like your opinion on this matter, perhaps we might find a more suitable way of adding this feature.

Re: IME for Mac CEF3 off screen rendering

PostPosted: Fri Feb 22, 2013 11:10 am
by magreenblatt
I think the final solution is a good place to start. We might also want to put these methods in a new interface (CefTextInputClient?) retrievable via CefBrowserHost instead of hanging them on CefBrowserHost directly.

Re: IME for Mac CEF3 off screen rendering

PostPosted: Wed Feb 27, 2013 1:49 am
by mihait
Apparently, there's another, better approach on adding IME support: Since the 10.6 sdk, each NSView can hold a NSTextInputContext ( ... rence.html) object. This class can use a remote NSTextInputClient implementation which is not tied to the NSView.

In other words, we could implemement and expose in libcef something like TextInputClientOSRMac : id <NSTextInputClient>.
From this point, an embedder who wants IME enabled should:
* create a NSTextInputContext object and initialize it with TextInputClientOSRMac, via the - (id)initWithClient:(id <NSTextInputClient>)theClient method.
* implement the the NSView - (NSTextInputContext *)inputContext method to return the NSTextInputContext mentioned above
* Handle keyEvents in a way similar to this:
[[self inputContext] handleEvent];

I've tested this approach in a minimal application and IME works just fine.
I'll start the libcef id <NSTextInputClient> implementation using this approach, as it's a lot cleaner than any of the previous solutions.

Re: IME for Mac CEF3 off screen rendering

PostPosted: Wed Feb 27, 2013 10:19 am
by magreenblatt
Sounds good.

Re: IME for Mac CEF3 off screen rendering

PostPosted: Tue Mar 12, 2013 2:27 pm
by mihait
I've just finished this feature, I've been able to use the NSTextInputContext as described.
After OSR gets pushed on Mac, I will create an issue for this, perform the necessary cleanup and attach the patch.