Correct way do preserve shell aspect ratio in SWT

2019-09-17 15:33发布

问题:

I've been trying to find a way to force a shell to resize preserving a certain aspect ratio. This is the only way I've come up with:

shell.addControlListener(new ControlListener() {

    @Override
    public void controlMoved(ControlEvent e) {}

    @Override
    public void controlResized(ControlEvent e) {
        final Rectangle window = mShell.getClientArea();

        if (aspectRatio <= 1) {
            window.width = (int) (window.height * aspectRatio);
        } else {
            window.height = (int) (window.width / aspectRatio);
        }
        shell.setSize(window.width, window.height);
    }
});

The problem with this approach is that shell.setSize() also sets off a resize event (and it also screws up the title bar in OS X Lion)...

Any ideas? This problem has really been killing me for ages and I couldn't find a solution anywhere.