Vala hide Gtk.InfoBar after a few seconds

Viewed 267

In my Vala program I'm showing a Gtk.InfoBar when the user clicked a button. Now I want to autohide the Gtk.InfoBar after a few seconds and put the focus back to the default Gtk.Entry.

After some research I think it is best to do this with GLib.Timeout, but the Valadoc.org-Documentation on this is not very helpful.

Also I was not able to find some Examples on the Internet.

Can someone tell me how to do this?

This is my source:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.TreeView treeview1 = new Gtk.TreeView ();
        [GtkChild]
        Gtk.Button refreshbutton;
        [GtkChild]
        Gtk.MenuButton menubutton;
        [GtkChild]
        Gtk.Button menubuttonrefresh;
        [GtkChild]
        Gtk.Button menubuttonsave;
        [GtkChild]
        Gtk.Button menubuttonquit;
        [GtkChild]
        Gtk.InfoBar infobar1;
        [GtkChild]
        Gtk.Label infobar1label;
        [GtkChild]
        Gtk.Entry user_entry;
        [GtkChild]
        Gtk.Entry job_entry;
        [GtkChild]
        Gtk.Button addbutton;

        Gtk.TreeIter iter;
        Gtk.ListStore liststore1 = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (string));

        private void setup_treeview (Gtk.TreeView treeview1) {
            treeview1.set_model (liststore1);

            treeview1.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText (), "text", 0, null);
            treeview1.insert_column_with_attributes (-1, "Job", new Gtk.CellRendererText (), "text", 1, null);
            treeview1.insert_column_with_attributes (-1, "Time", new Gtk.CellRendererText (), "text", 2, null);

            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job1", 2, "2018-01-01 18:23", -1);
        }

        void refresh () {
            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job1", 2, "2018-01-01 18:23", -1);
            infobar1.set_revealed (true);
            infobar1label.set_label ("Refreshed!");
        }

        void save () {
            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job2", 2, "2018-01-01 24:00", -1);
            user_entry.set_text ("");
            job_entry.set_text ("");
            user_entry.grab_default ();
            infobar1.set_revealed (true);
            infobar1label.set_label ("Saved!");
        }

        void hideinfobar () {
            infobar1.set_revealed (false);
            infobar1label.set_label ("Close");
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            this.maximize ();

            this.setup_treeview (treeview1);

            // Don't show infobar1 on start
            infobar1.set_revealed (false);

            // Close infobar1 when Esc is hit.
            infobar1.close.connect (this.hideinfobar);

            // Close infobar1 when the close button is clicked.
            infobar1.response.connect (this.hideinfobar);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);
            menubuttonsave.clicked.connect (this.save);
            menubuttonquit.clicked.connect (app.quit);
            addbutton.clicked.connect (this.save);

            job_entry.set_activates_default (true);
            job_entry.activate.connect (this.save);
            user_entry.activate.connect (job_entry.grab_focus_without_selecting);


            this.show_all ();
        }
    }
}

You can find the full source on github.com

2 Answers

With the help of Jens Mühlenhoff this is a working code:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button button1;
        [GtkChild]
        Gtk.InfoBar infobar1;
        [GtkChild]
        Gtk.Label infobar1label;

        public void hideinfobar () {
            infobar1.set_revealed (false);
            infobar1label.set_label ("");
        }

        public void showinfobar (string message) {
            infobar1label.set_label (message);
            infobar1.set_revealed (true);

            Timeout.add_seconds (5, () => {
                this.hideinfobar ();
                return false;
            });
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            // Don't show infobar1 on start
            this.hideinfobar ();

            // Close infobar1 when Esc is hit.
            infobar1.close.connect (this.hideinfobar);

            // Close infobar1 when the close button is clicked.
            infobar1.response.connect (this.hideinfobar);

            // Connect Button and show infobar1
            button1.clicked.connect (() => {
                this.showinfobar ("Message");
            });    

            this.show_all ();
        }
    }
}

You can use GLib.Timeout like this:

Timeout.add_seconds (5, () => {
    stdout.printf ("Hello from timeout");
    return false;
});

This will print a message in approximately 5 seconds. It only works when a MainLoop is already running (which is the case for every Gtk application).

Related