mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
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.
186 lines
6.0 KiB
C++
186 lines
6.0 KiB
C++
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
// can be found in the LICENSE file.
|
|
|
|
#include "tests/cefsimple/simple_app.h"
|
|
|
|
#include <string>
|
|
|
|
#include "include/cef_browser.h"
|
|
#include "include/cef_command_line.h"
|
|
#include "include/views/cef_browser_view.h"
|
|
#include "include/views/cef_window.h"
|
|
#include "include/wrapper/cef_helpers.h"
|
|
#include "tests/cefsimple/simple_handler.h"
|
|
|
|
namespace {
|
|
|
|
// When using the Views framework this object provides the delegate
|
|
// implementation for the CefWindow that hosts the Views-based browser.
|
|
class SimpleWindowDelegate : public CefWindowDelegate {
|
|
public:
|
|
SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view,
|
|
cef_runtime_style_t runtime_style,
|
|
cef_show_state_t initial_show_state)
|
|
: browser_view_(browser_view),
|
|
runtime_style_(runtime_style),
|
|
initial_show_state_(initial_show_state) {}
|
|
|
|
void OnWindowCreated(CefRefPtr<CefWindow> window) override {
|
|
// Add the browser view and show the window.
|
|
window->AddChildView(browser_view_);
|
|
|
|
if (initial_show_state_ != CEF_SHOW_STATE_HIDDEN) {
|
|
window->Show();
|
|
}
|
|
}
|
|
|
|
void OnWindowDestroyed(CefRefPtr<CefWindow> window) override {
|
|
browser_view_ = nullptr;
|
|
}
|
|
|
|
bool CanClose(CefRefPtr<CefWindow> window) override {
|
|
// Allow the window to close if the browser says it's OK.
|
|
CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
|
|
if (browser) {
|
|
return browser->GetHost()->TryCloseBrowser();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
|
|
return CefSize(800, 600);
|
|
}
|
|
|
|
cef_show_state_t GetInitialShowState(CefRefPtr<CefWindow> window) override {
|
|
return initial_show_state_;
|
|
}
|
|
|
|
cef_runtime_style_t GetWindowRuntimeStyle() override {
|
|
return runtime_style_;
|
|
}
|
|
|
|
private:
|
|
CefRefPtr<CefBrowserView> browser_view_;
|
|
const cef_runtime_style_t runtime_style_;
|
|
const cef_show_state_t initial_show_state_;
|
|
|
|
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
|
|
};
|
|
|
|
class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
|
|
public:
|
|
explicit SimpleBrowserViewDelegate(cef_runtime_style_t runtime_style)
|
|
: runtime_style_(runtime_style) {}
|
|
|
|
bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
|
|
CefRefPtr<CefBrowserView> popup_browser_view,
|
|
bool is_devtools) override {
|
|
// Create a new top-level Window for the popup. It will show itself after
|
|
// creation.
|
|
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(
|
|
popup_browser_view, runtime_style_, CEF_SHOW_STATE_NORMAL));
|
|
|
|
// We created the Window.
|
|
return true;
|
|
}
|
|
|
|
cef_runtime_style_t GetBrowserRuntimeStyle() override {
|
|
return runtime_style_;
|
|
}
|
|
|
|
private:
|
|
const cef_runtime_style_t runtime_style_;
|
|
|
|
IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
|
|
};
|
|
|
|
} // namespace
|
|
|
|
SimpleApp::SimpleApp() = default;
|
|
|
|
void SimpleApp::OnContextInitialized() {
|
|
CEF_REQUIRE_UI_THREAD();
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
CefCommandLine::GetGlobalCommandLine();
|
|
|
|
// Check if Alloy style will be used.
|
|
cef_runtime_style_t runtime_style = CEF_RUNTIME_STYLE_DEFAULT;
|
|
bool use_alloy_style = command_line->HasSwitch("use-alloy-style");
|
|
if (use_alloy_style) {
|
|
runtime_style = CEF_RUNTIME_STYLE_ALLOY;
|
|
}
|
|
|
|
// SimpleHandler implements browser-level callbacks.
|
|
CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_alloy_style));
|
|
|
|
// Specify CEF browser settings here.
|
|
CefBrowserSettings browser_settings;
|
|
|
|
std::string url;
|
|
|
|
// Check if a "--url=" value was provided via the command-line. If so, use
|
|
// that instead of the default URL.
|
|
url = command_line->GetSwitchValue("url");
|
|
if (url.empty()) {
|
|
url = "https://www.google.com";
|
|
}
|
|
|
|
// Views is enabled by default (add `--use-native` to disable).
|
|
const bool use_views = !command_line->HasSwitch("use-native");
|
|
|
|
// If using Views create the browser using the Views framework, otherwise
|
|
// create the browser using the native platform framework.
|
|
if (use_views) {
|
|
// Create the BrowserView.
|
|
CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
|
|
handler, url, browser_settings, nullptr, nullptr,
|
|
new SimpleBrowserViewDelegate(runtime_style));
|
|
|
|
// Optionally configure the initial show state.
|
|
cef_show_state_t initial_show_state = CEF_SHOW_STATE_NORMAL;
|
|
const std::string& show_state_value =
|
|
command_line->GetSwitchValue("initial-show-state");
|
|
if (show_state_value == "minimized") {
|
|
initial_show_state = CEF_SHOW_STATE_MINIMIZED;
|
|
} else if (show_state_value == "maximized") {
|
|
initial_show_state = CEF_SHOW_STATE_MAXIMIZED;
|
|
}
|
|
#if defined(OS_MAC)
|
|
// Hidden show state is only supported on MacOS.
|
|
else if (show_state_value == "hidden") {
|
|
initial_show_state = CEF_SHOW_STATE_HIDDEN;
|
|
}
|
|
#endif
|
|
|
|
// Create the Window. It will show itself after creation.
|
|
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(
|
|
browser_view, runtime_style, initial_show_state));
|
|
} else {
|
|
// Information used when creating the native window.
|
|
CefWindowInfo window_info;
|
|
|
|
#if defined(OS_WIN)
|
|
// On Windows we need to specify certain flags that will be passed to
|
|
// CreateWindowEx().
|
|
window_info.SetAsPopup(nullptr, "cefsimple");
|
|
#endif
|
|
|
|
// Alloy style will create a basic native window. Chrome style will create a
|
|
// fully styled Chrome UI window.
|
|
window_info.runtime_style = runtime_style;
|
|
|
|
// Create the first browser window.
|
|
CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
|
|
nullptr, nullptr);
|
|
}
|
|
}
|
|
|
|
CefRefPtr<CefClient> SimpleApp::GetDefaultClient() {
|
|
// Called when a new browser window is created via Chrome style UI.
|
|
return SimpleHandler::GetInstance();
|
|
}
|