linux 如何在全屏模式下启动cefsimple?

djp7away  于 2023-05-22  发布在  Linux
关注(0)|答案(1)|浏览(278)

我想在Ubuntu 20.04上以全屏模式启动cefsimple应用程序(作为cef smaple项目)。
Spotify AutoBuild下载cef,然后将代码添加到cefsimple项目中的OnWindowCreated或OnAfterCreated中。但似乎不起作用。有人有好的想法吗?谢谢

/* 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
}
nr9pn0ug

nr9pn0ug1#

我还努力使窗口全屏。最后我做的是通过X11 API最大化窗口。
试试这个:

#include <X11/Xlib.h>

void makeBrowserWindowFullscreen(CefRefPtr<CefBrowser> browser) {
    auto xDisplay = cef_get_xdisplay();
    auto xWindow = browser->GetHost()->GetWindowHandle();

    int screen_number = DefaultScreen(xDisplay);
    auto display_width = DisplayWidth(xDisplay, screen_number);
    auto display_height = DisplayHeight(xDisplay, screen_number);
    XResizeWindow(xDisplay, xWindow, display_width * 2, display_height * 2);
    XMoveWindow(xDisplay, xWindow, 0, 0);
    XFlush(xDisplay);
  }
}

然后,只需在OnAfterCreated方法中调用makeBrowserWindowFullScreen(browser)

void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
  CEF_REQUIRE_UI_THREAD();
  
  makeBrowserWindowFullScreen(browser);
}

更新

makeBrowserWindowFullscreen的另一种实现可以通过发送_NET_WM_STATE_FULLSCREEN消息来最大化窗口:

#include <X11/Xlib.h>

#define _NET_WM_STATE_ADD 1

void makeBrowserWindowFullscreen(CefRefPtr<CefBrowser> browser) {
  auto xDisplay = cef_get_xdisplay();
  auto xWindow = browser->GetHost()->GetWindowHandle();

  Atom wm_state = XInternAtom(xDisplay, "_NET_WM_STATE", False);
  Atom wm_fullscreen = XInternAtom(xDisplay, "_NET_WM_STATE_FULLSCREEN", False);

  XEvent ev = {0};

  ev.type = ClientMessage;
  ev.xclient.window = xWindow;
  ev.xclient.message_type = wm_state;
  ev.xclient.format = 32;
  ev.xclient.data.l[0] = _NET_WM_STATE_ADD;
  ev.xclient.data.l[1] = wm_fullscreen;
  ev.xclient.data.l[2] = 0;

  XSendEvent(xDisplay, DefaultRootWindow(xDisplay), False, SubstructureNotifyMask | SubstructureRedirectMask, &ev);
  XFlush(xDisplay);
}

相关问题