考虑粘贴在下面的源代码test.vala
。这是一个简单的应用程序,它应该在顶部栏/面板上显示一个图标,单击图标时,它应该显示一个包含一个项目的菜单(打开),单击打开时,它应该显示一个包含多个项目的子菜单。我编译了它:
$ cat /etc/issue
Ubuntu 18.04.1 LTS \n \l
$ uname -a
Linux MyPC 4.15.0-38-generic #41-Ubuntu SMP Wed Oct 10 10:59:38 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
$ printf 'Desktop: %s\nSession: %s\n' "$XDG_CURRENT_DESKTOP" "$GDMSESSION"
Desktop: ubuntu:GNOME
Session: ubuntu
$ gnome-shell --version
GNOME Shell 3.28.3
...我编译如下:
valac -X -D'GETTEXT_PACKAGE="my-indicator"' -D NEWMETHOD --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
...为此你还需要安装libappindicator-dev 包。
然后我运行该应用程序:
$ ./test
main() ...
Main(): ok
Creating MainWindow
^C
...我得到的结果显示在这个动画 gif 上:
请注意,显示了 appindicator 图标(如预期的那样),单击它后,将显示带有“打开”项的一级菜单(如预期的那样) - 但是当我单击“打开”时,我并没有真正得到我期望的子菜单;相反,它看起来像是尝试打开子菜单,然后它立即关闭?
我需要做什么才能让这个应用程序正确打开子菜单?
这是test.vala
:
// build with:
// valac -X -D'GETTEXT_PACKAGE="my-indicator"' --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
// "It's not possible to define a preprocessor symbol inside the Vala code (like with C). The only way to define a symbol is to feed it through the valac option -D."
// valac -X -D'GETTEXT_PACKAGE="my-indicator"' -D NEWMETHOD --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
// see also: https://valadoc.org/gtk+-3.0/Gtk.MenuItem.html
using GLib;
using Gtk;
using AppIndicator;
public Main App;
public const string AppName = "Test";
extern void exit(int exit_code);
public class MyIndicator: GLib.Object{
protected Indicator indicator;
protected string icon;
protected string name;
public MyIndicator(){
App.my_indicator = this;
this.name = "My Indicator";
this.icon = "account-logged-in"; // looks like a checkmark
this.indicator = new Indicator("my_indicator", icon, IndicatorCategory.APPLICATION_STATUS);
indicator.set_status(IndicatorStatus.ACTIVE);
var menu = new Gtk.Menu();
// open -------------------------------------
#if NEWMETHOD
var item = new Gtk.MenuItem.with_label(_("Open"));
#else
var item = new Gtk.ImageMenuItem.with_label(_("Open"));
#endif
menu.append(item);
var item_open = item;
item.set_reserve_indicator(false);
item.activate.connect(() => {
var submenu = new Gtk.Menu();
submenu.reserve_toggle_size = true;
//var dummy_window = new Gtk.Window();
//Gtk.Image icon = null;
int i;
for (i = 0; i < 10; i++) {
#if NEWMETHOD
var subitem = new Gtk.MenuItem.with_label ( "Exit %d".printf(i) );
#else
var subitem = new Gtk.ImageMenuItem.with_label ( "Exit %d".printf(i) );
#endif
subitem.set_reserve_indicator(true);
submenu.append(subitem);
subitem.activate.connect(() => {
App.exit_app();
exit(0);
});
//subitem.activate();
}
submenu.show_all();
item_open.set_submenu(submenu);
});
item.activate(); // so it shows submenu triangle
indicator.set_menu(menu);
menu.show_all();
}
}
public class Main : GLib.Object{
public MyIndicator my_indicator;
public static int main (string[] args) {
stdout.printf("main() ... \n");
stdout.flush();
Gtk.init(ref args);
App = new Main(args);
bool success = App.start_application(args);
App.exit_app();
return (success) ? 0 : 1;
}
public Main(string[] args){
stdout.printf("Main(): ok\n");
stdout.flush();
}
public bool start_application(string[] args){
stdout.printf("Creating MainWindow\n");
stdout.flush();
new MyIndicator(); // var ind = new MyIndicator();
//start event loop
Gtk.main();
return true;
}
public void exit_app (){
stdout.printf("exit_app()\n");
stdout.flush();
Gtk.main_quit ();
}
}
答案1
编辑:另请参阅https://stackoverflow.com/questions/53805975/re-creating-gtk-menu-in-event-handler-with-vala
好的,明白了——重写了上面的代码,以便子菜单创建是一个单独的函数,因此更容易识别问题出在这里:
item_open.activate.connect(() => {
//~ item_open.set_submenu(createSubmenu()); // NO; having the set_submenu run in .connect, causes immediate shutdown of the created submenu!!
stdout.printf("item.activate.connect running\n");
});
item_open.set_submenu(createSubmenu()); // is OK here
item_open.activate(); // so it shows submenu triangle
正如注释所说,运行set_submenu
会.connect,
导致立即关闭创建的子菜单。我猜是因为在.connect
处理程序内部,我们有一些“匿名上下文”或其他内容,这会导致在处理程序退出时销毁在那里创建的所有局部变量,无论其中是否有任何变量在其他地方引用。所以解决方案是在createSubmenu
连接处理程序的外部运行。
请注意,即使使用此工作代码,在编译它时我也会得到:
$ valac -X -D'GETTEXT_PACKAGE="my-indicator"' -D NEWMETHOD --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
/tmp/test.vala.c: In function ‘my_indicator_createSubmenu’:
/tmp/test.vala.c:182:52: warning: passing argument 2 of ‘gtk_menu_shell_append’ from incompatible pointer type [-Wincompatible-pointer-types]
gtk_menu_shell_append ((GtkMenuShell*) _tmp11_, _tmp12_);
^~~~~~~
In file included from /usr/include/gtk-3.0/gtk/gtkmenu.h:33:0,
from /usr/include/gtk-3.0/gtk/gtklabel.h:34,
from /usr/include/gtk-3.0/gtk/gtkaccellabel.h:35,
from /usr/include/gtk-3.0/gtk/gtk.h:33,
from /usr/include/libappindicator3-0.1/libappindicator/app-indicator.h:33,
from /tmp/test.vala.c:15:
/usr/include/gtk-3.0/gtk/gtkmenushell.h:91:10: note: expected ‘GtkWidget * {aka struct _GtkWidget *}’ but argument is of type ‘GtkMenuItem * {aka struct _GtkMenuItem *}’
void gtk_menu_shell_append (GtkMenuShell *menu_shell,
^~~~~~~~~~~~~~~~~~~~~
/tmp/test.vala.c: In function ‘my_indicator_construct’:
/tmp/test.vala.c:260:47: warning: passing argument 2 of ‘gtk_menu_shell_append’ from incompatible pointer type [-Wincompatible-pointer-types]
gtk_menu_shell_append ((GtkMenuShell*) menu, item_open);
^~~~~~~~~
In file included from /usr/include/gtk-3.0/gtk/gtkmenu.h:33:0,
from /usr/include/gtk-3.0/gtk/gtklabel.h:34,
from /usr/include/gtk-3.0/gtk/gtkaccellabel.h:35,
from /usr/include/gtk-3.0/gtk/gtk.h:33,
from /usr/include/libappindicator3-0.1/libappindicator/app-indicator.h:33,
from /tmp/test.vala.c:15:
/usr/include/gtk-3.0/gtk/gtkmenushell.h:91:10: note: expected ‘GtkWidget * {aka struct _GtkWidget *}’ but argument is of type ‘GtkMenuItem * {aka struct _GtkMenuItem *}’
void gtk_menu_shell_append (GtkMenuShell *menu_shell,
^~~~~~~~~~~~~~~~~~~~~
/tmp/test.vala.c:265:40: warning: passing argument 2 of ‘gtk_menu_item_set_submenu’ from incompatible pointer type [-Wincompatible-pointer-types]
gtk_menu_item_set_submenu (item_open, _tmp10_);
^~~~~~~
In file included from /usr/include/gtk-3.0/gtk/gtkcheckmenuitem.h:33:0,
from /usr/include/gtk-3.0/gtk/gtk.h:72,
from /usr/include/libappindicator3-0.1/libappindicator/app-indicator.h:33,
from /tmp/test.vala.c:15:
/usr/include/gtk-3.0/gtk/gtkmenuitem.h:120:12: note: expected ‘GtkWidget * {aka struct _GtkWidget *}’ but argument is of type ‘GtkMenu * {aka struct _GtkMenu *}’
void gtk_menu_item_set_submenu (GtkMenuItem *menu_item,
^~~~~~~~~~~~~~~~~~~~~~~~~
... 但我想这不是什么大问题。可能需要更改 libappindicator3。
无论如何,这里是完整更新的(并且有效)代码test.vala
:
// build with:
// valac -X -D'GETTEXT_PACKAGE="my-indicator"' --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
// "It's not possible to define a preprocessor symbol inside the Vala code (like with C). The only way to define a symbol is to feed it through the valac option -D."
// valac -X -D'GETTEXT_PACKAGE="my-indicator"' -D NEWMETHOD --pkg=gtk+-3.0 --pkg appindicator3-0.1 test.vala
// see also: https://valadoc.org/gtk+-3.0/Gtk.MenuItem.html
using GLib;
using Gtk;
using AppIndicator;
public Main App;
public const string AppName = "Test";
extern void exit(int exit_code);
public class MyIndicator: GLib.Object{
protected Indicator indicator;
protected string icon;
protected string name;
public Gtk.Menu createSubmenu(){
var submenu = new Gtk.Menu();
submenu.reserve_toggle_size = true;
//var dummy_window = new Gtk.Window();
//Gtk.Image icon = null;
int i;
for (i = 0; i < 10; i++) {
#if NEWMETHOD
var subitem = new Gtk.MenuItem.with_label ( "Exit %d".printf(i) );
#else
var subitem = new Gtk.ImageMenuItem.with_label ( "Exit %d".printf(i) );
#endif
subitem.set_reserve_indicator(true);
submenu.append(subitem);
subitem.activate.connect(() => {
App.exit_app();
exit(0);
});
//subitem.activate(); // no way, causes immediate exit!
}
submenu.show_all();
return submenu;
}
public MyIndicator(){
App.my_indicator = this;
this.name = "My Indicator";
this.icon = "account-logged-in"; // looks like a checkmark
this.indicator = new Indicator("my_indicator", icon, IndicatorCategory.APPLICATION_STATUS);
indicator.set_status(IndicatorStatus.ACTIVE);
/*
// from https://valadoc.org/gtk+-3.0/Gtk.Menu.html
// MenuBar:
//Gtk.MenuBar bar = new Gtk.MenuBar (); //error: Argument 1: Cannot convert from `unowned Gtk.MenuBar?' to `unowned Gtk.Menu?'
Gtk.Menu bar = new Gtk.Menu ();
//indicator.add (bar); // error: The name `add' does not exist in the context of `AppIndicator.Indicator'
indicator.set_menu(bar);
// File:
Gtk.MenuItem item_file = new Gtk.MenuItem.with_label ("File");
bar.add (item_file);
Gtk.Menu filemenu = new Gtk.Menu ();
item_file.set_submenu (filemenu);
Gtk.MenuItem item_open = new Gtk.MenuItem.with_label ("Open");
filemenu.add (item_open);
// Submenu:
Gtk.Menu submenu = new Gtk.Menu ();
item_open.set_submenu (submenu);
Gtk.MenuItem item_foo = new Gtk.MenuItem.with_label ("foo");
submenu.add (item_foo);
Gtk.MenuItem item_bar = new Gtk.MenuItem.with_label ("bar");
submenu.add (item_bar);
bar.show_all();
*/
var menu = new Gtk.Menu();
// open -------------------------------------
#if NEWMETHOD
var item_open = new Gtk.MenuItem.with_label(_("Open"));
#else
var item_open = new Gtk.ImageMenuItem.with_label(_("Open"));
#endif
menu.append(item_open);
item_open.set_reserve_indicator(false);
item_open.activate.connect(() => {
//~ item_open.set_submenu(createSubmenu()); // NO; having the set_submenu run in .connect, causes immediate shutdown of the created submenu!!
stdout.printf("item.activate.connect running\n");
});
item_open.set_submenu(createSubmenu()); // is OK here
item_open.activate(); // so it shows submenu triangle
indicator.set_menu(menu);
menu.show_all();
}
}
public class Main : GLib.Object{
public MyIndicator my_indicator;
public static int main (string[] args) {
stdout.printf("main() ... \n");
stdout.flush();
Gtk.init(ref args);
App = new Main(args);
bool success = App.start_application(args);
App.exit_app();
return (success) ? 0 : 1;
}
public Main(string[] args){
stdout.printf("Main(): ok\n");
stdout.flush();
}
public bool start_application(string[] args){
stdout.printf("Creating MainWindow\n");
stdout.flush();
new MyIndicator(); // var ind = new MyIndicator();
//start event loop
Gtk.main();
return true;
}
public void exit_app (){
stdout.printf("exit_app()\n");
stdout.flush();
Gtk.main_quit ();
}
}