Skip to content

Commit 3df5f07

Browse files
millosrmagreenblatt
authored andcommitted
Add CefDisplayHandler.onFullscreenModeChange callback (fixes chromiumembedded#239)
- Detailed test: Don't display error for frames other than main frame
1 parent b5612ba commit 3df5f07

7 files changed

Lines changed: 128 additions & 14 deletions

File tree

java/org/cef/CefClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ public void onTitleChange(CefBrowser browser, String title) {
311311
displayHandler_.onTitleChange(browser, title);
312312
}
313313

314+
@Override
315+
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
316+
if (displayHandler_ != null && browser != null)
317+
displayHandler_.OnFullscreenModeChange(browser, fullscreen);
318+
}
319+
314320
@Override
315321
public boolean onTooltip(CefBrowser browser, String text) {
316322
if (displayHandler_ != null && browser != null) {

java/org/cef/handler/CefDisplayHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public interface CefDisplayHandler {
2828
*/
2929
public void onTitleChange(CefBrowser browser, String title);
3030

31+
/**
32+
* Browser fullscreen mode changed.
33+
* @param browser The browser generating the event.
34+
* @param fullscreen True if fullscreen mode is on.
35+
*/
36+
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen);
37+
3138
/**
3239
* About to display a tooltip.
3340
* @param browser The browser generating the event.

java/org/cef/handler/CefDisplayHandlerAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public void onTitleChange(CefBrowser browser, String title) {
2424
return;
2525
}
2626

27+
@Override
28+
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
29+
return;
30+
}
31+
2732
@Override
2833
public boolean onTooltip(CefBrowser browser, String text) {
2934
return false;

java/tests/detailed/MainFrame.java

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.cef.network.CefCookieManager;
2323

2424
import java.awt.BorderLayout;
25+
import java.awt.Component;
26+
import java.awt.GraphicsConfiguration;
2527
import java.awt.KeyboardFocusManager;
2628
import java.awt.event.FocusAdapter;
2729
import java.awt.event.FocusEvent;
@@ -30,6 +32,7 @@
3032

3133
import javax.swing.JFrame;
3234
import javax.swing.JPanel;
35+
import javax.swing.SwingUtilities;
3336

3437
import tests.detailed.dialog.DownloadDialog;
3538
import tests.detailed.handler.AppHandler;
@@ -101,6 +104,8 @@ public static void main(String[] args) {
101104
private boolean browserFocus_ = true;
102105
private boolean osr_enabled_;
103106
private boolean transparent_painting_enabled_;
107+
private JPanel contentPanel_;
108+
private JFrame fullscreenFrame_;
104109

105110
public MainFrame(boolean osrEnabled, boolean transparentPaintingEnabled,
106111
boolean createImmediately, int windowless_frame_rate, String[] args) {
@@ -178,6 +183,10 @@ public void onTitleChange(CefBrowser browser, String title) {
178183
public void onStatusMessage(CefBrowser browser, String value) {
179184
status_panel_.setStatusText(value);
180185
}
186+
@Override
187+
public void OnFullscreenModeChange(CefBrowser browser, boolean fullscreen) {
188+
setBrowserFullscreen(fullscreen);
189+
}
181190
});
182191

183192
// 2.2) To disable/enable navigation buttons and to display a prgress bar
@@ -190,19 +199,25 @@ public void onStatusMessage(CefBrowser browser, String value) {
190199
@Override
191200
public void onLoadingStateChange(CefBrowser browser, boolean isLoading,
192201
boolean canGoBack, boolean canGoForward) {
193-
control_pane_.update(browser, isLoading, canGoBack, canGoForward);
194-
status_panel_.setIsInProgress(isLoading);
195-
196-
if (!isLoading && !errorMsg_.isEmpty()) {
197-
browser.loadURL(DataUri.create("text/html", errorMsg_));
198-
errorMsg_ = "";
199-
}
202+
SwingUtilities.invokeLater(new Runnable() {
203+
@Override
204+
public void run() {
205+
control_pane_.update(browser, isLoading, canGoBack, canGoForward);
206+
status_panel_.setIsInProgress(isLoading);
207+
208+
if (!isLoading && !errorMsg_.isEmpty()) {
209+
browser.loadURL(DataUri.create("text/html", errorMsg_));
210+
errorMsg_ = "";
211+
}
212+
}
213+
});
200214
}
201215

202216
@Override
203217
public void onLoadError(CefBrowser browser, CefFrame frame, ErrorCode errorCode,
204218
String errorText, String failedUrl) {
205-
if (errorCode != ErrorCode.ERR_NONE && errorCode != ErrorCode.ERR_ABORTED) {
219+
if (errorCode != ErrorCode.ERR_NONE && errorCode != ErrorCode.ERR_ABORTED
220+
&& frame == browser.getMainFrame()) {
206221
errorMsg_ = "<html><head>";
207222
errorMsg_ += "<title>Error while loading</title>";
208223
errorMsg_ += "</head><body>";
@@ -224,8 +239,8 @@ public void onLoadError(CefBrowser browser, CefFrame frame, ErrorCode errorCode,
224239
setBrowser(browser);
225240

226241
// Set up the UI for this example implementation.
227-
JPanel contentPanel = createContentPanel();
228-
getContentPane().add(contentPanel, BorderLayout.CENTER);
242+
contentPanel_ = createContentPanel();
243+
getContentPane().add(contentPanel_, BorderLayout.CENTER);
229244

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

259274
// Add the browser to the UI.
260-
contentPanel.add(getBrowser().getUIComponent(), BorderLayout.CENTER);
275+
contentPanel_.add(getBrowser().getUIComponent(), BorderLayout.CENTER);
261276

262277
MenuBar menuBar = new MenuBar(
263278
this, browser, control_pane_, downloadDialog, CefCookieManager.getGlobalManager());
@@ -277,6 +292,8 @@ public void onTakeFocus(CefBrowser browser, boolean next) {
277292
menuBar.addBookmark("Spellcheck Test", "client://tests/spellcheck.html");
278293
menuBar.addBookmark("LocalStorage Test", "client://tests/localstorage.html");
279294
menuBar.addBookmark("Transparency Test", "client://tests/transparency.html");
295+
menuBar.addBookmark("Fullscreen Test",
296+
"https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_fullscreen2");
280297
menuBar.addBookmarkSeparator();
281298
menuBar.addBookmark(
282299
"javachromiumembedded", "https://bitbucket.org/chromiumembedded/java-cef");
@@ -300,4 +317,33 @@ public boolean isOsrEnabled() {
300317
public boolean isTransparentPaintingEnabled() {
301318
return transparent_painting_enabled_;
302319
}
320+
321+
public void setBrowserFullscreen(boolean fullscreen) {
322+
SwingUtilities.invokeLater(new Runnable() {
323+
@Override
324+
public void run() {
325+
Component browserUI = getBrowser().getUIComponent();
326+
if (fullscreen) {
327+
if (fullscreenFrame_ == null) {
328+
fullscreenFrame_ = new JFrame();
329+
fullscreenFrame_.setUndecorated(true);
330+
fullscreenFrame_.setResizable(true);
331+
}
332+
GraphicsConfiguration gc = MainFrame.this.getGraphicsConfiguration();
333+
fullscreenFrame_.setBounds(gc.getBounds());
334+
gc.getDevice().setFullScreenWindow(fullscreenFrame_);
335+
336+
contentPanel_.remove(browserUI);
337+
fullscreenFrame_.add(browserUI);
338+
fullscreenFrame_.setVisible(true);
339+
fullscreenFrame_.validate();
340+
} else {
341+
fullscreenFrame_.remove(browserUI);
342+
fullscreenFrame_.setVisible(false);
343+
contentPanel_.add(browserUI, BorderLayout.CENTER);
344+
contentPanel_.validate();
345+
}
346+
}
347+
});
348+
}
303349
}

java/tests/simple/MainFrame.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.awt.BorderLayout;
1919
import java.awt.Component;
20+
import java.awt.GraphicsConfiguration;
2021
import java.awt.KeyboardFocusManager;
2122
import java.awt.event.ActionEvent;
2223
import java.awt.event.ActionListener;
@@ -26,7 +27,9 @@
2627
import java.awt.event.WindowEvent;
2728

2829
import javax.swing.JFrame;
30+
import javax.swing.JPanel;
2931
import javax.swing.JTextField;
32+
import javax.swing.SwingUtilities;
3033

3134
/**
3235
* This is a simple example application using JCEF.
@@ -46,8 +49,9 @@ public class MainFrame extends JFrame {
4649
private final CefApp cefApp_;
4750
private final CefClient client_;
4851
private final CefBrowser browser_;
49-
private final Component browerUI_;
52+
private final Component browserUI_;
5053
private boolean browserFocus_ = true;
54+
private JFrame fullscreenFrame_;
5155

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

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

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

188+
public void setBrowserFullscreen(boolean fullscreen) {
189+
SwingUtilities.invokeLater(new Runnable() {
190+
@Override
191+
public void run() {
192+
if (fullscreen) {
193+
if (fullscreenFrame_ == null) {
194+
fullscreenFrame_ = new JFrame();
195+
fullscreenFrame_.setUndecorated(true);
196+
fullscreenFrame_.setResizable(true);
197+
}
198+
GraphicsConfiguration gc = MainFrame.this.getGraphicsConfiguration();
199+
fullscreenFrame_.setBounds(gc.getBounds());
200+
gc.getDevice().setFullScreenWindow(fullscreenFrame_);
201+
202+
getContentPane().remove(browserUI_);
203+
fullscreenFrame_.add(browserUI_);
204+
fullscreenFrame_.setVisible(true);
205+
fullscreenFrame_.validate();
206+
} else {
207+
fullscreenFrame_.remove(browserUI_);
208+
fullscreenFrame_.setVisible(false);
209+
getContentPane().add(browserUI_, BorderLayout.CENTER);
210+
getContentPane().validate();
211+
}
212+
}
213+
});
214+
}
215+
180216
public static void main(String[] args) {
181217
// Perform startup initialization on platforms that require it.
182218
if (!CefApp.startup(args)) {

native/display_handler.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ void DisplayHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
112112
jbrowser.get(), jtitle.get());
113113
}
114114

115+
void DisplayHandler::OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
116+
bool fullscreen) {
117+
ScopedJNIEnv env;
118+
if (!env)
119+
return;
120+
121+
ScopedJNIBrowser jbrowser(env, browser);
122+
JNI_CALL_VOID_METHOD(env, handle_, "OnFullscreenModeChange",
123+
"(Lorg/cef/browser/CefBrowser;Z)V", jbrowser.get(),
124+
(jboolean)fullscreen);
125+
}
126+
115127
bool DisplayHandler::OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text) {
116128
ScopedJNIEnv env;
117129
if (!env)

native/display_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class DisplayHandler : public CefDisplayHandler {
2323
const CefString& url) override;
2424
void OnTitleChange(CefRefPtr<CefBrowser> browser,
2525
const CefString& title) override;
26+
void OnFullscreenModeChange(CefRefPtr<CefBrowser> browser,
27+
bool fullscreen) override;
2628
bool OnTooltip(CefRefPtr<CefBrowser> browser, CefString& text) override;
2729
void OnStatusMessage(CefRefPtr<CefBrowser> browser,
2830
const CefString& value) override;

0 commit comments

Comments
 (0)