Skip to content

Commit

Permalink
Add CefDisplayHandler.onFullscreenModeChange callback (fixes chromium…
Browse files Browse the repository at this point in the history
…embedded#239)

- Detailed test: Don't display error for frames other than main frame
  • Loading branch information
millosr authored and magreenblatt committed Jan 19, 2024
1 parent b5612ba commit 3df5f07
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 14 deletions.
6 changes: 6 additions & 0 deletions java/org/cef/CefClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ public void onTitleChange(CefBrowser browser, String title) {
displayHandler_.onTitleChange(browser, title);
}

@Override
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
if (displayHandler_ != null && browser != null)
displayHandler_.OnFullscreenModeChange(browser, fullscreen);
}

@Override
public boolean onTooltip(CefBrowser browser, String text) {
if (displayHandler_ != null && browser != null) {
Expand Down
7 changes: 7 additions & 0 deletions java/org/cef/handler/CefDisplayHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public interface CefDisplayHandler {
*/
public void onTitleChange(CefBrowser browser, String title);

/**
* Browser fullscreen mode changed.
* @param browser The browser generating the event.
* @param fullscreen True if fullscreen mode is on.
*/
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen);

/**
* About to display a tooltip.
* @param browser The browser generating the event.
Expand Down
5 changes: 5 additions & 0 deletions java/org/cef/handler/CefDisplayHandlerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void onTitleChange(CefBrowser browser, String title) {
return;
}

@Override
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
return;
}

@Override
public boolean onTooltip(CefBrowser browser, String text) {
return false;
Expand Down
68 changes: 57 additions & 11 deletions java/tests/detailed/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.cef.network.CefCookieManager;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.KeyboardFocusManager;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
Expand All @@ -30,6 +32,7 @@

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import tests.detailed.dialog.DownloadDialog;
import tests.detailed.handler.AppHandler;
Expand Down Expand Up @@ -101,6 +104,8 @@ public static void main(String[] args) {
private boolean browserFocus_ = true;
private boolean osr_enabled_;
private boolean transparent_painting_enabled_;
private JPanel contentPanel_;
private JFrame fullscreenFrame_;

public MainFrame(boolean osrEnabled, boolean transparentPaintingEnabled,
boolean createImmediately, int windowless_frame_rate, String[] args) {
Expand Down Expand Up @@ -178,6 +183,10 @@ public void onTitleChange(CefBrowser browser, String title) {
public void onStatusMessage(CefBrowser browser, String value) {
status_panel_.setStatusText(value);
}
@Override
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
setBrowserFullscreen(fullscreen);
}
});

// 2.2) To disable/enable navigation buttons and to display a prgress bar
Expand All @@ -190,19 +199,25 @@ public void onStatusMessage(CefBrowser browser, String value) {
@Override
public void onLoadingStateChange(CefBrowser browser, boolean isLoading,
boolean canGoBack, boolean canGoForward) {
control_pane_.update(browser, isLoading, canGoBack, canGoForward);
status_panel_.setIsInProgress(isLoading);

if (!isLoading && !errorMsg_.isEmpty()) {
browser.loadURL(DataUri.create("text/html", errorMsg_));
errorMsg_ = "";
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
control_pane_.update(browser, isLoading, canGoBack, canGoForward);
status_panel_.setIsInProgress(isLoading);

if (!isLoading && !errorMsg_.isEmpty()) {
browser.loadURL(DataUri.create("text/html", errorMsg_));
errorMsg_ = "";
}
}
});
}

@Override
public void onLoadError(CefBrowser browser, CefFrame frame, ErrorCode errorCode,
String errorText, String failedUrl) {
if (errorCode != ErrorCode.ERR_NONE && errorCode != ErrorCode.ERR_ABORTED) {
if (errorCode != ErrorCode.ERR_NONE && errorCode != ErrorCode.ERR_ABORTED
&& frame == browser.getMainFrame()) {
errorMsg_ = "<html><head>";
errorMsg_ += "<title>Error while loading</title>";
errorMsg_ += "</head><body>";
Expand All @@ -224,8 +239,8 @@ public void onLoadError(CefBrowser browser, CefFrame frame, ErrorCode errorCode,
setBrowser(browser);

// Set up the UI for this example implementation.
JPanel contentPanel = createContentPanel();
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel_ = createContentPanel();
getContentPane().add(contentPanel_, BorderLayout.CENTER);

// Clear focus from the browser when the address field gains focus.
control_pane_.getAddressField().addFocusListener(new FocusAdapter() {
Expand Down Expand Up @@ -257,7 +272,7 @@ public void onTakeFocus(CefBrowser browser, boolean next) {
if (createImmediately) browser.createImmediately();

// Add the browser to the UI.
contentPanel.add(getBrowser().getUIComponent(), BorderLayout.CENTER);
contentPanel_.add(getBrowser().getUIComponent(), BorderLayout.CENTER);

MenuBar menuBar = new MenuBar(
this, browser, control_pane_, downloadDialog, CefCookieManager.getGlobalManager());
Expand All @@ -277,6 +292,8 @@ public void onTakeFocus(CefBrowser browser, boolean next) {
menuBar.addBookmark("Spellcheck Test", "client://tests/spellcheck.html");
menuBar.addBookmark("LocalStorage Test", "client://tests/localstorage.html");
menuBar.addBookmark("Transparency Test", "client://tests/transparency.html");
menuBar.addBookmark("Fullscreen Test",
"https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_fullscreen2");
menuBar.addBookmarkSeparator();
menuBar.addBookmark(
"javachromiumembedded", "https://bitbucket.org/chromiumembedded/java-cef");
Expand All @@ -300,4 +317,33 @@ public boolean isOsrEnabled() {
public boolean isTransparentPaintingEnabled() {
return transparent_painting_enabled_;
}

public void setBrowserFullscreen(boolean fullscreen) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Component browserUI = getBrowser().getUIComponent();
if (fullscreen) {
if (fullscreenFrame_ == null) {
fullscreenFrame_ = new JFrame();
fullscreenFrame_.setUndecorated(true);
fullscreenFrame_.setResizable(true);
}
GraphicsConfiguration gc = MainFrame.this.getGraphicsConfiguration();
fullscreenFrame_.setBounds(gc.getBounds());
gc.getDevice().setFullScreenWindow(fullscreenFrame_);

contentPanel_.remove(browserUI);
fullscreenFrame_.add(browserUI);
fullscreenFrame_.setVisible(true);
fullscreenFrame_.validate();
} else {
fullscreenFrame_.remove(browserUI);
fullscreenFrame_.setVisible(false);
contentPanel_.add(browserUI, BorderLayout.CENTER);
contentPanel_.validate();
}
}
});
}
}
42 changes: 39 additions & 3 deletions java/tests/simple/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand All @@ -26,7 +27,9 @@
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
* This is a simple example application using JCEF.
Expand All @@ -46,8 +49,9 @@ public class MainFrame extends JFrame {
private final CefApp cefApp_;
private final CefClient client_;
private final CefBrowser browser_;
private final Component browerUI_;
private final Component browserUI_;
private boolean browserFocus_ = true;
private JFrame fullscreenFrame_;

/**
* To display a simple browser window, it suffices completely to create an
Expand Down Expand Up @@ -105,7 +109,7 @@ public void stateHasChanged(org.cef.CefApp.CefAppState state) {
// The UI component is inherited from a java.awt.Component and therefore
// it can be embedded into any AWT UI.
browser_ = client_.createBrowser(startURL, useOSR, isTransparent);
browerUI_ = browser_.getUIComponent();
browserUI_ = browser_.getUIComponent();

// (4) For this minimal browser, we need only a text field to enter an URL
// we want to navigate to and a CefBrowser window to display the content
Expand All @@ -128,6 +132,10 @@ public void actionPerformed(ActionEvent e) {
public void onAddressChange(CefBrowser browser, CefFrame frame, String url) {
address_.setText(url);
}
@Override
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
setBrowserFullscreen(fullscreen);
}
});

// Clear focus from the browser when the address field gains focus.
Expand Down Expand Up @@ -160,7 +168,7 @@ public void onTakeFocus(CefBrowser browser, boolean next) {
// (5) All UI components are assigned to the default content pane of this
// JFrame and afterwards the frame is made visible to the user.
getContentPane().add(address_, BorderLayout.NORTH);
getContentPane().add(browerUI_, BorderLayout.CENTER);
getContentPane().add(browserUI_, BorderLayout.CENTER);
pack();
setSize(800, 600);
setVisible(true);
Expand All @@ -177,6 +185,34 @@ public void windowClosing(WindowEvent e) {
});
}

public void setBrowserFullscreen(boolean fullscreen) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (fullscreen) {
if (fullscreenFrame_ == null) {
fullscreenFrame_ = new JFrame();
fullscreenFrame_.setUndecorated(true);
fullscreenFrame_.setResizable(true);
}
GraphicsConfiguration gc = MainFrame.this.getGraphicsConfiguration();
fullscreenFrame_.setBounds(gc.getBounds());
gc.getDevice().setFullScreenWindow(fullscreenFrame_);

getContentPane().remove(browserUI_);
fullscreenFrame_.add(browserUI_);
fullscreenFrame_.setVisible(true);
fullscreenFrame_.validate();
} else {
fullscreenFrame_.remove(browserUI_);
fullscreenFrame_.setVisible(false);
getContentPane().add(browserUI_, BorderLayout.CENTER);
getContentPane().validate();
}
}
});
}

public static void main(String[] args) {
// Perform startup initialization on platforms that require it.
if (!CefApp.startup(args)) {
Expand Down
12 changes: 12 additions & 0 deletions native/display_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ void DisplayHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
jbrowser.get(), jtitle.get());
}

void DisplayHandler::OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
bool fullscreen) {
ScopedJNIEnv env;
if (!env)
return;

ScopedJNIBrowser jbrowser(env, browser);
JNI_CALL_VOID_METHOD(env, handle_, "OnFullscreenModeChange",
"(Lorg/cef/browser/CefBrowser;Z)V", jbrowser.get(),
(jboolean)fullscreen);
}

bool DisplayHandler::OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text) {
ScopedJNIEnv env;
if (!env)
Expand Down
2 changes: 2 additions & 0 deletions native/display_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class DisplayHandler : public CefDisplayHandler {
const CefString& url) override;
void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) override;
void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
bool fullscreen) override;
bool OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text) override;
void OnStatusMessage(CefRefPtr<CefBrowser> browser,
const CefString& value) override;
Expand Down

0 comments on commit 3df5f07

Please sign in to comment.