From 9f15aa7692178890d7c74e1e0ef79a329c36f9fa Mon Sep 17 00:00:00 2001 From: Lucas Bullen Date: Wed, 18 Apr 2018 10:15:48 -0400 Subject: Bug 232501 - Snippet to compute browser size Change-Id: Ib12e18cbdb4a0b1773a74c8818d4f0ca3469266a Signed-off-by: Lucas Bullen --- .../src/org/eclipse/swt/snippets/Snippet372.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java new file mode 100644 index 0000000000..289780cfca --- /dev/null +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet372.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lucas Bullen (Red Hat Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * Browser: Resize the shell to the fit the html content after it's loaded. + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + * + * @since 4.8 + */ +import org.eclipse.swt.*; +import org.eclipse.swt.browser.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet372 { + static Double width = null; + public static void main(String [] args) { + int maximumWidth = 200; + int maximumHeight= 2000; + + String html = "HTML Test"; + for (int i = 0; i < 15; i++) html += "

This is line "+i+"

"; + html += ""; + + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new FillLayout()); + Browser browser; + try { + browser = new Browser(shell, SWT.NONE); + } catch (SWTError e) { + System.out.println("Could not instantiate Browser: " + e.getMessage()); + display.dispose(); + return; + } + browser.setText(html); + browser.addProgressListener(ProgressListener.completedAdapter(event -> { + // Set the display to something known to be smaller than the content + shell.setSize(50, 50); + browser.execute("document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"nowrap\""); //$NON-NLS-1$ + // Save the width to either be a decided maximum or the browser's content width plus the margin + Double width = Math.min(maximumWidth, 10 + (Double) browser.evaluate("return document.body.scrollWidth;")); //$NON-NLS-1$ + shell.setSize(width.intValue(), 0); + browser.execute("document.getElementsByTagName(\"html\")[0].style.whiteSpace = \"normal\""); //$NON-NLS-1$ + shell.layout(); + // Set the height to either be a decided maximum or the browser's content height plus the margin + Double height = Math.min(maximumHeight, 5 + (Double) browser.evaluate("return document.body.scrollHeight;")); //$NON-NLS-1$ + shell.setSize(width.intValue(), height.intValue()); + })); + + shell.open(); + while (!shell.isDisposed()) { + shell.layout(); + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } +} -- cgit v1.2.3