0

I am trying to add a dialog to a small application with genie. It is an openfile dialog that should open upon clicking in a headerbar button.

Examples are lacking in Genie, so I am now trying to adapt something from vala. However, all examples that I found used a switch command that I am not being able to translate to Genie.

This is the vala code:

    public void on_open_image (Button self) {
    var filter = new FileFilter ();
    var dialog = new FileChooserDialog ("Open image",
                                        window,
                                        FileChooserAction.OPEN,
                                        Stock.OK,     ResponseType.ACCEPT,
                                        Stock.CANCEL, ResponseType.CANCEL);
    filter.add_pixbuf_formats ();
    dialog.add_filter (filter);

    switch (dialog.run ())
    {
        case ResponseType.ACCEPT:
            var filename = dialog.get_filename ();
            image.set_from_file (filename);
            break;
        default:
            break;
    }
    dialog.destroy ();
}

And this is what I worked out from the previous code:

def openfile (self:Button)
    var dialog = new FileChooserDialog ("Open file",
                                    window,
                                    FileChooserAction.OPEN,
                                    Stock.OK,     ResponseType.ACCEPT,
                                    Stock.CANCEL, ResponseType.CANCEL)

    switch (dialog.run ())

    case ResponseType.ACCEPT
        var filename
        filename = dialog.get_filename ()
        image.set_from_file (filename)
        break
    default
        break

    dialog.destroy ()

It obviously throws an error at the case statement. How to use switch in Genie?

Community
  • 1
  • 1
lf_araujo
  • 1,991
  • 2
  • 16
  • 39
  • Think of `switch` as shorthand for an `if-else if` chain, comparing the variable after the `switch` to each `case` value. `break` exits the current case body; if you don't have `break` at the end it will fall through to the next `case`. I don't know Genie so I don't know what the equivalent is. – andlabs Apr 06 '16 at 13:21

1 Answers1

1

Ah, I see now. The equivalent to switch in Vala is case...when. The switch in your upper example would be

case dialog.run()
    when ResponseType.ACCEPT
        var filename = dialog.get_filename()
        image.set_from_file(filename)
    default
        pass
andlabs
  • 11,290
  • 1
  • 31
  • 52
  • The empty statement would use the `pass` keyword, but it is not needed. Why have a default that does nothing? Just omit it. – AlThomas Apr 06 '16 at 18:36
  • @AlThomas To do a fully literal translation with nothing omitted, to demonstrate. Thanks. – andlabs Apr 06 '16 at 18:53