Skip navigation.

exploreopera

| Help

Sign up | Help

Web Applications Blog

avatar

Moving and resizing widgets

, , , , , ,

One of the last additions we made to widgets in the stages before releasing Opera 9.0 was giving widgets the ability to resize and move themselves.

The main use for resizing behavior is in those cases where you want to emulate UI widgets (yes, that other kind of widget) like maximize, restore, and regular resize and drag handlers.

interface Window {
  void resizeTo( in int w, in int h );
  void resizeBy( in int delta_w, in int delta_h );
  void moveTo( in int x, in int y );
  void moveBy( in int delta_x , in int delta_y );
};

window.resizeTo()

The window.resizeTo() method takes two positive integers as arguments: A width w and height h. These sizes are the desired dimensions of the widgets in pixels, and correspond to the width and height elements in a widget's configuration file. The method sets the window.outerWidth and window.outerHeight properties. Example:

// Resize the widget to 400x300 px
window.resizeTo( 400, 300 );

window.resizeBy()

The window.resizeBy method takes two integers as arguments: A delta_w that is the desired change in width, measured in pixels, and a delta_h that is the desired change in height, again measured in pixels. The width and height of the widget is then recalculated to be equivalent to the following pseudocode:

window.resizeTo( window.outerWidth + delta_x, window.outerHeight + delta_y );

In all cases of window resizing, the resizing happens from the lower-right corner of the widget window, while the upper-left corner remains static.

// Grow the widget by 100px horizontally
window.resizeBy( 100, 0 );

window.moveTo()

The window.moveTo method takes two integers as arguments: An x that is the desired x coordinate for the upper-left corner of the widget, and an y that is the desired y coordinate for the upper-left corner of the widget, with both values mentioned in pixels.

// Move the widget to the upper-left corner of the screen
window.moveTo( 0, 0 )

window.moveBy()

The window.resizeBy method takes two integers as arguments: A delta_x that is the desired change in x position, measured in pixels, and a delta_y that is the desired change in y position, again measured in pixels. Both values can be negative. Example:

// Move the 50px left and 50px down
window.moveBy( -50, 50 )

Closing note

If you change these settings for a widget, the settings are volatile, and the change will be lost if the widget is closed and then reopened. This means that if your widget relies on being a certain size, which may be different from the install-time default, you should make sure that you store the widget size in the persistent storage. The widget storage interface is compatible with Apple's interface, using the methods preferenceForKey and setPreferenceForKey.

3D CanvasEvent Streaming to Web Browsers

Comments

avatar
excellent!, I didn't kwon about resizeBy and moveBy.

On previous builds I was not able to resize to a dimension larger than the spicified on the config.xml file. Has this changed? If not, are there plans to change it?

By Guille, # 21. June 2006, 15:54:18

avatar
Guille: Yes, resizing to larger than the initial size works. Which build does it fail in?

By virtuelvis, # 21. June 2006, 17:17:09

avatar
Are you going to put in options to forbid these functions? I think i would be quite easy to write "Mal-Widgets" that are only good for pranks or messing up the computer.

By XenoFur, # 22. June 2006, 11:24:42

avatar
@virtuelvis: I don't remember the build, but it was the same weekly when the "moveTo" method was announced. At that time I did a simple test, I will try to reproduce it again this weekend on the final version.

By Guille, # 22. June 2006, 14:14:58

avatar
There needs to be a suitable event handler for when a widget is dragged, there is widget.ondragstart and widget.ondragstop but no way of detecting the events while a widget is being dragged. window.onmousemove does not effectively detect this intermediate event.

By Benjamin Joffe, # 3. August 2006, 08:54:50

avatar
I have finally added this functionality to PlanetWerks! You can now dynamically resize the solar system from the Options menu. Whee!
http://widgets.opera.com/widget/4246

Now there is no need for the Large version, so I hope the two listings can be merged. From four widgets for me to three, oh well :smile:

By GreyWyvern, # 25. August 2006, 00:13:49

avatar
Yes, me too. My latest widget can be manually resized to any dimension: http://widgets.opera.com/widget/5069

By Benjamin Joffe, # 25. August 2006, 17:11:34

Write a comment

You must be logged in to write a comment. if you're not a registered member, please sign up.