Move the menu positioning code inside menu shell and
unify it.
This commit is contained in:
@@ -17,14 +17,11 @@
|
||||
|
||||
import hippo
|
||||
import gobject
|
||||
import logging
|
||||
|
||||
from sugar.graphics.canvasicon import CanvasIcon
|
||||
from sugar.graphics.timeline import Timeline
|
||||
|
||||
class _MenuStrategy:
|
||||
def get_menu_position(self, menu, item):
|
||||
return item.get_context().translate_to_widget(item)
|
||||
|
||||
class MenuIcon(CanvasIcon):
|
||||
def __init__(self, menu_shell, **kwargs):
|
||||
CanvasIcon.__init__(self, **kwargs)
|
||||
@@ -32,7 +29,6 @@ class MenuIcon(CanvasIcon):
|
||||
self._menu_shell = menu_shell
|
||||
self._menu = None
|
||||
self._hover_menu = False
|
||||
self._menu_strategy = _MenuStrategy()
|
||||
|
||||
self._timeline = Timeline(self)
|
||||
self._timeline.add_tag('popup', 6, 6)
|
||||
@@ -41,9 +37,6 @@ class MenuIcon(CanvasIcon):
|
||||
|
||||
self.connect('motion-notify-event', self._motion_notify_event_cb)
|
||||
|
||||
def set_menu_strategy(self, strategy):
|
||||
self._menu_strategy = strategy
|
||||
|
||||
def do_popup(self, current, n_frames):
|
||||
if self._menu:
|
||||
return
|
||||
@@ -55,8 +48,7 @@ class MenuIcon(CanvasIcon):
|
||||
self._menu.connect('leave-notify-event',
|
||||
self._menu_leave_notify_event_cb)
|
||||
|
||||
strategy = self._menu_strategy
|
||||
[x, y] = strategy.get_menu_position(self._menu, self)
|
||||
[x, y] = self._menu_shell.get_position(self._menu, self)
|
||||
|
||||
self._menu.move(x, y)
|
||||
self._menu.show()
|
||||
|
||||
@@ -25,8 +25,10 @@ class MenuShell(gobject.GObject):
|
||||
gobject.TYPE_NONE, ([])),
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, parent_canvas):
|
||||
gobject.GObject.__init__(self)
|
||||
|
||||
self._parent_canvas = parent_canvas
|
||||
self._menu_controller = None
|
||||
|
||||
def is_active(self):
|
||||
@@ -41,3 +43,30 @@ class MenuShell(gobject.GObject):
|
||||
if self._menu_controller:
|
||||
self._menu_controller.popdown()
|
||||
self._menu_controller = controller
|
||||
|
||||
def _get_item_origin(self, item):
|
||||
[x, y] = item.get_context().translate_to_widget(item)
|
||||
|
||||
[origin_x, origin_y] = self._parent_canvas.window.get_origin()
|
||||
x += origin_x
|
||||
y += origin_y
|
||||
|
||||
return [x, y]
|
||||
|
||||
def get_position(self, menu, item):
|
||||
[x, y] = self._get_item_origin(item)
|
||||
[width, height] = item.get_allocation()
|
||||
|
||||
[canvas_x, canvas_y] = self._parent_canvas.window.get_origin()
|
||||
canvas_rect = self._parent_canvas.get_allocation()
|
||||
[menu_w, menu_h] = menu.size_request()
|
||||
|
||||
menu_x = x
|
||||
menu_y = y + height
|
||||
|
||||
if (menu_x + menu_w > canvas_x) and \
|
||||
(menu_y < canvas_y + canvas_rect.height):
|
||||
menu_x = x - menu_w
|
||||
menu_y = y
|
||||
|
||||
return [menu_x, menu_y]
|
||||
|
||||
Reference in New Issue
Block a user