Reimplement menu using hippo and hook it up for the

activity menu.
This commit is contained in:
Marco Pesenti Gritti 2006-10-02 16:37:30 +02:00
parent 4958c43b33
commit a951b36347
6 changed files with 88 additions and 62 deletions

View File

@ -1,28 +1,15 @@
from sugar.graphics.grid import Grid
class MenuStrategy:
def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2):
grid = menu.get_grid()
def get_menu_position(self, menu, x, y, width, height):
grid = Grid()
[x1, y1] = grid.micro_to_macro(grid_x1, grid_y1)
[x2, y2] = grid.micro_to_macro(grid_x2, grid_y2)
[grid_x1, grid_y1] = grid.fit_point(x, y)
[grid_x2, grid_y2] = grid.fit_point(x + width, y + height)
if x1 == 0:
x = x2
y = y1
elif x2 == grid.get_macro_cols():
x = x1
y = y1
elif y2 == grid.get_macro_rows():
x = x1
y = y1
else:
x = x1
y = y2
menu_grid_x = grid_x1
menu_grid_y = grid_y2
[grid_x, grid_y] = grid.macro_to_micro(x, y)
[menu_x, menu_y] = grid.point(menu_grid_x, menu_grid_y)
if x2 == grid.get_macro_cols():
grid_x -= menu.get_width()
elif y2 == grid.get_macro_rows():
grid_y -= menu.get_height()
return [grid_x, grid_y]
return [menu_x, menu_y]

View File

@ -3,9 +3,8 @@ import hippo
from sugar.graphics.canvasicon import CanvasIcon
from sugar.graphics.menuicon import MenuIcon
from sugar.graphics.menu import Menu
from sugar.graphics import style
from sugar.canvas.Menu import Menu
from sugar.canvas.IconItem import IconItem
from view.frame.MenuStrategy import MenuStrategy
import sugar
@ -14,13 +13,14 @@ class ActivityMenu(Menu):
ACTION_CLOSE = 2
def __init__(self, grid, activity_host):
title = activity_host.get_title()
Menu.__init__(self, grid, title)
Menu.__init__(self, activity_host.get_title())
icon = IconItem(icon_name='stock-share-mesh')
icon = CanvasIcon(icon_name='stock-share-mesh')
style.apply_stylesheet(icon, 'menu-action-icon')
self.add_action(icon, ActivityMenu.ACTION_SHARE)
icon = IconItem(icon_name='stock-close')
icon = CanvasIcon(icon_name='stock-close')
style.apply_stylesheet(icon, 'menu-action-icon')
self.add_action(icon, ActivityMenu.ACTION_CLOSE)
class ActivityIcon(MenuIcon):
@ -64,22 +64,22 @@ class ZoomBox(hippo.CanvasBox):
icon = CanvasIcon(icon_name='stock-zoom-mesh')
style.apply_stylesheet(icon, 'frame-zoom-icon')
icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_MESH)
self.append(icon, 0)
self.append(icon)
icon = CanvasIcon(icon_name='stock-zoom-friends')
style.apply_stylesheet(icon, 'frame-zoom-icon')
icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_FRIENDS)
self.append(icon, 0)
self.append(icon)
icon = CanvasIcon(icon_name='stock-zoom-home')
style.apply_stylesheet(icon, 'frame-zoom-icon')
icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_HOME)
self.append(icon, 0)
self.append(icon)
icon = CanvasIcon(icon_name='stock-zoom-activity')
style.apply_stylesheet(icon, 'frame-zoom-icon')
icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_ACTIVITY)
self.append(icon, 0)
self.append(icon)
shell.connect('activity-changed', self._activity_changed_cb)
self._set_current_activity(shell.get_current_activity())

View File

@ -18,3 +18,8 @@ _stylesheet = {
'size' : _medium_icon_size
}
style.register_stylesheet('frame-zoom-icon', _stylesheet)
_stylesheet = {
'size' : _medium_icon_size
}
style.register_stylesheet('menu-action-icon', _stylesheet)

