Rework palette positioning. Cleanup and get at cursor

positioning actually right.
This commit is contained in:
Marco Pesenti Gritti 2007-07-18 20:15:54 +02:00
parent daa95aadd7
commit 6b57baa075
2 changed files with 70 additions and 59 deletions

View File

@ -25,6 +25,7 @@ import hippo
from sugar.graphics import palettegroup from sugar.graphics import palettegroup
from sugar.graphics import animator from sugar.graphics import animator
from sugar.graphics import units from sugar.graphics import units
from sugar.graphics import style
from sugar import _sugarext from sugar import _sugarext
_BOTTOM_LEFT = 0 _BOTTOM_LEFT = 0
@ -162,39 +163,6 @@ class Palette(gobject.GObject):
else: else:
raise AssertionError raise AssertionError
def _get_position(self, alignment, inv_rect=None):
# Invoker: x, y, width and height
if inv_rect == None:
inv_rect = self._invoker.get_rect()
palette_width, palette_height = self._menu.size_request()
if alignment == _BOTTOM_LEFT:
x = inv_rect.x
y = inv_rect.y + inv_rect.height
elif alignment == _BOTTOM_RIGHT:
x = (inv_rect.x + inv_rect.width) - palette_width
y = inv_rect.y + inv_rect.height
elif alignment == _LEFT_BOTTOM:
x = inv_rect.x - palette_width
y = inv_rect.y
elif alignment == _LEFT_TOP:
x = inv_rect.x - palette_width
y = (inv_rect.y + inv_rect.height) - palette_height
elif alignment == _RIGHT_BOTTOM:
x = inv_rect.x + inv_rect.width
y = inv_rect.y
elif alignment == _RIGHT_TOP:
x = inv_rect.x + inv_rect.width
y = (inv_rect.y + inv_rect.height) - palette_height
elif alignment == _TOP_LEFT:
x = inv_rect.x
y = inv_rect.y - palette_height
elif alignment == _TOP_RIGHT:
x = (inv_rect.x + inv_rect.width) - palette_width
y = inv_rect.y - palette_height
return x, y
def _in_screen(self, x, y): def _in_screen(self, x, y):
[width, height] = self._menu.size_request() [width, height] = self._menu.size_request()
screen_area = self._invoker.get_screen_area() screen_area = self._invoker.get_screen_area()
@ -204,19 +172,66 @@ class Palette(gobject.GObject):
x + width <= screen_area.width and \ x + width <= screen_area.width and \
y + height <= screen_area.height y + height <= screen_area.height
def _get_automatic_position(self, inv_rect=None): def _get_position(self, palette_halign, palette_valign,
alignments = [ _BOTTOM_LEFT, _BOTTOM_RIGHT, invoker_halign, invoker_valign, inv_rect=None):
_LEFT_BOTTOM, _LEFT_TOP, if inv_rect == None:
_RIGHT_BOTTOM, _RIGHT_TOP, inv_rect = self._invoker.get_rect()
_TOP_LEFT, _TOP_RIGHT ]
for alignment in alignments: palette_width, palette_height = self._menu.size_request()
x, y = self._get_position(alignment, inv_rect)
if self._in_screen(x, y):
return x, y
# We fail x = inv_rect.x + inv_rect.width * invoker_halign + \
return (0, 0) palette_width * palette_halign
y = inv_rect.y + inv_rect.height * invoker_valign + \
palette_height * palette_valign
return int(x), int(y)
def _get_left_position(self, inv_rect=None):
x, y = self._get_position(-1.0, 0.0, 0.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, -1.0, 0.0, 1.0, inv_rect)
return x, y
def _get_right_position(self, inv_rect=None):
x, y = self._get_position(0.0, 0.0, 1.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(0.0, -1.0, 1.0, 1.0, inv_rect)
return x, y
def _get_top_position(self, inv_rect=None):
x, y = self._get_position(0.0, -1.0, 0.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, -1.0, 1.0, 0.0, inv_rect)
return x, y
def _get_bottom_position(self, inv_rect=None):
x, y = self._get_position(0.0, 0.0, 0.0, 1.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, 0.0, 1.0, 1.0, inv_rect)
return x, y
def _get_around_position(self, inv_rect=None):
x, y = self._get_bottom_position(inv_rect)
if not self._in_screen(x, y):
x, y = self._get_right_position(inv_rect)
if not self._in_screen(x, y):
x, y = self._get_top_position(inv_rect)
if not self._in_screen(x, y):
x, y = self._get_left_position(inv_rect)
return x, y
def _get_at_cursor_position(self, inv_rect=None):
x, y = self._get_position(0.0, 0.0, 1.0, 1.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(0.0, -1.0, 1.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, -1.0, 0.0, 0.0, inv_rect)
if not self._in_screen(x, y):
x, y = self._get_position(-1.0, 0.0, 0.0, 1.0, inv_rect)
return x, y
def _show(self): def _show(self):
if self._up: if self._up:
@ -232,26 +247,20 @@ class Palette(gobject.GObject):
if position == self.AT_CURSOR: if position == self.AT_CURSOR:
display = gtk.gdk.display_get_default() display = gtk.gdk.display_get_default()
screen, x, y, mask = display.get_pointer() screen, x, y, mask = display.get_pointer()
rect = gtk.gdk.Rectangle(x, y, 0, 0) dist = style.PALETTE_CURSOR_DISTANCE
x, y = self._get_automatic_position(rect)
rect = gtk.gdk.Rectangle(x - dist, y - dist, dist * 2, dist * 2)
x, y = self._get_at_cursor_position(rect)
elif position == self.AROUND: elif position == self.AROUND:
x, y = self._get_automatic_position() x, y = self._get_around_position()
elif position == self.BOTTOM: elif position == self.BOTTOM:
x, y = self._get_position(_BOTTOM_LEFT) x, y = self._get_bottom_position()
if not self._in_screen(x, y):
x, y = self._get_position(_BOTTOM_RIGHT)
elif position == self.LEFT: elif position == self.LEFT:
x, y = self._get_position(_LEFT_BOTTOM) x, y = self._get_left_position()
if not self._in_screen(x, y):
x, y = self._get_position(_LEFT_TOP)
elif position == self.RIGHT: elif position == self.RIGHT:
x, y = self._get_position(_RIGHT_BOTTOM) x, y = self._get_right_position()
if not self._in_screen(x, y):
x, y = self._get_position(_RIGHT_TOP)
elif position == self.TOP: elif position == self.TOP:
x, y = self._get_position(_TOP_LEFT) x, y = self._get_top_position()
if not self._in_screen(x, y):
x, y = self._get_position(_TOP_RIGHT)
self._invoker.connect_to_parent() self._invoker.connect_to_parent()

View File

@ -106,3 +106,5 @@ TOOLBOX_TAB_LABEL_WIDTH = zoom(150 - 15 * 2)
COLOR_WHITE = Color('#FFFFFF') COLOR_WHITE = Color('#FFFFFF')
COLOR_PANEL_GREY = Color('#C0C0C0') COLOR_PANEL_GREY = Color('#C0C0C0')
COLOR_SELECTION_GREY = Color('#A6A6A6') COLOR_SELECTION_GREY = Color('#A6A6A6')
PALETTE_CURSOR_DISTANCE = zoom(10)