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 \ ConsoleWindow.py \
FirstTimeDialog.py \ FirstTimeDialog.py \
BuddyIcon.py \ BuddyIcon.py \
BuddyPopup.py \ BuddyMenu.py \
Shell.py Shell.py

View File

@ -5,4 +5,5 @@ sugar_PYTHON = \
PanelWindow.py \ PanelWindow.py \
Frame.py \ Frame.py \
TopPanel.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 sugar.presence import PresenceService
from view.BuddyIcon import BuddyIcon from view.BuddyIcon import BuddyIcon
from model.BuddyInfo import BuddyInfo from model.BuddyInfo import BuddyInfo
from view.frame.MenuStrategy import MenuStrategy
class RightPanel(CanvasBox): class RightPanel(CanvasBox):
def __init__(self, shell): def __init__(self, shell):
@ -25,7 +26,7 @@ class RightPanel(CanvasBox):
def add(self, buddy): def add(self, buddy):
icon = BuddyIcon(self._shell, BuddyInfo(buddy)) icon = BuddyIcon(self._shell, BuddyInfo(buddy))
icon.set_menu_distance(1) icon.set_menu_strategy(MenuStrategy())
self.set_constraints(icon, 3, 3) self.set_constraints(icon, 3, 3)
self.add_child(icon) 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.IconItem import IconItem
from sugar.canvas.MenuIcon import MenuIcon from sugar.canvas.MenuIcon import MenuIcon
from sugar.canvas.Menu import Menu from sugar.canvas.Menu import Menu
from view.frame.MenuStrategy import MenuStrategy
import sugar import sugar
class ActivityMenu(Menu): class ActivityMenu(Menu):
@ -27,6 +28,8 @@ class ActivityIcon(MenuIcon):
MenuIcon.__init__(self, shell.get_grid(), icon_name=icon_name, MenuIcon.__init__(self, shell.get_grid(), icon_name=icon_name,
color=icon_color) color=icon_color)
self.set_menu_strategy(MenuStrategy())
def create_menu(self): def create_menu(self):
menu = ActivityMenu(self._shell.get_grid(), self._activity_host) menu = ActivityMenu(self._shell.get_grid(), self._activity_host)
menu.connect('action', self._action_cb) menu.connect('action', self._action_cb)

View File

@ -7,6 +7,21 @@ from sugar.canvas.IconItem import IconItem
class Grid: class Grid:
COLS = 80.0 COLS = 80.0
ROWS = 60.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): def convert_from_screen(self, x, y):
factor = Grid.COLS / gtk.gdk.screen_width() factor = Grid.COLS / gtk.gdk.screen_width()

View File

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

View File

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

View File

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