1

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);
}
gpoo
  • 8,408
  • 3
  • 38
  • 53
hba
  • 7,406
  • 10
  • 63
  • 105

2 Answers2

1

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.

Colin Alworth
  • 17,801
  • 2
  • 26
  • 39
  • Colin, Size is my own brew...I'm not using GXT. – hba Jan 18 '13 at 01:35
  • Oh, so when you say window, you mean the entire browser window? I'll update my answer accordingly... – Colin Alworth Jan 18 '13 at 02:44
  • Colin, thanks, this is exactly how I'm tracking the window resize in my app. Thank you for explaining the different browser behaviours. – hba Jan 18 '13 at 17:28
0

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

agelbess
  • 4,249
  • 3
  • 20
  • 21