If you've followed my postings here lately (http://code.activestate.com/recipes/users/4166930/), you'll see that I've been playing with what I call "toggle macros", macros that toggle a state on and off, with different behaviors for each state. Unfortunately I found that it was easy to get Komodo to crash on shutdown. Meanwhile I wanted to build a framework to make it easier to build these.
This recipe accomplishes both. It looks like putting a listener on a view object can trigger this crash, but so can storing a method on a view object, even with a unique name. So while I wanted to use the "view.view_closing" event, I can't, and I notice no other core Komodo code is. The template instead uses a global hash, ko.extensions.togglers, to store the code objects. And now I'm not getting a crash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | var view = ko.views.manager.currentView;
if (!('extensions' in ko)) {
ko.extensions = {};
}
if (!('togglers' in ko.extensions)) {
ko.extensions.togglers = {};
}
var methodName = "*[[%tabstop1:togglerName]]*" + view.uid;
var methodObject;
if (!(methodName in ko.extensions.togglers)) {
var scimoz = view.scimoz;
methodObject = ko.extensions.togglers[methodName] = function(event) {
[[%tabstop0:code]]
else if (event.keyCode == event.DOM_VK_ESCAPE) {
methodObject.toggleOff();
}
};
methodObject.active = false;
var macro = ko.macros.current;
methodObject.macro = macro;
methodObject.orig_iconurl = macro.iconurl;
methodObject.toggleOn = function() {
view.addEventListener('keypress', methodObject, true);
dump("[[%tabstop1]] on!\n");
methodObject.active = true;
methodObject.columnPos = scimoz.getColumn(scimoz.currentPos);
methodObject.macro.iconurl = 'chrome://famfamfamsilk/skin/icons/[[%tabstop3:iconName]].png';
methodObject.macro.save();
window.addEventListener('current_view_changed', methodObject.toggleOff, false);
}
methodObject.toggleOff = function() {
dump("[[%tabstop1]] off!\n");
view.removeEventListener('keypress', methodObject, true);
window.removeEventListener('current_view_changed', methodObject.toggleOff, false);
methodObject.active = false;
methodObject.macro.iconurl = "";
methodObject.macro.save();
}
view.scintilla.focus();
} else {
methodObject = ko.extensions.togglers[methodName];
}
if (!methodObject.active) {
methodObject.toggleOn();
} else {
methodObject.toggleOff();
}
[[%tabstop4://]]dump("Running macro " + methodObject.macro.id + "\n");
|
To use this template:
Create a snippet in your toolbox
Copy and paste the code into the snippet
Remember to check "maintain indentation context"
Save the snippet
Create a new macro. Komodo won't let you drag and drop a snippet into the macros properties box, so you'll need to edit the macro in the editor.
Drag and drop the toggle template snippet.
Tab through, and fill in the blanks.
Enjoy. Serves 12.