GLib.Menu行动不起作用

时间:2016-12-06 21:41:51

标签: gtk3 glib vala

我正在努力学习vala。使用我的示例应用程序,我遇到了GLib.Menu操作的问题。

我宣布了一个新的动作quit_action,它应该退出应用程序。编译器运行时没有任何警告或错误,但是当我运行应用程序时,我可以打开菜单,但“退出”项目是灰色的。

/* main.vala */

using Gtk;

class mainWindow : Gtk.ApplicationWindow {
        public mainWindow (Application app) {

            // Window Properties
            this.set_default_size (800, 600);
            this.window_position = Gtk.WindowPosition.CENTER;
        this.destroy.connect (Gtk.main_quit);

            // HeaderBar
            var headerBar = new Gtk.HeaderBar ();
            headerBar.set_title ("Test Title");
            headerBar.set_subtitle ("Test Subtitle");
            headerBar.set_show_close_button (true);

            // Menu
            var menuButton = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
            headerBar.pack_end (menuButton);

            var menu = new GLib.Menu ();
        menu.append ("Quit", "app.quit");

            var popover = new Gtk.Popover (menuButton);
            popover.bind_model (menu, "app");

            menuButton.clicked.connect (() =>  {
                popover.popup ();
                popover.show_all ();
            });

            this.set_titlebar (headerBar);

            this.show_all ();
        }
}

public class Application : Gtk.Application {
    public Application () {
        Object (application_id: "org.example.application", flags: 0);
    }

    protected override void activate () {
        // Create a new window for this application.
        mainWindow win = new mainWindow (this);
        win.show_all ();
        Gtk.main ();
    }

    protected override void startup () {
        base.startup ();

        var quit_action = new GLib.SimpleAction ("quit", null);
        quit_action.activate.connect (this.quit);
        this.add_action (quit_action);
    }
}

int main (string[] args) {
    Gtk.init (ref args);
    return new Application ().run (args);
}

1 个答案:

答案 0 :(得分:1)

这里有两件事是错的:

  1. 您必须将Application分配给ApplicationWindow(这通常在构造函数中完成,但由于它是GLib样式构造函数,因此无法调用继承的构造函数):

    this.application = app;
    
  2. 动作名称必须匹配,如果你想使用app.quit,必须在两个地方都这样调用。

  3. 完全正常的代码:

    /* main.vala */
    
    using Gtk;
    
    class mainWindow : Gtk.ApplicationWindow {
      public mainWindow (Application app) {
        this.application = app;
        // Window Properties
        this.set_default_size (800, 600);
        this.window_position = Gtk.WindowPosition.CENTER;
        this.destroy.connect (Gtk.main_quit);
    
        // HeaderBar
        var headerBar = new Gtk.HeaderBar ();
        headerBar.set_title ("Test Title");
        headerBar.set_subtitle ("Test Subtitle");
        headerBar.set_show_close_button (true);
    
        // Menu
        var menuButton = new Gtk.Button.from_icon_name ("open-menu-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
        headerBar.pack_end (menuButton);
    
        var menu = new GLib.Menu ();
        menu.append ("Quit", "app.quit");
    
        var popover = new Gtk.Popover (menuButton);
        popover.bind_model (menu, "app");
    
        menuButton.clicked.connect (() =>  {
          //popover.popdown ();
          popover.show_all ();
        });
    
        this.set_titlebar (headerBar);
    
        this.show_all ();
      }
    }
    
    public class Application : Gtk.Application {
      public Application () {
        Object (application_id: "org.example.application", flags: 0);
      }
    
      protected override void activate () {
        // Create a new window for this application.
        mainWindow win = new mainWindow (this);
        win.show_all ();
        Gtk.main ();
      }
    
      protected override void startup () {
        base.startup ();
    
        var quit_action = new GLib.SimpleAction ("app.quit", null);
        quit_action.activate.connect (this.quit);
        this.add_action (quit_action);
      }
    }
    
    int main (string[] args) {
        Gtk.init (ref args);
        return new Application ().run (args);
    }