Skip to content

Fix layout of tables with checkboxes on mac #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# DurianSwt releases

## [Unreleased]
### Fixed
- Mac tables with a checkbox column now size the rest of their columns correctly. ([#14](https://github.com/diffplug/durian-swt/pull/14))

## [3.4.0] - 2021-07-17
### Added
Expand Down
13 changes: 9 additions & 4 deletions durian-swt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ dependencies {
}
*/
// in java-library mode, durian-swt.os remains as a folder of classes for gradle internally, which the osgi plugin does not like
tasks.jar.dependsOn(':durian-swt.os:jar')
tasks.named('jar').configure {
dependsOn(':durian-swt.os:jar')
}

/////////////////////////
// INTERACTIVE TESTING //
/////////////////////////
// standard `gradlew test` will autoclose after 500ms
boolean isMac = System.getProperty("os.name").toLowerCase().contains("mac")
test {
systemProperty 'com.diffplug.test.autoclose.milliseconds', '500'
useJUnit {
// there are some tests that can't pass without a user, so we'll exclude them
excludeCategories 'com.diffplug.common.swt.InteractiveTest$FailsWithoutUser'
// SWT tests don't work in gradle on OS X (https://github.com/ReadyTalk/swt-bling/issues/4)
boolean isMac = System.getProperty("os.name").toLowerCase().contains("mac");
if (isMac) {
excludeCategories 'com.diffplug.common.swt.InteractiveTest'
jvmArgs = ['-XstartOnFirstThread']
}
}
// log all test events
Expand All @@ -51,9 +53,12 @@ test {
}

// only run the interactive tests
task interactiveTest(type: Test) {
tasks.register('interactiveTest', Test) {
systemProperty 'com.diffplug.test.autoclose.milliseconds', null
useJUnit {
includeCategories 'com.diffplug.common.swt.InteractiveTest'
if (isMac) {
jvmArgs = ['-XstartOnFirstThread']
}
}
}
36 changes: 34 additions & 2 deletions durian-swt/src/main/java/com/diffplug/common/swt/ColumnFormat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 DiffPlug
* Copyright (C) 2020-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package com.diffplug.common.swt;


import com.diffplug.common.swt.os.OS;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -109,12 +110,43 @@ protected static Table buildTable(Composite parent, int style, boolean linesVisi
// create the columns and layout
Function<ColumnBuilder, TableColumn> buildFunc = builder -> builder.build(control);
List<TableColumn> columns = columnBuilders.stream().map(buildFunc).collect(Collectors.toList());
buildLayout(control, new TableColumnLayout(), columns, columnBuilders);

boolean needsSpecialLayout = SwtMisc.flagIsSet(SWT.CHECK, style) && OS.getNative().isMac();
TableColumnLayout layout = needsSpecialLayout ? new MacCheckTableColumnLayout() : new TableColumnLayout();
buildLayout(control, layout, columns, columnBuilders);

// return the control
return control;
}

/** Adds a phantom column for the checkboxes so that the rest of the space is calculated correctly. */
private static class MacCheckTableColumnLayout extends TableColumnLayout {
private static final int CHECK_COLUMN_WIDTH = 15;

@Override
protected int getColumnCount(Scrollable tableTree) {
return super.getColumnCount(tableTree) + 1;
}

@Override
protected void setColumnWidths(Scrollable tableTree, int[] widths) {
TableColumn[] columns = ((Table) tableTree).getColumns();
for (int i = 0; i < columns.length; i++) {
columns[i].setWidth(widths[i]);
}
}

@Override
protected ColumnLayoutData getLayoutData(Scrollable tableTree, int columnIndex) {
Table table = (Table) tableTree;
if (columnIndex < table.getColumnCount()) {
return (ColumnLayoutData) table.getColumn(columnIndex).getData(LAYOUT_DATA);
} else {
return new ColumnPixelData(CHECK_COLUMN_WIDTH);
}
}
}

/** Builds a table with the given columns. */
protected static Tree buildTree(Composite parent, int style, boolean linesVisible, boolean headerVisible, List<? extends ColumnBuilder> columnBuilders) {
SwtMisc.assertClean(parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2020-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.common.swt.jface;


import com.diffplug.common.swt.InteractiveTest;
import com.diffplug.common.swt.Layouts;
import java.util.Arrays;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(InteractiveTest.class)
public class ColumnViewerFormatTest {
@Test
public void testCopy() {
InteractiveTest.testCoat("Column viewer", 10, 10, cmp -> {
Layouts.setFill(cmp);
buildTableWithCheck(new Composite(cmp, SWT.BORDER), true);
buildTableWithCheck(new Composite(cmp, SWT.BORDER), false);
});
}

private void buildTableWithCheck(Composite cmp, boolean hasCheck) {
ColumnViewerFormat<String> format = ColumnViewerFormat.builder();
format.setStyle(SWT.FULL_SELECTION | (hasCheck ? SWT.CHECK : SWT.NONE));
format.addColumn().setLabelProviderText(s -> s);
TableViewer viewer = format.buildTable(cmp);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(Arrays.asList("Onesy twosy threesy foursy", "red orange yellow green blue purple"));
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 10 additions & 8 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
Expand Down Expand Up @@ -172,12 +172,14 @@ if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
esac
fi

ARGV=("$@")
eval set -- $DEFAULT_JVM_OPTS
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=`save "$@"`

IFS=$'
' read -rd '' -a JAVA_OPTS_ARR <<< "$(echo $JAVA_OPTS | xargs -n1)"
IFS=$'
' read -rd '' -a GRADLE_OPTS_ARR <<< "$(echo $GRADLE_OPTS | xargs -n1)"
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

exec "$JAVACMD" "$@" "${JAVA_OPTS_ARR[@]}" "${GRADLE_OPTS_ARR[@]}" "-Dorg.gradle.appname=$APP_BASE_NAME" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "${ARGV[@]}"
exec "$JAVACMD" "$@"
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
id 'org.jdrupes.mdoclet' apply false
}
blowdryerSetup {
github 'diffplug/blowdryer-diffplug', 'tag', '5.0.4'
github 'diffplug/blowdryer-diffplug', 'tag', '5.0.6'
//devLocal '../blowdryer-diffplug'
}

Expand Down