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