1 package org
.argeo
.minidesktop
;
3 import java
.net
.InetAddress
;
4 import java
.net
.UnknownHostException
;
5 import java
.nio
.file
.Path
;
6 import java
.util
.Arrays
;
9 import org
.eclipse
.swt
.SWT
;
10 import org
.eclipse
.swt
.custom
.CTabFolder
;
11 import org
.eclipse
.swt
.custom
.CTabItem
;
12 import org
.eclipse
.swt
.events
.SelectionAdapter
;
13 import org
.eclipse
.swt
.events
.SelectionEvent
;
14 import org
.eclipse
.swt
.graphics
.Color
;
15 import org
.eclipse
.swt
.graphics
.Image
;
16 import org
.eclipse
.swt
.graphics
.ImageData
;
17 import org
.eclipse
.swt
.graphics
.Point
;
18 import org
.eclipse
.swt
.graphics
.Rectangle
;
19 import org
.eclipse
.swt
.layout
.GridData
;
20 import org
.eclipse
.swt
.layout
.GridLayout
;
21 import org
.eclipse
.swt
.program
.Program
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Control
;
24 import org
.eclipse
.swt
.widgets
.Display
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
26 import org
.eclipse
.swt
.widgets
.ToolBar
;
27 import org
.eclipse
.swt
.widgets
.ToolItem
;
29 /** A very minimalistic desktop manager based on Java and Eclipse SWT. */
30 public class MiniDesktopManager
{
31 private Display display
;
33 private Shell rootShell
;
34 private Shell toolBarShell
;
35 private CTabFolder tabFolder
;
36 private int maxTabTitleLength
= 16;
38 private final boolean fullscreen
;
39 private final boolean stacking
;
41 private MiniDesktopImages images
;
43 public MiniDesktopManager(boolean fullscreen
, boolean stacking
) {
44 this.fullscreen
= fullscreen
;
45 this.stacking
= stacking
;
49 Display
.setAppName("Mini SWT Desktop");
50 display
= Display
.getCurrent();
52 throw new IllegalStateException("Already a display " + display
);
53 display
= new Display();
55 if (display
.getTouchEnabled()) {
56 System
.out
.println("Touch enabled.");
59 images
= new MiniDesktopImages(display
);
64 rootShell
= new Shell(display
, SWT
.NO_TRIM
);
65 rootShell
.setFullScreen(true);
66 Rectangle bounds
= display
.getBounds();
67 rootShell
.setLocation(0, 0);
68 rootShell
.setSize(bounds
.width
, bounds
.height
);
70 rootShell
= new Shell(display
, SWT
.CLOSE
| SWT
.RESIZE
);
71 Rectangle shellArea
= rootShell
.computeTrim(200, 200, 800, 480);
72 rootShell
.setSize(shellArea
.width
, shellArea
.height
);
73 rootShell
.setText(Display
.getAppName());
74 rootShell
.setImage(images
.terminalIcon
);
77 rootShell
.setLayout(noSpaceGridLayout(new GridLayout(2, false)));
78 Composite toolBarArea
= new Composite(rootShell
, SWT
.NONE
);
79 toolBarArea
.setLayoutData(new GridData(toolBarSize
, rootShell
.getSize().y
));
83 toolBarShell
= new Shell(rootShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
84 toolBar
= new ToolBar(toolBarShell
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
87 toolBarArea
.setLayoutData(new GridData(toolBar
.getSize().x
, toolBar
.getSize().y
));
89 toolBar
= new ToolBar(toolBarArea
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
91 toolBarArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
95 tabFolder
= new CTabFolder(rootShell
, SWT
.MULTI
| SWT
.BORDER
| SWT
.BOTTOM
);
96 tabFolder
.setLayout(noSpaceGridLayout(new GridLayout()));
97 tabFolder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
99 Color selectionBackground
= display
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
100 tabFolder
.setSelectionBackground(selectionBackground
);
103 Control background
= createBackground(tabFolder
);
104 CTabItem homeTabItem
= new CTabItem(tabFolder
, SWT
.NONE
);
105 homeTabItem
.setText("Home");
106 homeTabItem
.setImage(images
.homeIcon
);
107 homeTabItem
.setControl(background
);
108 tabFolder
.setFocus();
110 createBackground(rootShell
);
114 // rootShell.layout(true, true);
116 if (toolBarShell
!= null) {
117 int toolBarShellY
= (display
.getBounds().height
- toolBar
.getSize().y
) / 2;
118 toolBarShell
.setLocation(0, toolBarShellY
);
123 protected void createDock(ToolBar toolBar
) {
125 addToolItem(toolBar
, images
.terminalIcon
, "Terminal", () -> {
126 String url
= System
.getProperty("user.home");
127 AppContext appContext
= createAppParent(images
.terminalIcon
);
128 new MiniTerminal(appContext
.getAppParent(), url
) {
131 protected void exitCalled() {
132 if (appContext
.shell
!= null)
133 appContext
.shell
.dispose();
134 if (appContext
.tabItem
!= null)
135 appContext
.tabItem
.dispose();
140 title
= System
.getProperty("user.name") + "@" + InetAddress
.getLocalHost().getHostName();
141 } catch (UnknownHostException e
) {
142 title
= System
.getProperty("user.name") + "@localhost";
144 if (appContext
.shell
!= null)
145 appContext
.shell
.setText(title
);
146 if (appContext
.tabItem
!= null) {
147 appContext
.tabItem
.setText(tabTitle(title
));
148 appContext
.tabItem
.setToolTipText(title
);
154 addToolItem(toolBar
, images
.browserIcon
, "Browser", () -> {
155 String url
= "https://start.duckduckgo.com/";
156 AppContext appContext
= createAppParent(images
.browserIcon
);
157 new MiniBrowser(appContext
.getAppParent(), url
, false, false) {
159 protected void titleChanged(String title
) {
160 if (appContext
.shell
!= null)
161 appContext
.shell
.setText(title
);
162 if (appContext
.tabItem
!= null) {
163 appContext
.tabItem
.setText(tabTitle(title
));
164 appContext
.tabItem
.setToolTipText(title
);
172 addToolItem(toolBar
, images
.explorerIcon
, "Explorer", () -> {
173 String url
= System
.getProperty("user.home");
174 AppContext appContext
= createAppParent(images
.explorerIcon
);
175 new MiniExplorer(appContext
.getAppParent(), url
) {
178 protected void pathChanged(Path path
) {
179 if (appContext
.shell
!= null)
180 appContext
.shell
.setText(path
.toString());
181 if (appContext
.tabItem
!= null) {
182 appContext
.tabItem
.setText(path
.getFileName().toString());
183 appContext
.tabItem
.setToolTipText(path
.toString());
191 new ToolItem(toolBar
, SWT
.SEPARATOR
);
194 addToolItem(toolBar
, images
.exitIcon
, "Exit", () -> rootShell
.dispose());
199 protected String
tabTitle(String title
) {
200 return title
.length() > maxTabTitleLength ? title
.substring(0, maxTabTitleLength
) : title
;
203 protected void addToolItem(ToolBar toolBar
, Image icon
, String name
, Runnable action
) {
204 ToolItem searchI
= new ToolItem(toolBar
, SWT
.PUSH
);
205 searchI
.setImage(icon
);
206 searchI
.setToolTipText(name
);
207 searchI
.addSelectionListener(new SelectionAdapter() {
210 public void widgetSelected(SelectionEvent e
) {
217 protected AppContext
createAppParent(Image icon
) {
219 Composite appParent
= new Composite(tabFolder
, SWT
.CLOSE
);
220 appParent
.setLayout(noSpaceGridLayout(new GridLayout()));
221 CTabItem item
= new CTabItem(tabFolder
, SWT
.CLOSE
);
223 item
.setControl(appParent
);
224 return new AppContext(item
);
226 Shell shell
= isFullscreen() ?
new Shell(rootShell
, SWT
.SHELL_TRIM
)
227 : new Shell(rootShell
.getDisplay(), SWT
.SHELL_TRIM
);
228 shell
.setImage(icon
);
229 return new AppContext(shell
);
233 protected void openApp(AppContext appContext
) {
234 if (appContext
.shell
!= null) {
235 Shell shell
= (Shell
) appContext
.shell
;
237 shell
.setSize(new Point(800, 480));
239 if (appContext
.tabItem
!= null) {
240 tabFolder
.setFocus();
241 tabFolder
.setSelection(appContext
.tabItem
);
245 protected Control
createBackground(Composite parent
) {
246 Composite backgroundArea
= new Composite(parent
, SWT
.NONE
);
247 backgroundArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
248 initBackground(backgroundArea
);
249 return backgroundArea
;
252 protected void initBackground(Composite backgroundArea
) {
253 MiniHomePart homePart
= new MiniHomePart() {
256 protected void fillAppsToolBar(ToolBar toolBar
) {
260 homePart
.createUiPart(backgroundArea
, null);
264 while (!rootShell
.isDisposed()) {
265 if (!display
.readAndDispatch())
270 public void dispose() {
271 if (!rootShell
.isDisposed())
275 protected boolean isFullscreen() {
279 protected boolean isStacking() {
283 protected Image
getIconForExt(String ext
) {
284 Program program
= Program
.findProgram(ext
);
286 return display
.getSystemImage(SWT
.ICON_INFORMATION
);
288 ImageData iconData
= program
.getImageData();
289 if (iconData
== null) {
290 return display
.getSystemImage(SWT
.ICON_INFORMATION
);
292 return new Image(display
, iconData
);
297 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
298 layout
.horizontalSpacing
= 0;
299 layout
.verticalSpacing
= 0;
300 layout
.marginWidth
= 0;
301 layout
.marginHeight
= 0;
305 public static void main(String
[] args
) {
306 List
<String
> options
= Arrays
.asList(args
);
307 if (options
.contains("--help")) {
308 System
.out
.println("Usage: java " + MiniDesktopManager
.class.getName().replace('.', '/') + " [OPTION]");
309 System
.out
.println("A minimalistic desktop manager based on Java and Eclipse SWT.");
310 System
.out
.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
311 System
.out
.println(" --stacking : open apps as tabs (default is to create new windows)");
312 System
.out
.println(" --help : print this help and exit");
315 boolean fullscreen
= options
.contains("--fullscreen");
316 boolean stacking
= options
.contains("--stacking");
318 MiniDesktopManager desktopManager
= new MiniDesktopManager(fullscreen
, stacking
);
319 desktopManager
.init();
320 desktopManager
.run();
321 desktopManager
.dispose();
327 private CTabItem tabItem
;
329 public AppContext(Shell shell
) {
333 public AppContext(CTabItem tabItem
) {
334 this.tabItem
= tabItem
;
337 Composite
getAppParent() {
341 return (Composite
) tabItem
.getControl();
342 throw new IllegalStateException();