Branch 2526 : CEF Activates Browser Window by Default

Issue #1856 new
Akash Jana created an issue

What steps will reproduce the problem?

Write a Custom App and don't activate the hosting window. When the CEF Browser window is created and shown, it gets activated and takes Focus.

What is the expected output? What do you see instead?

CEF Browser contains a setting or doesn't take focus when its created the first time

What version of the product are you using? On what operating system?

Cef 3.2526.1262 on Windows

Does the problem reproduce with the cefclient or cefsimple sample application at the same version? How about with a newer or older version?

N/A. This issue didn't repro on 2357 branch IIRC

Does the problem reproduce with Google Chrome at the same version? How about with a newer or older version?

N/A

Issue discussed at http://magpcss.org/ceforum/viewtopic.php?f=6&t=13891. Consider changing the default activation such that it doesnt' activate or create a browser configuration to control this.

Comments (18)

  1. Aasma Garg

    @jiaojiaoxia I dont know when PR will be approved. Just one query, did you try applying those changes ?

  2. Marshall Greenblatt

    On Windows WS_EX_NOACTIVATE can be used to control initial window activation starting with master revision b8eaec0, 3683 branch revision db143a5 and 3626 branch revision 00e6af4.

    To test:

    1. Open a console window.
    2. Run cefclient.exe --no-activate.
      • Verify that focus stays in the console window.
      • Click on the browser window. Verify that a task bar button is created for the window (Windows 10), and that focus is assigned to the window as expected.
    3. Select Tests > New Window.
      • Verify that focus stays in the previous browser window.
      • Click on the new browser window. Verify that a task bar button is created for the window (Windows 10), and that focus is assigned to the window as expected.
    4. Select Tests > New Popup.
      • Verify that focus stays in the previous browser window.
      • Click on the new browser window. Verify that a task bar button is created for the window (Windows 10), and that focus is assigned to the window as expected.

    Repeat the above tests with cefclient.exe --no-activate --off-screen-rendering-enabled.

  3. Marshall Greenblatt

    To implement on Linux we can probably have CefWindowDelegateView::CanActivate return false just around the call to Widget::Show in CefBrowserPlatformDelegateNativeLinux::CreateHostWindow. That should give us the ui::SHOW_STATE_INACTIVE status that we need in DesktopWindowTreeHostX11::Show without otherwise breaking focus handling.

    We can add an int cef_window_info_t::no_activate in cef_types_linux.h to control this behavior.

  4. Marc Durdin

    Thank you Marshall for your work on this.

    From my perspective, the WS_EX_NOACTIVATE fix adds a number of undesirable side effects. Quoting from MSDN:

    • A top-level window created with this style does not become the foreground window when the user clicks it. The system does not bring this window to the foreground when the user minimizes or closes the foreground window.
    • The window should not be activated through programmatic access or via keyboard navigation by accessible technology, such as Narrator.
    • To activate the window, use the SetActiveWindow or SetForegroundWindow function.
    • The window does not appear on the taskbar by default. To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style.

    Some of these side-effects change the activation behaviour of our application in awkward ways which are not trivial to resolve. (We are hosting the CEF window within an existing legacy Delphi (via CEF4Delphi) app and have many native widgets and forms.)

    It seems to me that a safer resolution is for CEF to call ShowWindow(hwnd, SWP_NOACTIVATE) as it appears was the intent of the original PR, and allow separation of the concerns of displaying and activating the CEF windows. What do you think?

  5. Emil Kalchev

    Its unbelievable how such simple issue can cause so much trouble for years. ShowWindow(hwnd, SWP_NOACTIVATE) should fix all this problems

  6. Marshall Greenblatt

    Chromium is complex, it’s hard to say what will work without trying it in the context of a PR. If you would like to submit such a PR we can have the discussion there.

  7. Roberto Neto

    Guys, how is this issue? I facing problems with this also…

    Even creating the cef window with WS_EX_NOACTIVATE the problem continues to occur.

    I also tried the client args --no-activate and --off-screen-rendering-enabled but without success.

  8. Kristjan Skutta

    I also had this issue, currently using CEF 4103 / 83.0.4103.97, and I modified HWNDMessageHandler::Activate and HWNDMessageHandler::Deactivate for this, so they wouldn’t call SetForegroundWindow if a custom command line switch was present. This fix is only for Windows though.

  9. Kristjan Skutta

    In hwnd_message_handler.cc:

    void HWNDMessageHandler::Activate() {
      if (IsMinimized()) {
        base::AutoReset<bool> restoring_activate(&notify_restore_on_activate_,
                                                 true);
        ::ShowWindow(hwnd(), SW_RESTORE);
      }
    
      ::SetWindowPos(hwnd(), HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
    
      if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
              views::switches::kMYNEWSWITCH)) {
        SetForegroundWindow(hwnd());
      }
    }
    
    void HWNDMessageHandler::Deactivate() {
      if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
              views::switches::kMYNEWSWITCH)) {
        HWND next_hwnd = ::GetNextWindow(hwnd(), GW_HWNDNEXT);
        while (next_hwnd) {
          if (::IsWindowVisible(next_hwnd)) {
            ::SetForegroundWindow(next_hwnd);
            return;
          }
          next_hwnd = ::GetNextWindow(next_hwnd, GW_HWNDNEXT);
        }
      }
    }
    

    views::switches::kMYNEWSWITCH is just a string literal that you need to replace and you need to include #include "base/command_line.h" for this.

  10. Log in to comment