Rework menu positioning. Cleanups.
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -6,4 +6,5 @@ sugar_PYTHON = \
|
||||
Colors.py \
|
||||
Grid.py \
|
||||
IconItem.py \
|
||||
IconColor.py
|
||||
IconColor.py \
|
||||
MenuIcon.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()
|
||||
|
||||
+23
-16
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user