Ubuntu 20.04下如何让CEF示例项目cefsimple全屏启动?
Ubuntu 20.04下CEF cefsimple全屏启动失效的解决思路
问题描述
在Ubuntu 20.04系统中,尝试将CEF示例项目cefsimple设置为全屏启动,已使用Spotify AutoBuild提供的CEF包,并在OnWindowCreated(simple_app.cc)和OnAfterCreated(simple_handler.cc)方法中添加了全屏代码,但未生效。添加的代码如下:
simple_app.cc 中修改的代码
class SimpleWindowDelegate : public CefWindowDelegate { public: explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view) : browser_view_(browser_view) {} void OnWindowCreated(CefRefPtr<CefWindow> window) override { // Add the browser view and show the window. window->AddChildView(browser_view_); window->Show(); // BEG: added window->SetFullscreen(true); // END // Give keyboard focus to the browser view. browser_view_->RequestFocus(); }
simple_handler.cc 中修改的代码
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) { CEF_REQUIRE_UI_THREAD(); // Add to the list of existing browsers. browser_list_.push_back(browser); // BEG: added CefRefPtr<CefBrowserView> browser_view = CefBrowserView::GetForBrowser(browser); if (browser_view) { CefRefPtr<CefWindow> window = browser_view->GetWindow(); if (window) { window->SetFullscreen(true); } } // END }
可行解决思路
延迟调用全屏操作:窗口创建后立即调用
SetFullscreen可能因窗口未完成初始化而失效,通过CefPostDelayedTask将操作延迟到UI线程后续执行,给窗口足够的初始化时间:void OnWindowCreated(CefRefPtr<CefWindow> window) override { window->AddChildView(browser_view_); window->Show(); browser_view_->RequestFocus(); // 延迟100ms执行全屏切换 CefPostDelayedTask(TID_UI, base::BindOnce( [](CefRefPtr<CefWindow> win) { win->SetFullscreen(true); }, window), 100); }指定窗口创建参数:创建窗口时使用无边框风格,避免窗口管理器的样式干扰全屏效果,修改
SimpleApp::OnContextInitialized中的窗口创建代码:void SimpleApp::OnContextInitialized() { CEF_REQUIRE_UI_THREAD(); CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine(); // 获取屏幕尺寸 CefSize screen_size = CefScreenInfo::GetPrimaryScreen().size; // 设置窗口创建参数 CefWindowCreateParams window_params; window_params.style = WS_POPUP; // 无边框窗口 window_params.width = screen_size.width; window_params.height = screen_size.height; // 创建浏览器视图 CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView( this, command_line->GetArg(0), browser_settings, nullptr, nullptr); // 创建顶层窗口并关联浏览器视图 CefRefPtr<CefWindow> window = CefWindow::CreateTopLevelWindow(window_params); window->SetDelegate(new SimpleWindowDelegate(browser_view)); }调用系统原生API实现全屏:如果CEF的
SetFullscreen方法在Linux下存在兼容性问题,直接使用X11 API发送全屏请求:#include <X11/Xlib.h> #include <X11/Xutil.h> void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) { CEF_REQUIRE_UI_THREAD(); browser_list_.push_back(browser); CefWindowHandle hwnd = browser->GetHost()->GetWindowHandle(); Display* display = XOpenDisplay(nullptr); if (!display || !hwnd) return; Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False); Atom fullscreen_atom = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False); XEvent xev{}; xev.type = ClientMessage; xev.xclient.window = hwnd; xev.xclient.message_type = wm_state; xev.xclient.format = 32; xev.xclient.data.l[0] = 1; // 启用全屏状态 xev.xclient.data.l[1] = fullscreen_atom; xev.xclient.data.l[2] = 0; XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev); XCloseDisplay(display); }编译时需要链接X11库,在项目的CMakeLists.txt中添加:
target_link_libraries(cefsimple X11)验证CEF版本与系统兼容性:检查Spotify AutoBuild的CEF版本是否适配Ubuntu 20.04,尝试更换为最新稳定版CEF,部分旧版本存在Linux全屏功能的bug。
排查窗口管理器限制:Ubuntu默认的GNOME窗口管理器可能限制应用全屏权限,可在系统设置的“窗口管理”选项中检查相关设置,或切换到Openbox等轻量窗口管理器测试,排除系统层面的干扰。
内容的提问来源于stack exchange,提问作者erinus
相关产品推荐
相关产品推荐

