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): def get_owner(self):
return self._owner 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: class HomeActivity:
def __init__(self, registry, window): def __init__(self, registry, window):
self._window = window self._window = window
self._xid = window.get_xid()
self._service = Activity.get_service(window.get_xid()) self._service = Activity.get_service(window.get_xid())
self._id = self._service.get_id() self._id = self._service.get_id()
@ -45,6 +46,9 @@ class HomeActivity:
def get_id(self): def get_id(self):
return self._id return self._id
def get_xid(self):
return self._xid
def get_window(self): def get_window(self):
return self._window return self._window

View File

@ -14,6 +14,8 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import gobject import gobject
import wnck import wnck
@ -22,12 +24,15 @@ from model.homeactivity import HomeActivity
class HomeModel(gobject.GObject): class HomeModel(gobject.GObject):
__gsignals__ = { __gsignals__ = {
'activity-added': (gobject.SIGNAL_RUN_FIRST, 'activity-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])), ([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST, 'activity-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])) ([gobject.TYPE_PYOBJECT])),
'active-activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
} }
def __init__(self, bundle_registry): def __init__(self, bundle_registry):
@ -35,10 +40,16 @@ class HomeModel(gobject.GObject):
self._activities = {} self._activities = {}
self._bundle_registry = bundle_registry self._bundle_registry = bundle_registry
self._current_activity = None
screen = wnck.screen_get_default() screen = wnck.screen_get_default()
screen.connect('window-opened', self._window_opened_cb) screen.connect('window-opened', self._window_opened_cb)
screen.connect('window-closed', self._window_closed_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): def __iter__(self):
return iter(self._activities) return iter(self._activities)
@ -57,6 +68,20 @@ class HomeModel(gobject.GObject):
if window.get_window_type() == wnck.WINDOW_NORMAL: if window.get_window_type() == wnck.WINDOW_NORMAL:
self._remove_activity(window.get_xid()) 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): def _add_activity(self, window):
activity = HomeActivity(self._bundle_registry, window) activity = HomeActivity(self._bundle_registry, window)
self._activities[window.get_xid()] = activity self._activities[window.get_xid()] = activity
@ -66,3 +91,5 @@ class HomeModel(gobject.GObject):
if self._activities.has_key(xid): if self._activities.has_key(xid):
self.emit('activity-removed', self._activities[xid]) self.emit('activity-removed', self._activities[xid])
del 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 import sugar
class Shell(gobject.GObject): class Shell(gobject.GObject):
__gsignals__ = {
'activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
}
def __init__(self, model): def __init__(self, model):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
@ -65,9 +60,8 @@ class Shell(gobject.GObject):
home_model = self._model.get_home() home_model = self._model.get_home()
home_model.connect('activity-added', self._activity_added_cb) home_model.connect('activity-added', self._activity_added_cb)
home_model.connect('activity-removed', self._activity_removed_cb) home_model.connect('activity-removed', self._activity_removed_cb)
home_model.connect('active-activity-changed',
self._screen.connect('active-window-changed', self._active_activity_changed_cb)
self.__active_window_changed_cb)
self._frame = Frame(self) self._frame = Frame(self)
self._frame.show_and_hide(3) self._frame.show_and_hide(3)
@ -141,37 +135,14 @@ class Shell(gobject.GObject):
activity_host = ActivityHost(home_activity) activity_host = ActivityHost(home_activity)
self._hosts[activity_host.get_xid()] = activity_host 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): def _activity_removed_cb(self, home_model, home_activity):
xid = home_activity.get_window().get_xid() xid = home_activity.get_xid()
if not self._hosts.has_key(xid): if self._hosts.has_key(xid):
return self._hosts[xid].destroy()
del self._hosts[xid]
self._hosts[xid].destroy() def _active_activity_changed_cb(self, home_model, home_activity):
del self._hosts[xid] host = self._hosts[home_activity.get_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)
if self._current_host: if self._current_host:
self._current_host.set_active(False) self._current_host.set_active(False)
@ -181,8 +152,6 @@ class Shell(gobject.GObject):
if self._current_host: if self._current_host:
self._current_host.set_active(True) self._current_host.set_active(True)
self.emit('activity-changed', host)
def get_model(self): def get_model(self):
return self._model return self._model
@ -229,11 +198,7 @@ class Shell(gobject.GObject):
self._home_window.set_zoom_level(level) self._home_window.set_zoom_level(level)
def get_current_activity(self): def get_current_activity(self):
activity_id = self._model.get_current_activity() return self._current_host
if activity_id:
return self.get_activity(activity_id)
else:
return None
def get_activity(self, activity_id): def get_activity(self, activity_id):
for host in self._hosts.values(): for host in self._hosts.values():

View File

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

View File

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