-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJexerTilingWindowManager.java
More file actions
224 lines (195 loc) · 7.88 KB
/
Copy pathJexerTilingWindowManager.java
File metadata and controls
224 lines (195 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import jexer.TApplication;
import jexer.TTerminalWindow;
import jexer.TWindow;
import jexer.event.TKeypressEvent;
import jexer.event.TMenuEvent;
import jexer.event.TMouseEvent;
import jexer.event.TResizeEvent;
import jexer.menu.TMenu;
/**
* Implements a simple tiling window manager. A root non-moveable
* non-resizable terminal window is created first, which can be split
* horizontally or vertically. Each new window retains a reference to its
* "parent", and upon closing resizes that parent back to its original size.
*
* This example shows what can be done with minimal changes to stock Jexer
* widgets. You will quickly see that closing a "parent" tile does not cause
* the "child" tile to resize. You could make a real subclass of
* TTerminalWindow that has extra fields and/or communicates more with
* JexerTilingWindowManager to get full coverage of tile creation,
* destruction, placement, movement, and so on.
*/
public class JexerTilingWindowManager extends TApplication {
/**
* Menu item: split the terminal vertically.
*/
private static final int MENU_SPLIT_VERTICAL = 2000;
/**
* Menu item: split the terminal horizontally.
*/
private static final int MENU_SPLIT_HORIZONTAL = 2001;
/**
* Main entry point.
*/
public static void main(String [] args) throws Exception {
// For this application, we must use ptypipe so that the tile shells
// can be aware of their size.
System.setProperty("jexer.TTerminal.ptypipe", "true");
JexerTilingWindowManager jtwm = new JexerTilingWindowManager();
(new Thread(jtwm)).start();
}
/**
* Public constructor chooses the ECMA-48 / Xterm backend.
*/
public JexerTilingWindowManager() throws Exception {
super(BackendType.XTERM);
// The stock tool menu has items for redrawing the screen, opening
// images, and (when using the Swing backend) setting the font.
addToolMenu();
// We will have one menu containing a mix of new and stock commands
TMenu tileMenu = addMenu("&Tile");
// New commands for this example: split vertical and horizontal.
tileMenu.addItem(MENU_SPLIT_VERTICAL, "&Vertical Split");
tileMenu.addItem(MENU_SPLIT_HORIZONTAL, "&Horizontal Split");
// Stock commands: a new shell with resizable window, previous, next,
// close, and exit program.
tileMenu.addItem(TMenu.MID_SHELL, "&Floating");
tileMenu.addSeparator();
tileMenu.addDefaultItem(TMenu.MID_WINDOW_PREVIOUS);
tileMenu.addDefaultItem(TMenu.MID_WINDOW_NEXT);
tileMenu.addDefaultItem(TMenu.MID_WINDOW_CLOSE);
tileMenu.addSeparator();
tileMenu.addDefaultItem(TMenu.MID_EXIT);
// Spin up the root tile
TTerminalWindow rootTile = makeTile(0, 0, getScreen().getWidth(),
getDesktopBottom() - 1, null);
// Let's add some bling! Enable focus-follows-mouse.
setFocusFollowsMouse(true);
}
/**
* Process menu events.
*/
@Override
protected boolean onMenu(TMenuEvent event) {
if (event.getId() == MENU_SPLIT_VERTICAL) {
splitVertical();
return true;
}
if (event.getId() == MENU_SPLIT_HORIZONTAL) {
splitHorizontal();
return true;
}
return super.onMenu(event);
}
/**
* Perform the vertical split.
*/
private void splitVertical() {
TWindow window = getActiveWindow();
if (!(window instanceof TTerminalWindow)) {
return;
}
TTerminalWindow tile = (TTerminalWindow) window;
// Give the extra column to the new tile.
int newWidth = (tile.getWidth() + 1) / 2;
int newY = tile.getY() - 1;
int newX = tile.getX() + tile.getWidth() - newWidth;
makeTile(newX, newY, newWidth, tile.getHeight(), tile);
tile.setWidth(tile.getWidth() - newWidth);
tile.onResize(new TResizeEvent(getBackend(), TResizeEvent.Type.WIDGET,
tile.getWidth(), tile.getHeight()));
}
/**
* Perform the horizontal split.
*/
private void splitHorizontal() {
TWindow window = getActiveWindow();
if (!(window instanceof TTerminalWindow)) {
return;
}
TTerminalWindow tile = (TTerminalWindow) window;
// Give the extra row to the new tile.
int newHeight = (tile.getHeight() + 1) / 2;
int newY = tile.getY() - 1 + tile.getHeight() - newHeight;
int newX = tile.getX();
makeTile(newX, newY, tile.getWidth(), newHeight, tile);
tile.setHeight(tile.getHeight() - newHeight);
tile.onResize(new TResizeEvent(getBackend(), TResizeEvent.Type.WIDGET,
tile.getWidth(), tile.getHeight()));
}
/**
* Create a non-resizable non-movable terminal window.
*
* @param x the column number to place the top-left corner at. 0 is the
* left-most column.
* @param y the row number to place the top-left corner at. 0 is the
* top-most column.
* @param width the width of the window
* @param height the height of the window
* @param otherTile the other tile to resize when this window closes
*/
private TTerminalWindow makeTile(int x, int y, int width, int height,
final TTerminalWindow otherTile) {
// We pass flags to disable the zoom (maximize) button, disable
// "smart" window placement, and set the specific location.
TTerminalWindow tile = new TTerminalWindow(this, x, y,
TWindow.NOZOOMBOX | TWindow.ABSOLUTEXY,
new String[] { "/bin/bash", "--login" }, true) {
/**
* When this terminal closes, if otherTile is defined then resize
* it to overcover me.
*/
@Override
public void onClose() {
super.onClose();
if (otherTile != null) {
if (otherTile.getX() != getX()) {
// Undo the vertical split
otherTile.setX(Math.min(otherTile.getX(), getX()));
otherTile.setWidth(otherTile.getWidth() + getWidth());
}
if (otherTile.getY() != getY()) {
otherTile.setY(Math.min(otherTile.getY(), getY()));
otherTile.setHeight(otherTile.getHeight() + getHeight());
}
otherTile.onResize(new TResizeEvent(getBackend(),
TResizeEvent.Type.WIDGET,
otherTile.getWidth(), otherTile.getHeight()));
}
}
/**
* Prevent the user from resizing or moving this window.
*/
@Override
public void onMouseDown(final TMouseEvent mouse) {
super.onMouseDown(mouse);
stopMovements();
}
/**
* Prevent the user from resizing or moving this window.
*/
@Override
public void onKeypress(final TKeypressEvent keypress) {
super.onKeypress(keypress);
stopMovements();
}
/**
* Permit the user to use all of the menu items.
*/
@Override
public void onIdle() {
super.onIdle();
removeShortcutKeypress(jexer.TKeypress.kbAltT);
removeShortcutKeypress(jexer.TKeypress.kbF6);
}
};
// The initial window size was stock VT100 80x24. Change that now,
// and then call onResize() to notify ptypipe to set the shell's
// window size.
tile.setWidth(width);
tile.setHeight(height);
tile.onResize(new TResizeEvent(getBackend(), TResizeEvent.Type.WIDGET,
tile.getWidth(), tile.getHeight()));
return tile;
}
}