Window Focus

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Window Focus

Postby tux316 » Thu Jun 11, 2009 6:20 pm

Ok, let's say we have an application with 2 windows. One window is for "typing" and communicating with other users, the other window is strictly for a CEF Browser. When we call CefFrame->load_url(), the focus is taken from our main window, to the CEF Browser window. Is there a way to offset or stop this behavior thru CEF?

The following is a sample of what we are doing, demonstrated with "Pseudo Code:"
Code: Select all
void Navigate(wchar_t *ws_Address)
{
    cef_frame_t *CefFrame = CefBrowser->get_main_frame(CefBrowser);
    CefFrame->load_url(CefFrame, ws_Address);
    BaseRelease(CefFrame);
}

So if we was to call /Navigate http://www.google.com/ from our main application window, the focus would go from our main window, to the CefBrowser window. Is there a way to stop this?

In the handler documentation (C-Interface), there's a call back "handle_take_focus," but from my understanding this is currently not into effect (since the value is still currently being ignored), and I also think that this is only for HTML Components/Elements. There's also a "set_focus(cef_browser_t *, int)" function that's part of the _cef_browser_t structure, but if I am reading the comments right this is only for removing the focus on the window.
tux316
Newbie
 
Posts: 2
Joined: Sat May 30, 2009 11:09 pm

Re: Window Focus

Postby magreenblatt » Thu Jun 11, 2009 7:33 pm

The focus is being claimed by the browser window at line 557 in browser_impl.cc:

Code: Select all
  UIT_SetFocus(GetWebViewHost(), true);


Calling the CefBrowser::SetFocus() method with an 'enable' value of false (which results in a call to WinAPI SetFocus(NULL)) will not return focus to the previously focused window but instead cause all windows to lose keyboard focus. Your best bet for keeping focus on your window will likely be one of the below options.

Option A:
Re-set the focus. Before navigating set a state flag on your CefHandler implementation indicating that you don't want the browser to take focus. When a CefHandler callback is executed return the focus to your window using WinAPI SetFocus() and reset the state flag. You'll have to experiment to identify the best callback to use for this. A disadvantage to this option is the possibility of "focus flicker" as the focus passes between the windows.

Option B:
Disable focus stealing by improving the CEF API. Introduce a method, callback or parameter for enabling/disabling setting of the focus after navigating. This could take one of (at least) three forms:
1. Add a method to CefBrowser for global enable/disable of focus setting.
2. Add a callback to CefHandler that gets called before the focus is set giving the user an option to cancel it.
3. Add a parameter to all of the CefBrowser methods that can result in navigation indicating if the focus should be set.

I think either #2 or #3 would be reasonable choices.
magreenblatt
Site Admin
 
Posts: 12407
Joined: Fri May 29, 2009 6:57 pm

Re: Window Focus

Postby tux316 » Thu Jun 11, 2009 11:12 pm

Option #2 it is.

http://code.google.com/p/chromiumembedded/issues/detail?id=34


A sample as to how we implement it in our project:

Code: Select all
/**
 * handle_set_focus.  Called when a browser is about to set the focus.
 *
 * @param handler       Handler
 * @param browser       Browser
 *
 * @return RV_CONTINUE  To let the browser set focus.
 * @return RV_HANDLED   Stops the browser from stealing focus.
 */
CEF_CALL handle_set_focus(struct _cef_handler_t* handler, cef_browser_t* browser)
{
    CF_DB_START;

    BaseRelease(browser);

    CF_DB_END;
    return RV_HANDLED;
}

/**
 * handle_set_focus_widget.  Called when a widget is about set focus.
 *
 * @param handler       Handler
 * @param browser       Browser
 *
 * @return RV_CONTINUE  Let the widget set focus.
 * @return RV_HANDLED   Stops the widget from stealing focus.
 */
CEF_CALL handle_set_focus_widget(struct _cef_handler_t* handler, cef_browser_t* browser)
{
    CF_DB_START;

    BaseRelease(browser);

    CF_DB_END;
    return RV_HANDLED;
}
tux316
Newbie
 
Posts: 2
Joined: Sat May 30, 2009 11:09 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 21 guests