Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/frame/Frame.py
blob: 92437fefd253fcc341248907b4418f92444e2540 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Copyright (C) 2006, 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
import wnck

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.PanelWindow import PanelWindow
from view.frame.clipboardpanelwindow import ClipboardPanelWindow
from view.frame.notificationtray import NotificationTray
from model.ShellModel import ShellModel
from sugar.graphics.timeline import Timeline
from sugar.graphics.grid import Grid
from sugar.graphics.menushell import MenuShell

class EventFrame(gobject.GObject):
    __gsignals__ = {
        'enter-edge':    (gobject.SIGNAL_RUN_FIRST,
                          gobject.TYPE_NONE, ([])),
        'enter-corner':  (gobject.SIGNAL_RUN_FIRST,
                          gobject.TYPE_NONE, ([])),
        'leave':         (gobject.SIGNAL_RUN_FIRST,
                          gobject.TYPE_NONE, ([]))
    }

    HOVER_NONE = 0
    HOVER_CORNER = 1
    HOVER_EDGE = 2

    def __init__(self):
        gobject.GObject.__init__(self)

        self._windows = []
        self._hover = EventFrame.HOVER_NONE
        self._active = False

        invisible = self._create_invisible(0, 0, gtk.gdk.screen_width(), 6)
        self._windows.append(invisible)

        invisible = self._create_invisible(0, 0, 6, gtk.gdk.screen_height())
        self._windows.append(invisible)

        invisible = self._create_invisible(gtk.gdk.screen_width() - 6, 0,
                                           gtk.gdk.screen_width(),
                                           gtk.gdk.screen_height())
        self._windows.append(invisible)

        invisible = self._create_invisible(0, gtk.gdk.screen_height() - 6,
                                           gtk.gdk.screen_width(),
                                           gtk.gdk.screen_height())
        self._windows.append(invisible)

        screen = wnck.screen_get_default()
        screen.connect('active-window-changed',
                       self._active_window_changed_cb)

    def _create_invisible(self, x, y, width, height):
        invisible = gtk.Invisible()
        invisible.connect('motion-notify-event', self._motion_notify_cb)
        invisible.connect('enter-notify-event', self._enter_notify_cb)
        invisible.connect('leave-notify-event', self._leave_notify_cb)
        
        invisible.drag_dest_set(0, [], 0)
        invisible.connect('drag_motion', self._drag_motion_cb)
        invisible.connect('drag_leave', self._drag_leave_cb)

        invisible.realize()
        invisible.window.set_events(gtk.gdk.POINTER_MOTION_MASK |
                                    gtk.gdk.ENTER_NOTIFY_MASK |
                                    gtk.gdk.LEAVE_NOTIFY_MASK)
        invisible.window.move_resize(x, y, width, height)

        return invisible

    def _enter_notify_cb(self, widget, event):
        self._notify_enter(event.x, event.y)
        logging.debug('EventFrame._enter_notify_cb ' + str(self._hover))

    def _motion_notify_cb(self, widget, event):
        self._notify_enter(event.x, event.y)
        logging.debug('EventFrame._motion_notify_cb ' + str(self._hover))
        
    def _drag_motion_cb(self, widget, drag_context, x, y, timestamp):
        drag_context.drag_status(0, timestamp);
        self._notify_enter(x, y)
        logging.debug('EventFrame._drag_motion_cb ' + str(self._hover))
        return True

    def _notify_enter(self, x, y):
        screen_w = gtk.gdk.screen_width()
        screen_h = gtk.gdk.screen_height()

        if (x == 0 and y == 0) or \
           (x == 0 and y == screen_h - 1) or \
           (x == screen_w - 1 and y == 0) or \
           (x == screen_w - 1 and y == screen_h - 1):
            if self._hover != EventFrame.HOVER_CORNER:
                self._hover = EventFrame.HOVER_CORNER
                self.emit('enter-corner')
        else:
            if self._hover != EventFrame.HOVER_EDGE:
                self._hover = EventFrame.HOVER_EDGE
                self.emit('enter-edge')

    def _leave_notify_cb(self, widget, event):
        self._notify_leave()
        logging.debug('EventFrame._leave_notify_cb ' + str(self._hover))
        
    def _drag_leave_cb(self, widget, drag_context, timestamp):
        self._notify_leave()
        logging.debug('EventFrame._drag_leave_cb ' + str(self._hover))
        return True
        
    def _notify_leave(self):
        self._hover = EventFrame.HOVER_NONE
        if self._active:
            self.emit('leave')

    def show(self):
        self._active = True
        for window in self._windows:
            window.show()

    def hide(self):
        self._active = False
        for window in self._windows:
            window.hide()

    def _active_window_changed_cb(self, screen):
        for window in self._windows:
            window.window.raise_()

