1 package org
.argeo
.minidesktop
;
3 import java
.lang
.management
.ManagementFactory
;
4 import java
.net
.InetAddress
;
5 import java
.net
.UnknownHostException
;
6 import java
.nio
.file
.Path
;
7 import java
.util
.Arrays
;
10 import org
.eclipse
.swt
.SWT
;
11 import org
.eclipse
.swt
.custom
.CTabFolder
;
12 import org
.eclipse
.swt
.custom
.CTabItem
;
13 import org
.eclipse
.swt
.events
.SelectionAdapter
;
14 import org
.eclipse
.swt
.events
.SelectionEvent
;
15 import org
.eclipse
.swt
.graphics
.Color
;
16 import org
.eclipse
.swt
.graphics
.Image
;
17 import org
.eclipse
.swt
.graphics
.ImageData
;
18 import org
.eclipse
.swt
.graphics
.Point
;
19 import org
.eclipse
.swt
.graphics
.Rectangle
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.program
.Program
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Display
;
26 import org
.eclipse
.swt
.widgets
.Shell
;
27 import org
.eclipse
.swt
.widgets
.ToolBar
;
28 import org
.eclipse
.swt
.widgets
.ToolItem
;
30 /** A very minimalistic desktop manager based on Java and Eclipse SWT. */
31 public class MiniDesktopManager
{
32 private Display display
;
34 private Shell rootShell
;
35 private Shell toolBarShell
;
36 private CTabFolder tabFolder
;
37 private int maxTabTitleLength
= 16;
39 private final boolean fullscreen
;
40 private final boolean stacking
;
42 private MiniDesktopImages images
;
44 public MiniDesktopManager(boolean fullscreen
, boolean stacking
) {
45 this.fullscreen
= fullscreen
;
46 this.stacking
= stacking
;
50 Display
.setAppName("Mini SWT Desktop");
51 display
= Display
.getCurrent();
53 throw new IllegalStateException("Already a display " + display
);
54 display
= new Display();
56 if (display
.getTouchEnabled()) {
57 System
.out
.println("Touch enabled.");
60 images
= new MiniDesktopImages(display
);
65 rootShell
= new Shell(display
, SWT
.NO_TRIM
);
66 rootShell
.setFullScreen(true);
67 Rectangle bounds
= display
.getBounds();
68 rootShell
.setLocation(0, 0);
69 rootShell
.setSize(bounds
.width
, bounds
.height
);
71 rootShell
= new Shell(display
, SWT
.CLOSE
| SWT
.RESIZE
);
72 Rectangle shellArea
= rootShell
.computeTrim(200, 200, 800, 480);
73 rootShell
.setSize(shellArea
.width
, shellArea
.height
);
74 rootShell
.setText(Display
.getAppName());
75 rootShell
.setImage(images
.terminalIcon
);
78 rootShell
.setLayout(noSpaceGridLayout(new GridLayout(2, false)));
79 Composite toolBarArea
= new Composite(rootShell
, SWT
.NONE
);
80 toolBarArea
.setLayoutData(new GridData(toolBarSize
, rootShell
.getSize().y
));
84 toolBarShell
= new Shell(rootShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
85 toolBar
= new ToolBar(toolBarShell
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
88 toolBarArea
.setLayoutData(new GridData(toolBar
.getSize().x
, toolBar
.getSize().y
));
90 toolBar
= new ToolBar(toolBarArea
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
92 toolBarArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
96 tabFolder
= new CTabFolder(rootShell
, SWT
.MULTI
| SWT
.BORDER
| SWT
.BOTTOM
);
97 tabFolder
.setLayout(noSpaceGridLayout(new GridLayout()));
98 tabFolder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
100 Color selectionBackground
= display
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
101 tabFolder
.setSelectionBackground(selectionBackground
);
104 Control background
= createBackground(tabFolder
);
105 CTabItem homeTabItem
= new CTabItem(tabFolder
, SWT
.NONE
);
106 homeTabItem
.setText("Home");
107 homeTabItem
.setImage(images
.homeIcon
);
108 homeTabItem
.setControl(background
);
109 tabFolder
.setFocus();
111 createBackground(rootShell
);
115 // rootShell.layout(true, true);
117 if (toolBarShell
!= null) {
118 int toolBarShellY
= (display
.getBounds().height
- toolBar
.getSize().y
) / 2;
119 toolBarShell
.setLocation(0, toolBarShellY
);
123 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
124 System
.out
.println("SWT Mini Desktop Manager available in " + jvmUptime
+ " ms.");
127 protected void createDock(ToolBar toolBar
) {
129 addToolItem(toolBar
, images
.terminalIcon
, "Terminal", () -> {
130 String url
= System
.getProperty("user.home");
131 AppContext appContext
= createAppParent(images
.terminalIcon
);
132 new MiniTerminal(appContext
.getAppParent(), url
) {
135 protected void exitCalled() {
136 if (appContext
.shell
!= null)
137 appContext
.shell
.dispose();
138 if (appContext
.tabItem
!= null)
139 appContext
.tabItem
.dispose();
144 title
= System
.getProperty("user.name") + "@" + InetAddress
.getLocalHost().getHostName();
145 } catch (UnknownHostException e
) {
146 title
= System
.getProperty("user.name") + "@localhost";
148 if (appContext
.shell
!= null)
149 appContext
.shell
.setText(title
);
150 if (appContext
.tabItem
!= null) {
151 appContext
.tabItem
.setText(tabTitle(title
));
152 appContext
.tabItem
.setToolTipText(title
);
158 addToolItem(toolBar
, images
.browserIcon
, "Browser", () -> {
159 String url
= "https://start.duckduckgo.com/";
160 AppContext appContext
= createAppParent(images
.browserIcon
);
161 new MiniBrowser(appContext
.getAppParent(), url
, false, false) {
163 protected void titleChanged(String title
) {
164 if (appContext
.shell
!= null)
165 appContext
.shell
.setText(title
);
166 if (appContext
.tabItem
!= null) {
167 appContext
.tabItem
.setText(tabTitle(title
));
168 appContext
.tabItem
.setToolTipText(title
);
176 addToolItem(toolBar
, images
.explorerIcon
, "Explorer", () -> {
177 String url
= System
.getProperty("user.home");
178 AppContext appContext
= createAppParent(images
.explorerIcon
);
179 new MiniExplorer(appContext
.getAppParent(), url
) {
182 protected void pathChanged(Path path
) {
183 if (appContext
.shell
!= null)
184 appContext
.shell
.setText(path
.toString());
185 if (appContext
.tabItem
!= null) {
186 appContext
.tabItem
.setText(path
.getFileName().toString());
187 appContext
.tabItem
.setToolTipText(path
.toString());
195 new ToolItem(toolBar
, SWT
.SEPARATOR
);
198 addToolItem(toolBar
, images
.exitIcon
, "Exit", () -> rootShell
.dispose());
203 protected String
tabTitle(String title
) {
204 return title
.length() > maxTabTitleLength ? title
.substring(0, maxTabTitleLength
) : title
;
207 protected void addToolItem(ToolBar toolBar
, Image icon
, String name
, Runnable action
) {
208 ToolItem searchI
= new ToolItem(toolBar
, SWT
.PUSH
);
209 searchI
.setImage(icon
);
210 searchI
.setToolTipText(name
);
211 searchI
.addSelectionListener(new SelectionAdapter() {
214 public void widgetSelected(SelectionEvent e
) {
221 protected AppContext
createAppParent(Image icon
) {
223 Composite appParent
= new Composite(tabFolder
, SWT
.CLOSE
);
224 appParent
.setLayout(noSpaceGridLayout(new GridLayout()));
225 CTabItem item
= new CTabItem(tabFolder
, SWT
.CLOSE
);
227 item
.setControl(appParent
);
228 return new AppContext(item
);
230 Shell shell
= isFullscreen() ?
new Shell(rootShell
, SWT
.SHELL_TRIM
)
231 : new Shell(rootShell
.getDisplay(), SWT
.SHELL_TRIM
);
232 shell
.setImage(icon
);
233 return new AppContext(shell
);
237 protected void openApp(AppContext appContext
) {
238 if (appContext
.shell
!= null) {
239 Shell shell
= (Shell
) appContext
.shell
;
241 shell
.setSize(new Point(800, 480));
243 if (appContext
.tabItem
!= null) {
244 tabFolder
.setFocus();
245 tabFolder
.setSelection(appContext
.tabItem
);
249 protected Control
createBackground(Composite parent
) {
250 Composite backgroundArea
= new Composite(parent
, SWT
.NONE
);
251 backgroundArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
252 initBackground(backgroundArea
);
253 return backgroundArea
;
256 protected void initBackground(Composite backgroundArea
) {
257 MiniHomePart homePart
= new MiniHomePart() {
260 protected void fillAppsToolBar(ToolBar toolBar
) {
264 homePart
.createUiPart(backgroundArea
, null);
268 while (!rootShell
.isDisposed()) {
269 if (!display
.readAndDispatch())
274 public void dispose() {
275 if (!rootShell
.isDisposed())
279 protected boolean isFullscreen() {
283 protected boolean isStacking() {
287 protected Image
getIconForExt(String ext
) {
288 Program program
= Program
.findProgram(ext
);
290 return display
.getSystemImage(SWT
.ICON_INFORMATION
);
292 ImageData iconData
= program
.getImageData();
293 if (iconData
== null) {
294 return display
.getSystemImage(SWT
.ICON_INFORMATION
);
296 return new Image(display
, iconData
);
301 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
302 layout
.horizontalSpacing
= 0;
303 layout
.verticalSpacing
= 0;
304 layout
.marginWidth
= 0;
305 layout
.marginHeight
= 0;
309 public static void main(String
[] args
) {
310 List
<String
> options
= Arrays
.asList(args
);
311 if (options
.contains("--help")) {
312 System
.out
.println("Usage: java " + MiniDesktopManager
.class.getName().replace('.', '/') + " [OPTION]");
313 System
.out
.println("A minimalistic desktop manager based on Java and Eclipse SWT.");
314 System
.out
.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
315 System
.out
.println(" --stacking : open apps as tabs (default is to create new windows)");
316 System
.out
.println(" --help : print this help and exit");
319 boolean fullscreen
= options
.contains("--fullscreen");
320 boolean stacking
= options
.contains("--stacking");
322 MiniDesktopManager desktopManager
= new MiniDesktopManager(fullscreen
, stacking
);
323 desktopManager
.init();
324 desktopManager
.run();
325 desktopManager
.dispose();
331 private CTabItem tabItem
;
333 public AppContext(Shell shell
) {
337 public AppContext(CTabItem tabItem
) {
338 this.tabItem
= tabItem
;
341 Composite
getAppParent() {
345 return (Composite
) tabItem
.getControl();
346 throw new IllegalStateException();