Tray support added to libgreattao(to console backend too)

    I added tray support to all backends of libgreattao(mWidgets). It’s good information, because is was added to console backend too. In mWidgets look&behavior it works in this way.

    1. There’s special window class called system tray
    2. This window class uses slots. so you can attach windows to tray window
    3. This window is not visible by default, but tray icon are special window, that’s don’t looks of parent visibility
    4. By clicking on tray icon you change visibility of tray window

    Below you have tray class code

    <root>
    <variable name="root-name" action="change" value="root-window" />
    <variable name="true"  action="change" value="TRUE" />
    <variable name="false" action="change" value="FALSE" />
    <variable name="root-visibility" action="change" value="FALSE" />
    <attr-connect name="widget-name" variable="root-name" />
    <variable name="is_first_if_processed" action="change" value="FALSE" />
    <hide name="root-name" />
    <template path="/icon">
    <tray-icon dir-for="icon,label">
    <handler>
    <if type="equal" variable1="root-visibility" variable2="true">
    <hide name="root-name" />
    <variable name="root-visibility" action="change" value="FALSE" />
    <variable name="is_first_if_processed" action="change" value="TRUE" />
    </if>
    <if type="equal" variable1="is_first_if_processed" variable2="false">
    <if type="equal" variable1="root-visibility" variable2="false">
    <variable name="root-visibility" action="change" value="TRUE" />
    <show name="root-name" />
    </if>
    </if>
    <variable name="is_first_if_processed" action="change" value="FALSE" />
    </handler>
    </tray-icon>
    </template>

    <slot spath="/window" />
    </root>

    First of all we declare variables. We declare additional variables, because there’s no way to compare variable and value. These variables are:

    1. true
    2. false

    We also remember state of visibility our window, because there’s not implemented toggling of visibility. It’s called root-visibility and default it’s false

    We also set’s name of window to hide/display this window.

    Also there’s additional is_first_if_processed with default value FALSE, because we don’t support else element type. If this variable is set – we don’t sets visibility to true. And, of course, we sets this variable to FALSE after checking it’s true, so there’s no way to hide/show icon permanently.

    dir-for in tray-icon(template of path /icon) attach icon and label of /icon template to our tray icon.

    Handler element is attached to default signal of icon and it depends on backend. In most cases it means click event.

    Now, let’s look how good programmer you are? How do you like to optimize this file? It’s simple – by moving setting default value of is_first_if_processed before first if element(as first child of handler) and removing element after second if, but I’m not very good in programming, so I don’t seen it before writing this article.

    And how writes application using tray icon in libgreatao? It’s simple. There you are code:

    #include
    #include

    void *main_window;

    void destroy_main_windopw(void *mesh1, void *mesh2)
    {
    tao_release_window(main_window);
    tao_close();
    }

    void init_main_window(void)
    {
    struct tao_image *img;
    struct tao_icon_set *iset;
    main_window = tao_new_window("/system/tray");

    tao_add_handler(main_window, "/:abort", destroy_main_windopw, 0);
    img = tao_load_image("internet-indicator.png");
    if (img) {

    iset= tao_new_icon_set();
    tao_add_image_to_icon_set(iset, img);
    tao_add_handler(main_window, "/icon", NULL, 0);
    tao_set_hint(main_window, "/icon", 3, iset);
    tao_set_hint(main_window, "/icon", 0, "Network Management");
    }
    }

    void add_to_main_window(void *window)
    {
    tao_attach_window(main_window, "/window", window);
    }

    int main(int argc, char **argv)
    {
    tao_initialize("tao-network-manager", "", &argc, argv);
    init_main_window();
    tao_handle_events();
    }

    Code inserted below load icon and on success add tray icon to our tray window:

    img = tao_load_image("internet-indicator.png");
    if (img) {

    iset= tao_new_icon_set();
    tao_add_image_to_icon_set(iset, img);
    tao_add_handler(main_window, "/icon", NULL, 0);
    tao_set_hint(main_window, "/icon", 3, iset);
    tao_set_hint(main_window, "/icon", 0, "Network Management");
    }

    tao_load_image load image on path given as first argument. We can’t attach single icon to icon element – we must create icon set and attach icon to icon set. In next step we attach icon set tom tray icon. It’s because giving possibilities to provide icons with different size and libgreattao will select the best icon to view.

    That’s all!

    Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s