AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1089516
Accepted
sdaau
sdaau
Asked: 2018-11-03 07:16:27 +0800 CST2018-11-03 07:16:27 +0800 CST 2018-11-03 07:16:27 +0800 CST

Gnome3 AppIndicator Vala 应用程序无法显示子菜单(它会立即自动关闭)?

  • 772

考虑下面粘贴的源代码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 上:

出.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 ();
  }
}
gnome vala 18.04
  • 1 1 个回答
  • 270 Views

1 个回答

  • Voted
  1. Best Answer
    sdaau
    2018-12-17T07:43:45+08:002018-12-17T07:43:45+08:00

    编辑:另见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 ();
      }
    }
    
    • 0

相关问题

  • 如何安装 KDE?

  • 为什么我的时钟、指示器小程序和通知区域有时会在我重新启动时移动?我怎样才能防止这种情况?

  • 停止菜单图标闪烁

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 如果顶部面板中缺少会话小程序,如何注销?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve