Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/frame/frame.py
blob: 659df1929e1ae3288b8432f03ae37bf76e5b7311 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# 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 os

from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject

from sugar3.graphics import animator
from sugar3.graphics import style
from sugar3.graphics import palettegroup
from sugar3 import profile

from jarabe.frame.eventarea import EventArea
from jarabe.frame.activitiestray import ActivitiesTray
from jarabe.frame.zoomtoolbar import ZoomToolbar
from jarabe.frame.friendstray import FriendsTray
from jarabe.frame.devicestray import DevicesTray
from jarabe.frame.framewindow import FrameWindow
from jarabe.frame.clipboardpanelwindow import ClipboardPanelWindow
from jarabe.frame.notification import NotificationIcon, NotificationWindow
from jarabe.frame.notification import NotificationButton, HistoryPalette
from jarabe.model import notifications


TOP_RIGHT = 0
TOP_LEFT = 1
BOTTOM_RIGHT = 2
BOTTOM_LEFT = 3

_NOTIFICATION_DURATION = 5000

_DEFAULT_ICON = 'emblem-notification'


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

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


class _KeyListener(object):
    def __init__(self, frame):
        self._frame = frame

    def key_press(self):
        if self._frame.visible:
            self._frame.hide()
        else:
            self._frame.show()


