~rsalveti/notify-osd/phablet-bump-epoch

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
////////////////////////////////////////////////////////////////////////////////
//3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
//      10        20        30        40        50        60        70        80
//
// notify-osd
//
// notifications-view.cpp - implements all the nux-based rendering of a bubble
//
// Copyright 2012 Canonical Ltd.
//
// Authors:
//    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
//
// self program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 3, as published
// by the Free Software Foundation.
//
// self program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranties of
// MERCHANTABILITY, SATISFACTORY QUALITY, 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 self program.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////

#include <Nux/Nux.h>
#include <Nux/BaseWindow.h>
#include <Nux/HLayout.h>
#include <Nux/Layout.h>
#include <Nux/WindowCompositor.h>

#include <Nux/CairoWrapper.h>
#include <NuxGraphics/ImageSurface.h>
#include <NuxCore/Logger.h>

#include <NuxGraphics/GLThread.h>
#include <NuxGraphics/RenderingPipe.h>
#include <NuxGraphics/GraphicsDisplay.h>

#include <glib.h>

#include <hybris/ubuntu/ui/ubuntu_ui_session_service.h>

#include "notifications-view.h"

#define FPS 60.0
#define FADE_IN_DURATION  0.2
#define FADE_OUT_DURATION 0.1

void
NotificationsView::DrawCallback (nux::Geometry const& geom, cairo_t* cr)
{
  if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
    return;

  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
  cairo_scale (cr, 1.0, 1.0);

  if (_source_surface)
  {
    cairo_set_source_surface (cr,
                              _source_surface,
                              1 * _units->getGU (),
                              4 * _units->getGU ());
    cairo_paint (cr);
  }
}

NUX_IMPLEMENT_OBJECT_TYPE(NotificationsView);

NotificationsView::NotificationsView(NUX_FILE_LINE_DECL)
  : View(NUX_FILE_LINE_PARAM)
{
  _dirty             = false;
  _source_surface    = NULL;
  _visible           = false;
  _alpha             = 1.0;
  height             = 0;
  _in_hiding         = false;
  _ignore_geo.x      = 0;
  _ignore_geo.y      = 0;
  _ignore_geo.width  = 0;
  _ignore_geo.height = 0;
  _accept_geo.x      = 0;
  _accept_geo.y      = 0;
  _accept_geo.width  = 0;
  _accept_geo.height = 0;
  _units             = Units::getInstance ();

  Units* units = Units::getInstance ();
  int gu = units->getGU ();
  nux::Geometry const& geo = {0, 0, nux::GetWindowThread()->GetGraphicsDisplay().GetScreenWidth(), 30 * gu};
  SetGeometry (geo);

  _canvas.reset (new nux::CairoWrapper (geo, sigc::mem_fun (this, &NotificationsView::DrawCallback)));
}

NotificationsView::~NotificationsView()
{
}

gboolean
NotificationsView::_fade_in_cb (gpointer data)
{
  NotificationsView* self = (NotificationsView*) data;

  self->SetOpacity (self->GetOpacity () + 1.0/ (1000.0 / FPS));

  if (self->GetOpacity () < 1.0)
    return true;
  else
    return false;
}

void
NotificationsView::EmitSignal()
{
    FadedOut.emit();
}

void
NotificationsView::SetIgnoreGeometry (nux::Geometry const& geo)
{
  _ignore_geo.x      = geo.x;
  _ignore_geo.y      = geo.y;
  _ignore_geo.width  = geo.width;
  _ignore_geo.height = geo.height;
}

void
NotificationsView::SetAcceptGeometry (nux::Geometry const& geo)
{
  _accept_geo.x      = geo.x;
  _accept_geo.y      = geo.y;
  _accept_geo.width  = geo.width;
  _accept_geo.height = geo.height;
}

bool
NotificationsView::IsIgnoreHit (gint x, gint y)
{
  nux::Point point = nux::Point (x,y);

  if (_ignore_geo.IsInside(point))
    return true;
  else
    return false;
}

bool
NotificationsView::IsAcceptHit (gint x, gint y)
{
  nux::Point point = nux::Point (x,y);

  if (_accept_geo.IsInside(point))
    return true;
  else
    return false;
}

gboolean
NotificationsView::_fade_out_cb (gpointer data)
{
  NotificationsView* self = (NotificationsView*) data;

  self->SetOpacity (self->GetOpacity () - 1.0 / (1000.0 / FPS));

  if (self->GetOpacity () > 0.0)
    return true;
  else
  {
    self->EmitSignal();
    self->SetVisible (false);
    nux::GetWindowThread()->GetGraphicsDisplay().Hide();
    ubuntu_ui_report_notification_invisible ();
    return false;
  }
}

void
NotificationsView::_fade_in ()
{
  _alpha = 0.0;
  _fade_in_id = g_timeout_add (1000.0 / FPS,
                               &NotificationsView::_fade_in_cb,
                               this);
}

void
NotificationsView::_fade_out ()
{
  _in_hiding = true;
  _alpha = 1.0;
  _fade_out_id = g_timeout_add (1000.0 / FPS,
                               &NotificationsView::_fade_out_cb,
                               this);
}

void
NotificationsView::Show ()
{
  _in_hiding = false;
  _visible = true;
  nux::GetWindowThread()->GetGraphicsDisplay().Show();
  ubuntu_ui_report_notification_visible ();
  _fade_in ();
}

void
NotificationsView::Hide ()
{
  if (!_in_hiding)
    _fade_out ();
}

void
NotificationsView::SetOpacity (double alpha)
{
  _alpha = alpha;
  QueueDraw ();
}

double
NotificationsView::GetOpacity ()
{
  return _alpha;
}

void
NotificationsView::SetVisible (bool visible)
{
  _visible = visible;
  QueueDraw ();
}

bool
NotificationsView::GetVisible ()
{
  return _visible;
}

void
NotificationsView::SetSourceSurface (cairo_surface_t* surface)
{
  if (surface)
  {
    _source_surface = surface;
    height = cairo_image_surface_get_height (surface);
    _dirty = true;
  }
}

void
NotificationsView::SetAppSwitchName(gchar* name)
{
  if (this->_app_name) { g_free(this->_app_name); }
  this->_app_name = g_strdup(name);
}

gchar*
NotificationsView::GetAppSwitchName()
{
  return this->_app_name;
}

void
NotificationsView::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
{
  nux::Geometry const& geo = GetGeometry();

  GfxContext.PushClippingRectangle(geo);

  GfxContext.QRP_Color (geo.x,
                        geo.y,
                        geo.width,
                        geo.height,
                        nux::color::Transparent);

  if (_dirty)
  {
    _canvas->Recreate ();
    _dirty = false;
  }

  if (_visible)
  {
    _canvas->Invalidate (geo);
    _bubble_tex = _canvas->GetTexture();        
 
    nux::TexCoordXForm texxform;

    GfxContext.QRP_1Tex(geo.x,
                        geo.y,
                        _bubble_tex->GetWidth(), 
                        _bubble_tex->GetHeight(),
                        _bubble_tex->GetDeviceTexture(),
                        texxform,
                        nux::Color (_alpha, _alpha, _alpha, _alpha));
  }

  GfxContext.PopClippingRectangle();
}

void
NotificationsView::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
{
}