Create ShellApp, rebase things on it

Previously, we had ShellAppInfo, which contains fundamental
information about an application, and methods on ShellAppMonitor
to retrieve "live" information like the window list.

AppIcon ended up being used as the "App" class which was painful
for various reasons; among them that we need to handle window
list changes, and some consumers weren't ready for that.

Clean things up a bit by introducing a new ShellApp class in C,
which currently wraps a ShellAppInfo.

AppIcon then is more like the display actor for a ShellApp.  Notably,
the ".windows" property moves out of it.  The altTab code which
won't handle dynamic changes instead is changed to maintain a
cached version.

ShellAppMonitor gains some more methods related to ShellApp now.

In the future, we might consider changing ShellApp to be a GInterface,
which could be implemented by ShellDesktopFileApp, ShellWindowApp.

Then we could axe ShellAppInfo from the "public" API and it would
return to being an internal loss mitigation layer for GMenu.

https://bugzilla.gnome.org/show_bug.cgi?id=598227
This commit is contained in:
Colin Walters 2009-10-11 16:40:00 -04:00
parent 6623ca1287
commit 38c06ca837
11 changed files with 611 additions and 271 deletions

View File

@ -93,7 +93,7 @@ AltTabPopup.prototype = {
// Make the initial selection // Make the initial selection
if (this._appIcons.length == 1) { if (this._appIcons.length == 1) {
if (!backward && this._appIcons[0].windows.length > 1) { if (!backward && this._appIcons[0].cachedWindows.length > 1) {
// For compatibility with the multi-app case below // For compatibility with the multi-app case below
this._select(0, 1); this._select(0, 1);
} else } else
@ -101,9 +101,10 @@ AltTabPopup.prototype = {
} else if (backward) { } else if (backward) {
this._select(this._appIcons.length - 1); this._select(this._appIcons.length - 1);
} else { } else {
if (this._appIcons[0].windows.length > 1) { let firstWindows = this._appIcons[0].cachedWindows;
let curAppNextWindow = this._appIcons[0].windows[1]; if (firstWindows.length > 1) {
let nextAppWindow = this._appIcons[1].windows[0]; let curAppNextWindow = firstWindows[1];
let nextAppWindow = this._appIcons[1].cachedWindows[0];
// If the next window of the current app is more-recently-used // If the next window of the current app is more-recently-used
// than the first window of the next app, then select it. // than the first window of the next app, then select it.
@ -139,11 +140,11 @@ AltTabPopup.prototype = {
_nextWindow : function() { _nextWindow : function() {
return mod(this._currentWindows[this._currentApp] + 1, return mod(this._currentWindows[this._currentApp] + 1,
this._appIcons[this._currentApp].windows.length); this._appIcons[this._currentApp].cachedWindows.length);
}, },
_previousWindow : function() { _previousWindow : function() {
return mod(this._currentWindows[this._currentApp] - 1, return mod(this._currentWindows[this._currentApp] - 1,
this._appIcons[this._currentApp].windows.length); this._appIcons[this._currentApp].cachedWindows.length);
}, },
_keyPressEvent : function(actor, event) { _keyPressEvent : function(actor, event) {
@ -188,7 +189,7 @@ AltTabPopup.prototype = {
}, },
_appActivated : function(appSwitcher, n) { _appActivated : function(appSwitcher, n) {
Main.activateWindow(this._appIcons[n].windows[this._currentWindows[n]]); Main.activateWindow(this._appIcons[n].cachedWindows[this._currentWindows[n]]);
this.destroy(); this.destroy();
}, },
@ -200,7 +201,7 @@ AltTabPopup.prototype = {
}, },
_windowActivated : function(thumbnailList, n) { _windowActivated : function(thumbnailList, n) {
Main.activateWindow(this._appIcons[this._currentApp].windows[n]); Main.activateWindow(this._appIcons[this._currentApp].cachedWindows[n]);
this.destroy(); this.destroy();
}, },
@ -225,7 +226,7 @@ AltTabPopup.prototype = {
_finish : function() { _finish : function() {
let app = this._appIcons[this._currentApp]; let app = this._appIcons[this._currentApp];
let window = app.windows[this._currentWindows[this._currentApp]]; let window = app.cachedWindows[this._currentWindows[this._currentApp]];
Main.activateWindow(window); Main.activateWindow(window);
this.destroy(); this.destroy();
}, },
@ -267,7 +268,7 @@ AltTabPopup.prototype = {
this._appSwitcher.showArrow(app); this._appSwitcher.showArrow(app);
} else { } else {
this._appSwitcher.highlight(app); this._appSwitcher.highlight(app);
if (this._appIcons[this._currentApp].windows.length > 1) if (this._appIcons[this._currentApp].cachedWindows.length > 1)
this._appSwitcher.showArrow(app); this._appSwitcher.showArrow(app);
} }
@ -276,7 +277,7 @@ AltTabPopup.prototype = {
this._createThumbnails(); this._createThumbnails();
this._currentWindows[this._currentApp] = window; this._currentWindows[this._currentApp] = window;
this._thumbnails.highlight(window); this._thumbnails.highlight(window);
} else if (this._appIcons[this._currentApp].windows.length > 1 && } else if (this._appIcons[this._currentApp].cachedWindows.length > 1 &&
!noTimeout) { !noTimeout) {
this._thumbnailTimeoutId = Mainloop.timeout_add ( this._thumbnailTimeoutId = Mainloop.timeout_add (
THUMBNAIL_POPUP_TIME, THUMBNAIL_POPUP_TIME,
@ -289,7 +290,7 @@ AltTabPopup.prototype = {
}, },
_createThumbnails : function() { _createThumbnails : function() {
this._thumbnails = new ThumbnailList (this._appIcons[this._currentApp].windows); this._thumbnails = new ThumbnailList (this._appIcons[this._currentApp].cachedWindows);
this._thumbnails.connect('item-activated', Lang.bind(this, this._windowActivated)); this._thumbnails.connect('item-activated', Lang.bind(this, this._windowActivated));
this._thumbnails.connect('item-hovered', Lang.bind(this, this._windowHovered)); this._thumbnails.connect('item-hovered', Lang.bind(this, this._windowHovered));
@ -552,8 +553,11 @@ AppSwitcher.prototype = {
let workspaceIcons = []; let workspaceIcons = [];
let otherIcons = []; let otherIcons = [];
for (let i = 0; i < apps.length; i++) { for (let i = 0; i < apps.length; i++) {
let appIcon = new AppIcon.AppIcon({ appInfo: apps[i], let appIcon = new AppIcon.AppIcon({ app: apps[i],
size: POPUP_APPICON_SIZE }); size: POPUP_APPICON_SIZE });
// Cache the window list now; we don't handle dynamic changes here,
// and we don't want to be continually retrieving it
appIcon.cachedWindows = appIcon.app.get_windows();
if (this._hasWindowsOnWorkspace(appIcon, activeWorkspace)) if (this._hasWindowsOnWorkspace(appIcon, activeWorkspace))
workspaceIcons.push(appIcon); workspaceIcons.push(appIcon);
else else
@ -621,35 +625,16 @@ AppSwitcher.prototype = {
}, },
_hasWindowsOnWorkspace: function(appIcon, workspace) { _hasWindowsOnWorkspace: function(appIcon, workspace) {
for (let i = 0; i < appIcon.windows.length; i++) { let windows = appIcon.cachedWindows;
if (appIcon.windows[i].get_workspace() == workspace) for (let i = 0; i < windows.length; i++) {
return true; if (windows[i].get_workspace() == workspace)
}
return false;
},
_hasVisibleWindows : function(appIcon) {
for (let i = 0; i < appIcon.windows.length; i++) {
if (appIcon.windows[i].showing_on_its_workspace())
return true; return true;
} }
return false; return false;
}, },
_sortAppIcon : function(appIcon1, appIcon2) { _sortAppIcon : function(appIcon1, appIcon2) {
let vis1 = this._hasVisibleWindows(appIcon1); return appIcon1.app.compare(appIcon2.app);
let vis2 = this._hasVisibleWindows(appIcon2);
if (vis1 && !vis2) {
return -1;
} else if (vis2 && !vis1) {
return 1;
} else {
// The app's most-recently-used window is first
// in its list
return (appIcon2.windows[0].get_user_time() -
appIcon1.windows[0].get_user_time());
}
} }
}; };

View File

@ -476,15 +476,15 @@ AppDisplay.prototype = {
Signals.addSignalMethods(AppDisplay.prototype); Signals.addSignalMethods(AppDisplay.prototype);
function BaseWellItem(appInfo, isFavorite, hasMenu) { function BaseWellItem(app, isFavorite, hasMenu) {
this._init(appInfo, isFavorite, hasMenu); this._init(app, isFavorite, hasMenu);
} }
BaseWellItem.prototype = { BaseWellItem.prototype = {
__proto__: AppIcon.AppIcon.prototype, __proto__: AppIcon.AppIcon.prototype,
_init: function(appInfo, isFavorite) { _init: function(app, isFavorite) {
AppIcon.AppIcon.prototype._init.call(this, { appInfo: appInfo, AppIcon.AppIcon.prototype._init.call(this, { app: app,
menuType: AppIcon.MenuType.ON_RIGHT, menuType: AppIcon.MenuType.ON_RIGHT,
glow: true }); glow: true });
@ -523,7 +523,7 @@ BaseWellItem.prototype = {
// as say Pidgin, but ideally what we do there is have the app // as say Pidgin, but ideally what we do there is have the app
// express to us that it doesn't do relaunch=new-window in the // express to us that it doesn't do relaunch=new-window in the
// .desktop file. // .desktop file.
this.appInfo.launch(); this.app.get_info().launch();
}, },
getDragActor: function() { getDragActor: function() {
@ -537,15 +537,15 @@ BaseWellItem.prototype = {
} }
} }
function RunningWellItem(appInfo, isFavorite) { function RunningWellItem(app, isFavorite) {
this._init(appInfo, isFavorite); this._init(app, isFavorite);
} }
RunningWellItem.prototype = { RunningWellItem.prototype = {
__proto__: BaseWellItem.prototype, __proto__: BaseWellItem.prototype,
_init: function(appInfo, isFavorite) { _init: function(app, isFavorite) {
BaseWellItem.prototype._init.call(this, appInfo, isFavorite); BaseWellItem.prototype._init.call(this, app, isFavorite);
this._dragStartX = 0; this._dragStartX = 0;
this._dragStartY = 0; this._dragStartY = 0;
@ -557,15 +557,14 @@ RunningWellItem.prototype = {
let modifiers = Shell.get_event_state(event); let modifiers = Shell.get_event_state(event);
if (modifiers & Clutter.ModifierType.CONTROL_MASK) { if (modifiers & Clutter.ModifierType.CONTROL_MASK) {
this.appInfo.launch(); this.app.get_info().launch();
} else { } else {
this.activateMostRecentWindow(); this.activateMostRecentWindow();
} }
}, },
activateMostRecentWindow: function () { activateMostRecentWindow: function () {
// The _get_windows_for_app sorts them for us let mostRecentWindow = this.app.get_windows()[0];
let mostRecentWindow = this.windows[0];
Main.overview.activateWindow(mostRecentWindow, Main.currentTime()); Main.overview.activateWindow(mostRecentWindow, Main.currentTime());
}, },
@ -582,7 +581,7 @@ RunningWellItem.prototype = {
}, },
menuPoppedUp: function() { menuPoppedUp: function() {
Main.overview.getWorkspacesForWindow(null).setApplicationWindowSelection(this.appInfo.get_id()); Main.overview.getWorkspacesForWindow(null).setApplicationWindowSelection(this.app.get_id());
}, },
menuPoppedDown: function() { menuPoppedDown: function() {
@ -593,15 +592,15 @@ RunningWellItem.prototype = {
} }
}; };
function InactiveWellItem(appInfo, isFavorite) { function InactiveWellItem(app, isFavorite) {
this._init(appInfo, isFavorite); this._init(app, isFavorite);
} }
InactiveWellItem.prototype = { InactiveWellItem.prototype = {
__proto__: BaseWellItem.prototype, __proto__: BaseWellItem.prototype,
_init : function(appInfo, isFavorite) { _init : function(app, isFavorite) {
BaseWellItem.prototype._init.call(this, appInfo, isFavorite); BaseWellItem.prototype._init.call(this, app, isFavorite);
this.actor.connect('notify::pressed', Lang.bind(this, this._onPressedChanged)); this.actor.connect('notify::pressed', Lang.bind(this, this._onPressedChanged));
this.actor.connect('activate', Lang.bind(this, this._onActivate)); this.actor.connect('activate', Lang.bind(this, this._onActivate));
@ -612,12 +611,9 @@ InactiveWellItem.prototype = {
}, },
_onActivate: function() { _onActivate: function() {
if (this.windows.length == 0) { this.app.get_info().launch();
this.appInfo.launch(); Main.overview.hide();
Main.overview.hide(); return true;
return true;
}
return false;
}, },
menuPoppedUp: function() { menuPoppedUp: function() {
@ -824,21 +820,11 @@ AppWell.prototype = {
this._redisplay(); this._redisplay();
}, },
_lookupApps: function(appIds) { _appIdListToHash: function(apps) {
let result = []; let ids = {};
for (let i = 0; i < appIds.length; i++) { for (let i = 0; i < apps.length; i++)
let id = appIds[i]; ids[apps[i].get_id()] = apps[i];
let app = this._appSystem.lookup_cached_app(id); return ids;
if (!app)
continue;
result.push(app);
}
return result;
},
_arrayValues: function(array) {
return array.reduce(function (values, id, index) {
values[id] = index; return values; }, {});
}, },
_onMappedNotify: function() { _onMappedNotify: function() {
@ -857,32 +843,29 @@ AppWell.prototype = {
this._grid.removeAll(); this._grid.removeAll();
let favoriteIds = this._appSystem.get_favorites(); let favorites = this._appMonitor.get_favorites();
let favoriteIdsHash = this._arrayValues(favoriteIds); let favoriteIds = this._appIdListToHash(favorites);
/* hardcode here pending some design about how exactly desktop contexts behave */ /* hardcode here pending some design about how exactly desktop contexts behave */
let contextId = ""; let contextId = "";
let running = this._appMonitor.get_running_apps(contextId).filter(function (e) { let running = this._appMonitor.get_running_apps(contextId);
return !(e.get_id() in favoriteIdsHash); let runningIds = this._appIdListToHash(running)
});
let favorites = this._lookupApps(favoriteIds);
let displays = [] for (let i = 0; i < favorites.length; i++) {
this._addApps(favorites, true); let app = favorites[i];
this._addApps(running, false);
this._displays = displays;
},
_addApps: function(apps, isFavorite) {
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
let windows = this._appMonitor.get_windows_for_app(app.get_id());
let display; let display;
if (windows.length > 0) if (app.get_windows().length > 0) {
display = new RunningWellItem(app, isFavorite); display = new RunningWellItem(app, true);
else } else {
display = new InactiveWellItem(app, isFavorite); display = new InactiveWellItem(app, true);
}
this._grid.actor.add_actor(display.actor);
}
for (let i = 0; i < running.length; i++) {
let app = running[i];
let display = new RunningWellItem(app, false);
this._grid.actor.add_actor(display.actor); this._grid.actor.add_actor(display.actor);
} }
}, },

View File

@ -55,9 +55,9 @@ function AppIcon(params) {
AppIcon.prototype = { AppIcon.prototype = {
_init : function(params) { _init : function(params) {
this.appInfo = params.appInfo; this.app = params.app;
if (!this.appInfo) if (!this.app)
throw new Error('AppIcon constructor requires "appInfo" param'); throw new Error('AppIcon constructor requires "app" param');
this._menuType = ('menuType' in params) ? params.menuType : MenuType.NONE; this._menuType = ('menuType' in params) ? params.menuType : MenuType.NONE;
this._iconSize = ('size' in params) ? params.size : APPICON_DEFAULT_ICON_SIZE; this._iconSize = ('size' in params) ? params.size : APPICON_DEFAULT_ICON_SIZE;
@ -70,20 +70,6 @@ AppIcon.prototype = {
reactive: true }); reactive: true });
this.actor._delegate = this; this.actor._delegate = this;
this.highlight_border_color = APPICON_DEFAULT_BORDER_COLOR; this.highlight_border_color = APPICON_DEFAULT_BORDER_COLOR;
this._signalIds = [];
// Note, we don't presently update the window list dynamically here; this actor
// gets destroyed and recreated by AppWell, and in alt-tab the whole thing is
// created each time
this.windows = Shell.AppMonitor.get_default().get_windows_for_app(this.appInfo.get_id());
for (let i = 0; i < this.windows.length; i++) {
let sigId = this.windows[i].connect('notify::user-time', Lang.bind(this, this._resortWindows));
this._signalIds.push([this.windows[i], sigId]);
}
this._resortWindows();
this.actor.connect('destroy', Lang.bind(this, this._destroy));
this.actor.connect('notify::mapped', Lang.bind(this, this._onMappedChanged));
if (this._menuType != MenuType.NONE) { if (this._menuType != MenuType.NONE) {
this.actor.connect('button-press-event', Lang.bind(this, this._updateMenuOnButtonPress)); this.actor.connect('button-press-event', Lang.bind(this, this._updateMenuOnButtonPress));
@ -99,7 +85,7 @@ AppIcon.prototype = {
y_align: Big.BoxAlignment.CENTER, y_align: Big.BoxAlignment.CENTER,
width: this._iconSize, width: this._iconSize,
height: this._iconSize }); height: this._iconSize });
this.icon = this.appInfo.create_icon_texture(this._iconSize); this.icon = this.app.create_icon_texture(this._iconSize);
iconBox.append(this.icon, Big.BoxPackFlags.NONE); iconBox.append(this.icon, Big.BoxPackFlags.NONE);
this.actor.append(iconBox, Big.BoxPackFlags.EXPAND); this.actor.append(iconBox, Big.BoxPackFlags.EXPAND);
@ -114,12 +100,13 @@ AppIcon.prototype = {
font_name: "Sans 12px", font_name: "Sans 12px",
line_alignment: Pango.Alignment.CENTER, line_alignment: Pango.Alignment.CENTER,
ellipsize: Pango.EllipsizeMode.END, ellipsize: Pango.EllipsizeMode.END,
text: this.appInfo.get_name() }); text: this.app.get_name() });
nameBox.add_actor(this._name); nameBox.add_actor(this._name);
if (showGlow) { if (showGlow) {
this._glowBox = new Big.Box({ orientation: Big.BoxOrientation.HORIZONTAL }); this._glowBox = new Big.Box({ orientation: Big.BoxOrientation.HORIZONTAL });
let glowPath = GLib.filename_to_uri(global.imagedir + 'app-well-glow.png', ''); let glowPath = GLib.filename_to_uri(global.imagedir + 'app-well-glow.png', '');
for (let i = 0; i < this.windows.length && i < 3; i++) { let windows = this.app.get_windows();
for (let i = 0; i < windows.length && i < 3; i++) {
let glow = Shell.TextureCache.get_default().load_uri_sync(Shell.TextureCachePolicy.FOREVER, let glow = Shell.TextureCache.get_default().load_uri_sync(Shell.TextureCachePolicy.FOREVER,
glowPath, -1, -1); glowPath, -1, -1);
glow.keep_aspect_ratio = false; glow.keep_aspect_ratio = false;
@ -175,53 +162,11 @@ AppIcon.prototype = {
} }
}, },
_destroy: function() {
for (let i = 0; i < this._signalIds.length; i++) {
let [obj, sigId] = this._signalIds[i];
obj.disconnect(sigId);
}
},
_onMappedChanged: function() {
let mapped = this.actor.mapped;
if (mapped && this._windowSortStale)
this._resortWindows();
},
_resortWindows: function() {
let mapped = this.actor.mapped;
if (!mapped) {
this._windowSortStale = true;
return;
}
this._windowSortStale = false;
this.windows.sort(function (a, b) {
let activeWorkspace = global.screen.get_active_workspace();
let wsA = a.get_workspace() == activeWorkspace;
let wsB = b.get_workspace() == activeWorkspace;
if (wsA && !wsB)
return -1;
else if (wsB && !wsA)
return 1;
let visA = a.showing_on_its_workspace();
let visB = b.showing_on_its_workspace();
if (visA && !visB)
return -1;
else if (visB && !visA)
return 1;
else
return b.get_user_time() - a.get_user_time();
});
},
// AppIcon itself is not a draggable, but if you want to make // AppIcon itself is not a draggable, but if you want to make
// a subclass of it draggable, you can use this method to create // a subclass of it draggable, you can use this method to create
// a drag actor // a drag actor
createDragActor: function() { createDragActor: function() {
return this.appInfo.create_icon_texture(this._iconSize); return this.app.create_icon_texture(this._iconSize);
}, },
setHighlight: function(highlight) { setHighlight: function(highlight) {
@ -421,7 +366,7 @@ AppIconMenu.prototype = {
_redisplay: function() { _redisplay: function() {
this._windowContainer.remove_all(); this._windowContainer.remove_all();
let windows = this._source.windows; let windows = this._source.app.get_windows();
this._windowContainer.show(); this._windowContainer.show();
@ -462,7 +407,7 @@ AppIconMenu.prototype = {
this._newWindowMenuItem = windows.length > 0 ? this._appendMenuItem(null, _("New Window")) : null; this._newWindowMenuItem = windows.length > 0 ? this._appendMenuItem(null, _("New Window")) : null;
let favorites = Shell.AppSystem.get_default().get_favorites(); let favorites = Shell.AppSystem.get_default().get_favorites();
let id = this._source.appInfo.get_id(); let id = this._source.app.get_id();
this._isFavorite = false; this._isFavorite = false;
for (let i = 0; i < favorites.length; i++) { for (let i = 0; i < favorites.length; i++) {
if (id == favorites[i]) { if (id == favorites[i]) {
@ -615,14 +560,14 @@ AppIconMenu.prototype = {
let metaWindow = child._window; let metaWindow = child._window;
this.emit('activate-window', metaWindow); this.emit('activate-window', metaWindow);
} else if (child == this._newWindowMenuItem) { } else if (child == this._newWindowMenuItem) {
this._source.appInfo.launch(); this._source.app.get_info().launch();
this.emit('activate-window', null); this.emit('activate-window', null);
} else if (child == this._toggleFavoriteMenuItem) { } else if (child == this._toggleFavoriteMenuItem) {
let appSys = Shell.AppSystem.get_default(); let appSys = Shell.AppSystem.get_default();
if (this._isFavorite) if (this._isFavorite)
appSys.remove_favorite(this._source.appInfo.get_id()); appSys.remove_favorite(this._source.app.get_id());
else else
appSys.add_favorite(this._source.appInfo.get_id()); appSys.add_favorite(this._source.app.get_id());
} }
this.popdown(); this.popdown();
}, },

View File

@ -148,10 +148,11 @@ AppPanelMenu.prototype = {
this._iconBox.hide(); this._iconBox.hide();
this._label.text = ''; this._label.text = '';
if (this._focusedApp != null) { if (this._focusedApp != null) {
let icon = focusedApp.create_icon_texture(PANEL_ICON_SIZE); let info = focusedApp.get_info();
let icon = info.create_icon_texture(PANEL_ICON_SIZE);
this._iconBox.append(icon, Big.BoxPackFlags.NONE); this._iconBox.append(icon, Big.BoxPackFlags.NONE);
this._iconBox.show(); this._iconBox.show();
this._label.text = focusedApp.get_name(); this._label.text = info.get_name();
} else if (this._activeSequence != null) { } else if (this._activeSequence != null) {
let icon = this._activeSequence.create_icon(PANEL_ICON_SIZE); let icon = this._activeSequence.create_icon(PANEL_ICON_SIZE);
this._iconBox.append(icon, Big.BoxPackFlags.NONE); this._iconBox.append(icon, Big.BoxPackFlags.NONE);

View File

@ -1227,7 +1227,7 @@ Workspace.prototype = {
_createWindowIcon: function(window) { _createWindowIcon: function(window) {
let appSys = Shell.AppSystem.get_default(); let appSys = Shell.AppSystem.get_default();
let appMon = Shell.AppMonitor.get_default() let appMon = Shell.AppMonitor.get_default()
let appInfo = appMon.get_window_app(window.metaWindow); let appInfo = appMon.get_window_app(window.metaWindow).get_info();
let iconTexture = null; let iconTexture = null;
// The design is application based, so prefer the application // The design is application based, so prefer the application
// icon here if we have it. FIXME - should move this fallback code // icon here if we have it. FIXME - should move this fallback code

View File

@ -54,6 +54,8 @@ CLEANFILES += $(SHELL_STAMP_FILES)
libgnome_shell_la_SOURCES = \ libgnome_shell_la_SOURCES = \
$(shell_built_sources) \ $(shell_built_sources) \
gnome-shell-plugin.c \ gnome-shell-plugin.c \
shell-app.c \
shell-app.h \
shell-app-monitor.c \ shell-app-monitor.c \
shell-app-monitor.h \ shell-app-monitor.h \
shell-app-system.c \ shell-app-system.c \

View File

@ -129,9 +129,12 @@ struct _ShellAppMonitor
long watch_start_time; long watch_start_time;
MetaWindow *watched_window; MetaWindow *watched_window;
/* <MetaWindow * window, ShellAppInfo *app> */ /* <MetaWindow * window, ShellApp *app> */
GHashTable *window_to_app; GHashTable *window_to_app;
/* <const char *, ShellApp *app> */
GHashTable *running_apps;
/* <char *context, GHashTable<char *appid, AppUsage *usage>> */ /* <char *context, GHashTable<char *appid, AppUsage *usage>> */
GHashTable *app_usages_for_context; GHashTable *app_usages_for_context;
}; };
@ -214,35 +217,35 @@ static void shell_app_monitor_class_init(ShellAppMonitorClass *klass)
G_SIGNAL_RUN_LAST, G_SIGNAL_RUN_LAST,
0, 0,
NULL, NULL, NULL, NULL,
_shell_marshal_VOID__BOXED, g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, G_TYPE_NONE, 1,
SHELL_TYPE_APP_INFO); SHELL_TYPE_APP);
signals[APP_REMOVED] = g_signal_new ("app-removed", signals[APP_REMOVED] = g_signal_new ("app-removed",
SHELL_TYPE_APP_MONITOR, SHELL_TYPE_APP_MONITOR,
G_SIGNAL_RUN_LAST, G_SIGNAL_RUN_LAST,
0, 0,
NULL, NULL, NULL, NULL,
_shell_marshal_VOID__BOXED, g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, G_TYPE_NONE, 1,
SHELL_TYPE_APP_INFO); SHELL_TYPE_APP);
signals[WINDOW_ADDED] = g_signal_new ("window-added", signals[WINDOW_ADDED] = g_signal_new ("window-added",
SHELL_TYPE_APP_MONITOR, SHELL_TYPE_APP_MONITOR,
G_SIGNAL_RUN_LAST, G_SIGNAL_RUN_LAST,
0, 0,
NULL, NULL, NULL, NULL,
_shell_marshal_VOID__BOXED_OBJECT, _shell_marshal_VOID__OBJECT_OBJECT,
G_TYPE_NONE, 2, G_TYPE_NONE, 2,
SHELL_TYPE_APP_INFO, SHELL_TYPE_APP,
META_TYPE_WINDOW); META_TYPE_WINDOW);
signals[WINDOW_REMOVED] = g_signal_new ("window-removed", signals[WINDOW_REMOVED] = g_signal_new ("window-removed",
SHELL_TYPE_APP_MONITOR, SHELL_TYPE_APP_MONITOR,
G_SIGNAL_RUN_LAST, G_SIGNAL_RUN_LAST,
0, 0,
NULL, NULL, NULL, NULL,
_shell_marshal_VOID__BOXED_OBJECT, _shell_marshal_VOID__OBJECT_OBJECT,
G_TYPE_NONE, 2, G_TYPE_NONE, 2,
SHELL_TYPE_APP_INFO, SHELL_TYPE_APP,
META_TYPE_WINDOW); META_TYPE_WINDOW);
signals[STARTUP_SEQUENCE_CHANGED] = g_signal_new ("startup-sequence-changed", signals[STARTUP_SEQUENCE_CHANGED] = g_signal_new ("startup-sequence-changed",
@ -396,12 +399,6 @@ shell_app_monitor_is_window_usage_tracked (MetaWindow *window)
return TRUE; return TRUE;
} }
static ShellAppInfo *
create_transient_app_for_window (MetaWindow *window)
{
return shell_app_system_create_from_window (shell_app_system_get_default (), window);
}
/** /**
* get_app_for_window_direct: * get_app_for_window_direct:
* *
@ -409,12 +406,13 @@ create_transient_app_for_window (MetaWindow *window)
* an application based on WM_CLASS. If that fails, then * an application based on WM_CLASS. If that fails, then
* a "transient" application is created. * a "transient" application is created.
* *
* Return value: (transfer full): A newly-referenced #ShellAppInfo * Return value: (transfer full): A newly-referenced #ShellApp
*/ */
static ShellAppInfo * static ShellApp *
get_app_for_window_direct (MetaWindow *window) get_app_for_window_direct (MetaWindow *window)
{ {
ShellAppInfo *result; ShellApp *app;
ShellAppInfo *appinfo;
ShellAppSystem *appsys; ShellAppSystem *appsys;
char *wmclass; char *wmclass;
char *with_desktop; char *with_desktop;
@ -422,26 +420,28 @@ get_app_for_window_direct (MetaWindow *window)
wmclass = get_cleaned_wmclass_for_window (window); wmclass = get_cleaned_wmclass_for_window (window);
if (!wmclass) if (!wmclass)
return create_transient_app_for_window (window); return _shell_app_new_for_window (window);
with_desktop = g_strjoin (NULL, wmclass, ".desktop", NULL); with_desktop = g_strjoin (NULL, wmclass, ".desktop", NULL);
g_free (wmclass); g_free (wmclass);
appsys = shell_app_system_get_default (); appsys = shell_app_system_get_default ();
result = shell_app_system_lookup_heuristic_basename (appsys, with_desktop); appinfo = shell_app_system_lookup_heuristic_basename (appsys, with_desktop);
g_free (with_desktop); g_free (with_desktop);
if (result == NULL) if (appinfo == NULL)
{ {
const char *id = get_app_id_from_title (window); const char *id = get_app_id_from_title (window);
if (id != NULL) if (id != NULL)
result = shell_app_system_load_from_desktop_file (appsys, id, NULL); appinfo = shell_app_system_load_from_desktop_file (appsys, id, NULL);
} }
if (result == NULL) if (appinfo == NULL)
result = create_transient_app_for_window (window); return _shell_app_new_for_window (window);
return result; app = _shell_app_new (appinfo);
shell_app_info_unref (appinfo);
return app;
} }
/** /**
@ -451,11 +451,11 @@ get_app_for_window_direct (MetaWindow *window)
* all available information such as the window's MetaGroup, * all available information such as the window's MetaGroup,
* and what we know about other windows. * and what we know about other windows.
*/ */
static ShellAppInfo * static ShellApp *
get_app_for_window (ShellAppMonitor *monitor, get_app_for_window (ShellAppMonitor *monitor,
MetaWindow *window) MetaWindow *window)
{ {
ShellAppInfo *result; ShellApp *result;
MetaWindow *source_window; MetaWindow *source_window;
GSList *group_windows; GSList *group_windows;
MetaGroup *group; MetaGroup *group;
@ -489,7 +489,7 @@ get_app_for_window (ShellAppMonitor *monitor,
if (result != NULL) if (result != NULL)
{ {
shell_app_info_ref (result); g_object_ref (result);
return result; return result;
} }
@ -502,6 +502,12 @@ get_window_context (MetaWindow *window)
return ""; return "";
} }
static const char *
get_app_context (ShellApp *app)
{
return "";
}
static GHashTable * static GHashTable *
get_usages_for_context (ShellAppMonitor *monitor, get_usages_for_context (ShellAppMonitor *monitor,
const char *context) const char *context)
@ -543,7 +549,7 @@ static AppUsage *
get_app_usage_from_window (ShellAppMonitor *monitor, get_app_usage_from_window (ShellAppMonitor *monitor,
MetaWindow *window) MetaWindow *window)
{ {
ShellAppInfo *app; ShellApp *app;
const char *context; const char *context;
app = g_hash_table_lookup (monitor->window_to_app, window); app = g_hash_table_lookup (monitor->window_to_app, window);
@ -552,7 +558,7 @@ get_app_usage_from_window (ShellAppMonitor *monitor,
context = get_window_context (window); context = get_window_context (window);
return get_app_usage_for_context_and_id (monitor, context, shell_app_info_get_id (app)); return get_app_usage_for_context_and_id (monitor, context, shell_app_get_id (app));
} }
static MetaWindow * static MetaWindow *
@ -714,14 +720,9 @@ on_transient_window_title_changed (MetaWindow *window,
ShellAppMonitor *self) ShellAppMonitor *self)
{ {
ShellAppSystem *appsys; ShellAppSystem *appsys;
ShellAppInfo *current_app;
ShellAppInfo *new_app; ShellAppInfo *new_app;
const char *id; const char *id;
current_app = g_hash_table_lookup (self->window_to_app, window);
/* Can't have lost the app */
g_assert (current_app != NULL);
/* Check if we now have a mapping using the window title */ /* Check if we now have a mapping using the window title */
id = get_app_id_from_title (window); id = get_app_id_from_title (window);
if (id == NULL) if (id == NULL)
@ -742,7 +743,7 @@ static void
track_window (ShellAppMonitor *self, track_window (ShellAppMonitor *self,
MetaWindow *window) MetaWindow *window)
{ {
ShellAppInfo *app; ShellApp *app;
AppUsage *usage; AppUsage *usage;
if (!window_is_tracked (window)) if (!window_is_tracked (window))
@ -764,7 +765,7 @@ track_window (ShellAppMonitor *self,
return; return;
usage = get_app_usage_from_window (self, window); usage = get_app_usage_from_window (self, window);
usage->transient = shell_app_info_is_transient (app); usage->transient = shell_app_info_is_transient (shell_app_get_info (app));
if (usage->transient) if (usage->transient)
{ {
@ -776,6 +777,8 @@ track_window (ShellAppMonitor *self,
g_signal_connect (window, "notify::title", G_CALLBACK (on_transient_window_title_changed), self); g_signal_connect (window, "notify::title", G_CALLBACK (on_transient_window_title_changed), self);
} }
_shell_app_add_window (app, window);
/* Keep track of the number of windows open for this app, when it /* Keep track of the number of windows open for this app, when it
* switches between 0 and 1 we emit an app-added signal. * switches between 0 and 1 we emit an app-added signal.
*/ */
@ -784,7 +787,12 @@ track_window (ShellAppMonitor *self,
usage->initially_seen_sequence = ++self->initially_seen_sequence; usage->initially_seen_sequence = ++self->initially_seen_sequence;
usage->last_seen = get_time (); usage->last_seen = get_time ();
if (usage->window_count == 1) if (usage->window_count == 1)
g_signal_emit (self, signals[APP_ADDED], 0, app); {
/* key is owned by the app */
g_hash_table_insert (self->running_apps, (char*)shell_app_get_id (app),
app);
g_signal_emit (self, signals[APP_ADDED], 0, app);
}
/* Emit window-added after app-added */ /* Emit window-added after app-added */
g_signal_emit (self, signals[WINDOW_ADDED], 0, app, window); g_signal_emit (self, signals[WINDOW_ADDED], 0, app, window);
@ -800,18 +808,17 @@ shell_app_monitor_on_window_added (MetaWorkspace *workspace,
track_window (self, window); track_window (self, window);
} }
static void static void
disassociate_window (ShellAppMonitor *self, disassociate_window (ShellAppMonitor *self,
MetaWindow *window) MetaWindow *window)
{ {
ShellAppInfo *app; ShellApp *app;
app = g_hash_table_lookup (self->window_to_app, window); app = g_hash_table_lookup (self->window_to_app, window);
if (!app) if (!app)
return; return;
shell_app_info_ref (app); g_object_ref (app);
if (window == self->watched_window) if (window == self->watched_window)
self->watched_window = NULL; self->watched_window = NULL;
@ -827,18 +834,22 @@ disassociate_window (ShellAppMonitor *self,
g_hash_table_remove (self->window_to_app, window); g_hash_table_remove (self->window_to_app, window);
_shell_app_remove_window (app, window);
g_signal_emit (self, signals[WINDOW_REMOVED], 0, app, window); g_signal_emit (self, signals[WINDOW_REMOVED], 0, app, window);
if (usage->window_count == 0) if (usage->window_count == 0)
{ {
const char *id = shell_app_get_id (app);
g_hash_table_remove (self->running_apps, id);
g_signal_emit (self, signals[APP_REMOVED], 0, app); g_signal_emit (self, signals[APP_REMOVED], 0, app);
reset_usage (self, context, shell_app_info_get_id (app), usage); reset_usage (self, context, id, usage);
} }
} }
else else
g_hash_table_remove (self->window_to_app, window); g_hash_table_remove (self->window_to_app, window);
shell_app_info_unref (app); g_object_unref (app);
} }
static void static void
@ -931,8 +942,8 @@ shell_app_monitor_get_windows_for_app (ShellAppMonitor *self,
while (g_hash_table_iter_next (&iter, &key, &value)) while (g_hash_table_iter_next (&iter, &key, &value))
{ {
MetaWindow *window = key; MetaWindow *window = key;
ShellAppInfo *app = value; ShellApp *app = value;
const char *id = shell_app_info_get_id (app); const char *id = shell_app_get_id (app);
if (!shell_app_monitor_is_window_usage_tracked (window)) if (!shell_app_monitor_is_window_usage_tracked (window))
continue; continue;
@ -1033,7 +1044,9 @@ shell_app_monitor_init (ShellAppMonitor *self)
(GDestroyNotify) g_hash_table_destroy); (GDestroyNotify) g_hash_table_destroy);
self->window_to_app = g_hash_table_new_full (g_direct_hash, g_direct_equal, self->window_to_app = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify) shell_app_info_unref); NULL, (GDestroyNotify) g_object_unref);
self->running_apps = g_hash_table_new (g_str_hash, g_str_equal);
g_object_get (shell_global_get(), "configdir", &shell_config_dir, NULL), g_object_get (shell_global_get(), "configdir", &shell_config_dir, NULL),
path = g_build_filename (shell_config_dir, DATA_FILENAME, NULL); path = g_build_filename (shell_config_dir, DATA_FILENAME, NULL);
@ -1068,6 +1081,7 @@ shell_app_monitor_finalize (GObject *object)
gconf_client_notify_remove (self->gconf_client, self->gconf_notify); gconf_client_notify_remove (self->gconf_client, self->gconf_notify);
g_object_unref (self->gconf_client); g_object_unref (self->gconf_client);
g_object_unref (self->display); g_object_unref (self->display);
g_hash_table_destroy (self->running_apps);
g_hash_table_destroy (self->app_usages_for_context); g_hash_table_destroy (self->app_usages_for_context);
for (i = 0; title_patterns[i].app_id; i++) for (i = 0; title_patterns[i].app_id; i++)
g_regex_unref (title_patterns[i].regex); g_regex_unref (title_patterns[i].regex);
@ -1108,48 +1122,22 @@ shell_app_monitor_get_most_used_apps (ShellAppMonitor *monitor,
* *
* Returns: Application associated with window * Returns: Application associated with window
*/ */
ShellAppInfo * ShellApp *
shell_app_monitor_get_window_app (ShellAppMonitor *monitor, shell_app_monitor_get_window_app (ShellAppMonitor *monitor,
MetaWindow *metawin) MetaWindow *metawin)
{ {
MetaWindow *transient_for; MetaWindow *transient_for;
ShellAppInfo *info; ShellApp *app;
transient_for = meta_window_get_transient_for (metawin); transient_for = meta_window_get_transient_for (metawin);
if (transient_for != NULL) if (transient_for != NULL)
metawin = transient_for; metawin = transient_for;
info = g_hash_table_lookup (monitor->window_to_app, metawin); app = g_hash_table_lookup (monitor->window_to_app, metawin);
if (info) if (app)
shell_app_info_ref (info); g_object_ref (app);
return info;
}
typedef struct { return app;
ShellAppMonitor *self;
const char *context_id;
} AppOpenSequenceSortData;
static int
sort_apps_by_open_sequence (gconstpointer a,
gconstpointer b,
gpointer datap)
{
AppOpenSequenceSortData *data = datap;
ShellAppInfo *app_a = (ShellAppInfo*)a;
ShellAppInfo *app_b = (ShellAppInfo*)b;
const char *id_a = shell_app_info_get_id (app_a);
const char *id_b = shell_app_info_get_id (app_b);
AppUsage *usage_a;
AppUsage *usage_b;
usage_a = get_app_usage_for_context_and_id (data->self, data->context_id, id_a);
usage_b = get_app_usage_for_context_and_id (data->self, data->context_id, id_b);
if (usage_a->initially_seen_sequence == usage_b->initially_seen_sequence)
return 0;
if (usage_a->initially_seen_sequence < usage_b->initially_seen_sequence)
return -1;
return 1;
} }
/** /**
@ -1158,50 +1146,94 @@ sort_apps_by_open_sequence (gconstpointer a,
* @context: Activity identifier * @context: Activity identifier
* *
* Returns the set of applications which currently have at least one open * Returns the set of applications which currently have at least one open
* window in the given context. * window in the given context. The returned list will be sorted
* by shell_app_compare().
* *
* Returns: (element-type ShellAppInfo) (transfer container): Active applications * Returns: (element-type ShellApp) (transfer container): Active applications
*/ */
GSList * GSList *
shell_app_monitor_get_running_apps (ShellAppMonitor *monitor, shell_app_monitor_get_running_apps (ShellAppMonitor *monitor,
const char *context) const char *context)
{ {
GHashTableIter iter;
gpointer key, value; gpointer key, value;
GSList *ret; GSList *ret;
AppOpenSequenceSortData data; GHashTableIter iter;
GHashTable *unique_apps;
unique_apps = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_iter_init (&iter, monitor->running_apps);
g_hash_table_iter_init (&iter, monitor->window_to_app);
ret = NULL; ret = NULL;
while (g_hash_table_iter_next (&iter, &key, &value)) while (g_hash_table_iter_next (&iter, &key, &value))
{ {
MetaWindow *window = key; ShellApp *app = value;
ShellAppInfo *app = value;
const char *id;
if (strcmp (get_window_context (window), context) != 0) if (strcmp (context, get_app_context (app)) != 0)
continue; continue;
if (!shell_app_monitor_is_window_usage_tracked (window))
continue;
id = shell_app_info_get_id (app);
if (g_hash_table_lookup (unique_apps, id))
continue;
g_hash_table_insert (unique_apps, (gpointer)id, (gpointer)id);
ret = g_slist_prepend (ret, app); ret = g_slist_prepend (ret, app);
} }
g_hash_table_destroy (unique_apps);
data.self = monitor; ret = g_slist_sort (ret, (GCompareFunc)shell_app_compare);
data.context_id = context;
return g_slist_sort_with_data (ret, sort_apps_by_open_sequence, &data); return ret;
}
/**
* shell_app_monitor_get_app:
* @montior:
* @id: Application identifier
*
* For running applications, returns the existing instance
* of the running application model object. Otherwise,
* returns a new object.
*
* Returns: (transfer full): Application associated with id
*/
ShellApp *
shell_app_monitor_get_app (ShellAppMonitor *monitor,
const char *id)
{
ShellApp *app;
ShellAppInfo *info;
app = g_hash_table_lookup (monitor->running_apps, id);
if (app)
return g_object_ref (app);
info = shell_app_system_lookup_cached_app (shell_app_system_get_default(), id);
if (!info)
return NULL;
app = _shell_app_new (info);
shell_app_info_unref (info);
return app;
}
/**
* shell_app_monitor_get_favorites:
* @monitor:
*
* Returns: (transfer full) (element-type ShellApp): List of favorite applications
*/
GSList *
shell_app_monitor_get_favorites (ShellAppMonitor *monitor)
{
GSList *apps = NULL;
GList *favorite_ids, *iter;
favorite_ids = shell_app_system_get_favorites (shell_app_system_get_default ());
for (iter = favorite_ids; iter; iter = iter->next)
{
const char *id = iter->data;
ShellApp *app;
app = shell_app_monitor_get_app (monitor, id);
if (app)
apps = g_slist_prepend (apps, g_object_ref (app));
}
apps = g_slist_reverse (apps);
return apps;
} }
static gboolean static gboolean

View File

@ -6,9 +6,10 @@
#include <glib.h> #include <glib.h>
#include "window.h" #include "window.h"
#include "shell-app.h"
#include "shell-app-system.h" #include "shell-app-system.h"
/* /*
* This object provides monitoring of system application directories (.desktop files) * This object provides monitoring of system application directories (.desktop files)
* and activity-based statistics about applications usage * and activity-based statistics about applications usage
*/ */
@ -29,14 +30,17 @@ typedef struct _ShellAppMonitorPrivate ShellAppMonitorPrivate;
struct _ShellAppMonitorClass struct _ShellAppMonitorClass
{ {
GObjectClass parent_class; GObjectClass parent_class;
}; };
GType shell_app_monitor_get_type (void) G_GNUC_CONST; GType shell_app_monitor_get_type (void) G_GNUC_CONST;
ShellAppMonitor* shell_app_monitor_get_default(void); ShellAppMonitor* shell_app_monitor_get_default(void);
ShellAppInfo *shell_app_monitor_get_window_app (ShellAppMonitor *monitor, MetaWindow *metawin); ShellApp *shell_app_monitor_get_window_app (ShellAppMonitor *monitor, MetaWindow *metawin);
ShellApp *shell_app_monitor_get_app (ShellAppMonitor *monitor, const char *id);
GSList *shell_app_monitor_get_favorites (ShellAppMonitor *monitor);
GList *shell_app_monitor_get_most_used_apps (ShellAppMonitor *monitor, GList *shell_app_monitor_get_most_used_apps (ShellAppMonitor *monitor,
const char *context, const char *context,

331
src/shell-app.c Normal file
View File

@ -0,0 +1,331 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
#include "shell-app.h"
#include "shell-global.h"
/**
* SECTION:shell-app
* @short_description: Object representing an application
*
* This object wraps a #ShellAppInfo, providing methods and signals
* primarily useful for running applications.
*/
struct _ShellApp
{
GObject parent;
ShellAppInfo *info;
guint workspace_switch_id;
gboolean window_sort_stale;
GSList *windows;
};
G_DEFINE_TYPE (ShellApp, shell_app, G_TYPE_OBJECT);
enum {
WINDOWS_CHANGED,
LAST_SIGNAL
};
static guint shell_app_signals[LAST_SIGNAL] = { 0 };
const char *
shell_app_get_id (ShellApp *app)
{
return shell_app_info_get_id (app->info);
}
/**
* shell_app_create_icon_texture:
*
* Look up the icon for this application, and create a #ClutterTexture
* for it at the given size.
*
* Return value: (transfer none): A floating #ClutterActor
*/
ClutterActor *
shell_app_create_icon_texture (ShellApp *app,
float size)
{
return shell_app_info_create_icon_texture (app->info, size);
}
char *
shell_app_get_name (ShellApp *app)
{
return shell_app_info_get_name (app->info);
}
char *
shell_app_get_description (ShellApp *app)
{
return shell_app_info_get_description (app->info);
}
/**
* shell_app_get_info:
*
* Returns: (transfer none): Associated app info
*/
ShellAppInfo *
shell_app_get_info (ShellApp *app)
{
return app->info;
}
typedef struct {
ShellApp *app;
MetaWorkspace *active_workspace;
} CompareWindowsData;
static int
shell_app_compare_windows (gconstpointer a,
gconstpointer b,
gpointer datap)
{
MetaWindow *win_a = (gpointer)a;
MetaWindow *win_b = (gpointer)b;
CompareWindowsData *data = datap;
gboolean ws_a, ws_b;
gboolean vis_a, vis_b;
ws_a = meta_window_get_workspace (win_a) == data->active_workspace;
ws_b = meta_window_get_workspace (win_b) == data->active_workspace;
if (ws_a && !ws_b)
return -1;
else if (!ws_a && ws_b)
return 1;
vis_a = meta_window_showing_on_its_workspace (win_a);
vis_b = meta_window_showing_on_its_workspace (win_b);
if (vis_a && !vis_b)
return -1;
else if (!vis_a && vis_b)
return 1;
return meta_window_get_user_time (win_b) - meta_window_get_user_time (win_a);
}
/**
* shell_app_get_windows:
* @app:
*
* Get the toplevel, interesting windows which are associated with this
* application. The returned list will be sorted first by whether
* they're on the active workspace, then by whether they're visible,
* and finally by the time the user last interacted with them.
*
* Returns: (transfer none) (element-type MetaWindow): List of windows
*/
GSList *
shell_app_get_windows (ShellApp *app)
{
if (app->window_sort_stale)
{
CompareWindowsData data;
data.app = app;
data.active_workspace = meta_screen_get_active_workspace (shell_global_get_screen (shell_global_get ()));
app->windows = g_slist_sort_with_data (app->windows, shell_app_compare_windows, &data);
app->window_sort_stale = FALSE;
}
return app->windows;
}
static gboolean
shell_app_has_visible_windows (ShellApp *app)
{
GSList *iter;
for (iter = app->windows; iter; iter = iter->next)
{
MetaWindow *window = iter->data;
if (!meta_window_showing_on_its_workspace (window))
return FALSE;
}
return TRUE;
}
gboolean
shell_app_is_on_workspace (ShellApp *app,
MetaWorkspace *workspace)
{
GSList *iter;
for (iter = app->windows; iter; iter = iter->next)
{
if (meta_window_get_workspace (iter->data) == workspace)
return TRUE;
}
return FALSE;
}
/**
* shell_app_compare:
* @app:
* @other: A #ShellApp
*
* Compare one #ShellApp instance to another, in the following way:
* - If one of them has visible windows and the other does not, the one
* with visible windows is first.
* - If one has no windows at all (i.e. it's not running) and the other
* does, the one with windows is first.
* - Finally, the application which the user interacted with most recently
* compares earlier.
*/
int
shell_app_compare (ShellApp *app,
ShellApp *other)
{
gboolean vis_app, vis_other;
GSList *windows_app, *windows_other;
vis_app = shell_app_has_visible_windows (app);
vis_other = shell_app_has_visible_windows (other);
if (vis_app && !vis_other)
return -1;
else if (!vis_app && vis_other)
return 1;
if (app->windows && !other->windows)
return -1;
else if (!app->windows && other->windows)
return 1;
windows_app = shell_app_get_windows (app);
windows_other = shell_app_get_windows (other);
return meta_window_get_user_time (windows_other->data) - meta_window_get_user_time (windows_app->data);
}
ShellApp *
_shell_app_new_for_window (MetaWindow *window)
{
ShellApp *app;
app = g_object_new (SHELL_TYPE_APP, NULL);
app->info = shell_app_system_create_from_window (shell_app_system_get_default (), window);
_shell_app_add_window (app, window);
return app;
}
ShellApp *
_shell_app_new (ShellAppInfo *info)
{
ShellApp *app;
app = g_object_new (SHELL_TYPE_APP, NULL);
app->info = shell_app_info_ref (info);
return app;
}
static void
shell_app_on_unmanaged (MetaWindow *window,
ShellApp *app)
{
_shell_app_remove_window (app, window);
}
static void
shell_app_on_ws_switch (ShellApp *self)
{
self->window_sort_stale = TRUE;
g_signal_emit (self, shell_app_signals[WINDOWS_CHANGED], 0);
}
void
_shell_app_add_window (ShellApp *app,
MetaWindow *window)
{
if (g_slist_find (app->windows, window))
return;
app->windows = g_slist_prepend (app->windows, g_object_ref (window));
g_signal_connect (window, "unmanaged", G_CALLBACK(shell_app_on_unmanaged), app);
app->window_sort_stale = TRUE;
g_signal_emit (app, shell_app_signals[WINDOWS_CHANGED], 0);
if (app->workspace_switch_id == 0)
{
MetaScreen *screen = shell_global_get_screen (shell_global_get ());
app->workspace_switch_id =
g_signal_connect (screen, "workspace-switched", G_CALLBACK(shell_app_on_ws_switch), app);
}
}
static void
disconnect_workspace_switch (ShellApp *app)
{
MetaScreen *screen;
if (app->workspace_switch_id == 0)
return;
screen = shell_global_get_screen (shell_global_get ());
g_signal_handler_disconnect (screen, app->workspace_switch_id);
app->workspace_switch_id = 0;
}
void
_shell_app_remove_window (ShellApp *app,
MetaWindow *window)
{
g_object_unref (window);
app->windows = g_slist_remove (app->windows, window);
if (app->windows == NULL)
disconnect_workspace_switch (app);
}
static void
shell_app_init (ShellApp *self)
{
}
static void
shell_app_dispose (GObject *object)
{
ShellApp *app = SHELL_APP (object);
if (app->info)
{
shell_app_info_unref (app->info);
app->info = NULL;
}
if (app->windows)
{
g_slist_foreach (app->windows, (GFunc) g_object_unref, NULL);
g_slist_free (app->windows);
app->windows = NULL;
}
disconnect_workspace_switch (app);
}
static void
shell_app_class_init(ShellAppClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = shell_app_dispose;
shell_app_signals[WINDOWS_CHANGED] = g_signal_new ("windows-changed",
SHELL_TYPE_APP,
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}

56
src/shell-app.h Normal file
View File

@ -0,0 +1,56 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
#ifndef __SHELL_APP_H__
#define __SHELL_APP_H__
#include <glib-object.h>
#include <glib.h>
#include "window.h"
#include "shell-app-system.h"
G_BEGIN_DECLS
typedef struct _ShellApp ShellApp;
typedef struct _ShellAppClass ShellAppClass;
typedef struct _ShellAppPrivate ShellAppPrivate;
#define SHELL_TYPE_APP (shell_app_get_type ())
#define SHELL_APP(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), SHELL_TYPE_APP, ShellApp))
#define SHELL_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SHELL_TYPE_APP, ShellAppClass))
#define SHELL_IS_APP(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), SHELL_TYPE_APP))
#define SHELL_IS_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SHELL_TYPE_APP))
#define SHELL_APP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SHELL_TYPE_APP, ShellAppClass))
struct _ShellAppClass
{
GObjectClass parent_class;
};
GType shell_app_get_type (void) G_GNUC_CONST;
const char *shell_app_get_id (ShellApp *app);
ClutterActor *shell_app_create_icon_texture (ShellApp *app, float size);
char *shell_app_get_name (ShellApp *app);
char *shell_app_get_description (ShellApp *app);
ShellAppInfo *shell_app_get_info (ShellApp *app);
GSList *shell_app_get_windows (ShellApp *app);
gboolean shell_app_is_on_workspace (ShellApp *app, MetaWorkspace *workspace);
int shell_app_compare (ShellApp *app, ShellApp *other);
ShellApp* _shell_app_new_for_window (MetaWindow *window);
ShellApp* _shell_app_new (ShellAppInfo *appinfo);
void _shell_app_add_window (ShellApp *app, MetaWindow *window);
void _shell_app_remove_window (ShellApp *app, MetaWindow *window);
G_END_DECLS
#endif /* __SHELL_APP_H__ */

View File

@ -2,4 +2,5 @@ VOID:INT,INT,INT
VOID:OBJECT,INT,INT,INT,INT VOID:OBJECT,INT,INT,INT,INT
VOID:BOXED VOID:BOXED
VOID:BOXED,OBJECT VOID:BOXED,OBJECT
VOID:OBJECT,OBJECT
VOID:STRING,OBJECT,BOOLEAN VOID:STRING,OBJECT,BOOLEAN