lgi-devs / lgi

Dynamic Lua binding to GObject libraries using GObject-Introspection
MIT License
440 stars 70 forks source link

AppIndicator3 demo #233

Open v1993 opened 5 years ago

v1993 commented 5 years ago

I tried to make a demo application that uses AppIndicator3 library. That's what I got so far:

local lgi = require 'lgi'

local GLib, Gtk, Gdk, Gio, GObject = lgi.GLib, lgi.Gtk, lgi.Gdk, lgi.Gio, lgi.GObject
local AppIndicator = lgi.AppIndicator3

local app = Gio.Application { application_id = 'org.lgi.appindicator3' }

local menu = Gtk.Menu()

menu:append(
    Gtk.MenuItem {
        label = 'Exit';
        on_activate = function() app:release() end;
        sensitive = true;
    }
)

function app:on_activate()
    local ind = AppIndicator.Indicator.new(app.application_id, 'gtk-info', 'SYSTEM_SERVICES')
    ind:set_menu(menu)
    ind:set_status('ACTIVE')
    app:hold()
end

app:run({arg[0], ...})

I expect it to show icon with single drop-down option "Exit". What I get, however, is icon with empty menu (no Exit option).

What could be a problem here? If it matters, I'm using cinnamon with indicators support turned on.

psychon commented 5 years ago

What could be a problem here?

(Just so that there is an answer here:) No idea, sorry. This is the first time I hear about AppIndicator3 and I never did a menu with Gtk.

v1993 commented 5 years ago

@psychon Oh man, just found a really stupid problem: had to :show_all() on menu to activate elements. Works now ☺.

What do you think about extending my demo and later adding it as sample?

rokf commented 5 years ago

I wanted to write a comment about this show_all thingy previously, it almost always fixed my issues with stuff not displaying. I haven't written anything with lgi for a long time and therefore hesitated with blindly writing an answer. Great that you found the solution :+1: :smile:

psychon commented 5 years ago

I think that I still do not know my way around Gtk and AppIndicator3, so I cannot answer that question. Perhaps you get an answer from @pavouk ...?

v1993 commented 5 years ago

@psychon Here's a full demo:

local lgi = require 'lgi'

local GLib, Gtk, Gdk, Gio, GObject = lgi.GLib, lgi.Gtk, lgi.Gdk, lgi.Gio, lgi.GObject
local AppIndicator = lgi.AppIndicator3

local app = Gio.Application { application_id = 'org.lgi.appindicator3' }

local ind

local notif = 'This must never be shown'

local radios = {}
do
    local r1 = Gtk.RadioMenuItem.new_with_label(nil, 'Apple')
    function r1:on_activate()
        notif = "You will get an apple today!"
    end
    r1:activate()
    r1:set_active(true)
    local r2 = Gtk.RadioMenuItem.new_with_label(r1:get_group(), 'Banana')
    function r2:on_activate()
        notif = "Here is your banana!"
    end
    radios = {r1, r2}
end

local menu = Gtk.Menu {
    Gtk.MenuItem {
        label = 'Send notification with dessert';
        on_activate = function()
            local notification = Gio.Notification.new("Dessert")
            notification:set_body(notif)
            app:send_notification('lgi-test-dessert', notification)
        end;
    };
    Gtk.SeparatorMenuItem {};
    Gtk.MenuItem {
        label = 'Select your dessert';
        sensitive = false;
    };
    radios[1];
    radios[2];
    Gtk.SeparatorMenuItem {};
    Gtk.CheckMenuItem {
        label = 'Show alternate icon';
        on_toggled = function(self)
            if self.active then
                ind.icon_name = 'gtk-no'
            else
                ind.icon_name = 'gtk-info'
            end
        end
    };
    Gtk.SeparatorMenuItem {};
    Gtk.MenuItem {
        label = 'Exit';
        on_activate = function()
            app:release()
        end;
    };
}

function app:on_activate()
    ind = AppIndicator.Indicator.new(app.application_id, 'gtk-info', 'APPLICATION_STATUS')
    ind:set_menu(menu)
    menu:show_all()
    ind:set_status('ACTIVE')
    app:hold()
end

app:run({arg[0], ...})

And how it looks like (in cinnamon):

https://youtu.be/-rY-smK85gE

Those don't seem to be supported out-of-box in Gnome (or they weren't), so extension is required to show them: https://extensions.gnome.org/extension/615/appindicator-support/