Skip to content

Conversation

@ShahzaibIbrahim
Copy link
Contributor

The Table.DRAG_IMAGE_SIZE constant specifies the width of the drag image when a table item is dragged. Previously, this value was fixed in pixels, which caused the drag image to appear too small on high-DPI monitors.

This change redefines DRAG_IMAGE_SIZE in points and converts it to pixels based on the current zoom level, ensuring consistent drag image sizing across different display scales.

How to test

  • Run the following snippet in 100% monitor zoom and then 250% monitor zoom
  • Try to drag the leaf item
  • The drag item should look bigger in 250% monitor
import org.eclipse.swt.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class DragImageOfTableItemExample {

    public static void main(String[] args) {
        final Display display = new Display();
        System.setProperty("swt.autoScale", "quarter");
        System.setProperty("swt.autoScale.updateOnRuntime", "true");
        final Shell shell = new Shell(display);
        shell.setText("Table Drag & Drop Example");
        shell.setLayout(new FillLayout());

        final Table table = new Table(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        TableColumn col1 = new TableColumn(table, SWT.NONE);
        col1.setText("Column 1");
        col1.setWidth(150);

        TableColumn col2 = new TableColumn(table, SWT.NONE);
        col2.setText("Column 2");
        col2.setWidth(150);

        for (int i = 0; i < 10; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[]{"Row " + i, "Value " + i});
        }

        Transfer[] types = new Transfer[]{TextTransfer.getInstance()};
        int operations = DND.DROP_MOVE | DND.DROP_COPY;

        final DragSource source = new DragSource(table, operations);
        source.setTransfer(types);
        final TableItem[] dragSourceItem = new TableItem[1];

        source.addDragListener(new DragSourceListener() {
            @Override
            public void dragStart(DragSourceEvent event) {
                TableItem[] selection = table.getSelection();
                if (selection.length > 0) {
                    dragSourceItem[0] = selection[0];
                    event.doit = true;
                } else {
                    event.doit = false;
                }
            }

            @Override
            public void dragSetData(DragSourceEvent event) {
                event.data = dragSourceItem[0].getText(0); // send first column text
            }

            @Override
            public void dragFinished(DragSourceEvent event) {
                if (event.detail == DND.DROP_MOVE && dragSourceItem[0] != null) {
                    dragSourceItem[0].dispose();
                }
                dragSourceItem[0] = null;
            }
        });

        DropTarget target = new DropTarget(table, operations);
        target.setTransfer(types);
        target.addDropListener(new DropTargetAdapter() {
            @Override
            public void dragOver(DropTargetEvent event) {
                event.feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_INSERT_BEFORE | DND.FEEDBACK_INSERT_AFTER;
                if (event.item != null) {
                    TableItem item = (TableItem) event.item;
                    Point pt = display.map(null, table, event.x, event.y);
                    Rectangle bounds = item.getBounds();
                    if (pt.y < bounds.y + bounds.height / 2) {
                        event.feedback = DND.FEEDBACK_INSERT_BEFORE;
                    } else {
                        event.feedback = DND.FEEDBACK_INSERT_AFTER;
                    }
                }
            }

            @Override
            public void drop(DropTargetEvent event) {
                if (event.data == null) {
                    event.detail = DND.DROP_NONE;
                    return;
                }
                String text = (String) event.data;

                if (event.item == null) {
                    TableItem newItem = new TableItem(table, SWT.NONE);
                    newItem.setText(new String[]{text, "new"});
                } else {
                    TableItem item = (TableItem) event.item;
                    Point pt = display.map(null, table, event.x, event.y);
                    Rectangle bounds = item.getBounds();
                    int index = table.indexOf(item);
                    if (pt.y < bounds.y + bounds.height / 2) {
                        TableItem newItem = new TableItem(table, SWT.NONE, index);
                        newItem.setText(new String[]{text, "inserted"});
                    } else {
                        TableItem newItem = new TableItem(table, SWT.NONE, index + 1);
                        newItem.setText(new String[]{text, "inserted"});
                    }
                }
            }
        });

        shell.setSize(400, 400);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

Result (250% Zoom Monitor)

image

@github-actions
Copy link
Contributor

github-actions bot commented Aug 27, 2025

Test Results

   546 files  ±0     546 suites  ±0   34m 43s ⏱️ - 1m 45s
 4 426 tests ±0   4 409 ✅ ±0   17 💤 ±0  0 ❌ ±0 
16 750 runs  ±0  16 623 ✅ ±0  127 💤 ±0  0 ❌ ±0 

Results for commit 0b95f4f. ± Comparison against base commit 2143c15.

♻️ This comment has been updated with latest results.

@ShahzaibIbrahim ShahzaibIbrahim force-pushed the master-404-TABLE-DRAG_IMAGE_SIZE branch from 819c1c3 to d123300 Compare August 27, 2025 13:12
Copy link
Contributor

@akoch-yatta akoch-yatta left a comment

Choose a reason for hiding this comment

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

Overall the change is sound and makes the usage of the dragging preview consistent across different zoom.

The Table.DRAG_IMAGE_SIZE constant specifies the width of the drag image
when a table item is dragged. Previously, this value was fixed in
pixels, which caused the drag image to appear too small on high-DPI
monitors.

This change redefines DRAG_IMAGE_SIZE in points and converts it to
pixels based on the current zoom level, ensuring consistent drag image
sizing across different display scales.
@akoch-yatta akoch-yatta force-pushed the master-404-TABLE-DRAG_IMAGE_SIZE branch from d123300 to 0b95f4f Compare September 3, 2025 07:46
@akoch-yatta akoch-yatta merged commit d6709a4 into eclipse-platform:master Sep 3, 2025
17 checks passed
@akoch-yatta akoch-yatta deleted the master-404-TABLE-DRAG_IMAGE_SIZE branch September 3, 2025 08:46
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.

Scale Table.DRAG_IMAGE_SIZE by zoom level instead of using fixed pixels

2 participants