Complete the shell model/view split, finally

This commit is contained in:
Marco Pesenti Gritti 2006-12-24 15:39:00 +01:00
parent 0d1e07d9c3
commit b3c2368eac
6 changed files with 74 additions and 74 deletions

View File

@ -64,10 +64,3 @@ class ShellModel:
def get_owner(self):
return self._owner
def set_current_activity(self, activity_id):
self._current_activity = activity_id
self._owner.set_current_activity(activity_id)
def get_current_activity(self):
return self._current_activity

View File

@ -21,6 +21,7 @@ from sugar import profile
class HomeActivity:
def __init__(self, registry, window):
self._window = window
self._xid = window.get_xid()
self._service = Activity.get_service(window.get_xid())
self._id = self._service.get_id()
@ -45,6 +46,9 @@ class HomeActivity:
def get_id(self):
return self._id
def get_xid(self):
return self._xid
def get_window(self):
return self._window

View File

@ -14,6 +14,8 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import gobject
import wnck
@ -22,12 +24,15 @@ from model.homeactivity import HomeActivity
class HomeModel(gobject.GObject):
__gsignals__ = {
'activity-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
'activity-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'active-activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
}
def __init__(self, bundle_registry):
@ -35,11 +40,17 @@ class HomeModel(gobject.GObject):
self._activities = {}
self._bundle_registry = bundle_registry
self._current_activity = None
screen = wnck.screen_get_default()
screen.connect('window-opened', self._window_opened_cb)
screen.connect('window-closed', self._window_closed_cb)
screen.connect('active-window-changed',
self._active_window_changed_cb)
def get_current_activity(self):
return self._current_activity
def __iter__(self):
return iter(self._activities)
@ -56,6 +67,20 @@ class HomeModel(gobject.GObject):
def _window_closed_cb(self, screen, window):
if window.get_window_type() == wnck.WINDOW_NORMAL:
self._remove_activity(window.get_xid())
def _active_window_changed_cb(self, screen):
window = screen.get_active_window()
if not window or window.get_window_type() != wnck.WINDOW_NORMAL:
return
xid = window.get_xid()
if self._activities.has_key(xid):
self._current_activity = self._activities[xid]
else:
self._current_activity = None
logging.error('Model for window %d does not exist.' % xid)
self.emit('active-activity-changed', self._current_activity)
def _add_activity(self, window):
activity = HomeActivity(self._bundle_registry, window)
@ -66,3 +91,5 @@ class HomeModel(gobject.GObject):
if self._activities.has_key(xid):
self.emit('activity-removed', self._activities[xid])
del self._activities[xid]
else:
logging.error('Model for window %d does not exist.' % xid)

View File

@ -34,11 +34,6 @@ from _sugar import KeyGrabber
import sugar
class Shell(gobject.GObject):
__gsignals__ = {
'activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
}
def __init__(self, model):
gobject.GObject.__init__(self)
@ -65,9 +60,8 @@ class Shell(gobject.GObject):
home_model = self._model.get_home()
home_model.connect('activity-added', self._activity_added_cb)
home_model.connect('activity-removed', self._activity_removed_cb)
self._screen.connect('active-window-changed',
self.__active_window_changed_cb)
home_model.connect('active-activity-changed',
self._active_activity_changed_cb)
self._frame = Frame(self)
self._frame.show_and_hide(3)
@ -141,37 +135,14 @@ class Shell(gobject.GObject):
activity_host = ActivityHost(home_activity)
self._hosts[activity_host.get_xid()] = activity_host
def __active_window_changed_cb(self, screen):
logging.debug('Shell.__active_window_changed_cb')
window = screen.get_active_window()
if not window or window.get_window_type() != wnck.WINDOW_NORMAL:
return
if not self._hosts.has_key(window.get_xid()):
return
activity_host = self._hosts[window.get_xid()]
current = self._model.get_current_activity()
if activity_host.get_id() == current:
return
self._set_current_activity(activity_host)
def _activity_removed_cb(self, home_model, home_activity):
xid = home_activity.get_window().get_xid()
if not self._hosts.has_key(xid):
return
xid = home_activity.get_xid()
if self._hosts.has_key(xid):
self._hosts[xid].destroy()
del self._hosts[xid]
self._hosts[xid].destroy()
del self._hosts[xid]
if len(self._hosts) == 0:
self._set_current_activity(None)
def _set_current_activity(self, host):
if host:
self._model.set_current_activity(host.get_id())
else:
self._model.set_current_activity(None)
def _active_activity_changed_cb(self, home_model, home_activity):
host = self._hosts[home_activity.get_xid()]
if self._current_host:
self._current_host.set_active(False)
@ -181,8 +152,6 @@ class Shell(gobject.GObject):
if self._current_host:
self._current_host.set_active(True)
self.emit('activity-changed', host)
def get_model(self):
return self._model
@ -229,11 +198,7 @@ class Shell(gobject.GObject):
self._home_window.set_zoom_level(level)
def get_current_activity(self):
activity_id = self._model.get_current_activity()
if activity_id:
return self.get_activity(activity_id)
else:
return None
return self._current_host
def get_activity(self, activity_id):
for host in self._hosts.values():

