2016-12-06 130 views
2

我正在嘗試學習vala。使用我的示例應用程序,我遇到了GLib.Menu操作的問題。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

有兩件事情錯在這裏:

  1. 你必須分配給ApplicationWindow應用程序(這是在構造函數正常完成,但因爲它是能說會道的風格構造你不能調用繼承的構造):

    this.application = app; 
    
  2. 的動作名稱必須匹配,如果你想使用app.quit它被稱爲在這兩個地方的方式。

全工作代碼:

/* 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); 
}