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 images
= new MiniDesktopImages(display
);
60 rootShell
= new Shell(display
, SWT
.NO_TRIM
);
61 rootShell
.setFullScreen(true);
62 Rectangle bounds
= display
.getBounds();
63 rootShell
.setSize(bounds
.width
, bounds
.height
);
65 rootShell
= new Shell(display
, SWT
.CLOSE
| SWT
.RESIZE
);
66 Rectangle shellArea
= rootShell
.computeTrim(200, 200, 800, 480);
67 rootShell
.setSize(shellArea
.width
, shellArea
.height
);
68 rootShell
.setText(Display
.getAppName());
69 rootShell
.setImage(images
.terminalIcon
);
72 rootShell
.setLayout(noSpaceGridLayout(new GridLayout(2, false)));
73 Composite toolBarArea
= new Composite(rootShell
, SWT
.NONE
);
74 toolBarArea
.setLayoutData(new GridData(toolBarSize
, rootShell
.getSize().y
));
78 toolBarShell
= new Shell(rootShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
79 toolBar
= new ToolBar(toolBarShell
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
82 toolBarArea
.setLayoutData(new GridData(toolBar
.getSize().x
, toolBar
.getSize().y
));
84 toolBar
= new ToolBar(toolBarArea
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
86 toolBarArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
90 tabFolder
= new CTabFolder(rootShell
, SWT
.MULTI
| SWT
.BORDER
| SWT
.BOTTOM
);
91 tabFolder
.setLayout(noSpaceGridLayout(new GridLayout()));
92 tabFolder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
94 Color selectionBackground
= display
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
95 tabFolder
.setSelectionBackground(selectionBackground
);
96 // tabFolder.setSelectionForeground(selectionBackground);
97 // tabFolder.setSelectionBackground(
98 // new Color[] { display.getSystemColor(SWT.COLOR_WHITE), display.getSystemColor(SWT.COLOR_BLUE) },
99 // new int[] { 100 }, true);
102 Control background
= createBackground(tabFolder
);
103 CTabItem homeTabItem
= new CTabItem(tabFolder
, SWT
.NONE
);
104 homeTabItem
.setText("Home");
105 homeTabItem
.setImage(images
.homeIcon
);
106 homeTabItem
.setControl(background
);
107 tabFolder
.setFocus();
110 createBackground(rootShell
);
116 // rootShell.layout(true, true);
118 if (toolBarShell
!= null) {
119 toolBarShell
.setLocation(new Point(0, 0));
124 protected void createDock(ToolBar toolBar
) {
126 addToolItem(toolBar
, images
.terminalIcon
, "Terminal", () -> {
127 String url
= System
.getProperty("user.home");
128 AppContext appContext
= createAppParent(images
.terminalIcon
);
129 new MiniTerminal(appContext
.getAppParent(), url
) {
132 protected void exitCalled() {
133 if (appContext
.shell
!= null)
134 appContext
.shell
.dispose();
135 if (appContext
.tabItem
!= null)
136 appContext
.tabItem
.dispose();
141 title
= System
.getProperty("user.name") + "@" + InetAddress
.getLocalHost().getHostName();
142 } catch (UnknownHostException e
) {
143 title
= System
.getProperty("user.name") + "@localhost";
145 if (appContext
.shell
!= null)
146 appContext
.shell
.setText(title
);
147 if (appContext
.tabItem
!= null) {
148 appContext
.tabItem
.setText(tabTitle(title
));
149 appContext
.tabItem
.setToolTipText(title
);
155 addToolItem(toolBar
, images
.browserIcon
, "Browser", () -> {
156 String url
= "https://start.duckduckgo.com/";
157 AppContext appContext
= createAppParent(images
.browserIcon
);
158 new MiniBrowser(appContext
.getAppParent(), url
, false, false) {
160 protected void titleChanged(String title
) {
161 if (appContext
.shell
!= null)
162 appContext
.shell
.setText(title
);
163 if (appContext
.tabItem
!= null) {
164 appContext
.tabItem
.setText(tabTitle(title
));
165 appContext
.tabItem
.setToolTipText(title
);
173 addToolItem(toolBar
, images
.explorerIcon
, "Explorer", () -> {
174 String url
= System
.getProperty("user.home");
175 AppContext appContext
= createAppParent(images
.explorerIcon
);
176 new MiniExplorer(appContext
.getAppParent(), url
) {
179 protected void pathChanged(Path path
) {
180 if (appContext
.shell
!= null)
181 appContext
.shell
.setText(path
.toString());
182 if (appContext
.tabItem
!= null) {
183 appContext
.tabItem
.setText(path
.getFileName().toString());
184 appContext
.tabItem
.setToolTipText(path
.toString());
192 new ToolItem(toolBar
, SWT
.SEPARATOR
);
195 addToolItem(toolBar
, images
.exitIcon
, "Exit", () -> rootShell
.dispose());
200 protected String
tabTitle(String title
) {
201 return title
.length() > maxTabTitleLength ? title
.substring(0, maxTabTitleLength
) : title
;
204 protected void addToolItem(ToolBar toolBar
, Image icon
, String name
, Runnable action
) {
205 ToolItem searchI
= new ToolItem(toolBar
, SWT
.PUSH
);
206 searchI
.setImage(icon
);
207 searchI
.setToolTipText(name
);
208 searchI
.addSelectionListener(new SelectionAdapter() {
211 public void widgetSelected(SelectionEvent e
) {
218 protected AppContext
createAppParent(Image icon
) {
220 Composite appParent
= new Composite(tabFolder
, SWT
.CLOSE
);
221 appParent
.setLayout(noSpaceGridLayout(new GridLayout()));
222 CTabItem item
= new CTabItem(tabFolder
, SWT
.CLOSE
);
224 item
.setControl(appParent
);
225 return new AppContext(item
);
227 Shell shell
= 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();