-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJexerTilingWindowManager2.java
More file actions
186 lines (162 loc) · 5.76 KB
/
JexerTilingWindowManager2.java
File metadata and controls
186 lines (162 loc) · 5.76 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
import jexer.TAction;
import jexer.TApplication;
import jexer.TDesktop;
import jexer.TTerminalWidget;
import jexer.TSplitPane;
import jexer.TWidget;
import jexer.event.TMenuEvent;
import jexer.menu.TMenu;
/**
* Implements a simple tiling window manager. A terminal widget is added to
* the desktop, which can be split horizontally or vertically. A close
* action is provided to each window to remove the split when its shell
* exits.
*
* This example shows what can be done with minimal changes to stock Jexer
* widgets.
*/
public class JexerTilingWindowManager2 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;
/**
* Menu item: recreate the root terminal.
*/
private static final int MENU_RESPAWN_ROOT = 2002;
/**
* Handle to the root widget.
*/
private TWidget root = null;
/**
* Main entry point.
*/
public static void main(String [] args) throws Exception {
// For this application, we must use ptypipe so that the terminal
// shells can be aware of their size.
System.setProperty("jexer.TTerminal.ptypipe", "true");
// Let's also suppress the status line.
System.setProperty("jexer.hideStatusBar", "true");
final JexerTilingWindowManager2 jtwm = new JexerTilingWindowManager2();
(new Thread(jtwm)).start();
jtwm.invokeLater(new Runnable() {
public void run() {
// Spin up the root terminal
jtwm.createRootTerminal();
}
});
}
/**
* Public constructor chooses the ECMA-48 / Xterm backend.
*/
public JexerTilingWindowManager2() 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");
tileMenu.addItem(MENU_RESPAWN_ROOT, "&Respawn Root Terminal");
// Stock commands: a new shell with resizable window, and exit
// program.
tileMenu.addSeparator();
tileMenu.addItem(TMenu.MID_SHELL, "&New Windowed Terminal");
tileMenu.addSeparator();
tileMenu.addDefaultItem(TMenu.MID_EXIT);
// TTerminalWidget can request the text-block mouse pointer be
// suppressed, but the default TDesktop will ignore it. Let's set a
// new TDesktop to pass that mouse pointer visibility option to
// TApplication.
setDesktop(new TDesktop(this) {
@Override
public boolean hasHiddenMouse() {
TWidget active = getActiveChild();
if (active instanceof TTerminalWidget) {
return ((TTerminalWidget) active).hasHiddenMouse();
}
return false;
}
});
}
/**
* Process menu events.
*/
@Override
protected boolean onMenu(TMenuEvent event) {
TWidget active = getDesktop().getActiveChild();
TSplitPane split = null;
switch (event.getId()) {
case MENU_RESPAWN_ROOT:
assert (root == null);
createRootTerminal();
return true;
case MENU_SPLIT_VERTICAL:
if (root == null) {
assert (getDesktop().getActiveChild() == null);
createRootTerminal();
return true;
}
split = active.splitVertical(false, createTerminal());
if (active == root) {
root = split;
}
return true;
case MENU_SPLIT_HORIZONTAL:
if (root == null) {
assert (getDesktop().getActiveChild() == null);
createRootTerminal();
return true;
}
split = active.splitHorizontal(false, createTerminal());
if (active == root) {
root = split;
}
return true;
default:
return super.onMenu(event);
}
}
/**
* Create the root terminal.
*/
private void createRootTerminal() {
assert (root == null);
disableMenuItem(MENU_RESPAWN_ROOT);
enableMenuItem(MENU_SPLIT_VERTICAL);
enableMenuItem(MENU_SPLIT_HORIZONTAL);
root = createTerminal();
}
/**
* Create a new terminal.
*
* @return the new terminal
*/
private TWidget createTerminal() {
return new TTerminalWidget(getDesktop(), 0, 0,
getDesktop().getWidth(), getDesktop().getHeight(),
new TAction() {
public void DO() {
if (source.getParent() instanceof TSplitPane) {
((TSplitPane) source.getParent()).removeSplit(source,
true);
} else {
source.getApplication().enableMenuItem(
MENU_RESPAWN_ROOT);
source.getApplication().disableMenuItem(
MENU_SPLIT_VERTICAL);
source.getApplication().disableMenuItem(
MENU_SPLIT_HORIZONTAL);
source.remove();
root = null;
}
}
});
}
}