]> git.argeo.org Git - gpl/argeo-slc.git/blob - MiniDesktopManager.java
4d7c03c62c0dc614ca14539ba5c83d28b3a6e53b
[gpl/argeo-slc.git] / MiniDesktopManager.java
1 package org.argeo.minidesktop;
2
3 import java.net.InetAddress;
4 import java.net.UnknownHostException;
5 import java.nio.file.Path;
6 import java.util.Arrays;
7 import java.util.List;
8
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;
28
29 /** A very minimalistic desktop manager based on Java and Eclipse SWT. */
30 public class MiniDesktopManager {
31 private Display display;
32
33 private Shell rootShell;
34 private Shell toolBarShell;
35 private CTabFolder tabFolder;
36 private int maxTabTitleLength = 16;
37
38 private final boolean fullscreen;
39 private final boolean stacking;
40
41 private MiniDesktopImages images;
42
43 public MiniDesktopManager(boolean fullscreen, boolean stacking) {
44 this.fullscreen = fullscreen;
45 this.stacking = stacking;
46 }
47
48 public void init() {
49 Display.setAppName("Mini SWT Desktop");
50 display = Display.getCurrent();
51 if (display != null)
52 throw new IllegalStateException("Already a display " + display);
53 display = new Display();
54
55 if (display.getTouchEnabled()) {
56 System.out.println("Touch enabled.");
57 }
58
59 images = new MiniDesktopImages(display);
60
61 int toolBarSize = 48;
62
63 if (isFullscreen()) {
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);
69 } else {
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);
75 }
76
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));
80
81 ToolBar toolBar;
82 if (isFullscreen()) {
83 toolBarShell = new Shell(rootShell, SWT.NO_TRIM | SWT.ON_TOP);
84 toolBar = new ToolBar(toolBarShell, SWT.VERTICAL | SWT.FLAT | SWT.BORDER);
85 createDock(toolBar);
86 toolBarShell.pack();
87 toolBarArea.setLayoutData(new GridData(toolBar.getSize().x, toolBar.getSize().y));
88 } else {
89 toolBar = new ToolBar(toolBarArea, SWT.VERTICAL | SWT.FLAT | SWT.BORDER);
90 createDock(toolBar);
91 toolBarArea.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
92 }
93
94 if (isStacking()) {
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));
98
99 Color selectionBackground = display.getSystemColor(SWT.COLOR_LIST_SELECTION);
100 tabFolder.setSelectionBackground(selectionBackground);
101
102 // background
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();
109 } else {
110 createBackground(rootShell);
111 }
112
113 rootShell.open();
114 // rootShell.layout(true, true);
115
116 if (toolBarShell != null) {
117 int toolBarShellY = (display.getBounds().height - toolBar.getSize().y) / 2;
118 toolBarShell.setLocation(0, toolBarShellY);
119 toolBarShell.open();
120 }
121 }
122
123 protected void createDock(ToolBar toolBar) {
124 // Terminal
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) {
129
130 @Override
131 protected void exitCalled() {
132 if (appContext.shell != null)
133 appContext.shell.dispose();
134 if (appContext.tabItem != null)
135 appContext.tabItem.dispose();
136 }
137 };
138 String title;
139 try {
140 title = System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName();
141 } catch (UnknownHostException e) {
142 title = System.getProperty("user.name") + "@localhost";
143 }
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);
149 }
150 openApp(appContext);
151 });
152
153 // Web browser
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) {
158 @Override
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);
165 }
166 }
167 };
168 openApp(appContext);
169 });
170
171 // File explorer
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) {
176
177 @Override
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());
184 }
185 }
186 };
187 openApp(appContext);
188 });
189
190 // Separator
191 new ToolItem(toolBar, SWT.SEPARATOR);
192
193 // Exit
194 addToolItem(toolBar, images.exitIcon, "Exit", () -> rootShell.dispose());
195
196 toolBar.pack();
197 }
198
199 protected String tabTitle(String title) {
200 return title.length() > maxTabTitleLength ? title.substring(0, maxTabTitleLength) : title;
201 }
202
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() {
208
209 @Override
210 public void widgetSelected(SelectionEvent e) {
211 action.run();
212 }
213
214 });
215 }
216
217 protected AppContext createAppParent(Image icon) {
218 if (isStacking()) {
219 Composite appParent = new Composite(tabFolder, SWT.CLOSE);
220 appParent.setLayout(noSpaceGridLayout(new GridLayout()));
221 CTabItem item = new CTabItem(tabFolder, SWT.CLOSE);
222 item.setImage(icon);
223 item.setControl(appParent);
224 return new AppContext(item);
225 } else {
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);
230 }
231 }
232
233 protected void openApp(AppContext appContext) {
234 if (appContext.shell != null) {
235 Shell shell = (Shell) appContext.shell;
236 shell.open();
237 shell.setSize(new Point(800, 480));
238 }
239 if (appContext.tabItem != null) {
240 tabFolder.setFocus();
241 tabFolder.setSelection(appContext.tabItem);
242 }
243 }
244
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;
250 }
251
252 protected void initBackground(Composite backgroundArea) {
253 MiniHomePart homePart = new MiniHomePart() {
254
255 @Override
256 protected void fillAppsToolBar(ToolBar toolBar) {
257 createDock(toolBar);
258 }
259 };
260 homePart.createUiPart(backgroundArea, null);
261 }
262
263 public void run() {
264 while (!rootShell.isDisposed()) {
265 if (!display.readAndDispatch())
266 display.sleep();
267 }
268 }
269
270 public void dispose() {
271 if (!rootShell.isDisposed())
272 rootShell.dispose();
273 }
274
275 protected boolean isFullscreen() {
276 return fullscreen;
277 }
278
279 protected boolean isStacking() {
280 return stacking;
281 }
282
283 protected Image getIconForExt(String ext) {
284 Program program = Program.findProgram(ext);
285 if (program == null)
286 return display.getSystemImage(SWT.ICON_INFORMATION);
287
288 ImageData iconData = program.getImageData();
289 if (iconData == null) {
290 return display.getSystemImage(SWT.ICON_INFORMATION);
291 } else {
292 return new Image(display, iconData);
293 }
294
295 }
296
297 private static GridLayout noSpaceGridLayout(GridLayout layout) {
298 layout.horizontalSpacing = 0;
299 layout.verticalSpacing = 0;
300 layout.marginWidth = 0;
301 layout.marginHeight = 0;
302 return layout;
303 }
304
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");
313 System.exit(1);
314 }
315 boolean fullscreen = options.contains("--fullscreen");
316 boolean stacking = options.contains("--stacking");
317
318 MiniDesktopManager desktopManager = new MiniDesktopManager(fullscreen, stacking);
319 desktopManager.init();
320 desktopManager.run();
321 desktopManager.dispose();
322 System.exit(0);
323 }
324
325 class AppContext {
326 private Shell shell;
327 private CTabItem tabItem;
328
329 public AppContext(Shell shell) {
330 this.shell = shell;
331 }
332
333 public AppContext(CTabItem tabItem) {
334 this.tabItem = tabItem;
335 }
336
337 Composite getAppParent() {
338 if (shell != null)
339 return shell;
340 if (tabItem != null)
341 return (Composite) tabItem.getControl();
342 throw new IllegalStateException();
343 }
344 }
345 }