View File

@ -7,9 +7,13 @@ class Grid(object):
def __init__(self):
self._factor = gtk.gdk.screen_width() / COLS
def point(self, x, y):
return [x * self._factor, y * self._factor]
def point(self, grid_x, grid_y):
return [grid_x * self._factor, grid_y * self._factor]
def rectangle(self, grid_x, grid_y, grid_w, grid_h):
return [grid_x * self._factor, grid_y * self._factor,
grid_w * self._factor, grid_h * self._factor]
def fit_point(self, x, y):
return [int(x / self._factor), int(y / self._factor)]
def rectangle(self, x, y, width, height):
return [x * self._factor, y * self._factor,
width * self._factor, height * self._factor]

50
sugar/graphics/menu.py Normal file
View File

@ -0,0 +1,50 @@
import gtk
import hippo
import gobject
from sugar.graphics.canvasicon import CanvasIcon
class Menu(gtk.Window):
__gsignals__ = {
'action': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([int])),
}
def __init__(self, title, content_box=None):
gtk.Window.__init__(self, gtk.WINDOW_POPUP)
canvas = hippo.Canvas()
self.add(canvas)
canvas.show()
self._root = hippo.CanvasBox(background_color=0x000000FF,
spacing=6)
canvas.set_root(self._root)
text = hippo.CanvasText(text=title, color=0xFFFFFFFF)
self._root.append(text)
if content_box:
separator = self._create_separator()
self._root.append(separator)
self._root.append(content_box)
separator = self._create_separator()
self._root.append(separator)
self._action_box = hippo.CanvasBox(
orientation=hippo.ORIENTATION_HORIZONTAL)
self._root.append(self._action_box)
def _create_separator(self):
separator = hippo.CanvasBox(background_color=0xFFFFFFFF,
border_left=6, border_right=6,
box_height=2)
return separator
def add_action(self, icon, action_id):
icon.connect('activated', self._action_clicked_cb, action_id)
self._action_box.append(icon)
def _action_clicked_cb(self, icon, action):
self.emit('action', action)

View File

@ -4,26 +4,14 @@ import gobject
from sugar.graphics.canvasicon import CanvasIcon
class _MenuStrategy:
def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2):
grid_x = grid_x2
if grid_x + menu.get_width() > Grid.COLS:
grid_x = grid_x1 - menu.get_width() + 1
grid_y = grid_y1
if grid_y < 0:
grid_y = 0
if grid_y + menu.get_width() > Grid.ROWS:
grid_y = Grid.ROWS - menu.get_width()
return [grid_x, grid_y]
def get_menu_position(self, menu, x1, y1, x2, y2):
return [x1, y1]
class MenuIcon(CanvasIcon):
def __init__(self, menu_shell, **kwargs):
CanvasIcon.__init__(self, **kwargs)
self._menu_shell = menu_shell
self._grid = menu_shell.get_grid()
self._menu = None
self._hover_menu = False
self._popdown_on_leave = False
@ -46,24 +34,16 @@ class MenuIcon(CanvasIcon):
self._menu_shell.set_active(None)
grid = self._shell.get_grid()
self._menu = self.create_menu()
self._menu.connect('enter-notify-event',
self._menu_enter_notify_event_cb)
self._menu.connect('leave-notify-event',
self._menu_leave_notify_event_cb)
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
strategy = self._menu_strategy
[grid_x, grid_y] = strategy.get_menu_position(self._menu,
grid_x1, grid_y1,
grid_x2, grid_y2)
grid.set_constraints(self._menu, grid_x, grid_y,
self._menu.get_width(), self._menu.get_height())
[x, y] = strategy.get_menu_position(self._menu, x1, y1, x2, y2)
self._menu.move(x, y)
self._menu.show()
self._menu_shell.set_active(self)