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
.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
.widgets
.Composite
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.swt
.widgets
.Display
;
24 import org
.eclipse
.swt
.widgets
.Shell
;
25 import org
.eclipse
.swt
.widgets
.ToolBar
;
26 import org
.eclipse
.swt
.widgets
.ToolItem
;
28 /** A very minimalistic desktop manager based on Java and Eclipse SWT. */
29 public class MiniDesktopManager
{
30 private Display display
;
32 private Shell rootShell
;
33 private Shell toolBarShell
;
34 private CTabFolder tabFolder
;
35 private int maxTabTitleLength
= 16;
37 private final boolean fullscreen
;
38 private final boolean stacking
;
40 private MiniDesktopImages images
;
42 public MiniDesktopManager(boolean fullscreen
, boolean stacking
) {
43 this.fullscreen
= fullscreen
;
44 this.stacking
= stacking
;
48 Display
.setAppName("Mini SWT Desktop");
49 display
= Display
.getCurrent();
51 throw new IllegalStateException("Already a display " + display
);
52 display
= new Display();
54 if (display
.getTouchEnabled()) {
55 System
.out
.println("Touch enabled.");
58 images
= new MiniDesktopImages(display
);
63 rootShell
= new Shell(display
, SWT
.NO_TRIM
);
64 rootShell
.setFullScreen(true);
65 Rectangle bounds
= display
.getBounds();
66 rootShell
.setLocation(0, 0);
67 rootShell
.setSize(bounds
.width
, bounds
.height
);
69 rootShell
= new Shell(display
, SWT
.CLOSE
| SWT
.RESIZE
);
70 Rectangle shellArea
= rootShell
.computeTrim(200, 200, 800, 480);
71 rootShell
.setSize(shellArea
.width
, shellArea
.height
);
72 rootShell
.setText(Display
.getAppName());
73 rootShell
.setImage(images
.terminalIcon
);
76 rootShell
.setLayout(noSpaceGridLayout(new GridLayout(2, false)));
77 Composite toolBarArea
= new Composite(rootShell
, SWT
.NONE
);
78 toolBarArea
.setLayoutData(new GridData(toolBarSize
, rootShell
.getSize().y
));
82 toolBarShell
= new Shell(rootShell
, SWT
.NO_TRIM
| SWT
.ON_TOP
);
83 toolBar
= new ToolBar(toolBarShell
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
86 toolBarArea
.setLayoutData(new GridData(toolBar
.getSize().x
, toolBar
.getSize().y
));
88 toolBar
= new ToolBar(toolBarArea
, SWT
.VERTICAL
| SWT
.FLAT
| SWT
.BORDER
);
90 toolBarArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false, false));
94 tabFolder
= new CTabFolder(rootShell
, SWT
.MULTI
| SWT
.BORDER
| SWT
.BOTTOM
);
95 tabFolder
.setLayout(noSpaceGridLayout(new GridLayout()));
96 tabFolder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
98 Color selectionBackground
= display
.getSystemColor(SWT
.COLOR_LIST_SELECTION
);
99 tabFolder
.setSelectionBackground(selectionBackground
);
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();
109 createBackground(rootShell
);
113 // rootShell.layout(true, true);
115 if (toolBarShell
!= null) {
116 int toolBarShellY
= (display
.getBounds().height
- toolBar
.getSize().y
) / 2;
117 toolBarShell
.setLocation(0, toolBarShellY
);
121 long jvmUptime
= ManagementFactory
.getRuntimeMXBean().getUptime();
122 System
.out
.println("SWT Mini Desktop Manager available in " + jvmUptime
+ " ms.");
125 protected void createDock(ToolBar toolBar
) {
127 addToolItem(toolBar
, images
.terminalIcon
, "Terminal", () -> {
128 String url
= System
.getProperty("user.home");
129 AppContext appContext
= createAppParent(images
.terminalIcon
);
130 new MiniTerminal(appContext
.getAppParent(), url
) {
133 protected void exitCalled() {
134 if (appContext
.shell
!= null)
135 appContext
.shell
.dispose();
136 if (appContext
.tabItem
!= null)
137 appContext
.tabItem
.dispose();
142 title
= System
.getProperty("user.name") + "@" + InetAddress
.getLocalHost().getHostName();
143 } catch (UnknownHostException e
) {
144 title
= System
.getProperty("user.name") + "@localhost";
146 if (appContext
.shell
!= null)
147 appContext
.shell
.setText(title
);
148 if (appContext
.tabItem
!= null) {
149 appContext
.tabItem
.setText(tabTitle(title
));
150 appContext
.tabItem
.setToolTipText(title
);
156 addToolItem(toolBar
, images
.browserIcon
, "Browser", () -> {
157 String url
= "https://start.duckduckgo.com/";
158 AppContext appContext
= createAppParent(images
.browserIcon
);
159 new MiniBrowser(appContext
.getAppParent(), url
, false, false) {
161 public void titleChanged(String title
) {
162 if (appContext
.shell
!= null)
163 appContext
.shell
.setText(title
);
164 if (appContext
.tabItem
!= null) {
165 appContext
.tabItem
.setText(tabTitle(title
));
166 appContext
.tabItem
.setToolTipText(title
);
174 addToolItem(toolBar
, images
.explorerIcon
, "Explorer", () -> {
175 String url
= System
.getProperty("user.home");
176 AppContext appContext
= createAppParent(images
.explorerIcon
);
177 new MiniExplorer(appContext
.getAppParent(), url
) {
180 protected void pathChanged(Path path
) {
181 if (appContext
.shell
!= null)
182 appContext
.shell
.setText(path
.toString());
183 if (appContext
.tabItem
!= null) {
184 appContext
.tabItem
.setText(path
.getFileName().toString());
185 appContext
.tabItem
.setToolTipText(path
.toString());
193 new ToolItem(toolBar
, SWT
.SEPARATOR
);
196 addToolItem(toolBar
, images
.exitIcon
, "Exit", () -> rootShell
.dispose());
201 protected String
tabTitle(String title
) {
202 return title
.length() > maxTabTitleLength ? title
.substring(0, maxTabTitleLength
) : title
;
205 protected void addToolItem(ToolBar toolBar
, Image icon
, String name
, Runnable action
) {
206 ToolItem searchI
= new ToolItem(toolBar
, SWT
.PUSH
);
207 searchI
.setImage(icon
);
208 searchI
.setToolTipText(name
);
209 searchI
.addSelectionListener(new SelectionAdapter() {
212 public void widgetSelected(SelectionEvent e
) {
219 protected AppContext
createAppParent(Image icon
) {
221 Composite appParent
= new Composite(tabFolder
, SWT
.CLOSE
);
222 appParent
.setLayout(noSpaceGridLayout(new GridLayout()));
223 CTabItem item
= new CTabItem(tabFolder
, SWT
.CLOSE
);
225 item
.setControl(appParent
);
226 return new AppContext(item
);
228 Shell shell
= isFullscreen() ?
new Shell(rootShell
, SWT
.SHELL_TRIM
)
229 : new Shell(rootShell
.getDisplay(), SWT
.SHELL_TRIM
);
230 shell
.setImage(icon
);
231 return new AppContext(shell
);
235 protected void openApp(AppContext appContext
) {
236 if (appContext
.shell
!= null) {
237 Shell shell
= (Shell
) appContext
.shell
;
239 shell
.setSize(new Point(800, 480));
241 if (appContext
.tabItem
!= null) {
242 tabFolder
.setFocus();
243 tabFolder
.setSelection(appContext
.tabItem
);
247 protected Control
createBackground(Composite parent
) {
248 Composite backgroundArea
= new Composite(parent
, SWT
.NONE
);
249 backgroundArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
250 initBackground(backgroundArea
);
251 return backgroundArea
;
254 protected void initBackground(Composite backgroundArea
) {
255 MiniHomePart homePart
= new MiniHomePart() {
258 protected void fillAppsToolBar(ToolBar toolBar
) {
262 homePart
.createUiPart(backgroundArea
, null);
266 while (!rootShell
.isDisposed()) {
267 if (!display
.readAndDispatch())
272 public void dispose() {
273 if (!rootShell
.isDisposed())
277 protected boolean isFullscreen() {
281 protected boolean isStacking() {
285 protected Image
getIconForExt(String ext
) {
286 // Program program = Program.findProgram(ext);
287 // if (program == null)
288 return display
.getSystemImage(SWT
.ICON_INFORMATION
);
290 // ImageData iconData = program.getImageData();
291 // if (iconData == null) {
292 // return display.getSystemImage(SWT.ICON_INFORMATION);
294 // return new Image(display, iconData);
299 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
300 layout
.horizontalSpacing
= 0;
301 layout
.verticalSpacing
= 0;
302 layout
.marginWidth
= 0;
303 layout
.marginHeight
= 0;
307 public static void main(String
[] args
) {
308 List
<String
> options
= Arrays
.asList(args
);
309 if (options
.contains("--help")) {
310 System
.out
.println("Usage: java " + MiniDesktopManager
.class.getName().replace('.', '/') + " [OPTION]");
311 System
.out
.println("A minimalistic desktop manager based on Java and Eclipse SWT.");
312 System
.out
.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
313 System
.out
.println(" --stacking : open apps as tabs (default is to create new windows)");
314 System
.out
.println(" --help : print this help and exit");
317 boolean fullscreen
= options
.contains("--fullscreen");
318 boolean stacking
= options
.contains("--stacking");
320 MiniDesktopManager desktopManager
= new MiniDesktopManager(fullscreen
, stacking
);
321 desktopManager
.init();
322 desktopManager
.run();
323 desktopManager
.dispose();
329 private CTabItem tabItem
;
331 public AppContext(Shell shell
) {
335 public AppContext(CTabItem tabItem
) {
336 this.tabItem
= tabItem
;
339 Composite
getAppParent() {
343 return (Composite
) tabItem
.getControl();
344 throw new IllegalStateException();