views: Fix Chrome style browser RequestFocus behavior (fixes #3819)

Fix implementation of CefBrowserView::RequestFocus for Chrome style
browsers. Match Alloy style behavior of requesting browser focus
(calling OnSetFocus) after initial navigation. Add CefView::HasFocus
and CefWindow::GetFocusedView that can be used in combination with
CefWindow::IsActive to determine global keyboard focus.

Update sample applications for the new behavior.

In cefclient:
- Browser receives initial focus via ViewsWindow::RequestBrowserFocus.
- When running with `--show-overlay-browser` (see #3790):
  - Give initial focus to the overlay browser.
  - Change the overlay popout shortcut to CTRL+SHIFT+O to avoid
    assigning focus to the menu in the main window.
  - Switching from overlay in the main window to popout browser
    window will give focus to the popout browser.
  - Switching from popout browser to overlay will leave current focus
    unchanged (e.g. in the overlay browser, or somewhere else). User
    gesture to activate the main window may be required on Mac/Linux.
- When running with `--no-active` don't give initial focus to either
  browser.

In cefsimple:
- Browser receives initial focus via default handling.
This commit is contained in:
Marshall Greenblatt
2024-11-05 14:58:45 -05:00
parent 9d40fa604d
commit b91be9fcc9
53 changed files with 652 additions and 91 deletions

View File

@@ -14,7 +14,9 @@ namespace client {
// Abstract base class for client handlers.
class BaseClientHandler : public CefClient,
public CefFocusHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRequestHandler,
public CefResourceRequestHandler {
public:
@@ -28,17 +30,28 @@ class BaseClientHandler : public CefClient,
static CefRefPtr<BaseClientHandler> GetForClient(CefRefPtr<CefClient> client);
// CefClient methods
CefRefPtr<CefFocusHandler> GetFocusHandler() override { return this; }
CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }
CefRefPtr<CefLoadHandler> GetLoadHandler() override { return this; }
CefRefPtr<CefRequestHandler> GetRequestHandler() override { return this; }
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) override;
// CefFocusHandler methods
bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) override;
// CefLifeSpanHandler methods
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
// CefLoadHandler methods
void OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading,
bool canGoBack,
bool canGoForward) override;
// CefRequestHandler methods
bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
@@ -95,6 +108,8 @@ class BaseClientHandler : public CefClient,
void SetHangAction(HangAction action);
HangAction GetHangAction() const;
bool ShouldRequestFocus();
// Used to determine the object type for each concrete implementation.
virtual const void* GetTypeKey() const = 0;
@@ -129,6 +144,9 @@ class BaseClientHandler : public CefClient,
HangAction hang_action_ = HangAction::kDefault;
// True for the initial navigation after browser creation.
bool initial_navigation_ = true;
DISALLOW_COPY_AND_ASSIGN(BaseClientHandler);
};