Skip to content

Instantly share code, notes, and snippets.

@yursan9
Last active March 27, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yursan9/3dfaa200bcf639415b160201e2051808 to your computer and use it in GitHub Desktop.
Save yursan9/3dfaa200bcf639415b160201e2051808 to your computer and use it in GitHub Desktop.
[Plugin]
Loader=python3
Module=hellobudgie
Name=Python Sample Applet
Description=Python Sample Applet
Authors=Yurizal Susanto
Copyright=Copyright © 2019 Yurizal Susanto
Website=https://getsol.us
Icon=user-desktop-symbolic
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Budgie', '1.0')
from gi.repository import Budgie, Gdk, Gio, GLib, GObject, Gtk
class BudgieHello(GObject.Object, Budgie.Plugin):
"""BudgieHello is top-level object to initialize the applet
"""
def __init__(self):
GObject.Object.__init__(self)
def do_get_panel_widget(self, uuid):
"""Show the Applet"""
return HelloApplet(uuid)
class HelloSettings(Gtk.Grid):
def __init__(self, setting):
super().__init__(row_spacing=8, column_spacing=6)
self.setting = setting
# Prepare Applet Settings
notify_label = Gtk.Label("Enable Notification", halign=Gtk.Align.START)
self.add(notify_label)
notify_switch = Gtk.Switch(halign=Gtk.Align.END, hexpand=True)
self.attach_next_to(notify_switch, notify_label, Gtk.PositionType.RIGHT, 1, 1)
# Bind settings to widget value
setting.bind("enable-notification", notify_switch, "active",
Gio.SettingsBindFlags.DEFAULT)
self.show_all()
class HelloApplet(Budgie.Applet):
def __init__(self, uuid):
# You can't use shorthand super().__init__() to initialize the Applet
Budgie.Applet.__init__(self)
self.uuid = uuid
self.manager = None
# Ensure schema and prefix is set properly
self.set_settings_prefix("/com/github/yursan9/budgie-caffeine-applet")
self.set_settings_schema("com.github.yursan9.budgie-caffeine-applet")
self.settings = self.get_applet_settings(self.uuid)
# Button Trigger
self.box = Gtk.EventBox()
self.icon = Gtk.Image.new_from_icon_name("user-desktop-symbolic", Gtk.IconSize.BUTTON)
self.box.add(self.icon)
self.add(self.box)
# Popover
self.popover = Budgie.Popover.new(self.box)
self.popover.get_style_context().add_class("hello-popover")
label = Gtk.Label("Hello World")
self.popover.add(label)
self.popover.get_child().show_all()
self.box.show_all()
self.show_all()
# Connect Callback
self.box.connect("button-press-event", self._on_press)
def do_get_settings_ui(self):
"""Return the applet settings with given uuid"""
return HelloSettings(self.get_applet_settings(self.uuid))
def do_supports_settings(self):
"""Return True if support setting through Budgie Setting,
False otherwise.
"""
return True
def do_update_popovers(self, manager):
"""Register popover to manager"""
manager.register_popover(self.box, self.popover)
self.manager = manager
def _on_press(self, box, e):
# Stop if not left mouse click button
if e.button != 1:
return Gdk.EVENT_PROPAGATE
# Popover show and hide
if self.popover.get_visible():
self.popover.hide()
else:
self.manager.show_popover(self.box)
return Gdk.EVENT_STOP
@yursan9
Copy link
Author

yursan9 commented Mar 27, 2019

To write Budgie Applet you need 2 file, the applet script (.py file) and the .plugin file. You need to place the file in /usr/lib/budgie-desktop/plugins/<plugin-directory>

Code Explanation

BudgieHello is the entry class for the applet, it inherits Budgie.Plugin. BudgieHello construct the applet in virtual method do_get_panel_widget.

HelloApplet is the main class for the applet. You need to implement button and popover in here. If the applet support settings, you need to override virtual method do_supports_settings and do_get_settings_ui.

HelloSettings is the class for applet's settings. It accept Gio.Setting, and you can use setting.bind to bind a widget value to setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment