From 89e2f5be91803bf99fcb326f4b5dd51200b7211a Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 17 Sep 2006 01:05:59 +0200 Subject: [PATCH] Rework menu positioning. Cleanups. --- shell/view/Makefile.am | 2 +- shell/view/frame/Makefile.am | 3 ++- shell/view/frame/MenuStrategy.py | 28 +++++++++++++++++++++++ shell/view/frame/RightPanel.py | 3 ++- shell/view/frame/TopPanel.py | 3 +++ sugar/canvas/Grid.py | 15 ++++++++++++ sugar/canvas/Makefile.am | 3 ++- sugar/canvas/Menu.py | 3 +++ sugar/canvas/MenuIcon.py | 39 +++++++++++++++++++------------- 9 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 shell/view/frame/MenuStrategy.py diff --git a/shell/view/Makefile.am b/shell/view/Makefile.am index 4b1c901d..d291cf34 100644 --- a/shell/view/Makefile.am +++ b/shell/view/Makefile.am @@ -7,5 +7,5 @@ sugar_PYTHON = \ ConsoleWindow.py \ FirstTimeDialog.py \ BuddyIcon.py \ - BuddyPopup.py \ + BuddyMenu.py \ Shell.py diff --git a/shell/view/frame/Makefile.am b/shell/view/frame/Makefile.am index a737e018..8d951142 100644 --- a/shell/view/frame/Makefile.am +++ b/shell/view/frame/Makefile.am @@ -5,4 +5,5 @@ sugar_PYTHON = \ PanelWindow.py \ Frame.py \ TopPanel.py \ - BottomPanel.py + BottomPanel.py \ + MenuStrategy.py diff --git a/shell/view/frame/MenuStrategy.py b/shell/view/frame/MenuStrategy.py new file mode 100644 index 00000000..314cb047 --- /dev/null +++ b/shell/view/frame/MenuStrategy.py @@ -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] diff --git a/shell/view/frame/RightPanel.py b/shell/view/frame/RightPanel.py index 08f41e60..d2387611 100644 --- a/shell/view/frame/RightPanel.py +++ b/shell/view/frame/RightPanel.py @@ -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) diff --git a/shell/view/frame/TopPanel.py b/shell/view/frame/TopPanel.py index 5d181415..af07ad4c 100644 --- a/shell/view/frame/TopPanel.py +++ b/shell/view/frame/TopPanel.py @@ -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) diff --git a/sugar/canvas/Grid.py b/sugar/canvas/Grid.py index 75630c62..c6a0ae7e 100644 --- a/sugar/canvas/Grid.py +++ b/sugar/canvas/Grid.py @@ -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() diff --git a/sugar/canvas/Makefile.am b/sugar/canvas/Makefile.am index ebccb01a..d5a9014b 100644 --- a/sugar/canvas/Makefile.am +++ b/sugar/canvas/Makefile.am @@ -6,4 +6,5 @@ sugar_PYTHON = \ Colors.py \ Grid.py \ IconItem.py \ - IconColor.py + IconColor.py \ + MenuIcon.py diff --git a/sugar/canvas/Menu.py b/sugar/canvas/Menu.py index ba487be8..ce34f5fa 100644 --- a/sugar/canvas/Menu.py +++ b/sugar/canvas/Menu.py @@ -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() diff --git a/sugar/canvas/MenuIcon.py b/sugar/canvas/MenuIcon.py index bd5772ae..b4297e52 100644 --- a/sugar/canvas/MenuIcon.py +++ b/sugar/canvas/MenuIcon.py @@ -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())