1 package org
.argeo
.minidesktop
;
3 import java
.util
.Arrays
;
6 import org
.eclipse
.swt
.SWT
;
7 import org
.eclipse
.swt
.browser
.Browser
;
8 import org
.eclipse
.swt
.browser
.LocationAdapter
;
9 import org
.eclipse
.swt
.browser
.LocationEvent
;
10 import org
.eclipse
.swt
.events
.KeyAdapter
;
11 import org
.eclipse
.swt
.events
.KeyEvent
;
12 import org
.eclipse
.swt
.events
.SelectionAdapter
;
13 import org
.eclipse
.swt
.events
.SelectionEvent
;
14 import org
.eclipse
.swt
.graphics
.Point
;
15 import org
.eclipse
.swt
.graphics
.Rectangle
;
16 import org
.eclipse
.swt
.layout
.FillLayout
;
17 import org
.eclipse
.swt
.layout
.GridData
;
18 import org
.eclipse
.swt
.layout
.GridLayout
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Control
;
21 import org
.eclipse
.swt
.widgets
.Display
;
22 import org
.eclipse
.swt
.widgets
.Shell
;
23 import org
.eclipse
.swt
.widgets
.Text
;
25 /** A very minimalistic web browser based on {@link Browser}. */
26 public class MiniBrowser
{
27 private static Point defaultShellSize
= new Point(800, 480);
29 private Browser browser
;
30 private Text addressT
;
32 private final boolean fullscreen
;
33 private final boolean appMode
;
35 public MiniBrowser(Composite composite
, String url
, boolean fullscreen
, boolean appMode
) {
36 this.fullscreen
= fullscreen
;
37 this.appMode
= appMode
;
42 public Control
createUi(Composite parent
) {
43 parent
.setLayout(noSpaceGridLayout(new GridLayout()));
45 Control toolBar
= createToolBar(parent
);
46 toolBar
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
48 Control body
= createBody(parent
);
49 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
53 protected Control
createToolBar(Composite parent
) {
54 Composite toolBar
= new Composite(parent
, SWT
.NONE
);
55 toolBar
.setLayout(new FillLayout());
56 addressT
= new Text(toolBar
, SWT
.SINGLE
);
57 addressT
.addSelectionListener(new SelectionAdapter() {
60 public void widgetDefaultSelected(SelectionEvent e
) {
61 setUrl(addressT
.getText().trim());
67 protected Control
createBody(Composite parent
) {
68 browser
= new Browser(parent
, SWT
.NONE
);
70 browser
.addKeyListener(new KeyAdapter() {
72 public void keyPressed(KeyEvent e
) {
73 if (e
.keyCode
== 0x77 && e
.stateMask
== 0x40000) {// Ctrl+W
74 browser
.getShell().dispose();
78 browser
.addLocationListener(new LocationAdapter() {
80 public void changed(LocationEvent event
) {
81 System
.out
.println(event
);
83 addressT
.setText(event
.location
);
87 browser
.addTitleListener(e
-> titleChanged(e
.title
));
88 browser
.addOpenWindowListener((e
) -> {
89 e
.browser
= openNewBrowserWindow();
94 protected Browser
openNewBrowserWindow() {
97 // TODO manage multiple tabs?
100 Shell newShell
= new Shell(browser
.getDisplay(), SWT
.SHELL_TRIM
);
101 MiniBrowser newMiniBrowser
= new MiniBrowser(newShell
, null, false, isAppMode());
102 newShell
.setSize(defaultShellSize
);
104 return newMiniBrowser
.browser
;
108 protected boolean isFullScreen() {
112 void setUrl(String url
) {
113 if (browser
!= null && url
!= null && !url
.equals(browser
.getUrl()))
114 browser
.setUrl(url
.toString());
117 /** Called when URL changed; to be overridden, does nothing by default. */
118 protected void urlChanged(String url
) {
121 /** Called when title changed; to be overridden, does nothing by default. */
122 protected void titleChanged(String title
) {
125 protected Browser
getBrowser() {
129 protected boolean isAppMode() {
133 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
134 layout
.horizontalSpacing
= 0;
135 layout
.verticalSpacing
= 0;
136 layout
.marginWidth
= 0;
137 layout
.marginHeight
= 0;
141 public static void main(String
[] args
) {
142 List
<String
> options
= Arrays
.asList(args
);
143 if (options
.contains("--help")) {
144 System
.out
.println("Usage: java " + MiniBrowser
.class.getName().replace('.', '/') + " [OPTION] [URL]");
145 System
.out
.println("A minimalistic web browser Eclipse SWT Browser integration.");
146 System
.out
.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
147 System
.out
.println(" --app : open without an address bar and a toolbar");
148 System
.out
.println(" --help : print this help and exit");
151 boolean fullscreen
= options
.contains("--fullscreen");
152 boolean appMode
= options
.contains("--app");
153 String url
= "https://start.duckduckgo.com/";
154 if (options
.size() > 0) {
155 String last
= options
.get(options
.size() - 1);
156 if (!last
.startsWith("--"))
160 Display display
= Display
.getCurrent() == null ?
new Display() : Display
.getCurrent();
163 shell
= new Shell(display
, SWT
.NO_TRIM
);
164 shell
.setFullScreen(true);
165 Rectangle bounds
= display
.getBounds();
166 shell
.setSize(bounds
.width
, bounds
.height
);
168 shell
= new Shell(display
, SWT
.SHELL_TRIM
);
169 shell
.setSize(defaultShellSize
);
172 new MiniBrowser(shell
, url
, fullscreen
, appMode
) {
175 protected void titleChanged(String title
) {
176 shell
.setText(title
);
181 while (!shell
.isDisposed()) {
182 if (!display
.readAndDispatch())