HaiderSahib
HaiderSahib

Reputation: 380

GWT - Browser window resized handler

I am developing a GWT application that render a text on a canvas. I want to resize the canvas whenever browser window resized. The problem is if I used Window.addResizeHandler, the rendering process with each resize will be very slow. So I need a way to resize the canvas only when the user release the mouse button after finishing resize. Is there anyway to do that?

Upvotes: 15

Views: 16599

Answers (2)

Ser Yoga
Ser Yoga

Reputation: 466

Window.addResizeHandler(new ResizeHandler() {

    @Override
    public void onResize(ResizeEvent event) {
        Scheduler.get().scheduleDeferred(
                new Scheduler.ScheduledCommand() {
                    public void execute() {
                        // layout stuff
                    }
                });
    }

});

Upvotes: 3

Riley Lark
Riley Lark

Reputation: 20890

You could add a delay, so that the resize is only processed after the window hasn't been resized for some number of milliseconds:

Window.addResizeHandler(new ResizeHandler() {

  Timer resizeTimer = new Timer() {  
    @Override
    public void run() {
      doComplexLayoutCalculations();
    }
  };

  @Override
  public void onResize(ResizeEvent event) {
    resizeTimer.cancel();
    resizeTimer.schedule(250);
  }
});

Upvotes: 37

Related Questions