From 08c786c7b182cbefb208375697988b11709c158d Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Wed, 10 Aug 2005 10:54:03 +0000 Subject: [PATCH] Holding the pointer over an interface in the main window shows a tooltip containing its URL and the start of its description. git-svn-id: file:///home/talex/Backups/sf.net/Subversion/zero-install/injector/injector-gui@411 9f8c893c-44ee-0310-b757-c8ca8341c71e --- iface_browser.py | 30 ++++++++++++++++++++++ treetips.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 treetips.py diff --git a/iface_browser.py b/iface_browser.py index 6486477..5ff2052 100644 --- a/iface_browser.py +++ b/iface_browser.py @@ -2,8 +2,24 @@ import gtk from zeroinstall.injector.model import Interface import properties +from treetips import TreeTips from gui import policy, pretty_size +class InterfaceTips(TreeTips): + def get_tooltip_text(self, interface): + assert interface + text = "(%s)\n" % interface.uri + if interface.description: + max_lines = 4 + lines = filter(None, interface.description.split('\n')) + if len(lines) > max_lines: + lines = lines[:max_lines] + ['...'] + text += '\n'.join(lines) + + return text + +tips = InterfaceTips() + class InterfaceBrowser(gtk.ScrolledWindow): model = None root = None @@ -54,6 +70,20 @@ class InterfaceBrowser(gtk.ScrolledWindow): selection = tree_view.get_selection() + def motion(tree_view, ev): + if ev.window is not tree_view.get_bin_window(): + return False + pos = tree_view.get_path_at_pos(int(ev.x), int(ev.y)) + if pos: + path = pos[0] + row = self.model[path] + tips.prime(tree_view, row[InterfaceBrowser.INTERFACE]) + else: + tips.hide() + + tree_view.connect('motion-notify-event', motion) + tree_view.connect('leave-notify-event', lambda tv, ev: tips.hide()) + def sel_changed(sel): store, iter = sel.get_selected() self.edit_properties.set_property('sensitive', iter != None) diff --git a/treetips.py b/treetips.py new file mode 100644 index 0000000..86e6c53 --- /dev/null +++ b/treetips.py @@ -0,0 +1,78 @@ +import time, gobject, gtk + +class TreeTips: + timeout = None + widget = None + item = None + time = 0 + + def show(self, parent): + if self.timeout: + gobject.source_remove(self.timeout) + self.timeout = None + + if self.widget: + self.widget.destroy() + self.widget = None + + if self.item is None: + return + + self.widget = gtk.Window(gtk.WINDOW_POPUP) + self.widget.set_app_paintable(True) + self.widget.set_name('gtk-tooltips') + + self.widget.connect('expose-event', self.tooltip_draw) + + label = gtk.Label(self.get_tooltip_text(self.item)) + label.set_padding(4, 2) + self.widget.add(label) + label.show() + + w, h = self.widget.size_request() + screen = parent.get_screen() + root = screen.get_root_window() + px, py, mask = gtk.gdk.Window.get_pointer(root) + + m = gtk.gdk.screen_get_default().get_monitor_at_point(px, py) + + x = px - w / 2 + y = py + 12 + + # Test if pointer is over the tooltip window + if py >= y and py <= y + h: + y = py - h - 2 + self.widget.move(x, y) + self.widget.show() + + self.widget.connect('destroy', self.tooltip_destroyed) + self.time = time.time() + + def prime(self, parent, item): + if item is self.item: + return + + self.hide() + assert self.timeout is None + self.item = item + + now = time.time() + if now - self.time > 2: + delay = 1000 + else: + delay = 100 + + self.timeout = gobject.timeout_add(delay, lambda: self.show(parent)) + + def tooltip_draw(self, widget, ev): + widget.window.draw_rectangle(widget.style.fg_gc[widget.state], + False, 0, 0, + widget.allocation.width - 1, + widget.allocation.height - 1) + + def tooltip_destroyed(self, widget): + pass + + def hide(self): + self.item = None + self.show(None) -- 2.11.4.GIT