Skip to content

Commit 38be638

Browse files
committed
[tabs] some heavy lifting on tabs handling
The situation is still a shit, but at least better than before.
1 parent ef78121 commit 38be638

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

Diff for: app/src/processing/app/EditorHeader.java

+45-26
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,7 @@ public void paintComponent(Graphics screen) {
223223
Dimension size = getSize();
224224
if ((size.width != sizeW) || (size.height != sizeH)) {
225225
// component has been resized
226-
227-
if ((size.width > imageW) || (size.height > imageH)) {
228-
// nix the image and recreate, it's too small
229-
offscreen = null;
230-
231-
} else {
232-
// who cares, just resize
233-
sizeW = size.width;
234-
sizeH = size.height;
235-
}
226+
offscreen = null;
236227
}
237228

238229
if (offscreen == null) {
@@ -255,6 +246,8 @@ public void paintComponent(Graphics screen) {
255246
g.setColor(backgroundColor);
256247
g.fillRect(0, 0, imageW, imageH);
257248

249+
imageW = sizeW - 30 - menuButtons[0].getWidth(this);
250+
258251
List<EditorTab> tabs = editor.getTabs();
259252

260253
int codeCount = tabs.size();
@@ -265,7 +258,8 @@ public void paintComponent(Graphics screen) {
265258

266259
int x = scale(6); // offset from left edge of the component
267260
int i = 0;
268-
int x_selected = 0;
261+
int selected = 0;
262+
int size_selected = 0;
269263

270264
// dry run, get the correct offset
271265
for (EditorTab tab : tabs) {
@@ -279,37 +273,33 @@ public void paintComponent(Graphics screen) {
279273
font.getStringBounds(text, g.getFontRenderContext()).getWidth();
280274

281275
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
282-
int pieceWidth = pieceCount * PIECE_WIDTH;
283276

284277
int state = (i == editor.getCurrentTabIndex()) ? SELECTED : UNSELECTED;
278+
int x_initial = x;
285279
x += PIECE_WIDTH;
286280

287-
int contentLeft = x;
288281
tabLeft[i] = x;
289-
for (int j = 0; j < pieceCount; j++) {
290-
x += PIECE_WIDTH;
291-
}
282+
x += PIECE_WIDTH * pieceCount;
292283
tabRight[i] = x;
293-
int textLeft = contentLeft + (pieceWidth - textWidth) / 2;
294-
295-
int baseline = (sizeH + fontAscent) / 2;
296-
//g.drawString(sketch.code[i].name, textLeft, baseline);
297284

298285
x += PIECE_WIDTH - 1; // overlap by 1 pixel
299286

300287
if (state == SELECTED) {
301-
x_selected = x;
288+
selected = i;
289+
size_selected = x - x_initial;
302290
}
303291

304292
i++;
305293
}
306294

307-
if (x_selected > imageW) {
308-
x = -(x_selected - imageW);
309-
} else {
310-
x = scale(6); // offset from left edge of the component
295+
int non_selected_tab_size = 0;
296+
297+
if (x > imageW) {
298+
// find scaling factor
299+
non_selected_tab_size = (imageW - size_selected)/(codeCount -1);
311300
}
312301
i = 0;
302+
x = scale(6); // offset from left edge of the component
313303

314304
for (EditorTab tab : tabs) {
315305
SketchFile file = tab.getSketchFile();
@@ -319,7 +309,36 @@ public void paintComponent(Graphics screen) {
319309
String text = " " + filename + (file.isModified() ? " \u00A7" : " ");
320310

321311
int textWidth = (int)
322-
font.getStringBounds(text, g.getFontRenderContext()).getWidth();
312+
font.getStringBounds(text, g.getFontRenderContext()).getWidth();
313+
314+
if (non_selected_tab_size > 0) {
315+
if (i != selected) {
316+
// find a suitable title
317+
while (textWidth + 3 * PIECE_WIDTH > non_selected_tab_size && filename.length() > 2) {
318+
filename = filename.substring(0, filename.length()-1);
319+
text = " " + filename + ".." + (file.isModified() ? " \u00A7" : " ");
320+
textWidth = (int)font.getStringBounds(text, g.getFontRenderContext()).getWidth();
321+
}
322+
}
323+
}
324+
325+
int current_tab_size = non_selected_tab_size;
326+
if (i == selected) {
327+
current_tab_size = size_selected;
328+
}
329+
330+
int padding = x + current_tab_size;
331+
332+
if (padding > imageW) {
333+
if (i <= selected) {
334+
// create another surface to overlay g
335+
g.setColor(backgroundColor);
336+
g.fillRect(0, 0, sizeW, imageH);
337+
x = scale(6);
338+
} else {
339+
break;
340+
}
341+
}
323342

324343
int pieceCount = 2 + (textWidth / PIECE_WIDTH);
325344
int pieceWidth = pieceCount * PIECE_WIDTH;

0 commit comments

Comments
 (0)