[gnome-shell] dash: Refactor some common code



commit 147a6e49dcf20053fb3e8d94d475ec783dd3c436
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Oct 2 21:13:47 2012 -0300

    dash: Refactor some common code
    
    Take the code that gets an app from a "source" that's duplicated
    in lots of spots and refactor it out.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=685313

 js/ui/dash.js |   45 +++++++++++++++++++--------------------------
 1 files changed, 19 insertions(+), 26 deletions(-)
---
diff --git a/js/ui/dash.js b/js/ui/dash.js
index 4367807..f2ccc78 100644
--- a/js/ui/dash.js
+++ b/js/ui/dash.js
@@ -21,6 +21,18 @@ const DASH_ITEM_LABEL_SHOW_TIME = 0.15;
 const DASH_ITEM_LABEL_HIDE_TIME = 0.1;
 const DASH_ITEM_HOVER_TIMEOUT = 300;
 
+function getAppFromSource(source) {
+    if (source instanceof AppDisplay.AppWellIcon) {
+        let appSystem = Shell.AppSystem.get_default();
+        return appSystem.lookup_app(source.getId());
+    } else if (source.metaWindow) {
+        let tracker = Shell.WindowTracker.get_default();
+        return tracker.get_window_app(source.metaWindow);
+    } else {
+        return null;
+    }
+}
+
 // A container like StBin, but taking the child's scale into account
 // when requesting a size
 const DashItemContainer = new Lang.Class({
@@ -279,14 +291,9 @@ const ShowAppsIcon = new Lang.Class({
     },
 
     acceptDrop: function(source, actor, x, y, time) {
-        let app = null;
-        if (source instanceof AppDisplay.AppWellIcon) {
-            let appSystem = Shell.AppSystem.get_default();
-            app = appSystem.lookup_app(source.getId());
-        } else if (source.metaWindow) {
-            let tracker = Shell.WindowTracker.get_default();
-            app = tracker.get_window_app(source.metaWindow);
-        }
+        let app = getAppFromSource(source);
+        if (app == null)
+            return false;
 
         let id = app.get_id();
 
@@ -382,7 +389,6 @@ const Dash = new Lang.Class({
 
         this._workId = Main.initializeDeferredWork(this._box, Lang.bind(this, this._redisplay));
 
-        this._tracker = Shell.WindowTracker.get_default();
         this._appSystem = Shell.AppSystem.get_default();
 
         this._appSystem.connect('installed-changed', Lang.bind(this, this._queueRedisplay));
@@ -429,12 +435,8 @@ const Dash = new Lang.Class({
     },
 
     _onDragMotion: function(dragEvent) {
-        let app = null;
-        if (dragEvent.source instanceof AppDisplay.AppWellIcon)
-            app = this._appSystem.lookup_app(dragEvent.source.getId());
-        else if (dragEvent.source.metaWindow)
-            app = this._tracker.get_window_app(dragEvent.source.metaWindow);
-        else
+        let app = getAppFromSource(dragEvent.source);
+        if (app == null)
             return DND.DragMotionResult.CONTINUE;
 
         let id = app.get_id();
@@ -762,11 +764,7 @@ const Dash = new Lang.Class({
     },
 
     handleDragOver : function(source, actor, x, y, time) {
-        let app = null;
-        if (source instanceof AppDisplay.AppWellIcon)
-            app = this._appSystem.lookup_app(source.getId());
-        else if (source.metaWindow)
-            app = this._tracker.get_window_app(source.metaWindow);
+        let app = getAppFromSource(source);
 
         // Don't allow favoriting of transient apps
         if (app == null || app.is_window_backed())
@@ -847,12 +845,7 @@ const Dash = new Lang.Class({
 
     // Draggable target interface
     acceptDrop : function(source, actor, x, y, time) {
-        let app = null;
-        if (source instanceof AppDisplay.AppWellIcon) {
-            app = this._appSystem.lookup_app(source.getId());
-        } else if (source.metaWindow) {
-            app = this._tracker.get_window_app(source.metaWindow);
-        }
+        let app = getAppFromSource(source);
 
         // Don't allow favoriting of transient apps
         if (app == null || app.is_window_backed()) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]