Skip to content

Instantly share code, notes, and snippets.

@cgranade
Created February 4, 2010 05:39
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 cgranade/294354 to your computer and use it in GitHub Desktop.
Save cgranade/294354 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
##
# overlay.py: small daemon for showing web pages in an overlay
##
# (c) 2010 Christopher E. Granade (cgranade@gmail.com).
# Licensed under GPL v3.
##
# WARNING: This is still a proof of concept. Do not use it
# without improving it first, unless you plan on filing bug
# reports.
##
import pygtk
pygtk.require('2.0')
import gtk
import webkit
class Configuration(object):
def __init__(self):
pass
def initial_url(self):
return "http://gmail.com/tasks"
def user_agent(self):
return "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11 Safari/525.20"
conf = Configuration()
class OverlayWindow(object):
def __init__(self):
# Initialize and configure the window.
self.window = gtk.Window()
#self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_MENU)
self.window.set_decorated(False)
#self.window.set_keep_above(True)
self.window.set_default_size(400,500)
# Make a browser.
self.browser = webkit.WebView()
self.browser.get_settings().set_property('user-agent', conf.user_agent())
# Pack the browser in a scollbox.
scroller = gtk.ScrolledWindow()
scroller.add(self.browser)
# Pack the browser in a box.
box = gtk.VBox(homogeneous=False, spacing=0)
box.add(scroller)
box.pack_start(scroller, expand=True, fill=True, padding=0)
self.window.add(box)
# Show everything but the window itself.
box.show()
scroller.show()
self.browser.show()
# Connect events.
self.window.connect('focus-out-event', self.on_lost_focus)
# Open a default page.
self.browser.open(conf.initial_url())
def show(self, x=0, y=0):
self.window.move(x,y)
self.window.show()
self.window.present()
def on_lost_focus(self, widget, event, data=None):
self.hide()
def hide(self):
self.window.hide()
class Main(object):
def __init__(self):
self.overlay = OverlayWindow()
self.status_icon = gtk.StatusIcon()
self.status_icon.set_from_stock("gtk-directory")
self.status_icon.connect("activate", self.on_show_overlay)
# Make a menu for popping up later.
self.popup_menu = self.make_menu()
self.status_icon.connect("popup-menu", self.on_status_popup)
def make_menu(self):
menu = gtk.Menu()
self.agr = gtk.AccelGroup()
#pref_item = self.make_menu_item(menu, self.on_pref_item_activate, accel_key="P", icon=gtk.STOCK_PREFERENCES)
quit_item = self.make_menu_item(menu, self.on_quit, accel_key="Q", icon=gtk.STOCK_QUIT)
return menu
def on_status_popup(self, icon, button, activate_time, user_data=None):
self.popup_menu.popup(None, None, None, button, activate_time, None)
def make_menu_item(self, formenu, activate_cb, label=None, icon=None, accel_key=None, accel_grp=None):
if icon:
agr = accel_grp or self.agr
item = gtk.ImageMenuItem(icon, agr)
k, m = gtk.accelerator_parse(accel_key)
item.add_accelerator("activate", agr, k, m, gtk.ACCEL_VISIBLE)
elif label:
item = gtk.MenuItem(label)
else:
# TODO: throw error
return
item.connect("activate", activate_cb)
formenu.append(item)
item.show()
return item
def on_show_overlay(self, widget, data=None):
screen, mouse_x, mouse_y, mask = self.status_icon.get_screen().get_display().get_pointer()
self.overlay.show(mouse_x, mouse_y)
def on_quit(self, widget, data=None):
gtk.main_quit()
def main(self):
gtk.main()
if __name__ == "__main__":
main = Main()
main.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment