Skip to content

Fix label cutoff #2381

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

amartya4256
Copy link
Contributor

@amartya4256 amartya4256 commented Aug 5, 2025

This PR contributes to precise scaling of computation of size inside gridLayout by using float precision. This fixes the issue reported in #2166

How to test

package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SnippetLabelCutOff {
	public static void main(String[] args) {
		Display display = new Display();


		/* Provide different resolutions for icons to get
		 * high quality rendering wherever the OS needs
		 * large icons. For example, the ALT+TAB window
		 * on certain systems uses a larger icon.
		 */
		Shell shell = new Shell(display);
		PartView partView = new Snippet138.PartView(display);
		partView.postConstruct(shell);
		shell.open();



		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		partView.preDestroy();
		shell.dispose();
		display.dispose();
	}

	public static class PartView
	{
	   public Display aDisplay;

	   private Composite aMidComposite;

	   private Label aRepairLabel;

	   public Font aNormalFont;

	   public PartView(Display display) {
		aDisplay = display;
	}

	   public void postConstruct(Composite pParent)
	   {
	      FormLayout vLayout = new FormLayout();
	      vLayout.marginHeight = 5;
	      vLayout.marginWidth = 6;
	      pParent.setLayout(vLayout);

	      createResources();

	      createMid(pParent);

	      pParent.pack();
	   }

	   private void createResources()
	   {
	      aNormalFont = new Font(
	            aDisplay,
	            new FontData("Arial", 11, SWT.NORMAL));

	   }

	   private void createMid(Composite pParent)
	   {
	      // MID
	      aMidComposite = new Composite(pParent, SWT.NONE);
	      aMidComposite.setBackground(aDisplay.getSystemColor(SWT.COLOR_WHITE));
	      FormData aFormDataMid = new FormData();
	      aFormDataMid.top = new FormAttachment(0, 0);
	      aFormDataMid.bottom = new FormAttachment(100, 0);
	      aFormDataMid.left = new FormAttachment(0, 0);
	      aFormDataMid.right = new FormAttachment(100, 0);
	      aMidComposite.setLayoutData(aFormDataMid);

	      GridLayout vLayout = new GridLayout();
	      aMidComposite.setLayout(vLayout);

	      aRepairLabel = new Label(aMidComposite, SWT.WRAP);
	      aRepairLabel.setFont(aNormalFont);
	      GridData vRepairLabelGridData = new GridData();
	      vRepairLabelGridData.horizontalAlignment = GridData.BEGINNING;
	      vRepairLabelGridData.verticalAlignment = GridData.CENTER;
	      vRepairLabelGridData.grabExcessHorizontalSpace = true;
	      vRepairLabelGridData.verticalIndent = Math.round(32);
	      aRepairLabel.setLayoutData(vRepairLabelGridData);

	      aRepairLabel
	            .setText(
	                  "Rhe erv fhe eovianae tb orao estnffd rniTaa eultoolte i sssube wreco.");
	   }

	   public void preDestroy()
	   {
	      aNormalFont.dispose();
	   }
	}
}

Before:
image

After:
image

Copy link
Contributor

github-actions bot commented Aug 5, 2025

Test Results

   546 files  ±0     546 suites  ±0   33m 58s ⏱️ + 4m 25s
 4 425 tests ±0   4 408 ✅ ±0   17 💤 ±0  0 ❌ ±0 
16 746 runs  ±0  16 619 ✅ ±0  127 💤 ±0  0 ❌ ±0 

Results for commit fc7dedf. ± Comparison against base commit 20d18aa.

♻️ This comment has been updated with latest results.

@amartya4256 amartya4256 force-pushed the amartya4256/fix_label_cutoff branch from e48df64 to 4e2fcda Compare August 6, 2025 08:41
This commit contributes to enable GridLayout:layout to utilize
Point.OfFloat in GridData to calculate precise size on scaling.

contributes to eclipse-platform#2166
This commit adapts Label to return Point.OfFloat object on
Label:computeSizeInPixels ultimately returning Point.OfFloat on calling
Label:computeSize. This allows precise scaling of Label by its parents.

contributes to eclipse-platform#2166
@amartya4256 amartya4256 force-pushed the amartya4256/fix_label_cutoff branch from 4e2fcda to fc7dedf Compare August 6, 2025 08:43
@amartya4256 amartya4256 changed the title Amartya4256/fix label cutoff Fix label cutoff Aug 6, 2025
@amartya4256 amartya4256 marked this pull request as ready for review August 6, 2025 11:26
@amartya4256 amartya4256 linked an issue Aug 6, 2025 that may be closed by this pull request
cacheWidth = cacheHeight = -1;
defaultWidth = defaultHeight = -1;
currentWidth = currentHeight = -1;
cacheSize = new Point.OfFloat(-1, -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cacheSize = new Point.OfFloat(-1, -1);
cacheSize = new Point.OfFloat(SWT.DEFAULT, SWT.DEFAULT);

@@ -487,34 +489,28 @@ public GridData (int width, int height) {
}

void computeSize (Control control, int wHint, int hHint, boolean flushCache) {
if (cacheWidth != -1 && cacheHeight != -1) return;
if (cacheSize.x != -1 && cacheSize.y != -1) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the -1 in all comparisons in this function should be replaced with SWT.DEFAULT

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option I think is cleaner is to have
public static final OfFloat UNDEFINED = new OfFloat(-1f, -1f);

in the point class so comparisons could be

cacheSize == Point.OfFloat.UNDEFINED;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot set UNDEFINED = new OfFloat(-1f, -1f) since -1, -1 can also represent a point in the coordinate system and it's only valid for cacheSize in the case of Grid data. Hence, that should be handled in GridData. I like the idea of checking with SWT.DEFAULT.

Copy link
Contributor

@HeikoKlare HeikoKlare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One general question on this: wouldn't it possible or even reasonable to stick to width/height values inside GridData but just make them floats and only use Point.OfFloat for the returns of API methods?
Two reasons for this:

  1. SWT is generally lacking some Size object and, unfortunately, at many places Point is used to represent a size (even though that semantically does not make that much sense). Thus, we usually try to stick to width/height when considering sizes.
  2. It would be less error prone as now if you accidentally use, e.g., size.x instead of size.getX(), you may calculate with a wrong value.

@amartya4256
Copy link
Contributor Author

One general question on this: wouldn't it possible or even reasonable to stick to width/height values inside GridData but just make them floats and only use Point.OfFloat for the returns of API methods? Two reasons for this:

  1. SWT is generally lacking some Size object and, unfortunately, at many places Point is used to represent a size (even though that semantically does not make that much sense). Thus, we usually try to stick to width/height when considering sizes.
  2. It would be less error prone as now if you accidentally use, e.g., size.x instead of size.getX(), you may calculate with a wrong value.

I agree, I'll make the changes.

@@ -705,7 +712,7 @@ Point layout (Composite composite, boolean move, int x, int y, int width, int he

// clean up cache
for (int i = 0; i < flushLength; i++) {
flush [i].cacheWidth = flush [i].cacheHeight = -1;
flush [i].cacheSize = new Point.OfFloat(SWT.DEFAULT, SWT.DEFAULT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be ok to simply change the existing point instead of creating a new one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean: to avoid unnecessary object creation. I assume that layout is called pretty often?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Labels are cut off by autoscaling
4 participants