Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/frame/frame.py
blob: 2b215767b158b6ecac678163cea544e388f17def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Copyright (C) 2006-2007 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging

import gtk
import gobject
import hippo

from view.frame.eventarea import EventArea
from view.frame.ActivitiesBox import ActivitiesBox
from view.frame.ZoomBox import ZoomBox
from view.frame.overlaybox import OverlayBox
from view.frame.FriendsBox import FriendsBox
from view.frame.framewindow import FrameWindow
from view.frame.clipboardpanelwindow import ClipboardPanelWindow
from model.shellmodel import ShellModel
from sugar.graphics import animator
from sugar.graphics import units
from sugar.clipboard import clipboardservice

MODE_NONE     = 0
MODE_MOUSE    = 1
MODE_KEYBOARD = 2
MODE_FORCE    = 3

_FRAME_HIDING_DELAY = 500

class _Animation(animator.Animation):
    def __init__(self, frame, end):
        start = frame.get_current_position()
        animator.Animation.__init__(self, start, end)
        self._frame = frame

    def next_frame(self, current):
        self._frame.move(current)

class _MouseListener(object):
    def __init__(self, frame):
        self._frame = frame
        self._hide_sid = 0

    def mouse_enter(self):
        if self._frame.mode == MODE_NONE or \
           self._frame.mode == MODE_MOUSE:
            self._show_frame()

    def mouse_leave(self):
        if self._frame.mode == MODE_MOUSE:
            self._hide_frame()

    def _show_frame(self):
        if self._hide_sid != 0:
            gobject.source_remove(self._hide_sid)
        self._frame.show()
        self._frame.mode = MODE_MOUSE

    def _hide_frame_timeout_cb(self):
        self._frame.hide()
        return False

    def _hide_frame(self):
        if self._hide_sid != 0:
            gobject.source_remove(self._hide_sid)
        self._hide_sid = gobject.timeout_add(
                  _FRAME_HIDING_DELAY, self._hide_frame_timeout_cb)

class _KeyListener(object):
    _HIDDEN = 1
    _SHOWN_PRESSED = 2
    _SHOWN_REPEAT = 3
    _SHOWN_RELEASED = 4

    def __init__(self, frame):
        self._frame = frame
        self._state = _KeyListener._HIDDEN

    def key_press(self):
        if self._frame.mode != MODE_NONE and \
           self._frame.mode != MODE_KEYBOARD:
            return

        if self._frame.visible:
            self._frame.hide()
        else:
            self._frame.show()
            self._frame.mode = MODE_KEYBOARD

        """
        if self._state == _KeyListener._HIDDEN:
            self._frame.show()
            self._frame.mode = MODE_KEYBOARD
            self._state = _KeyListener._SHOWN_PRESSED
        elif self._state == _KeyListener._SHOWN_PRESSED:
            self._state = _KeyListener._SHOWN_REPEAT
        elif self._state == _KeyListener._SHOWN_RELEASED:
            self._frame.hide()
            self._state = _KeyListener._HIDDEN
        """

    def key_release(self):
        pass
        """
        if self._state == _KeyListener._SHOWN_PRESSED:
            self._state = _KeyListener._SHOWN_RELEASED
        elif self._state == _KeyListener._SHOWN_REPEAT:
            self._frame.hide()
            self._state = _KeyListener._HIDDEN
        """

