solus-project / budgie-desktop-examples

Examples on how to create trivial Budgie Desktop Applets
MIT License
19 stars 11 forks source link

How to use new Budgie.Popover in Python #4

Closed yursan9 closed 7 years ago

yursan9 commented 7 years ago

Hello, I'm kinda confused how to use the new Budgie Popover. I tried to modify the python example, and looking at the source code for clock applet. This is what I got so far...

import gi.repository
gi.require_version('Budgie', '1.0')
from gi.repository import Budgie, GObject, Gtk, Gdk

class PyShowDesktop(GObject.GObject, Budgie.Plugin):
    __gtype_name__ = "PyShowDesktop"

    def __init__(self):
        GObject.Object.__init__(self)

    def do_get_panel_widget(self, uuid):
        return PyShowDesktopApplet(uuid)

class PyShowDesktopApplet(Budgie.Applet):
    def __init__(self, uuid):
        Budgie.Applet.__init__(self)
        self.widget = Gtk.EventBox()
        self.add(self.widget)
        self.manager = Budgie.PopoverManager()

        self.popover = Budgie.Popover(self.widget)
        self.label = Gtk.Label("Gotta Catch`m All!")
        self.popover.add(self.label)
        self.popover.get_child().show_all()

        self.manager.register_popover(self.widget, self.popover)
        self.show_all()

        self.widget.connect('button-press-event', self.on_clicked)

    def on_clicked(self, e):
        if (e.button != 1):
            return Gdk.EVENT_PROPAGATE
        if (popover.get_visible()):
            popover.hide();
        else:
            this.manager.show_popover(box);
        return Gdk.EVENT_STOP;
ikeydoherty commented 7 years ago

Hi - you should never create a new Budgie.PopoverManager. You override the register_popovers virtual method to register with the existing PopoverManager. In fact I'll see if I can hide the constructor for BudgiePopoverManager because this possibility frightens me.

yursan9 commented 7 years ago

Okay, sorry I put wrong example too.

yursan9 commented 7 years ago

So I redo the code for a little bit, and It's getting somewhere.

import gi.repository
gi.require_version('Budgie', '1.0')
from gi.repository import Budgie, GObject, Gtk, Gdk

class PyShowDesktop(GObject.GObject, Budgie.Plugin):
    """ This is simply an entry point into your Budgie Applet implementation.
        Note you must always override Object, and implement Plugin.
    """

    # Good manners, make sure we have unique name in GObject type system
    __gtype_name__ = "PyShowDesktop"

    def __init__(self):
        """ Initialisation is important.
        """
        GObject.Object.__init__(self)

    def do_get_panel_widget(self, uuid):
        """ This is where the real fun happens. Return a new Budgie.Applet
            instance with the given UUID. The UUID is determined by the
            BudgiePanelManager, and is used for lifetime tracking.
        """
        return PyShowDesktopApplet(uuid)

class PyShowDesktopApplet(Budgie.Applet):
    """ Budgie.Applet is in fact a Gtk.Bin """
    manager = None

    def __init__(self, uuid):
        Budgie.Applet.__init__(self)

        self.box = Gtk.EventBox()
        self.label = Gtk.Label("Hello")
        self.box.add(self.label)
        self.add(self.box)

        self.popover = Budgie.Popover(self.box)
        self.hello = Gtk.Label("World!")
        self.popover.add(self.hello)
        self.popover.get_child().show_all()

        self.box.show_all()
        self.show_all()
        self.box.connect("button-press-event", self.on_press)

    def on_press(self, box, e):
        if e.button != 1:
            return Gdk.EVENT_PROPAGATE
        if self.popover.get_visible():
            self.popover.hide()
        else:
            self.manager.show_popover(self.box)
        return Gdk.EVENT_STOP

    def do_update_popovers(self, manager):
        self.manager = manager
        self.manager.register_popover(self.box, self.popover)

But I got error when creating new Budgie.Popover`.

Traceback (most recent call last):
  File "/home/yurizal/.local/share/budgie-desktop/plugins/pydesktop/pyshowdesktop.py", line 45, in do_get_panel_widget
    return PyShowDesktopApplet(uuid)
  File "/home/yurizal/.local/share/budgie-desktop/plugins/pydesktop/pyshowdesktop.py", line 59, in __init__
    self.popover = Budgie.Popover(self.box)
  File "/usr/lib/python3.5/site-packages/gi/overrides/__init__.py", line 326, in new_init
    return super_init_func(self, **new_kwargs)
TypeError: could not convert value for property `type' from EventBox to GtkWindowType
sys:1: Warning: invalid (NULL) pointer instance
sys:1: Warning: g_signal_emit_by_name: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
ikeydoherty commented 7 years ago

I imagine you need to use the explicit constructor Budgie.Popover.new

yursan9 commented 7 years ago

Yep. That's fixed it. Thank you Ikey. I'll close this.

ikeydoherty commented 7 years ago

Thanks bud, I'll get the docs updated before we finalise the release. BTW can you link me to any of your applets? We need to do some migration code in Budgie to handle name -> description fixes..

yursan9 commented 7 years ago

This is my first time messing around trying to make Budgie Applet. I'll wait for the documentation to be up to date to try making more complete one.

p.s: Why don't you put a developer section in Budgie website later?

ikeydoherty commented 7 years ago

Yeah we're planning on centralizing the documentation to make it easier, much nicer than random assorted bits of info across GitHub :)

yursan9 commented 7 years ago

Great. I'll wait first, in the meantime I'll go to eat dinner. Thanks for your excellent work!

Majed6 commented 7 years ago

@ikeydoherty It keeps telling me AttributeError: 'gi.repository.Budgie' object has no attribute 'Popover'. What am I doing wrong? First time playin with Gtk; so bear with me ;P. It's strange since Budgie.Applet.__init__(self) is at the same level and works without any problems.

PS:

ikeydoherty commented 7 years ago

Budgie 17.04 isn't a thing. Budgie 10.3.1 is a thing. If you're not using Budgie git then BudgiePopover doesn't exist, its what will exist in Budgie 10.4

Majed6 commented 7 years ago

@ikeydoherty ooppsy doopsy thanks for that quick response. Yeah,I confused Ubuntu Budgie versioning with Budgie :( lol. I'm trying to code a dilbert applet in Ubuntu budgie and having some difficulty creating that popover effect. I'll stick to creating a separate window maybe.

ikeydoherty commented 7 years ago

Yeah,I confused Ubuntu Budgie versioning with Budgie

Yer I'm well used to that by now.

Honestly I'd recommend holding off until 10.4 which is really REALLY close - the API is way more sane, and you'll have no bugs from Budgie affecting your popover usage :]

Majed6 commented 7 years ago

@ikeydoherty lol. Great,I'll wait for that. Keep up the good work brother. Have a wonderful day/Night.

serdarsen commented 6 years ago

@ikeydoherty, @yursan9, @Majed6 thanks for your comments. This is my first applet for brightness (working with xrandr) https://github.com/serdarsen/UBrightnessController