Rework menu positioning. Cleanups.

This commit is contained in:
Marco Pesenti Gritti 2006-09-17 01:05:59 +02:00
parent f77046e76a
commit 89e2f5be91
9 changed files with 79 additions and 20 deletions

View File

@ -7,5 +7,5 @@ sugar_PYTHON = \
ConsoleWindow.py \
FirstTimeDialog.py \
BuddyIcon.py \
BuddyPopup.py \
BuddyMenu.py \
Shell.py

View File

@ -5,4 +5,5 @@ sugar_PYTHON = \
PanelWindow.py \
Frame.py \
TopPanel.py \
BottomPanel.py
BottomPanel.py \
MenuStrategy.py

View File

@ -0,0 +1,28 @@
class MenuStrategy:
def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2):
grid = menu.get_grid()
[x1, y1] = grid.micro_to_macro(grid_x1, grid_y1)
[x2, y2] = grid.micro_to_macro(grid_x2, grid_y2)
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
[grid_x, grid_y] = grid.macro_to_micro(x, 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]

View File

@ -6,6 +6,7 @@ from sugar.canvas.CanvasBox import CanvasBox
from sugar.presence import PresenceService
from view.BuddyIcon import BuddyIcon
from model.BuddyInfo import BuddyInfo
from view.frame.MenuStrategy import MenuStrategy
class RightPanel(CanvasBox):
def __init__(self, shell):
@ -25,7 +26,7 @@ class RightPanel(CanvasBox):
def add(self, buddy):
icon = BuddyIcon(self._shell, BuddyInfo(buddy))
icon.set_menu_distance(1)
icon.set_menu_strategy(MenuStrategy())
self.set_constraints(icon, 3, 3)
self.add_child(icon)

View File

@ -4,6 +4,7 @@ from sugar.canvas.CanvasBox import CanvasBox
from sugar.canvas.IconItem import IconItem
from sugar.canvas.MenuIcon import MenuIcon
from sugar.canvas.Menu import Menu
from view.frame.MenuStrategy import MenuStrategy
import sugar
class ActivityMenu(Menu):
@ -27,6 +28,8 @@ class ActivityIcon(MenuIcon):
MenuIcon.__init__(self, shell.get_grid(), icon_name=icon_name,
color=icon_color)
self.set_menu_strategy(MenuStrategy())
def create_menu(self):
menu = ActivityMenu(self._shell.get_grid(), self._activity_host)
menu.connect('action', self._action_cb)

View File

@ -7,7 +7,22 @@ from sugar.canvas.IconItem import IconItem
class Grid:
COLS = 80.0
ROWS = 60.0
MACRO_CELL_FACTOR = 5.0
def get_macro_rows(self):
return Grid.ROWS / Grid.MACRO_CELL_FACTOR
def get_macro_cols(self):
return Grid.COLS / Grid.MACRO_CELL_FACTOR
def macro_to_micro(self, x, y):
return [round(x * Grid.MACRO_CELL_FACTOR),
round(y * Grid.MACRO_CELL_FACTOR)]
def micro_to_macro(self, x, y):
return [round(x / Grid.MACRO_CELL_FACTOR),
round(y / Grid.MACRO_CELL_FACTOR)]
def convert_from_screen(self, x, y):
factor = Grid.COLS / gtk.gdk.screen_width()

View File

@ -6,4 +6,5 @@ sugar_PYTHON = \
Colors.py \
Grid.py \
IconItem.py \
IconColor.py
IconColor.py \
MenuIcon.py

View File

@ -61,6 +61,9 @@ class Menu(gtk.Window):
return box
def get_grid(self):
return self._grid
def add_action(self, icon, action_id):
if self._action_box == None:
self._action_box = self._create_action_box()

View File

@ -13,6 +13,21 @@ class _MenuShell:
self._menu_controller.popdown()
self._menu_controller = controller
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]
class MenuIcon(IconItem, goocanvas.Item):
_menu_shell = _MenuShell()
@ -21,19 +36,19 @@ class MenuIcon(IconItem, goocanvas.Item):
self._grid = grid
self._menu = None
self._menu_distance = 0
self._hover_menu = False
self._popdown_on_leave = False
self._popdown_sid = 0
def set_menu_distance(self, distance):
self._menu_distance = distance
self._menu_strategy = _MenuStrategy()
def popdown(self):
if self._menu:
self._menu.destroy()
self._menu = None
def set_menu_strategy(self, strategy):
self._menu_strategy = strategy
def _popup(self, x1, y1, x2, y2):
self.popdown()
@ -46,21 +61,13 @@ class MenuIcon(IconItem, goocanvas.Item):
self._menu.connect('leave-notify-event',
self._menu_leave_notify_event_cb)
distance = self._menu_distance
[grid_x1, grid_y1] = grid.convert_from_screen(x1, y1)
[grid_x2, grid_y2] = grid.convert_from_screen(x2, y2)
grid_x = grid_x2 + distance
if grid_x + self._menu.get_width() > Grid.COLS:
grid_x = grid_x1 - self._menu.get_width() + 1 - distance
grid_y = grid_y1
if grid_y < 0:
grid_y = 0
if grid_y + self._menu.get_width() > Grid.ROWS:
grid_y = Grid.ROWS - self._menu.get_width()
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())