~didrocks/unity/launcher-bug-fix-fest

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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2010 Canonical Ltd
 *
 * This 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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
 */

#include "config.h"

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

#include <NuxImage/CairoGraphics.h>
#include <NuxImage/ImageSurface.h>
#include <Nux/StaticText.h>
#include <Nux/MenuPage.h>

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

#include <glib.h>
#include <glib/gi18n-lib.h>

#include "ubus-server.h"
#include "UBusMessages.h"

#include "PlacesSearchBar.h"

#include "PlacesStyle.h"

#define LIVE_SEARCH_TIMEOUT 250

NUX_IMPLEMENT_OBJECT_TYPE (PlacesSearchBar);

PlacesSearchBar::PlacesSearchBar (NUX_FILE_LINE_DECL)
:   View (NUX_FILE_LINE_PARAM),
    _entry (NULL),
    _live_search_timeout (0)
{
  PlacesStyle      *style = PlacesStyle::GetDefault ();
  nux::BaseTexture *icon = style->GetSearchReadyIcon ();

  _bg_layer = new nux::ColorLayer (nux::Color (0xff595853), true);

  _layout = new nux::HLayout (NUX_TRACKER_LOCATION);
  _layout->SetHorizontalInternalMargin (12);

  _spinner = new PlacesSearchBarSpinner ();
  _spinner->SetMinMaxSize (icon->GetWidth (), icon->GetHeight ());
  //_spinner->SetMaximumWidth (icon->GetWidth ());
  _layout->AddView (_spinner, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
  _spinner->OnMouseClick.connect (sigc::mem_fun (this, &PlacesSearchBar::OnClearClicked));
  _spinner->SetCanFocus (false);

  _layered_layout = new nux::LayeredLayout ();

  _hint = new nux::StaticCairoText (" ");
  _hint->SetTextColor (nux::Color (1.0f, 1.0f, 1.0f, 0.5f));
  _hint->SetCanFocus (false);
  _layered_layout->AddLayer (_hint);

  _pango_entry = new nux::TextEntry ("", NUX_TRACKER_LOCATION);
  _pango_entry->sigTextChanged.connect (sigc::mem_fun (this, &PlacesSearchBar::OnSearchChanged));
  _pango_entry->SetCanFocus (true);
  _pango_entry->activated.connect (sigc::mem_fun (this, &PlacesSearchBar::OnEntryActivated));
  _layered_layout->AddLayer (_pango_entry);

  _layered_layout->SetPaintAll (true);
  _layered_layout->SetActiveLayerN (1);

  _layout->AddView (_layered_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FIX);
  
  _combo = new nux::ComboBoxSimple(NUX_TRACKER_LOCATION);
  _combo->SetMaximumWidth (style->GetTileWidth ());
  _combo->SetVisible (false);
  _combo->sigTriggered.connect (sigc::mem_fun (this, &PlacesSearchBar::OnComboChanged));
  _combo->GetMenuPage ()->sigMouseDownOutsideMenuCascade.connect (sigc::mem_fun (this, &PlacesSearchBar::OnMenuClosing));
  _layout->AddView (_combo, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FIX);

  _layout->SetVerticalExternalMargin (18);
  _layout->SetHorizontalExternalMargin (18);

  SetLayout (_layout);
  SetCompositionLayout (_layout);

  g_signal_connect (gtk_settings_get_default (), "notify::gtk-font-name",
                    G_CALLBACK (OnFontChanged), this);
  OnFontChanged (NULL, NULL, this);

  _pango_entry->cursor_moved.connect (sigc::mem_fun (this, &PlacesSearchBar::OnLayeredLayoutQueueDraw));
}

PlacesSearchBar::~PlacesSearchBar ()
{
  if (_bg_layer)
    delete _bg_layer;
}

const gchar* PlacesSearchBar::GetName ()
{
	return "PlacesSearchBar";
}

const gchar *
PlacesSearchBar::GetChildsName ()
{
  return "";
}

void PlacesSearchBar::AddProperties (GVariantBuilder *builder)
{
  nux::Geometry geo = GetGeometry ();

  g_variant_builder_add (builder, "{sv}", "x", g_variant_new_int32 (geo.x));
  g_variant_builder_add (builder, "{sv}", "y", g_variant_new_int32 (geo.y));
  g_variant_builder_add (builder, "{sv}", "width", g_variant_new_int32 (geo.width));
  g_variant_builder_add (builder, "{sv}", "height", g_variant_new_int32 (geo.height));
}

long
PlacesSearchBar::ProcessEvent (nux::IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
{
  long ret = TraverseInfo;
  ret = _layout->ProcessEvent (ievent, ret, ProcessEventInfo);

  return ret;
}

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

  UpdateBackground ();

  GfxContext.PushClippingRectangle (geo);

  nux::GetPainter ().PaintBackground (GfxContext, geo);

  _bg_layer->SetGeometry (nux::Geometry (geo.x, geo.y, _last_width, geo.height));
  nux::GetPainter ().RenderSinglePaintLayer (GfxContext,
                                             _bg_layer->GetGeometry (),
                                             _bg_layer);

  GfxContext.PopClippingRectangle ();
}

void
PlacesSearchBar::DrawContent (nux::GraphicsEngine &GfxContext, bool force_draw)
{
  nux::Geometry geo = GetGeometry ();

  GfxContext.PushClippingRectangle (geo);

  gPainter.PushLayer (GfxContext, _bg_layer->GetGeometry (), _bg_layer);

  _layout->ProcessDraw (GfxContext, force_draw);

  gPainter.PopBackground ();
  GfxContext.PopClippingRectangle();
}

void
PlacesSearchBar::SetActiveEntry (PlaceEntry *entry,
                                 guint       section_id,
                                 const char *search_string)
{
   std::map<gchar *, gchar *> hints;

   _entry = entry;

   _combo->RemoveAllItem ();
   _combo->SetVisible (false);

  if (_entry)
  {
    gchar       *markup;
    gchar       *tmp;

    tmp = g_markup_escape_text (entry->GetSearchHint (), -1);
    markup  = g_strdup_printf ("<span font_size='x-small' font_style='italic'> %s </span>", tmp);

    _hint->SetText (markup);
    _pango_entry->SetText (search_string ? search_string : "");

    _entry->SetActiveSection (section_id);
    _entry->SetSearch (search_string ? search_string : "", hints);

    _entry->ForeachSection (sigc::mem_fun (this, &PlacesSearchBar::OnSectionAdded));
    if (_combo->IsVisible ())
      _combo->SetSelectionIndex (section_id);

    g_free (tmp);
    g_free (markup);
  }
  else
  {
    _pango_entry->SetText ("");
  }
}

void
PlacesSearchBar::OnSectionAdded (PlaceEntry *entry, PlaceEntrySection& section)
{
  char *tmp = g_markup_escape_text (section.GetName (), -1);

  _combo->AddItem (tmp);
  _combo->SetVisible (true);

  g_free (tmp);
}

void
PlacesSearchBar::OnComboChanged (nux::ComboBoxSimple *simple)
{
  _entry->SetActiveSection (_combo->GetSelectionIndex());
  OnMenuClosing (NULL, 0, 0);
}

void
PlacesSearchBar::OnMenuClosing (nux::MenuPage *menu, int x, int y)
{
  ubus_server_send_message (ubus_server_get_default (),
                            UBUS_PLACE_VIEW_QUEUE_DRAW,
                            NULL);
}

void
PlacesSearchBar::OnSearchChanged (nux::TextEntry *text_entry)
{
  bool is_empty;

  if (_live_search_timeout)
    g_source_remove (_live_search_timeout);

  _live_search_timeout = g_timeout_add (LIVE_SEARCH_TIMEOUT,
                                        (GSourceFunc)&OnLiveSearchTimeout,
                                        this);

  search_changed.emit (_pango_entry->GetText ().c_str ());



  is_empty = g_strcmp0 (_pango_entry->GetText ().c_str (), "") == 0;
  _hint->SetVisible (is_empty);
  _spinner->SetState (is_empty ? STATE_READY : STATE_SEARCHING);

  _hint->QueueDraw ();
  _pango_entry->QueueDraw ();
  QueueDraw ();
}

bool
PlacesSearchBar::OnLiveSearchTimeout (PlacesSearchBar *self)
{
  self->EmitLiveSearch ();

  return FALSE;
}

void
PlacesSearchBar::EmitLiveSearch ()
{
  if (_entry)
  {
    std::map<gchar *, gchar *> hints;

    _entry->SetSearch (_pango_entry->GetText ().c_str (), hints);
  }
  _live_search_timeout = 0;
}

void
PlacesSearchBar::OnClearClicked (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  if (_pango_entry->GetText () != "")
  {
    _pango_entry->SetText ("");
    _spinner->SetState (STATE_READY);
    EmitLiveSearch ();
  }
}

void
PlacesSearchBar::OnEntryActivated ()
{
  activated.emit ();
}

void
PlacesSearchBar::OnLayeredLayoutQueueDraw (int i)
{
  QueueDraw ();
}

void
PlacesSearchBar::OnSearchFinished ()
{
  _spinner->SetState (STATE_CLEAR);
}

void
PlacesSearchBar::OnFontChanged (GObject *object, GParamSpec *pspec, PlacesSearchBar *self)
{
#define HOW_LARGE 8
  GtkSettings          *settings;
  gchar                *font_name = NULL;
  PangoFontDescription *desc;
  gint                  size;
  gchar                *font_desc;

  settings = gtk_settings_get_default ();
  g_object_get (settings, "gtk-font-name", &font_name, NULL);

  self->_combo->GetStaticText ()->SetFontName (font_name);
  self->_combo->GetMenuPage ()->SetFontName (font_name);

  desc = pango_font_description_from_string (font_name);
  self->_pango_entry->SetFontFamily (pango_font_description_get_family (desc));

  size = pango_font_description_get_size (desc);
  size /= pango_font_description_get_size_is_absolute (desc) ? 1 : PANGO_SCALE;
  self->_pango_entry->SetFontSize ( size + HOW_LARGE);

  self->_pango_entry->SetFontOptions (gdk_screen_get_font_options (gdk_screen_get_default ()));

  font_desc = g_strdup_printf ("%s %d", pango_font_description_get_family (desc), size + HOW_LARGE);
  self->_hint->SetFont (font_desc);

  pango_font_description_free (desc);
  g_free (font_name);
  g_free (font_desc);
}

static void
draw_rounded_rect (cairo_t* cr,
                   double   aspect,
                   double   x,
                   double   y,
                   double   cornerRadius,
                   double   width,
                   double   height)
{
    double radius = cornerRadius / aspect;

    // top-left, right of the corner
    cairo_move_to (cr, x + radius, y);

    // top-right, left of the corner
    cairo_line_to (cr, x + width - radius, y);

    // top-right, below the corner
    cairo_arc (cr,
               x + width - radius,
               y + radius,
               radius,
               -90.0f * G_PI / 180.0f,
               0.0f * G_PI / 180.0f);

    // bottom-right, above the corner
    cairo_line_to (cr, x + width, y + height - radius);

    // bottom-right, left of the corner
    cairo_arc (cr,
               x + width - radius,
               y + height - radius,
               radius,
               0.0f * G_PI / 180.0f,
               90.0f * G_PI / 180.0f);

    // bottom-left, right of the corner
    cairo_line_to (cr, x + radius, y + height);

    // bottom-left, above the corner
    cairo_arc (cr,
               x + radius,
               y + height - radius,
               radius,
               90.0f * G_PI / 180.0f,
               180.0f * G_PI / 180.0f);

    // top-left, right of the corner
    cairo_arc (cr,
               x + radius,
               y + radius,
               radius,
               180.0f * G_PI / 180.0f,
               270.0f * G_PI / 180.0f);
  cairo_close_path (cr);
}

void
PlacesSearchBar::UpdateBackground ()
{
#define PADDING 14
#define RADIUS  6
  int x, y, width, height;
  nux::Geometry geo = GetGeometry ();

  if (geo.width == _last_width && geo.height == _last_height)
    return;

  _last_width = geo.width;
  _last_height = geo.height;

  x = y = PADDING;
  width = _last_width - (2*PADDING);
  height = _last_height - (2*PADDING);

  nux::CairoGraphics cairo_graphics(CAIRO_FORMAT_ARGB32, _last_width, _last_height);
  cairo_t *cr = cairo_graphics.GetContext();
  cairo_translate (cr, 0.5, 0.5);
  cairo_set_line_width (cr, 1.0);

  draw_rounded_rect (cr, 1.0f, x, y, RADIUS, width, height);

  cairo_set_source_rgba (cr, 0.0f, 0.0f, 0.0f, 0.75f);
  cairo_fill_preserve (cr);

  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.8f);
  cairo_stroke (cr);

  //FIXME: This is until we get proper glow
  draw_rounded_rect (cr, 1.0f, x-1, y-1, RADIUS, width+2, height+2);
  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.4f);
  cairo_stroke (cr);

  draw_rounded_rect (cr, 1.0f, x-2, y-2, RADIUS, width+4, height+4);
  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.2f);
  cairo_stroke (cr);

  draw_rounded_rect (cr, 1.0f, x-3, y-3, RADIUS, width+6, height+6);
  cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 0.1f);
  cairo_stroke (cr);

  cairo_destroy (cr);

  nux::NBitmapData* bitmap =  cairo_graphics.GetBitmap();

  nux::BaseTexture* texture2D = nux::GetThreadGLDeviceFactory ()->CreateSystemCapableTexture ();
  texture2D->Update(bitmap);
  delete bitmap;

  nux::TexCoordXForm texxform;
  texxform.SetTexCoordType (nux::TexCoordXForm::OFFSET_COORD);
  texxform.SetWrap (nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);
  if (_bg_layer)
    delete _bg_layer;

  nux::ROPConfig rop;
  rop.Blend = true;                      // Disable the blending. By default rop.Blend is false.
  rop.SrcBlend = GL_ONE;                  // Set the source blend factor.
  rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;  // Set the destination blend factor.

  _bg_layer = new nux::TextureLayer (texture2D->GetDeviceTexture(),
                                     texxform,          // The Oject that defines the texture wraping and coordinate transformation.
                                     nux::Color::White, // The color used to modulate the texture.
                                     true,              // Write the alpha value of the texture to the destination buffer.
                                     rop                // Use the given raster operation to set the blending when the layer is being rendered.
  );

  texture2D->UnReference ();
}