Vala скрывает Gtk.InfoBar через несколько секунд - PullRequest
0 голосов
/ 30 августа 2018

В моей программе Vala я показываю Gtk.InfoBar, когда пользователь нажимает кнопку. Теперь я хочу автоматически скрыть Gtk.InfoBar через несколько секунд и вернуть фокус к стандартному Gtk.Entry.

После некоторых исследований я думаю, что лучше всего сделать это с GLib.Timeout, но Valadoc.org -Документация по этому вопросу не очень полезна.

Также я не смог найти несколько примеров в интернете.

Может кто-нибудь сказать мне, как это сделать?

Это мой источник:

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 ();
        }
    }
}

Полный исходный код вы можете найти на github.com

Ответы [ 2 ]

0 голосов
/ 30 августа 2018

С помощью Jens Mühlenhoff это рабочий код:

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 ();
        }
    }
}
0 голосов
/ 30 августа 2018

Вы можете использовать GLib.Timeout так:

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

Это напечатает сообщение приблизительно через 5 секунд. Он работает только тогда, когда MainLoop уже запущен (что имеет место для каждого приложения Gtk).

...