View File

@ -41,7 +41,9 @@ class FriendsBox(hippo.CanvasBox):
for activity in self._pservice.get_activities():
self.__activity_appeared_cb(self._pservice, activity)
shell.connect('activity-changed', self.__activity_changed_cb)
home_model = shell.get_model().get_home()
home_model.connect('active-activity-changed',
self._active_activity_changed_cb)
def add_buddy(self, buddy):
if self._buddies.has_key(buddy.get_name()):
@ -94,9 +96,10 @@ class FriendsBox(hippo.CanvasBox):
self._left_hid = activity_ps.connect(
'buddy-left', self.__buddy_left_cb)
def __activity_changed_cb(self, group, activity):
if activity:
ps = self._pservice.get_activity(activity.get_id())
def _active_activity_changed_cb(self, home_model, home_activity):
if home_activity:
activity_id = home_activity.get_id()
ps = self._pservice.get_activity(activity_id)
self._set_activity_ps(ps)
else:
self._set_activity_ps(None)

View File

@ -14,6 +14,8 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import hippo
from sugar.graphics.canvasicon import CanvasIcon
@ -43,10 +45,9 @@ class ActivityMenu(Menu):
self.add_action(icon, ActivityMenu.ACTION_CLOSE)
class ActivityIcon(MenuIcon):
def __init__(self, shell, menu_shell, activity):
def __init__(self, shell, menu_shell, activity_model):
self._shell = shell
self._activity = activity
self._activity_model = activity.get_model()
self._activity_model = activity_model
icon_name = self._activity_model.get_icon_name()
icon_color = self._activity_model.get_icon_color()
@ -62,10 +63,15 @@ class ActivityIcon(MenuIcon):
def _action_cb(self, menu, action):
self.popdown()
activity = self._shell.get_current_activity()
if activity == None:
logging.error('No active activity.')
return
if action == ActivityMenu.ACTION_SHARE:
self._activity.share()
activity.share()
if action == ActivityMenu.ACTION_CLOSE:
self._activity.close()
activity.close()
class ZoomBox(hippo.CanvasBox):
def __init__(self, shell, menu_shell):
@ -95,23 +101,25 @@ class ZoomBox(hippo.CanvasBox):
icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_ACTIVITY)
self.append(icon)
shell.connect('activity-changed', self._activity_changed_cb)
self._set_current_activity(shell.get_current_activity())
home_model = shell.get_model().get_home()
home_model.connect('active-activity-changed',
self._activity_changed_cb)
self._set_current_activity(home_model.get_current_activity())
def _set_current_activity(self, activity):
def _set_current_activity(self, home_activity):
if self._activity_icon:
self.remove(self._activity_icon)
if activity:
icon = ActivityIcon(self._shell, self._menu_shell, activity)
if home_activity:
icon = ActivityIcon(self._shell, self._menu_shell, home_activity)
style.apply_stylesheet(icon, 'frame.ZoomIcon')
self.append(icon, 0)
self._activity_icon = icon
else:
self._activity_icon = None
def _activity_changed_cb(self, shell_model, activity):
self._set_current_activity(activity)
def _activity_changed_cb(self, home_model, home_activity):
self._set_current_activity(home_activity)
def _level_clicked_cb(self, item, level):
self._shell.set_zoom_level(level)