[gnome-shell/wip/pressure: 10/14] popupMenu: Port to GrabHelper



commit 34fda0475265635beab3a982fbcb3450e41ee8f1
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Wed Feb 29 19:09:41 2012 -0500

    popupMenu: Port to GrabHelper

 js/ui/popupMenu.js |  159 +++++++++-------------------------------------------
 1 files changed, 26 insertions(+), 133 deletions(-)
---
diff --git a/js/ui/popupMenu.js b/js/ui/popupMenu.js
index 10253b8..04888db 100644
--- a/js/ui/popupMenu.js
+++ b/js/ui/popupMenu.js
@@ -12,6 +12,7 @@ const St = imports.gi.St;
 const Atk = imports.gi.Atk;
 
 const BoxPointer = imports.ui.boxpointer;
+const GrabHelper = imports.ui.grabHelper;
 const Main = imports.ui.main;
 const Params = imports.misc.params;
 const Tweener = imports.ui.tweener;
@@ -2019,17 +2020,12 @@ const PopupMenuManager = new Lang.Class({
 
     _init: function(owner) {
         this._owner = owner;
-        this.grabbed = false;
+        this._grabHelper = new GrabHelper.GrabHelper(owner.actor, { modal: true });
+        this._grabHelper.connect('ungrabbed', Lang.bind(this, this._closeMenu));
 
-        this._eventCaptureId = 0;
-        this._enterEventId = 0;
-        this._leaveEventId = 0;
-        this._keyFocusNotifyId = 0;
         this._activeMenu = null;
         this._menus = [];
         this._menuStack = [];
-        this._preGrabInputMode = null;
-        this._grabbedFromKeynav = false;
     },
 
     addMenu: function(menu, position) {
@@ -2043,8 +2039,12 @@ const PopupMenuManager = new Lang.Class({
             focusInId:         0
         };
 
+        this._grabHelper.addActor(menu.actor);
+
         let source = menu.sourceActor;
         if (source) {
+            if (!menu.blockSourceEvents)
+                this._grabHelper.addActor(source);
             menudata.enterId = source.connect('enter-event', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
             menudata.focusInId = source.connect('key-focus-in', Lang.bind(this, function() { this._onMenuSourceEnter(menu); }));
         }
@@ -2074,33 +2074,11 @@ const PopupMenuManager = new Lang.Class({
         if (menudata.focusInId)
             menu.sourceActor.disconnect(menudata.focusInId);
 
-        this._menus.splice(position, 1);
-    },
-
-    _grab: function() {
-        Main.pushModal(this._owner.actor);
-
-        this._eventCaptureId = global.stage.connect('captured-event', Lang.bind(this, this._onEventCapture));
-        // captured-event doesn't see enter/leave events
-        this._enterEventId = global.stage.connect('enter-event', Lang.bind(this, this._onEventCapture));
-        this._leaveEventId = global.stage.connect('leave-event', Lang.bind(this, this._onEventCapture));
-        this._keyFocusNotifyId = global.stage.connect('notify::key-focus', Lang.bind(this, this._onKeyFocusChanged));
-
-        this.grabbed = true;
-    },
-
-    _ungrab: function() {
-        global.stage.disconnect(this._eventCaptureId);
-        this._eventCaptureId = 0;
-        global.stage.disconnect(this._enterEventId);
-        this._enterEventId = 0;
-        global.stage.disconnect(this._leaveEventId);
-        this._leaveEventId = 0;
-        global.stage.disconnect(this._keyFocusNotifyId);
-        this._keyFocusNotifyId = 0;
+        this._grabHelper.removeActor(menu.actor);
+        if (menu.sourceActor)
+            this._grabHelper.removeActor(menu.sourceActor);
 
-        this.grabbed = false;
-        Main.popModal(this._owner.actor);
+        this._menus.splice(position, 1);
     },
 
     _onMenuOpenState: function(menu, open) {
@@ -2119,34 +2097,26 @@ const PopupMenuManager = new Lang.Class({
             }
         }
 
-        // Check what the focus was before calling pushModal/popModal
+        // Check what the focus was before calling grab/ungrab
         let focus = global.stage.key_focus;
         let hadFocus = focus && this._activeMenuContains(focus);
 
         if (open) {
-            if (!this.grabbed) {
-                this._preGrabInputMode = global.stage_input_mode;
-                this._grabbedFromKeynav = hadFocus;
-                this._grab();
-            }
+            this._grabHelper.grab();
 
             if (hadFocus)
                 focus.grab_key_focus();
             else
                 menu.actor.grab_key_focus();
         } else if (menu == this._activeMenu) {
-            if (this.grabbed)
-                this._ungrab();
-            this._activeMenu = null;
+            let newFocus = null;
+            if (hadFocus && menu.sourceActor)
+                newFocus = menu.sourceActor;
+            else if (focus)
+                newFocus = focus;
 
-            if (this._grabbedFromKeynav) {
-                if (this._preGrabInputMode == Shell.StageInputMode.FOCUSED)
-                    global.stage_input_mode = Shell.StageInputMode.FOCUSED;
-                if (hadFocus && menu.sourceActor)
-                    menu.sourceActor.grab_key_focus();
-                else if (focus)
-                    focus.grab_key_focus();
-            }
+            this._grabHelper.ungrab(newFocus);
+            this._activeMenu = null;
         }
     },
 
@@ -2161,21 +2131,15 @@ const PopupMenuManager = new Lang.Class({
     // change the currently-open menu without dropping grab
     _changeMenu: function(newMenu) {
         if (this._activeMenu) {
-            // _onOpenMenuState will drop the grab if it sees
-            // this._activeMenu being closed; so clear _activeMenu
-            // before closing it to keep that from happening
-            let oldMenu = this._activeMenu;
-            this._activeMenu = null;
-            for (let i = this._menuStack.length - 1; i >= 0; i--)
-                this._menuStack[i].close(BoxPointer.PopupAnimation.FADE);
-            oldMenu.close(BoxPointer.PopupAnimation.FADE);
-            newMenu.open(BoxPointer.PopupAnimation.FADE);
-        } else
-            newMenu.open(BoxPointer.PopupAnimation.FULL);
+            this._closeMenu();
+            newMenu.open(false);
+        } else {
+            newMenu.open(true);
+        }
     },
 
     _onMenuSourceEnter: function(menu) {
-        if (!this.grabbed || menu == this._activeMenu)
+        if (!this._grabHelper.grabbed || menu == this._activeMenu)
             return false;
 
         if (this._activeMenu && this._activeMenu.isChildMenu(menu))
@@ -2191,24 +2155,6 @@ const PopupMenuManager = new Lang.Class({
         return false;
     },
 
-    _onKeyFocusChanged: function() {
-        if (!this.grabbed || !this._activeMenu)
-            return;
-
-        let focus = global.stage.key_focus;
-        if (focus) {
-            if (this._activeMenuContains(focus))
-                return;
-            if (this._menuStack.length > 0)
-                return;
-            if (focus._delegate && focus._delegate.menu &&
-                this._findMenu(focus._delegate.menu) != -1)
-                return;
-        }
-
-        this._closeMenu();
-    },
-
     _onMenuDestroy: function(menu) {
         this.removeMenu(menu);
     },
@@ -2219,26 +2165,6 @@ const PopupMenuManager = new Lang.Class({
                     (this._activeMenu.sourceActor && this._activeMenu.sourceActor.contains(actor)));
     },
 
-    _eventIsOnActiveMenu: function(event) {
-        return this._activeMenuContains(event.get_source());
-    },
-
-    _shouldBlockEvent: function(event) {
-        let src = event.get_source();
-
-        if (this._activeMenu != null && this._activeMenu.actor.contains(src))
-            return false;
-
-        for (let i = 0; i < this._menus.length; i++) {
-            let menu = this._menus[i].menu;
-            if (menu.sourceActor && !menu.blockSourceEvents && menu.sourceActor.contains(src)) {
-                return false;
-            }
-        }
-
-        return true;
-    },
-
     _findMenu: function(item) {
         for (let i = 0; i < this._menus.length; i++) {
             let menudata = this._menus[i];
@@ -2248,39 +2174,6 @@ const PopupMenuManager = new Lang.Class({
         return -1;
     },
 
-    _onEventCapture: function(actor, event) {
-        if (!this.grabbed)
-            return false;
-
-        if (this._owner.menuEventFilter &&
-            this._owner.menuEventFilter(event))
-            return true;
-
-        if (this._didPop) {
-            this._didPop = false;
-            return true;
-        }
-
-        let activeMenuContains = this._eventIsOnActiveMenu(event);
-        let eventType = event.type();
-
-        if (eventType == Clutter.EventType.BUTTON_RELEASE) {
-            if (activeMenuContains) {
-                return false;
-            } else {
-                this._closeMenu();
-                return true;
-            }
-        } else if (eventType == Clutter.EventType.BUTTON_PRESS && !activeMenuContains) {
-            this._closeMenu();
-            return true;
-        } else if (!this._shouldBlockEvent(event)) {
-            return false;
-        }
-
-        return true;
-    },
-
     _closeMenu: function() {
         if (this._activeMenu != null)
             this._activeMenu.close(BoxPointer.PopupAnimation.FULL);



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