Snippets code from my daily experience

August 24, 2008

Migrating Komodo 4.x to Komodo 5.x

Filed under: extension,komodo,openkomodo — dafi @ 10:11 am

ActiveState has released Komodo 5.0.0 alpha 1 with many exciting enhancements.

The new features, for example the multi-window, have impact on existing code that requires modifications to continue to work.

The new model based on DOM events instead of global notifications requires only few lines of new code.

All notifications changed in events are shown below, remember they are no more usable as notifications!

  • codeintel_activated_window
  • codeintel_deactivated_window
  • current_view_changed
  • current_view_check_status
  • current_view_encoding_changed
  • current_view_language_changed
  • current_view_linecol_changed
  • view_closed
  • view_list_closed
  • view_opened

How to write new code

New code must be written using the window.addEventListener method, well known by Firefox (or Thunderbird) extensions developers.

Event handlers

The addEventListener method requires as parameter a callback function receiving the event object, the event’s originalTarget attribute contains (when applicable) the koIViews associated with event.

Removing listeners

When a window is closed every resource associated to it must be released so you must call the removeEventListener for every added listener.

The best place to remove listeners is inside the function associated to unload event.

Accessing to ‘this’ javascript object

When addEventListener is called from a prototyped object or simply from a namedspace method we must be sure the callback works with correct javascript ‘this’ object.

The Komodo code uses a well known approach; it declares a nested function and passes it to addEventListener

var moreKomodo = {
addListeners : function() {
var self = this;

this.handle_current_view_changed_setup = function(event) {
self.onCurrentViewChanged(event);
};

window.addEventListener(‘current_view_changed’,
this.handle_current_view_changed_setup, false);
},

_updateLockEdit : function(view) {

},
};

So the onCurrentViewChanged method can continue to work with the moreKomodo (this) instance

onCurrentViewChanged : function(event) {
var currView = event.originalTarget;

this._updateLockEdit(currView);
},

How to dispatch events

Should be necessary to dispatch events, for example MoreKomodo after renaming/moving a file requests to refresh title bar sending a current_view_changed.

Dispatching DOM events is easy but under Komodo developers can use the helper class domutils that contains the fireEvent utility method.

The xul file must contain the xtk inclusion

<script src=“chrome://xtk/content/xtk.js” type=“application/x-javascript;version=1.7″/>

Then the js file must contain the domutil inclusion

xtk.include(“domutils”);

Then you can call the fireEvent

if (typeof(xtk.domutils.fireEvent) != “undefined”) {
xtk.domutils.fireEvent(view, ‘current_view_changed’);
}

If you want to write code compatible with 4.4.x version you need to test the fireEvent existence as shown above

Benefits from Gecko 1.9

Komodo 5 uses the Firefox 3 Gecko version and this is immediately visibile, a faster startup but most important many bugs resolved.

The tooltip problem on MoreKomodo finally disappeared :)

I don’t know if related to Gecko 1.9 but finally the mouse scroll wheel works on file tab.

Conclusion

I like very much the new Komodo, developers should be hurted from these refactoring but they are simple and easy to do also on complex extensions.

I’ve made MoreKomodo, TabSwitcher and Klint compatible in about two hours ;)

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.