Board index FlightGear Development Canvas

Canvas dialog window close event

Canvas is FlightGear's new fully scriptable 2D drawing system that will allow you to easily create new instruments, HUDs and even GUI dialogs and custom GUI widgets, without having to write C++ code and without having to rebuild FlightGear.

Canvas dialog window close event

Postby sanhozay » Fri Dec 16, 2016 11:20 am

I've been dabbling with Canvas dialogs and I've run into a problem with handling the window close. I want to be able to run a method in my class when the window is closed to clean up timers, etc.

This is an abbreviated version of what I am trying to do and it should run as-is in the Nasal console. :!: Fair warning: the dialog won't close.

Code: Select all
var MyDialog = {
    new: func {
        var m = {
            parents: [MyDialog]
        };
        return m;
    },
    show: func {

        var window = canvas.Window.new([300, 300], "MyDialog")
            .set("title", "My Dialog");

        var myCanvas = window.createCanvas()
            .set("background", canvas.style.getColor("bg_color"));

        var root = myCanvas.createGroup();

        window.del = func {
            me.cleanup(); # <--- ERROR ON THIS LINE: No such member: cleanup
            call(canvas.Window.del, [], me);
        }
    },
    cleanup: func {
        print("Cleanup");
    },
};

MyDialog.new().show();

When I hit the close button I get an error "No such member: cleanup" in the window.del function.

This is stretching my limited understanding of Nasal and closures but presumably this is because "me" is the window rather than my class? Is there a neat solution? Substituting code for the method call won't help, because I want to access members of my class using "me".

As an aside, this use of window.del with the call to the superclass method feels wrong to me. Shouldn't there be a window-close event akin to the button clicked and toggled events? In GTK you'd connect a callback function to the delete_event on the window, in Java Swing you'd add a window listener, etc.
sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 12:57 pm
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04

Re: Canvas dialog window close event

Postby sanhozay » Fri Dec 16, 2016 3:15 pm

This seems to work, i.e. capture the owning class on the window and call the cleanup method on that reference.

Code: Select all
var MyDialog = {
    new: func {
        var m = {
            parents: [MyDialog]
        };
        return m;
    },
    show: func {

        var window = canvas.Window.new([300, 300], "MyDialog")
            .set("title", "My Dialog");
           
        window.owner = me;

        var myCanvas = window.createCanvas()
            .set("background", canvas.style.getColor("bg_color"));

        var root = myCanvas.createGroup();

        window.del = func {
            me.owner.cleanup();
            call(canvas.Window.del, [], me);
        }
    },
    cleanup: func {
        print("Cleanup");
    },
};

MyDialog.new().show();

Any issues? Reference cycle?
sanhozay
 
Posts: 1207
Joined: Thu Dec 26, 2013 12:57 pm
Location: EGNM
Callsign: G-SHOZ
Version: Git
OS: Ubuntu 16.04


Return to Canvas

Who is online

Users browsing this forum: No registered users and 6 guests