2222import org .cef .network .CefCookieManager ;
2323
2424import java .awt .BorderLayout ;
25+ import java .awt .Component ;
26+ import java .awt .GraphicsConfiguration ;
2527import java .awt .KeyboardFocusManager ;
2628import java .awt .event .FocusAdapter ;
2729import java .awt .event .FocusEvent ;
3032
3133import javax .swing .JFrame ;
3234import javax .swing .JPanel ;
35+ import javax .swing .SwingUtilities ;
3336
3437import tests .detailed .dialog .DownloadDialog ;
3538import 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}
0 commit comments