class Frame(object):
    def __init__(self):
        logging.debug('STARTUP: Loading the frame')

        self._palette_group = palettegroup.get_group('frame')

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

        self.current_position = 0.0
        self._animator = None

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

        self._activities_tray = None
        self._devices_tray = None
        self._friends_tray = None

        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 = Gdk.Screen.get_default()
        screen.connect('size-changed', self._size_changed_cb)

        self._key_listener = _KeyListener(self)

        self._notif_by_icon = {}
        self._notif_by_message = {}

        notification_service = notifications.get_service()
        notification_service.notification_received.connect(
                self.__notification_received_cb)
        notification_service.notification_cancelled.connect(
                self.__notification_cancelled_cb)

    def is_visible(self):
        return self.current_position != 0.0

    visible = property(is_visible, None)

    def hide(self):
        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()

    def show(self):
        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()

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

    def _create_top_panel(self):
        panel = self._create_panel(Gtk.PositionType.TOP)

        zoom_toolbar = ZoomToolbar()
        panel.append(zoom_toolbar, expand=False)
        zoom_toolbar.show()
        zoom_toolbar.connect('level-clicked', self._level_clicked_cb)

        activities_tray = ActivitiesTray()
        panel.append(activities_tray)
        activities_tray.show()

        self._activities_tray = activities_tray

        return panel

    def _create_bottom_panel(self):
        panel = self._create_panel(Gtk.PositionType.BOTTOM)

        devices_tray = DevicesTray()
        panel.append(devices_tray)
        devices_tray.show()

        self._devices_tray = devices_tray

        return panel

    def _create_right_panel(self):
        panel = self._create_panel(Gtk.PositionType.RIGHT)

        tray = FriendsTray()
        panel.append(tray)
        tray.show()

        self._friends_tray = tray

        return panel

    def _create_left_panel(self):
        panel = ClipboardPanelWindow(self, Gtk.PositionType.LEFT)

        return panel

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

        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 _level_clicked_cb(self, zoom_toolbar):
        self.hide()

    def _update_position(self):
        screen_h = Gdk.Screen.height()
        screen_w = Gdk.Screen.width()

        self._move_panel(self._top_panel, self.current_position,
                         0, - self._top_panel.size, 0, 0)

        self._move_panel(self._bottom_panel, self.current_position,
                         0, screen_h, 0, screen_h - self._bottom_panel.size)

        self._move_panel(self._left_panel, self.current_position,
                         - self._left_panel.size, 0, 0, 0)

        self._move_panel(self._right_panel, self.current_position,
                         screen_w, 0, screen_w - self._right_panel.size, 0)

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

    def _enter_corner_cb(self, event_area):
        if self.visible:
            self.hide()
        else:
            self.show()

    def _create_notification_window(self, corner):
        window = NotificationWindow()

        screen = Gdk.Screen.get_default()
        if corner == Gtk.CornerType.TOP_LEFT:
            window.move(0, 0)
        elif corner == Gtk.CornerType.TOP_RIGHT:
            window.move(screen.get_width() - style.GRID_CELL_SIZE, 0)
        elif corner == Gtk.CornerType.BOTTOM_LEFT:
            window.move(0, screen.get_height() - style.GRID_CELL_SIZE)
        elif corner == Gtk.CornerType.BOTTOM_RIGHT:
            window.move(screen.get_width() - style.GRID_CELL_SIZE,
                        screen.get_height() - style.GRID_CELL_SIZE)
        else:
            raise ValueError('Inalid corner: %r' % corner)

        return window

    def _add_message_button(self, button, corner):
        if corner == Gtk.CornerType.BOTTOM_RIGHT:
            self._devices_tray.add_item(button)
        elif corner == Gtk.CornerType.TOP_RIGHT:
            self._friends_tray.add_item(button)
        else:
            self._activities_tray.add_item(button)

    def _remove_message_button(self, button, corner):
        if corner == Gtk.CornerType.BOTTOM_RIGHT:
            self._devices_tray.remove_item(button)
        elif corner == Gtk.CornerType.TOP_RIGHT:
            self._friends_tray.remove_item(button)
        else:
            self._activities_tray.remove_item(button)

    def _launch_notification_icon(self, icon_name, xo_color,
                                  position, duration):
        icon = NotificationIcon()
        icon.props.xo_color = xo_color

        if icon_name.startswith(os.sep):
            icon.props.icon_filename = icon_name
        else:
            icon.props.icon_name = icon_name

        self.add_notification(icon, position, duration)

    def notify_key_press(self):
        self._key_listener.key_press()

    def add_notification(self, icon, corner=Gtk.CornerType.TOP_LEFT,
                         duration=_NOTIFICATION_DURATION):

        if not isinstance(icon, NotificationIcon):
            raise TypeError('icon must be a NotificationIcon.')

        window = self._create_notification_window(corner)

        window.add(icon)
        icon.show()
        window.show()

        self._notif_by_icon[icon] = window

        GObject.timeout_add(duration,
                        lambda: self.remove_notification(icon))

    def remove_notification(self, icon):
        if icon not in self._notif_by_icon:
            logging.debug('icon %r not in list of notifications.', icon)
            return

        window = self._notif_by_icon[icon]
        window.destroy()
        del self._notif_by_icon[icon]

    def add_message(self, body, summary='', icon_name=_DEFAULT_ICON,
                    xo_color=None, corner=Gtk.CornerType.TOP_LEFT,
                    duration=_NOTIFICATION_DURATION):

        if xo_color is None:
            xo_color = profile.get_color()

        button = self._notif_by_message.get(corner, None)
        if button is None:
            button = NotificationButton(_DEFAULT_ICON, xo_color)
            button.show()
            self._add_message_button(button, corner)
            self._notif_by_message[corner] = button

        palette = button.get_palette()
        if palette is None:
            palette = HistoryPalette()
            palette.set_group_id('frame')
            palette.connect('clear-messages', self.remove_message, corner)
            palette.connect('notice-messages', button.stop_pulsing)
            button.set_palette(palette)

        button.start_pulsing()

        palette.push_message(body, summary, icon_name, xo_color)
        if not self.visible:
            self._launch_notification_icon(_DEFAULT_ICON, xo_color, corner, duration)

    def remove_message(self, palette, corner):
        if corner not in self._notif_by_message:
            logging.debug('Button %s is not active', str(corner))
            return

        button = self._notif_by_message[corner]
        self._remove_message_button(button, corner)
        del self._notif_by_message[corner]

    def __notification_received_cb(self, **kwargs):
        logging.debug('__notification_received_cb %r', kwargs)

        hints = kwargs['hints']

        icon_name = hints.get('x-sugar-icon-file-name', '')
        if not icon_name:
            icon_name = _DEFAULT_ICON

        icon_colors = hints.get('x-sugar-icon-colors', '')
        if not icon_colors:
            icon_colors = profile.get_color()

        duration = kwargs.get('expire_timeout', -1)
        if duration == -1:
            duration = _NOTIFICATION_DURATION

        category = hints.get('category', '')
        if category == 'device':
            position = Gtk.CornerType.BOTTOM_RIGHT
        elif category == 'presence':
            position = Gtk.CornerType.TOP_RIGHT
        else:
            position = Gtk.CornerType.TOP_LEFT

        summary = kwargs.get('summary', '')
        body = kwargs.get('body', '')

        if summary or body:
            self.add_message(body, summary, icon_name,
                            icon_colors, position, duration)
        else:
            self._launch_notification_icon(icon_name, icon_colors,
                                          position, duration)

    def __notification_cancelled_cb(self, **kwargs):
        # Do nothing for now. Our notification UI is so simple, there's no
        # point yet.
        pass