Skip to content

Commit ee7eda5

Browse files
raghucssitiloveeclipse
authored andcommitted
Use known width and height of the shell instead of asking
GDK.gdk_window_get_frame_extents (window, rect). For GDK.gdk_window_get_frame_extents (window, rect) to return correct values shell should be open. Once SWT asks GTK to show the shell via Native calls it takes sometime to open the shell. On high performance machines adjustTrip is asking GTK to give it width and height before it has completely opened the shell. This results in returning default size.(which is {0,0,1,1}) So We can use the height and width which we already know when opening the shell. Fixes #1161
1 parent 5b437ee commit ee7eda5

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Shell.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,20 +479,45 @@ void addToolTip (ToolTip toolTip) {
479479
toolTips = newToolTips;
480480
}
481481

482-
void adjustTrim () {
482+
/**
483+
* Resize the shell based on the trim width and height. Trim width and height
484+
* adjustments are read from '$HOME/.swt/trims.prefs' for each trim style. If
485+
* such a file does not exist then the trim values are queried from GTK.
486+
*
487+
* Note that we are trying to resize the shell which is already open. Shell
488+
* already has width and height, Which is either set by setSize()/setBounds() or
489+
* size is calculated based on layout and content of the shell. Without setting
490+
* bounds/layout shell won't display content which is created on it.
491+
*
492+
* @param widthHint Current width of the shell that is already open.
493+
* @param heightHint Current height of the shell that is already open.
494+
*/
495+
void adjustTrim (int widthHint, int heightHint) {
483496
if (display.ignoreTrim) return;
484497
GtkAllocation allocation = new GtkAllocation ();
485498
GTK.gtk_widget_get_allocation (shellHandle, allocation);
486499
int width = allocation.width;
487500
int height = allocation.height;
488-
GdkRectangle rect = new GdkRectangle ();
489501

502+
//Ask Gtk for actual window frame extents
503+
GdkRectangle rect = new GdkRectangle ();
490504
if (!GTK.GTK4) {
491505
long window = gtk_widget_get_window (shellHandle);
492506
GDK.gdk_window_get_frame_extents (window, rect);
507+
508+
// Gtk returns default window dimensions as {0,0,1,1} if the window is not
509+
// ready. If GTK returns window dimensions we can use it, if not, we will use
510+
// "requested" shell size as returned by gtk_window_resize() in setVisible().
511+
// See https://github.com/eclipse-platform/eclipse.platform.swt/issues/1161
512+
if (rect.width > 1 || rect.height > 1) {
513+
widthHint = rect.width;
514+
heightHint = rect.height;
515+
}
493516
}
494-
int trimWidth = Math.max (0, rect.width - width);
495-
int trimHeight = Math.max (0, rect.height - height);
517+
518+
519+
int trimWidth = Math.max (0, widthHint - width);
520+
int trimHeight = Math.max (0, heightHint - height);
496521
/*
497522
* Bug in GTK. gdk_window_get_frame_extents() fails for various window
498523
* managers, causing a large incorrect value to be returned as the trim.
@@ -2936,6 +2961,8 @@ public void setVisible (boolean visible) {
29362961
GTK.gtk_widget_show (shellHandle);
29372962
GTK3.gtk_window_resize(shellHandle, init_width[0], init_height[0]);
29382963
resizeBounds (init_width[0], init_height[0], false);
2964+
oldWidth = init_width[0];
2965+
oldHeight = init_height[0];
29392966
} else {
29402967
GTK.gtk_widget_show (shellHandle);
29412968
}
@@ -2975,7 +3002,7 @@ public void setVisible (boolean visible) {
29753002
if (!iconic) {
29763003
update (true, true);
29773004
if (isDisposed ()) return;
2978-
adjustTrim ();
3005+
adjustTrim (oldWidth, oldHeight);
29793006
}
29803007
}
29813008
mapped = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.eclipse.swt.tests.gtk.snippets;
2+
3+
import org.eclipse.swt.SWT;
4+
import org.eclipse.swt.graphics.Point;
5+
import org.eclipse.swt.layout.GridData;
6+
import org.eclipse.swt.layout.GridLayout;
7+
import org.eclipse.swt.widgets.Display;
8+
import org.eclipse.swt.widgets.Label;
9+
import org.eclipse.swt.widgets.Shell;
10+
11+
/**
12+
* Please remove $HOME/swt/trims.prefs to see the issue.
13+
*/
14+
public class Issue1161_InitialWrongShellSizeDialogTrim {
15+
static String message = "Soooooooooooooooooooooooooooooooooooooooooooooooooome Extraaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.\n"
16+
+ "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeext";
17+
/**
18+
* This problem happens with SWT.DIALOG_TRIM. In general any trim with No Resize
19+
* style
20+
*/
21+
static int style = SWT.DIALOG_TRIM;
22+
23+
public static void main(String[] args) {
24+
Display display = new Display();
25+
Shell shell = new Shell(display, style);
26+
shell.setText("Issue1161_InitialWrongShellSizeDialogTrim");
27+
28+
// Even with the FillLayout issue exists.
29+
GridLayout layout = new GridLayout();
30+
layout.verticalSpacing = 20;
31+
layout.marginBottom = layout.marginTop = layout.marginLeft = layout.marginRight = 10;
32+
shell.setLayout(layout);
33+
34+
// many labels to make the dialog bigger
35+
for (int i = 0; i < 8; i++) {
36+
Label label = new Label(shell, SWT.NONE);
37+
label.setText("Line:" + i + " " + message);
38+
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
39+
}
40+
41+
Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
42+
// x and y location are randomly chosen.
43+
// width and height are correctly calculated.
44+
shell.setBounds(600, 600, size.x, size.y);
45+
46+
shell.pack();
47+
shell.open();
48+
49+
while (!shell.isDisposed()) {
50+
if (!display.readAndDispatch()) {
51+
display.sleep();
52+
}
53+
}
54+
display.dispose();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)