Skip to content

Create a new more semantic API to measure Controls and set their bounds #2488

@laeubi

Description

@laeubi

Currently SWT is heavy dependent on very generic datatypes (e.g. primitive int or simple containers like Rectangle and Point) and we try to circumvent this limitation with some extensions like Point/Rectangle with monitors, Float Points and so on but the general limitations stay:

  1. if using a int/float as a primitive value it is easy to mix up things and these can not carry internal data easily
  2. Points and Rectangles are used for different purposes, e.g. we use points to measure a size (e.g. Strings rendered with a font, or size of a Control) but also to express a location (e.g. where a shell should be shown or a click event or similar), also here it is absolutely possible to "mix" different things.
  3. There is no way to give something as a unit of things, e.g. when specify the width of a control that shows some text, giving that in any absolute unit usually does not give good results as the font-size might vary per system, per OS or even per user account. Instead one would want to use relative units (e.g. percent, or em) what is quite common in CSS and responsive designs.

I therefore like to suggest we are adding new types and APIs to controls that account for that fact especially:

  • A Width and a Height type that can carry a value and optionally a unit and a Size that is a combination of Width and Height
  • A Position and a Bounds what would be a combination of a Position and a Size
  • ...

These types should then offer ways to perform mathematical operations on them in a type-safe fashion, e.g. one should not be able to add a Width and a Height directly and we need new API methods that accept and return such types.

With that for example a Width given in pixels can easily carry a zoom factor or monitor from its control it was gathered from and return a value in Point or ems.

As an example

public void setBounds (Bounds bound);
public Size computeSize (Width wHint, Height hHint);

Would then a layout manager allow to compute all values with type Width when it comes to compute a size like this:

Bounds availableArea = control.getBounds();
Control[] childs = control.getChildren();
Width evenWidth = availableArea.divide(childs.length);
Position current = Position.origin();
foreach(Control c : childs) {
  c.setBounds(Bounds.of(current, Size.of(evenWidth, c.computeSize(evenWidth, Height.DEFAULT).height());
  current = current.add(evenWidth);
}

such code then can even use the native pixel values internally without any precision loss. Such values then even might be dynamically scaled base on the current Monitor position instead of being recomputed all the way long.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions