Skip to content

Commit 611accb

Browse files
kohlschuettermerks
authored andcommitted
cocoa: Enforce native item height for Table, Tree, List if necessary
Newer versions of macOS introduced a new style of table rows with a padding that is larger than previously seen. This broke item height computations (#677). An attempt to fix this had the unexpected side effect that now some UI elements in Eclipse IDE (and other apps with "-Dorg.eclipse.swt.internal.carbon.smallFonts" enabled), eclise.platform.ui #1674, and it also broke compatibility with older versions of macOS that did not have the additional padding. This change reverts the previous fix, and adds logic to enforce the native item height by default, without making assumption about the exact height (which varies with "smallFonts", for example). The setting can be controlled by a new System property, org.eclipse.swt.internal.cocoa.useNativeItemHeight, that, if set to false, overrules the default. This allows apps to bring brack the old macOS behavior. Lastly, update two tests in Test_org_eclipse_swt_widgets_List that made assumptions about the item height being different for two small font sizes -- this is no longer true when the native item height is enforced. We now increase the font size of the second font such that this corner case no longer matters. Fixes eclipse-platform/eclipse.platform.ui#1674 Fixes #677
1 parent e87e11e commit 611accb

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ enum APPEARANCE {
166166
NSFont textViewFont, tableViewFont, outlineViewFont, datePickerFont;
167167
NSFont boxFont, tabViewFont, progressIndicatorFont;
168168

169+
boolean useNativeItemHeight;
170+
169171
Shell [] modalShells;
170172
Dialog modalDialog;
171173
NSPanel modalPanel;
@@ -2404,6 +2406,8 @@ protected void init () {
24042406
initFonts ();
24052407
setDeviceZoom ();
24062408

2409+
useNativeItemHeight = initUseNativeItemHeight();
2410+
24072411
/*
24082412
* Create an application delegate for app-level notifications. The AWT may have already set a delegate;
24092413
* if so, hold on to it so messages can be forwarded to it.
@@ -2502,6 +2506,19 @@ public void run() {
25022506
isPainting = isPainting.initWithCapacity(12);
25032507
}
25042508

2509+
/**
2510+
* Checks if the native item height should be enforced as a minimum (which is true by default).
2511+
*
2512+
* Newer version of macOS may use a default item height in Table, Tree and List
2513+
* controls that is larger than what is traditionally expected.
2514+
*
2515+
* Enforcing the default height as a minimum may break existing assumptions and
2516+
* render UI elements with a padding that may be considered too large.
2517+
*/
2518+
private boolean initUseNativeItemHeight() {
2519+
return Boolean.parseBoolean(System.getProperty("org.eclipse.swt.internal.cocoa.useNativeItemHeight", "true"));
2520+
}
2521+
25052522
private static NSString getAwtRunLoopMode() {
25062523
// Special run loop mode mode used by AWT enters when it only wants related messages processed.
25072524
// The name of this mode is a defacto contract established by the JavaNativeFoundation (JNF) libary.

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/List.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ public class List extends Scrollable {
4646
int itemCount;
4747
boolean ignoreSelect, didSelect, rowsChanged, mouseIsDown;
4848

49+
final int nativeItemHeight;
50+
4951
static int NEXT_ID;
5052

5153
static final int CELL_GAP = 1;
5254

53-
/* Vertical cell padding for list item */
54-
static final int VERTICAL_CELL_PADDING= 8;
55-
5655
/**
5756
* Constructs a new instance of this class given its parent
5857
* and a style value describing its behavior and appearance.
@@ -84,6 +83,9 @@ public class List extends Scrollable {
8483
*/
8584
public List (Composite parent, int style) {
8685
super (parent, checkStyle (style));
86+
87+
this.nativeItemHeight = (int)((NSTableView)view).rowHeight();
88+
setFont(defaultFont ().handle); // update height
8789
}
8890

8991
@Override
@@ -1178,7 +1180,11 @@ void setFont (NSFont font) {
11781180
super.setFont (font);
11791181
double ascent = font.ascender ();
11801182
double descent = -font.descender () + font.leading ();
1181-
((NSTableView)view).setRowHeight ((int)Math.ceil (ascent + descent) + VERTICAL_CELL_PADDING);
1183+
int height = (int)Math.ceil (ascent + descent) + 1;
1184+
if (display.useNativeItemHeight) {
1185+
height = Math.max (height, nativeItemHeight);
1186+
}
1187+
((NSTableView)view).setRowHeight (height);
11821188
setScrollWidth();
11831189
}
11841190

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Table.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,15 @@ public class Table extends Composite {
8686
boolean shouldScroll = true;
8787
boolean keyDown;
8888

89+
final int nativeItemHeight;
90+
8991
static int NEXT_ID;
9092

9193
static final int FIRST_COLUMN_MINIMUM_WIDTH = 5;
9294
static final int IMAGE_GAP = 3;
9395
static final int TEXT_GAP = 2;
9496
static final int CELL_GAP = 1;
9597

96-
/* Vertical cell padding for table item */
97-
static final int VERTICAL_CELL_PADDING= 8;
98-
9998
/**
10099
* Constructs a new instance of this class given its parent
101100
* and a style value describing its behavior and appearance.
@@ -132,6 +131,9 @@ public class Table extends Composite {
132131
*/
133132
public Table (Composite parent, int style) {
134133
super (parent, checkStyle (style));
134+
135+
this.nativeItemHeight = (int)((NSTableView)view).rowHeight();
136+
setItemHeight(null, null, true);
135137
}
136138

137139
@Override
@@ -2821,7 +2823,10 @@ void setItemHeight (Image image, NSFont font, boolean set) {
28212823
if (font == null) font = getFont ().handle;
28222824
double ascent = font.ascender ();
28232825
double descent = -font.descender () + font.leading ();
2824-
int height = (int)Math.ceil (ascent + descent) + VERTICAL_CELL_PADDING;
2826+
int height = (int)Math.ceil (ascent + descent) + 1;
2827+
if (display.useNativeItemHeight) {
2828+
height = Math.max (height, nativeItemHeight);
2829+
}
28252830
Rectangle bounds = image != null ? image.getBounds () : imageBounds;
28262831
if (bounds != null) {
28272832
imageBounds = bounds;

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Tree.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public class Tree extends Composite {
9797
/* Used to control drop feedback when DND.FEEDBACK_EXPAND and DND.FEEDBACK_SCROLL is set/not set */
9898
boolean shouldExpand = true, shouldScroll = true;
9999

100+
final int nativeItemHeight;
101+
100102
static int NEXT_ID;
101103

102104
/*
@@ -109,9 +111,6 @@ public class Tree extends Composite {
109111
static final int TEXT_GAP = 2;
110112
static final int CELL_GAP = 1;
111113

112-
/* Vertical cell padding for tree item */
113-
static final int VERTICAL_CELL_PADDING= 8;
114-
115114
/**
116115
* Constructs a new instance of this class given its parent
117116
* and a style value describing its behavior and appearance.
@@ -147,6 +146,9 @@ public class Tree extends Composite {
147146
*/
148147
public Tree (Composite parent, int style) {
149148
super (parent, checkStyle (style));
149+
150+
this.nativeItemHeight = (int)((NSTableView)view).rowHeight();
151+
setItemHeight(null, null, true);
150152
}
151153

152154
@Override
@@ -3165,7 +3167,10 @@ void setItemHeight (Image image, NSFont font, boolean set) {
31653167
if (font == null) font = getFont ().handle;
31663168
double ascent = font.ascender ();
31673169
double descent = -font.descender () + font.leading ();
3168-
int height = (int)Math.ceil (ascent + descent) + VERTICAL_CELL_PADDING;
3170+
int height = (int)Math.ceil (ascent + descent) + 1;
3171+
if (display.useNativeItemHeight) {
3172+
height = Math.max (height, nativeItemHeight);
3173+
}
31693174
Rectangle bounds = image != null ? image.getBounds () : imageBounds;
31703175
if (bounds != null) {
31713176
imageBounds = bounds;

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_List.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void test_getItemHeight() {
498498
lineHeight = list.getItemHeight();
499499
list.setFont(null);
500500
font.dispose();
501-
font = new Font(list.getDisplay(), fontData.getName(), 12, fontData.getStyle());
501+
font = new Font(list.getDisplay(), fontData.getName(), 24, fontData.getStyle());
502502
list.setFont(font);
503503
int newLineHeight = list.getItemHeight();
504504
assertTrue(":a:", newLineHeight > lineHeight);
@@ -1625,7 +1625,7 @@ public void test_setFontLorg_eclipse_swt_graphics_Font() {
16251625
lineHeight = list.getItemHeight();
16261626
list.setFont(null);
16271627
font.dispose();
1628-
font = new Font(list.getDisplay(), fontData.getName(), 12, fontData.getStyle());
1628+
font = new Font(list.getDisplay(), fontData.getName(), 24, fontData.getStyle());
16291629
list.setFont(font);
16301630
assertEquals(font, list.getFont());
16311631
assertTrue("itemHeight=" + list.getItemHeight() + ", lineHeight=" + lineHeight, list.getItemHeight() > lineHeight);

0 commit comments

Comments
 (0)