如何创建Unity指标?


25

我对Unity指标感兴趣,想知道是否有关于如何对其进行编程的教程。我可以将现有资源的来源作为最后的手段,但是我更喜欢一种更友好的方法,因为我的编程技能非常有限。



也请查看答案。它描述了如何创建系统指示器,它比应用程序指示器具有更多的可能性。我做了一些实验,还查看了Unity的默认系统指示器,例如声音和蓝牙指示器。并提出了我自己的示例ScreenToolsIndicator,该示例也使用了按钮和滑块。我选择C是因为sneetsher的示例在C中,但是GLib也有一个C ++包装器(glibmm)。
okaresz

Answers:


21

此处提供带有示例和API文档的Application Indicator文档:

尚无关于Application Indicators的教程,但请继续关注App Developer网站的教程部分中的更多内容:


2
撞 所有这些链接已过时,并导致“找不到页面”消息。我是否可以找到任何新资源?正式的developer.ubuntu.com/apps/qml/cookbook/…网站以Is there any tutorial for programming Unity indicators?标题的形式链接到此问题。
Daniel W.

到处都有断开的链接,请参见ApplicationIndicators wiki.ubuntu.com/DesktopExperienceTeam/ApplicationIndicators,其API参考以HTML格式包含在libappindicator-doc软件包中。
user.dz 2014年

1
缺少的文档中存在一个错误:bugs.launchpad.net/ubuntudeveloperportal/+bug/1317065
Jorge Castro

4

这是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
环Ø17年

@ringø编辑sergej的答案,添加了工作链接。实际上,它与Jorge的答案中的链接相同。
Sergiy Kolodyazhnyy

1

我在此处做了一个简短的教程,用于在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 '17

不,缩进只有一个空间。。。只是其中之一,这使得阅读代码绝对痛苦并且不符合Python的PEP8标准
Sergiy Kolodyazhnyy
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.