[gnome-shell/wip/message-tray: 15/17] popupMenu: Port to GrabHelper



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

    popupMenu: Port to GrabHelper

 js/ui/popupMenu.js |  195 ++++++----------------------------------------------
 1 files changed, 21 insertions(+), 174 deletions(-)
---
diff --git a/js/ui/popupMenu.js b/js/ui/popupMenu.js
index 10253b8..005843c 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;
@@ -1185,7 +1186,6 @@ const PopupMenu = new Lang.Class({
         this.actor = this._boxPointer.actor;
         this.actor._delegate = this;
         this.actor.style_class = 'popup-menu-boxpointer';
-        this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
 
         this._boxWrapper = new Shell.GenericContainer();
         this._boxWrapper.connect('get-preferred-width', Lang.bind(this, this._boxGetPreferredWidth));
@@ -1215,15 +1215,6 @@ const PopupMenu = new Lang.Class({
         this.box.allocate(box, flags);
     },
 
-    _onKeyPressEvent: function(actor, event) {
-        if (event.get_key_symbol() == Clutter.Escape) {
-            this.close(BoxPointer.PopupAnimation.FULL);
-            return true;
-        }
-
-        return false;
-    },
-
     setArrowOrigin: function(origin) {
         this._boxPointer.setArrowOrigin(origin);
     },
@@ -1512,7 +1503,6 @@ const PopupComboMenu = new Lang.Class({
 
         this.actor = this.box;
         this.actor._delegate = this;
-        this.actor.connect('key-press-event', Lang.bind(this, this._onKeyPressEvent));
         this.actor.connect('key-focus-in', Lang.bind(this, this._onKeyFocusIn));
         sourceActor.connect('style-changed',
                             Lang.bind(this, this._onSourceActorStyleChanged));
@@ -1520,15 +1510,6 @@ const PopupComboMenu = new Lang.Class({
         global.focus_manager.add_group(this.actor);
     },
 
-    _onKeyPressEvent: function(actor, event) {
-        if (event.get_key_symbol() == Clutter.Escape) {
-            this.close(BoxPointer.PopupAnimation.FULL);
-            return true;
-        }
-
-        return false;
-    },
-
     _onKeyFocusIn: function(actor) {
         let items = this._getMenuItems();
         let activeItem = items[this._activeItemPos];
@@ -2019,17 +2000,12 @@ const PopupMenuManager = new Lang.Class({
 
     _init: function(owner) {
         this._owner = owner;
-        this.grabbed = false;
+        this._grabHelper = new GrabHelper.GrabHelper(owner.actor);
+        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 +2019,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,79 +2054,24 @@ 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) {
         if (open) {
             if (this._activeMenu && this._activeMenu.isChildMenu(menu)) {
                 this._menuStack.push(this._activeMenu);
-                menu.actor.grab_key_focus();
             }
+
             this._activeMenu = menu;
+            this._grabHelper.grab({ actor: menu.actor, focus: false, modal: true });
         } else {
-            if (this._menuStack.length > 0) {
-                this._activeMenu = this._menuStack.pop();
-                if (menu.sourceActor)
-                    menu.sourceActor.grab_key_focus();
-                this._didPop = true;
-            }
-        }
-
-        // Check what the focus was before calling pushModal/popModal
-        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();
-            }
-
-            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;
-
-            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._activeMenu = this._menuStack.pop() || null;
+            this._grabHelper.ungrab({ actor: menu.actor });
         }
     },
 
@@ -2158,24 +2083,17 @@ const PopupMenuManager = new Lang.Class({
         this.removeMenu(childMenu);
     },
 
-    // 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 +2109,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 +2119,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 +2128,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]