Move all the popup logic from IconItem to MenuIcon

This commit is contained in:
Marco Pesenti Gritti 2006-09-16 15:01:38 +02:00
parent dd6c3fd0fe
commit 8dbdda5384
3 changed files with 52 additions and 49 deletions

View File

@ -19,7 +19,7 @@ class BuddyIcon(MenuIcon):
return menu return menu
def _popup_action_cb(self, popup, action): def _popup_action_cb(self, popup, action):
self._popdown() self.popdown()
model = self._shell.get_model() model = self._shell.get_model()
if action == BuddyPopup.ACTION_REMOVE_FRIEND: if action == BuddyPopup.ACTION_REMOVE_FRIEND:

View File

@ -195,7 +195,6 @@ class IconItem(goocanvas.ItemSimple, goocanvas.Item):
self.size = 24 self.size = 24
self.color = None self.color = None
self.icon_name = None self.icon_name = None
self._popdown_sid = 0
goocanvas.ItemSimple.__init__(self, **kwargs) goocanvas.ItemSimple.__init__(self, **kwargs)
@ -233,43 +232,7 @@ class IconItem(goocanvas.ItemSimple, goocanvas.Item):
def do_create_view(self, canvas, parent_view): def do_create_view(self, canvas, parent_view):
view = IconView(canvas, parent_view, self) view = IconView(canvas, parent_view, self)
view.connect('button-press-event', self._button_press_cb) view.connect('button-press-event', self._button_press_cb)
view.connect('enter-notify-event', self._enter_notify_event_cb, canvas)
view.connect('leave-notify-event', self._leave_notify_event_cb)
return view return view
def _button_press_cb(self, view, target, event): def _button_press_cb(self, view, target, event):
self.emit('clicked') self.emit('clicked')
def _start_popdown_timeout(self):
self._stop_popdown_timeout()
self._popdown_sid = gobject.timeout_add(1000, self._popdown_timeout_cb)
def _stop_popdown_timeout(self):
if self._popdown_sid > 0:
gobject.source_remove(self._popdown_sid)
self._popdown_sid = 0
def _enter_notify_event_cb(self, view, target, event, canvas):
self._stop_popdown_timeout()
[x1, y1] = canvas.convert_to_pixels(view.get_bounds().x1,
view.get_bounds().y1)
[x2, y2] = canvas.convert_to_pixels(view.get_bounds().x2,
view.get_bounds().y2)
[window_x, window_y] = canvas.window.get_origin()
x1 += window_x
y1 += window_y
x2 += window_x
y2 += window_y
self.emit('popup', int(x1), int(y1), int(x2), int(y2))
def _popdown_timeout_cb(self):
self._popdown_sid = 0
self.emit('popdown')
return False
def _leave_notify_event_cb(self, view, target, event):
self._start_popdown_timeout()

View File

@ -1,3 +1,6 @@
import goocanvas
import gobject
from sugar.canvas.IconItem import IconItem from sugar.canvas.IconItem import IconItem
from sugar.canvas.Grid import Grid from sugar.canvas.Grid import Grid
@ -10,7 +13,7 @@ class _MenuShell:
self._menu_controller.popdown() self._menu_controller.popdown()
self._menu_controller = controller self._menu_controller = controller
class MenuIcon(IconItem): class MenuIcon(IconItem, goocanvas.Item):
_menu_shell = _MenuShell() _menu_shell = _MenuShell()
def __init__(self, grid, **kwargs): def __init__(self, grid, **kwargs):
@ -21,9 +24,7 @@ class MenuIcon(IconItem):
self._menu_distance = 0 self._menu_distance = 0
self._hover_menu = False self._hover_menu = False
self._popdown_on_leave = False self._popdown_on_leave = False
self._popdown_sid = 0
self.connect('popup', self._popup_cb)
self.connect('popdown', self._popdown_cb)
def set_menu_distance(self, distance): def set_menu_distance(self, distance):
self._menu_distance = distance self._menu_distance = distance
@ -33,7 +34,7 @@ class MenuIcon(IconItem):
self._menu.destroy() self._menu.destroy()
self._menu = None self._menu = None
def _popup_cb(self, icon, x1, y1, x2, y2): def _popup(self, x1, y1, x2, y2):
self.popdown() self.popdown()
MenuIcon._menu_shell.set_active(None) MenuIcon._menu_shell.set_active(None)
@ -68,12 +69,6 @@ class MenuIcon(IconItem):
MenuIcon._menu_shell.set_active(self) MenuIcon._menu_shell.set_active(self)
def _popdown_cb(self, friend):
if not self._hover_menu:
self.popdown()
else:
self._popdown_on_leave = True
def _menu_enter_notify_event_cb(self, widget, event): def _menu_enter_notify_event_cb(self, widget, event):
self._hover_menu = True self._hover_menu = True
@ -81,3 +76,48 @@ class MenuIcon(IconItem):
self._hover_menu = False self._hover_menu = False
if self._popdown_on_leave: if self._popdown_on_leave:
self.popdown() self.popdown()
def do_create_view(self, canvas, parent_view):
view = IconItem.do_create_view(self, canvas, parent_view)
view.connect('enter-notify-event', self._enter_notify_event_cb, canvas)
view.connect('leave-notify-event', self._leave_notify_event_cb)
return view
def _start_popdown_timeout(self):
self._stop_popdown_timeout()
self._popdown_sid = gobject.timeout_add(1000, self._popdown_timeout_cb)
def _stop_popdown_timeout(self):
if self._popdown_sid > 0:
gobject.source_remove(self._popdown_sid)
self._popdown_sid = 0
def _enter_notify_event_cb(self, view, target, event, canvas):
self._stop_popdown_timeout()
[x1, y1] = canvas.convert_to_pixels(view.get_bounds().x1,
view.get_bounds().y1)
[x2, y2] = canvas.convert_to_pixels(view.get_bounds().x2,
view.get_bounds().y2)
[window_x, window_y] = canvas.window.get_origin()
x1 += window_x
y1 += window_y
x2 += window_x
y2 += window_y
self._popup(int(x1), int(y1), int(x2), int(y2))
def _popdown_timeout_cb(self):
self._popdown_sid = 0
if not self._hover_menu:
self.popdown()
else:
self._popdown_on_leave = True
return False
def _leave_notify_event_cb(self, view, target, event):
self._start_popdown_timeout()