class Frame:
    INACTIVE = 0
    TEMPORARY = 1
    STICKY = 2
    HIDE_ON_LEAVE = 3
    AUTOMATIC = 4

    def __init__(self, shell):
        self._windows = []
        self._hover_frame = False
        self._shell = shell
        self._mode = Frame.INACTIVE

        self._timeline = Timeline(self)
        self._timeline.add_tag('slide_in', 18, 24)
        self._timeline.add_tag('before_slide_out', 48, 48)
        self._timeline.add_tag('slide_out', 49, 54)

        self._event_frame = EventFrame()
        self._event_frame.connect('enter-edge', self._enter_edge_cb)
        self._event_frame.connect('enter-corner', self._enter_corner_cb)
        self._event_frame.connect('leave', self._event_frame_leave_cb)
        self._event_frame.show()

        grid = Grid()

        # Top panel
        panel = self._create_panel(grid, 0, 0, 16, 1)
        menu_shell = panel.get_menu_shell()
        root = panel.get_root()

        menu_shell.set_position(MenuShell.BOTTOM)

        box = ZoomBox(self._shell, menu_shell)

        [x, y] = grid.point(1, 0)
        root.append(box, hippo.PACK_FIXED)
        root.move(box, x, y)

        tray = NotificationTray()
        tray_box = hippo.CanvasBox(box_width=grid.dimension(1),
                                   box_height=grid.dimension(1),
                                   xalign=hippo.ALIGNMENT_END)

        tray_widget = hippo.CanvasWidget()
        tray_widget.props.widget = tray
        tray_box.append(tray_widget, gtk.EXPAND)

        [x, y] = grid.point(13, 0)
        root.append(tray_box, hippo.PACK_FIXED)
        root.move(tray_box, x, y)

        box = OverlayBox(self._shell)

        [x, y] = grid.point(14, 0)
        root.append(box, hippo.PACK_FIXED)
        root.move(box, x, y)

        # Bottom panel
        panel = self._create_panel(grid, 0, 11, 16, 1)
        menu_shell = panel.get_menu_shell()
        root = panel.get_root()

        menu_shell.set_position(MenuShell.TOP)

        box = ActivitiesBox(self._shell)
        root.append(box, hippo.PACK_FIXED)

        [x, y] = grid.point(1, 0)
        root.move(box, x, y)

        # Right panel
        panel = self._create_panel(grid, 15, 1, 1, 10)
        menu_shell = panel.get_menu_shell()
        root = panel.get_root()

        menu_shell.set_position(MenuShell.LEFT)

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

        # Left panel
        panel = self._create_clipboard_panel(grid, 0, 1, 1, 10)

        shell.get_model().connect('notify::state',
                                  self._shell_state_changed_cb)

    def _shell_state_changed_cb(self, model, pspec):
        if model.props.state == ShellModel.STATE_SHUTDOWN:
            self._timeline.goto('slide_out', True)
        
    def _create_clipboard_panel(self, grid, x, y, width, height):
        [x, y, width, height] = grid.rectangle(x, y, width, height)
        panel = ClipboardPanelWindow(self, x, y, width, height)

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

        self._windows.append(panel)

        return panel

    def _create_panel(self, grid, x, y, width, height):
        [x, y, width, height] = grid.rectangle(x, y, width, height)
        panel = PanelWindow(x, y, width, height)
        self._connect_to_panel(panel)
        self._windows.append(panel)

        return panel

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

        self._menu_shell = panel.get_menu_shell()
        self._menu_shell.connect('activated',
                                 self._menu_shell_activated_cb)
        self._menu_shell.connect('deactivated',
                                 self._menu_shell_deactivated_cb)

    def _menu_shell_activated_cb(self, menu_shell):
        self._timeline.goto('slide_in', True)

    def _menu_shell_deactivated_cb(self, menu_shell):
        if self._mode != Frame.STICKY and not self._hover_frame:
            self._timeline.play('before_slide_out', 'slide_out')

    def _enter_notify_cb(self, window, event):
        self._enter_notify()
        logging.debug('Frame._enter_notify_cb ' + str(self._mode))
        
    def _drag_motion_cb(self, window, context, x, y, time):
        self._enter_notify()
        logging.debug('Frame._drag_motion_cb ' + str(self._mode))
        return True
        
    def _drag_leave_cb(self, window, drag_context, timestamp):
        self._leave_notify()
        logging.debug('Frame._drag_leave_cb ' + str(self._mode))
            
    def _leave_notify_cb(self, window, event):
        # FIXME for some reason every click cause also a leave-notify
        if event.state == gtk.gdk.BUTTON1_MASK:
            return

        self._leave_notify()
        logging.debug('Frame._leave_notify_cb ' + str(self._mode))

    def _enter_notify(self):
        self._hover_frame = True
        self._timeline.goto('slide_in', True) 
               
    def _leave_notify(self):
        self._hover_frame = False
        if not self._menu_shell.is_active() and \
           (self._mode == Frame.HIDE_ON_LEAVE or \
            self._mode == Frame.AUTOMATIC):
            self._timeline.play('before_slide_out', 'slide_out')

    def _enter_edge_cb(self, event_frame):
        self._mode = Frame.HIDE_ON_LEAVE
        self._timeline.play(None, 'slide_in')
        logging.debug('Frame._enter_edge_cb ' + str(self._mode))
        
    def _enter_corner_cb(self, event_frame):
        self._mode = Frame.HIDE_ON_LEAVE
        self._timeline.play('slide_in', 'slide_in')
        logging.debug('Frame._enter_corner_cb ' + str(self._mode))
        
    def _event_frame_leave_cb(self, event_frame):
        if self._mode != Frame.STICKY:
            self._timeline.goto('slide_out', True)
        logging.debug('Frame._event_frame_leave_cb ' + str(self._mode))
        
    def show_and_hide(self, seconds):
        self._mode = Frame.AUTOMATIC
        self._timeline.play()

    def notify_key_press(self):
        if self._timeline.on_tag('slide_in'):
            self._timeline.play('before_slide_out', 'slide_out')
        elif self._timeline.on_tag('before_slide_out'):
            self._mode = Frame.TEMPORARY
        else:
            self._mode = Frame.STICKY
            self._timeline.play('slide_in', 'slide_in')

    def notify_key_release(self):
        if self._mode == Frame.TEMPORARY:
            self._timeline.play('before_slide_out', 'slide_out')

    def do_slide_in(self, current=0, n_frames=0):
        if not self._windows[0].props.visible:
            for panel in self._windows:
                panel.show()
            self._event_frame.hide()

    def do_slide_out(self, current=0, n_frames=0):
        if self._windows[0].props.visible:
            for panel in self._windows:
                panel.hide()
            self._event_frame.show()

    def is_visible(self):
        if self._windows[0].props.visible:
            return True
        return False