如何创建Unity指示器?

我对Unity指示器很感兴趣,想知道是否有关于如何编程它们的教程。作为最后的选择,我可以使用现有指示器的源代码,但我更希望有一种更友好的方法,因为我的编程技能相当有限。

相关链接:http://askubuntu.com/q/46746/17789 http://askubuntu.com/q/46761/17789 - con-f-use
也可以看看这个答案。它描述了如何创建一个系统指示器,比应用程序指示器有更多的可能性。我进行了一些实验,并查看了Unity的默认系统指示器,如声音和蓝牙指示器;然后我自己做了一个例子,ScreenToolsIndicator,它还使用了按钮和一个滑块。我选择了C语言,因为sneetsher的示例是用C写的,但GLib也有一个C++封装库(glibmm)。 - okaresz
3个回答


2嘿,这些链接都已经过时了,点进去会显示“页面未找到”。有没有新的资源可以找到呢?官方网站http://developer.ubuntu.com/apps/qml/cookbook/ui-components-and-shell-integration/上的链接指向这个问题:“是否有关于编程Unity指示器的教程?” - Daniel W.
到处都是损坏的链接,请查看ApplicationIndicators https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators ,它的API参考以HTML形式在libappindicator-doc软件包中提供。 - user.dz
1有一个关于缺失文档的错误已经被提交:https://bugs.launchpad.net/ubuntudeveloperportal/+bug/1317065 - Jorge Castro

这是一个在 C 语言中的应用程序指示器示例。这是一个“仅指示器”版本(没有窗口),与 Ubuntu Wiki 提供的示例相同。
#include <libappindicator/app-indicator.h>

static void activate_action (GtkAction *action);

static GtkActionEntry entries[] = {
    {"New",  "document-new",     "_New",  "<control>N",
        "Create a new file",    G_CALLBACK(activate_action)},
    {"Open", "document-open",    "_Open", "<control>O",
        "Open a file",          G_CALLBACK(activate_action)},
    {"Save", "document-save",    "_Save", "<control>S",
        "Save file",            G_CALLBACK(activate_action)},
    {"Quit", "application-exit", "_Quit", "<control>Q",
        "Exit the application", G_CALLBACK(gtk_main_quit)},
};
static guint n_entries = G_N_ELEMENTS(entries);

static const gchar *ui_info =
"<ui>"
"  <popup name='IndicatorPopup'>"
"    <menuitem action='New' />"
"    <menuitem action='Open' />"
"    <menuitem action='Save' />"
"    <menuitem action='Quit' />"
"  </popup>"
"</ui>";

static void activate_action(GtkAction *action)
{
    const gchar *name = gtk_action_get_name (action);
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new(NULL,
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_CLOSE,
                                    "You activated action: \"%s\"",
                                    name);

    g_signal_connect(dialog, "response", 
                     G_CALLBACK(gtk_widget_destroy), NULL);

    gtk_widget_show(dialog);
}

int main(int argc, char **argv)
{
    GtkWidget*      indicator_menu;
    GtkActionGroup* action_group;
    GtkUIManager*   uim;
    AppIndicator* indicator;
    GError* error = NULL;

    gtk_init(&argc, &argv);

    /* Menus */
    action_group = gtk_action_group_new("AppActions");
    gtk_action_group_add_actions(action_group, entries, n_entries,
                                 NULL);

    uim = gtk_ui_manager_new();
    gtk_ui_manager_insert_action_group(uim, action_group, 0);

    if (!gtk_ui_manager_add_ui_from_string(uim, ui_info, -1, &error)) {
        g_message("Failed to build menus: %s\n", error->message);
        g_error_free(error);
        error = NULL;
    }

    /* Indicator */
    indicator = app_indicator_new("example-simple-client",
                                  "go-jump",
                                  APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    indicator_menu = gtk_ui_manager_get_widget(uim, "/ui/IndicatorPopup");

    app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_attention_icon(indicator, "indicator-messages-new");

    app_indicator_set_menu(indicator, GTK_MENU(indicator_menu));

    gtk_main();

    return 0;
}

链接无法访问(404错误)。 - Déjà vu
@ringø 修改了sergej的回答,并添加了有效链接。实际上,这个链接与Jorge的回答中的链接是一样的。 - Sergiy Kolodyazhnyy

我在这里做了一个简短的教程,教你如何用Python创建一个秒表应用程序指示器: http://www.steshadoku.com/blog/2017/elapses-creating-a-unity-stopwatch-indicator-w-python/
import gobject
import gtk
import appindicator
import os, sys
import time
from datetime import timedelta

if __name__ == "__main__":

    saveseconds = 0 #global variable to save how many seconds the clock has run
    dir_path = os.path.dirname(os.path.realpath(__file__))
    source_id = ""

    def on_timer(args=None):
      savetime = int(time.time() - timestart) + saveseconds
      ind.set_label(str(timedelta(seconds=savetime)))
      return True

    def finish(args=None):
        sys.exit()
        return True

    def stoptime(args=None):
        #print(source_id)
        global saveseconds
        saveseconds += int(time.time() - timestart)
        gtk.timeout_remove(source_id)
        return True

    def starttime(args=None):
        global timestart
        timestart = time.time()
        global source_id
        source_id = gtk.timeout_add(1000, on_timer)
            #sets timer to run every 1s
        return True

    def cleartime(args=None):
        global saveseconds
        saveseconds = 0
        ind.set_label(str(timedelta(seconds=0)))
        gtk.timeout_remove(source_id)
        return True

    #format below is category name, icon
    ind = appindicator.Indicator ("simple-clock-client", "hourglass", appindicator.CATEGORY_APPLICATION_STATUS, dir_path)
    ind.set_status (appindicator.STATUS_ACTIVE)
    ind.set_label("Elapses"); #name of program and initial display

    ##Setup Menu Items
    menu = gtk.Menu()

    stop = gtk.MenuItem("Stop")
    stop.connect("activate", stoptime)
    stop.show()
    menu.append(stop)

    start = gtk.MenuItem("Start")
    start.connect("activate", starttime)
    start.show()
    menu.append(start)

    clear = gtk.MenuItem("Clear")
    clear.connect("activate", cleartime)
    clear.show()
    menu.append(clear)

    exit = gtk.MenuItem("Exit")
    exit.connect("activate", finish)
    exit.show()
    menu.append(exit)

    ind.set_menu(menu) #set the menu with added items
    gtk.main()

1我没有深入研究实际的代码,但有一点是肯定的:你的缩进错误了。无论是在这里还是在链接的教程中都存在这个问题。 - Jacob Vlijm
不,只有一个缩进空格……只有一个,这让阅读代码非常痛苦,并且不符合Python的PEP8标准。 - Sergiy Kolodyazhnyy