Move popup activation logic from IconButton to CanvasIcon.

This commit is contained in:
Tomeu Vizoso 2007-02-21 14:13:52 +01:00
parent 4b7fabc33e
commit 00db5e15a5
2 changed files with 88 additions and 86 deletions

View File

@ -25,6 +25,7 @@ import cairo
import time
from sugar.graphics.iconcolor import IconColor
from sugar.graphics.timeline import Timeline
class _IconCacheIcon:
def __init__(self, name, color, now):
@ -152,9 +153,17 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
self._icon_name = None
self._cache = False
self._handle = None
self._popup = None
self._hover_popup = False
self._timeline = Timeline(self)
self._timeline.add_tag('popup', 6, 6)
self._timeline.add_tag('before_popdown', 7, 7)
self._timeline.add_tag('popdown', 8, 8)
hippo.CanvasBox.__init__(self, **kwargs)
self.connect('motion-notify-event', self._motion_notify_event_cb)
self.connect('button-press-event', self._button_press_event_cb)
def _clear_buffers(self):
@ -261,3 +270,81 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
def _button_press_event_cb(self, item, event):
item.emit_activated()
def get_popup(self):
return self._popup
def get_popup_context(self):
return None
def do_popup(self, current, n_frames):
if self._popup:
return
popup = self.get_popup()
if not popup:
return
popup_context = self.get_popup_context()
[x, y] = [None, None]
if popup_context:
try:
[x, y] = popup_context.get_position(self, popup)
except NotImplementedError:
pass
if [x, y] == [None, None]:
context = self.get_context()
#[x, y] = context.translate_to_screen(self)
[x, y] = context.translate_to_widget(self)
# TODO: Any better place to do this?
popup.props.box_width = max(popup.props.box_width,
self.get_width_request())
[width, height] = self.get_allocation()
y += height
position = [x, y]
popup.popup(x, y)
popup.connect('motion-notify-event',
self._popup_motion_notify_event_cb)
popup.connect('action-completed',
self._popup_action_completed_cb)
if popup_context:
popup_context.popped_up(popup)
self._popup = popup
def do_popdown(self, current, frame):
if self._popup:
self._popup.popdown()
popup_context = self.get_popup_context()
if popup_context:
popup_context.popped_down(self._popup)
self._popup = None
def popdown(self):
self._timeline.play('popdown', 'popdown')
def _motion_notify_event_cb(self, button, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._timeline.play(None, 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
if not self._hover_popup:
self._timeline.play('before_popdown', 'popdown')
def _popup_motion_notify_event_cb(self, popup, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._hover_popup = True
self._timeline.play('popup', 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
self._hover_popup = False
self._timeline.play('popdown', 'popdown')
def _popup_action_completed_cb(self, popup):
self.popdown()

View File

@ -49,100 +49,14 @@ class IconButton(CanvasIcon):
self._prelight_color = profile.get_color()
self._inactive_color = IconColor('#808080,#424242')
self._active = True
self._popup = None
self._hover_popup = False
CanvasIcon.__init__(self, icon_name=icon_name, cache=True,
color=self._normal_color)
self._set_size(STANDARD_SIZE)
self._timeline = Timeline(self)
self._timeline.add_tag('popup', 6, 6)
self._timeline.add_tag('before_popdown', 7, 7)
self._timeline.add_tag('popdown', 8, 8)
self.connect('motion-notify-event', self._motion_notify_event_cb)
self.connect('button-press-event', self._button_press_event_cb)
def get_popup(self):
return self._popup
def get_popup_context(self):
return None
def do_popup(self, current, n_frames):
if self._popup:
return
popup = self.get_popup()
if not popup:
return
popup_context = self.get_popup_context()
[x, y] = [None, None]
if popup_context:
try:
[x, y] = popup_context.get_position(self, popup)
except NotImplementedError:
pass
if [x, y] == [None, None]:
context = self.get_context()
#[x, y] = context.translate_to_screen(self)
[x, y] = context.translate_to_widget(self)
# TODO: Any better place to do this?
popup.props.box_width = max(popup.props.box_width,
self.get_width_request())
[width, height] = self.get_allocation()
y += height
position = [x, y]
popup.popup(x, y)
popup.connect('motion-notify-event',
self._popup_motion_notify_event_cb)
popup.connect('action-completed',
self._popup_action_completed_cb)
if popup_context:
popup_context.popped_up(popup)
self._popup = popup
def do_popdown(self, current, frame):
if self._popup:
self._popup.popdown()
popup_context = self.get_popup_context()
if popup_context:
popup_context.popped_down(self._popup)
self._popup = None
def popdown(self):
self._timeline.play('popdown', 'popdown')
def _motion_notify_event_cb(self, button, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._timeline.play(None, 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
if not self._hover_popup:
self._timeline.play('before_popdown', 'popdown')
def _popup_motion_notify_event_cb(self, popup, event):
if event.detail == hippo.MOTION_DETAIL_ENTER:
self._hover_popup = True
self._timeline.play('popup', 'popup')
elif event.detail == hippo.MOTION_DETAIL_LEAVE:
self._hover_popup = False
self._timeline.play('popdown', 'popdown')
def _popup_action_completed_cb(self, popup):
self.popdown()
def _set_size(self, size):
if size == SMALL_SIZE:
self.props.box_width = -1
@ -178,3 +92,4 @@ class IconButton(CanvasIcon):
def _button_press_event_cb(self, widget, event):
if self._active:
self.emit_activated()
return True