hba
hba

Reputation: 7800

GWT onResize & mouse up

I have a window resizeHandler, it is working fine. Except that I'm just interested in the final dimension of the window, in other words, I'm interested in the dimension of the window when the mouse button is released. Is there a way I can listen to window mouse events?

I've written a piece of code that accomplishes my goal but I'd prefer something more obvious. resizeRequestId is a field:

private void processResize() {

    final Timer timer = new Timer() {
    final Size windowSize = getWindowDimention();

    final int requestId = ++resizeRequestId;

    @Override
        public void run() {
            final boolean isLatestRequest = requestId == resizeRequestId;
        if (isLatestRequest) {
                //DO SOMETHING WITH windowSize
            }
        }
    };
    timer.schedule(100);
}

Upvotes: 1

Views: 676

Answers (2)

agelbess
agelbess

Reputation: 4349

Colin is right.

Moreover, if you do a lot of calculations "on resize" (e.g. forceLayout), it is a good idea to add a Timer. This way the calculations will be fired once every...10th of second?

final Timer resizeTimer = new Timer() {
    @Override
    public void run() {
        mainPanel.forceLayout();
    }
};
Window.addResizeHandler(new ResizeHandler() {
    public void onResize(ResizeEvent event) {
        int height = event.getHeight();
        mainPanel.setHeight(height + "px");
        resizeTimer.cancel();
        resizeTimer.schedule(100);
    }
});

That's it

Upvotes: 0

Colin Alworth
Colin Alworth

Reputation: 18356

The browser doesn't pass along events that happen outside of the page, and the window resize counts as outside the page. That said, you still get a hook for resize actions of the entire browser window:

Window.addResizeHandler(new ResizeHandler() {
  public void onResize(ResizeEvent event) {
    //get, process window resize behavior
  }
});

For some browsers and some resizes, you'll get lots of events as the mouse moves, and for others, you'll only get the complete one. In firefox, for example, the resize handle in the corner sends every change that is made, while the side handles each send only once the user releases the mouse. Minimizing and maximizing the window also result in a single event.

Upvotes: 1

Related Questions