Skip to content

Commit

Permalink
Ensure a minimum width of labels in Property sections
Browse files Browse the repository at this point in the history
- The system font can be larger than expected if, on Windows, a scaling of 125, 150, 175 is used. Or, on Mac, Java 21 uses a slightly larger font. This means that the fixed width of a label might be too short and some text characters wrap to a new line.

- So we determine the preferred width of a label by measuring the width of a string from its text extents. The larger of the preferred width or text extents is used.
  • Loading branch information
Phillipus committed Nov 21, 2024
1 parent 112c966 commit 5025d77
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/
package com.archimatetool.editor.propertysections;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
Expand Down Expand Up @@ -183,11 +187,11 @@ protected Composite createComposite(Composite parent, int numColumns, boolean ma
* Create Label control. Style is set to SWT.WRAP
* @param parent Parent composite
* @param text Text to display
* @param width Width of label in pixels
* @param width Preferred width of label in pixels. Might be bigger depending on OS font size.
* @param v_position Vertical position. Should be SWT.CENTER or SWT.NONE
* @return
*/
protected Label createLabel(Composite parent, String text, int width, int verticalPosition) {
protected Label createLabel(Composite parent, String text, int preferredWidth, int verticalPosition) {
Label label = getWidgetFactory().createLabel(parent, text, SWT.WRAP);

// On Mac a Group's backgound is grey but a child label is white so set the label's background to null.
Expand All @@ -197,7 +201,7 @@ protected Label createLabel(Composite parent, String text, int width, int vertic
}

GridData gd = new GridData(SWT.NONE, verticalPosition, false, false);
gd.widthHint = width;
gd.widthHint = getLabelWidth(label, preferredWidth);
label.setLayoutData(gd);
return label;
}
Expand All @@ -221,6 +225,26 @@ protected Composite createTableComposite(Composite parent, int style) {
return tableComp;
}

private static Map<Integer, Integer> labelWidths = new HashMap<>();

/**
* Get a label width given the preferred width.
* The label font will depend on OS, Java version and Display scale and resolution.
* @param label The Label
* @param preferredWidth The preferred width
* @return The maximum of preferred width or the text extents of the string "XXXXXXXXXXXXXX" in the Label's font size.
*/
protected static int getLabelWidth(Label label, int preferredWidth) {
if(!labelWidths.containsKey(preferredWidth)) {
final String s = "XXXXXXXXXXXXXX"; //$NON-NLS-1$
GC gc = new GC(label);
labelWidths.put(preferredWidth, Math.max(gc.textExtent(s).x, preferredWidth));
gc.dispose();
}
return labelWidths.get(preferredWidth);
}


// =========================== Global Action Disablement ==================================
// When a text control gets the focus we don't want global action handlers like Undo/Redo
// to be enabled otherwise the user thinks they are Undoing local text edits and actually
Expand Down Expand Up @@ -288,12 +312,4 @@ protected void addGlobalActionDisablementListener(Control control) {
}
});
}

// Add Mac kludge.
// Sub-classes that have text controls should implement this
// and update the text controls that might be affected by the Mac bug
@Deprecated
protected void focusGained(Control control) {
}

}

0 comments on commit 5025d77

Please sign in to comment.