class Frame(object):
    def __init__(self, shell):
        self.mode = MODE_NONE
        self.visible = False

        self._left_panel = None
        self._right_panel = None
        self._top_panel = None
        self._bottom_panel = None

        self._shell = shell
        self._current_position = 0.0
        self._animator = None
        self._hover = False

        self._event_area = EventArea()
        self._event_area.connect('enter', self._enter_corner_cb)
        self._event_area.show()

        self._top_panel = self._create_top_panel()
        self._bottom_panel = self._create_bottom_panel()
        self._left_panel = self._create_left_panel()
        self._right_panel = self._create_right_panel()

        screen = gtk.gdk.screen_get_default()
        screen.connect('size-changed', self._size_changed_cb)

        cb_service = clipboardservice.get_instance()
        cb_service.connect_after('object-added', self._clipboard_object_added_cb)

        self._key_listener = _KeyListener(self)
        self._mouse_listener = _MouseListener(self)

    def hide(self, force=False):
        if not self.visible:
            return
        if self._animator:
            self._animator.stop()

        self._animator = animator.Animator(0.5)
        self._animator.add(_Animation(self, 0.0))
        self._animator.start()

        self._event_area.show()

        self.visible = False
        if force:
            self.mode = MODE_NONE
        else:
            self.mode = MODE_FORCE
            self._animator.connect('completed', self._hide_completed_cb)

    def show(self):
        self.mode = MODE_FORCE

        if self.visible:
            return
        if self._animator:
            self._animator.stop()

        self._animator = animator.Animator(0.5)
        self._animator.add(_Animation(self, 1.0))
        self._animator.start()

        self._event_area.hide()

        self.visible = True

    def get_current_position(self):
        return self._current_position

    def move(self, pos):
        self._current_position = pos
        self._update_position()

    def _is_hover(self):
        return (self._top_panel.hover or \
                self._bottom_panel.hover or \
                self._left_panel.hover or \
                self._right_panel.hover)

    def _create_top_panel(self):
        panel = self._create_panel(hippo.ORIENTATION_HORIZONTAL)
        root = panel.get_root()

        box = ZoomBox(self._shell)
        root.append(box)

        #box = OverlayBox(self._shell)
        #root.append(box, hippo.PACK_END)

        return panel

    def _create_bottom_panel(self):
        panel = self._create_panel(hippo.ORIENTATION_HORIZONTAL)
        root = panel.get_root()

        box = ActivitiesBox(self._shell)
        root.append(box)

        return panel

    def _create_right_panel(self):
        panel = self._create_panel(hippo.ORIENTATION_VERTICAL)
        root = panel.get_root()

        box = FriendsBox(self._shell)
        root.append(box)

        return panel

    def _create_left_panel(self):
        panel = ClipboardPanelWindow(self, hippo.ORIENTATION_VERTICAL)

        self._connect_to_panel(panel)
        panel.connect('drag-motion', self._drag_motion_cb)
        panel.connect('drag-leave', self._drag_leave_cb)

        return panel

    def _create_panel(self, orientation):
        panel = FrameWindow(orientation)
        self._connect_to_panel(panel)

        return panel

    def _move_panel(self, panel, pos, x1, y1, x2, y2):
        x = (x2 - x1) * pos + x1
        y = (y2 - y1) * pos + y1

        panel.move(int(x), int(y))

        # FIXME we should hide and show as necessary to free memory
        if not panel.props.visible:
            panel.show()

    def _connect_to_panel(self, panel):
        panel.connect('enter-notify-event', self._enter_notify_cb)
        panel.connect('leave-notify-event', self._leave_notify_cb)

    def _update_position(self):
        screen_h = gtk.gdk.screen_height()
        screen_w = gtk.gdk.screen_width()

        self._move_panel(self._top_panel, self._current_position,
                         0, units.grid_to_pixels(-1),
                         0, 0)

        self._move_panel(self._bottom_panel, self._current_position,
                         0, screen_h,
                         0, screen_h - units.grid_to_pixels(1))

        self._move_panel(self._left_panel, self._current_position,
                         units.grid_to_pixels(-1), 0,
                         0, 0)

        self._move_panel(self._right_panel, self._current_position,
                         screen_w, 0,
                         screen_w - units.grid_to_pixels(1), 0)

    def _hide_completed_cb(self, animator):
        self.mode = MODE_NONE

    def _size_changed_cb(self, screen):
       self._update_position()

    def _clipboard_object_added_cb(self, cb_service, object_id, name):
        if not self.visible:
            self.show()
            gobject.timeout_add(2000, lambda: self.hide())

    def _enter_notify_cb(self, window, event):
        # FIXME clicks cause leave/notify, ignore
        if event.state == gtk.gdk.BUTTON1_MASK:
            return
        if self._hover:
            return

        self._hover = True
        self._mouse_listener.mouse_enter()

    def _leave_notify_cb(self, window, event):
        # FIXME clicks cause leave/notify, ignore
        if event.state == gtk.gdk.BUTTON1_MASK:
            return
        if not self._hover:
            return

        if not self._is_hover():
            self._hover = False
            self._mouse_listener.mouse_leave()

    def _drag_motion_cb(self, window, context, x, y, time):
        self._mouse_listener.mouse_enter()
        
    def _drag_leave_cb(self, window, drag_context, timestamp):
        self._mouse_listener.mouse_leave()
            
    def _enter_corner_cb(self, event_area):
        self._mouse_listener.mouse_enter()
        
    def notify_key_press(self):
        self._key_listener.key_press()

    def notify_key_release(self):
        self._key_listener.key_release()