Skip to content

Commit 2f95b3f

Browse files
committed
ColumnFormat now computes size correctly on mac.
1 parent 705b1d0 commit 2f95b3f

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

durian-swt/src/main/java/com/diffplug/common/swt/ColumnFormat.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package com.diffplug.common.swt;
1717

1818

19+
import com.diffplug.common.swt.os.OS;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -109,12 +110,43 @@ protected static Table buildTable(Composite parent, int style, boolean linesVisi
109110
// create the columns and layout
110111
Function<ColumnBuilder, TableColumn> buildFunc = builder -> builder.build(control);
111112
List<TableColumn> columns = columnBuilders.stream().map(buildFunc).collect(Collectors.toList());
112-
buildLayout(control, new TableColumnLayout(), columns, columnBuilders);
113+
114+
boolean needsSpecialLayout = SwtMisc.flagIsSet(SWT.CHECK, style) && OS.getNative().isMac();
115+
TableColumnLayout layout = needsSpecialLayout ? new MacCheckTableColumnLayout() : new TableColumnLayout();
116+
buildLayout(control, layout, columns, columnBuilders);
113117

114118
// return the control
115119
return control;
116120
}
117121

122+
/** Adds a phantom column for the checkboxes so that the rest of the space is calculated correctly. */
123+
private static class MacCheckTableColumnLayout extends TableColumnLayout {
124+
private static final int CHECK_COLUMN_WIDTH = 15;
125+
126+
@Override
127+
protected int getColumnCount(Scrollable tableTree) {
128+
return super.getColumnCount(tableTree) + 1;
129+
}
130+
131+
@Override
132+
protected void setColumnWidths(Scrollable tableTree, int[] widths) {
133+
TableColumn[] columns = ((Table) tableTree).getColumns();
134+
for (int i = 0; i < columns.length; i++) {
135+
columns[i].setWidth(widths[i]);
136+
}
137+
}
138+
139+
@Override
140+
protected ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex) {
141+
Table table = (Table) tableTree;
142+
if (columnIndex < table.getColumnCount()) {
143+
return (ColumnLayoutData) table.getColumn(columnIndex).getData(LAYOUT_DATA);
144+
} else {
145+
return new ColumnPixelData(CHECK_COLUMN_WIDTH);
146+
}
147+
}
148+
}
149+
118150
/** Builds a table with the given columns. */
119151
protected static Tree buildTree(Composite parent, int style, boolean linesVisible, boolean headerVisible, List<? extends ColumnBuilder> columnBuilders) {
120152
SwtMisc.assertClean(parent);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2020-2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.common.swt.jface;
17+
18+
19+
import com.diffplug.common.swt.InteractiveTest;
20+
import com.diffplug.common.swt.Layouts;
21+
import java.util.Arrays;
22+
import org.eclipse.jface.viewers.ArrayContentProvider;
23+
import org.eclipse.jface.viewers.TableViewer;
24+
import org.eclipse.swt.SWT;
25+
import org.eclipse.swt.widgets.Composite;
26+
import org.junit.Test;
27+
import org.junit.experimental.categories.Category;
28+
29+
@Category(InteractiveTest.class)
30+
public class ColumnViewerFormatTest {
31+
@Test
32+
public void testCopy() {
33+
InteractiveTest.testCoat("Column viewer", 10, 10, cmp -> {
34+
Layouts.setFill(cmp);
35+
buildTableWithCheck(new Composite(cmp, SWT.BORDER), true);
36+
buildTableWithCheck(new Composite(cmp, SWT.BORDER), false);
37+
});
38+
}
39+
40+
private void buildTableWithCheck(Composite cmp, boolean hasCheck) {
41+
ColumnViewerFormat<String> format = ColumnViewerFormat.builder();
42+
format.setStyle(SWT.FULL_SELECTION | (hasCheck ? SWT.CHECK : SWT.NONE));
43+
format.addColumn().setLabelProviderText(s -> s);
44+
TableViewer viewer = format.buildTable(cmp);
45+
viewer.setContentProvider(ArrayContentProvider.getInstance());
46+
viewer.setInput(Arrays.asList("Onesy twosy threesy foursy", "red orange yellow green blue purple"));
47+
}
48+
}

0 commit comments

Comments
 (0)