| 1 | # Copyright (C) 2012 One Laptop Per Child |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or modify |
| 4 | # it under the terms of the GNU General Public License as published by |
| 5 | # the Free Software Foundation; either version 2 of the License, or |
| 6 | # (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public License |
| 14 | # along with this program; if not, write to the Free Software |
| 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | |
| 17 | from gi.repository import Gdk |
| 18 | |
| 19 | from gi.repository import SugarExt |
| 20 | from gi.repository import SugarGestures |
| 21 | |
| 22 | from sugar3.graphics import style |
| 23 | |
| 24 | _instance = None |
| 25 | |
| 26 | |
| 27 | class GestureHandler(object): |
| 28 | '''Handling gestures to show/hide the frame |
| 29 | |
| 30 | We use SugarExt.GestureGrabber to listen for |
| 31 | gestures on the root window. Swiping from |
| 32 | the frame area towards the center does reveal |
| 33 | the Frame. Swiping from the center and ending |
| 34 | the swipe on the Frame area does hide the |
| 35 | Frame. |
| 36 | |
| 37 | ''' |
| 38 | def __init__(self, frame): |
| 39 | self._frame = frame |
| 40 | |
| 41 | self._gesture_grabber = SugarExt.GestureGrabber() |
| 42 | |
| 43 | # top, bottom, left, right |
| 44 | self._create_grabber(0, 0, Gdk.Screen.width(), style.GRID_CELL_SIZE, |
| 45 | SugarGestures.SwipeDirection.DOWN) |
| 46 | self._create_grabber(0, Gdk.Screen.height() - style.GRID_CELL_SIZE, |
| 47 | Gdk.Screen.width(), style.GRID_CELL_SIZE, |
| 48 | SugarGestures.SwipeDirection.UP) |
| 49 | self._create_grabber(0, 0, style.GRID_CELL_SIZE, Gdk.Screen.height(), |
| 50 | SugarGestures.SwipeDirection.RIGHT) |
| 51 | self._create_grabber(Gdk.Screen.width() - style.GRID_CELL_SIZE, |
| 52 | 0, style.GRID_CELL_SIZE, Gdk.Screen.height(), |
| 53 | SugarGestures.SwipeDirection.LEFT) |
| 54 | |
| 55 | def _create_grabber(self, x, y, width, height, desired_direction): |
| 56 | rect = Gdk.Rectangle() |
| 57 | rect.x = x |
| 58 | rect.y = y |
| 59 | rect.width = width |
| 60 | rect.height = height |
| 61 | |
| 62 | swipe = SugarGestures.SwipeController() |
| 63 | swipe.connect('swipe-ended', self.__swipe_ended_cb, |
| 64 | desired_direction) |
| 65 | self._gesture_grabber.add(swipe, rect) |
| 66 | |
| 67 | def __swipe_ended_cb(self, controller, event_direction, desired_direction): |
| 68 | if event_direction == desired_direction: |
| 69 | self._frame.show() |
| 70 | |
| 71 | |
| 72 | def setup(frame): |
| 73 | global _instance |
| 74 | |
| 75 | if _instance: |
| 76 | del _instance |
| 77 | |
| 78 | _instance = GestureHandler(frame) |