Skip to content

Instantly share code, notes, and snippets.

@sahib
Last active August 29, 2015 14:13
Show Gist options
  • Save sahib/7d6c03d360f8c620005b to your computer and use it in GitHub Desktop.
Save sahib/7d6c03d360f8c620005b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
from gi.repository import Gtk, GLib, Gio
import sys
class MainWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Welcome to Gylfeed", application=app)
self.set_default_size(400, 350)
def create_item(name, action, icon, variant=None):
if variant is not None:
name = '{n} ({v})'.format(n=name, v=str(variant))
item = Gio.MenuItem.new(name, action)
item.set_icon(Gio.ThemedIcon.new(icon))
if variant:
item.set_action_and_target_value(action, variant)
return item
button = Gtk.MenuButton()
button.set_border_width(30)
button.set_valign(Gtk.Align.START)
# Comment out line for traditional menu instead of Popover.
# button.set_use_popover(False)
main_menu = Gio.Menu()
conf_menu = Gio.Menu()
for secs in (10, 20, 30, 60, 120):
conf_menu.append_item(
create_item(
'Update Interval', 'app.set_interval', 'view-refresh',
GLib.Variant('i', secs)
)
)
main_menu.append_submenu("Settings", conf_menu)
misc_menu = Gio.Menu()
misc_menu.append_item(create_item('Speichern', 'app.save', 'document-save'))
misc_menu.append_item(create_item('Feed adden', 'app.add', 'add'))
misc_menu.append_item(create_item('Feed Update', 'app.update', 'application-rss+xml'))
main_menu.append_section('General', misc_menu)
main_menu.append_item(create_item('Beenden', 'app.quit', 'window-close'))
button.set_menu_model(main_menu)
self.add(button)
class MainApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(
self,
application_id='org.test.gylfeed',
flags=Gio.ApplicationFlags.FLAGS_NONE
)
def do_activate(self):
self.win.present()
def do_startup(self):
Gtk.Application.do_startup(self)
def create_action(name, callback=self.action_clicked, var_type=None, value=None):
if var_type is None:
action = Gio.SimpleAction.new(name, None)
else:
action = Gio.SimpleAction.new_stateful(
name,
GLib.VariantType.new(var_type),
GLib.Variant(var_type, value)
)
action.connect('activate', callback)
return action
self.add_action(create_action("add"))
self.add_action(create_action("update"))
self.add_action(create_action("quit", callback=lambda *_: self.quit()))
self.add_action(create_action("save"))
self.add_action(create_action("set_interval", var_type='i', value=30))
self.set_accels_for_action('app.add', ['<Ctrl>A'])
self.set_accels_for_action('app.quit', ['<Ctrl>Q'])
self.win = MainWindow(self)
self.win.show_all()
def action_clicked(self, action, variant):
print(action, variant)
if variant:
action.set_state(variant)
sys.exit(MainApplication().run(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment