Welcome, guest | Sign In | My Account | Store | Cart

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.

JavaScript, 51 lines
 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:

  1. Create a snippet in your toolbox

  2. Copy and paste the code into the snippet

  3. Remember to check "maintain indentation context"

  4. Save the snippet

  5. 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.

  6. Drag and drop the toggle template snippet.

  7. Tab through, and fill in the blanks.

  8. Enjoy. Serves 12.

Created by Eric Promislow on Thu, 7 Oct 2010 (MIT)
JavaScript recipes (69)
Eric Promislow's recipes (11)

Required Modules

  • (none specified)

Other Information and Tasks