~brandontschaefer/unity/bump-to-new-nux-abi

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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011-2012 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: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
 *              Neil Jagdish Patel <neil.patel@canonical.com>
 */

#include <Nux/Nux.h>
#include <Nux/Area.h>
#include <Nux/HLayout.h>

#include <NuxCore/Logger.h>

#include "PanelIndicatorsView.h"
#include "PanelIndicatorEntryDropdownView.h"

DECLARE_LOGGER(logger, "unity.indicators");

namespace unity
{
namespace panel
{
using namespace indicator;

NUX_IMPLEMENT_OBJECT_TYPE(PanelIndicatorsView);

PanelIndicatorsView::PanelIndicatorsView()
: View(NUX_TRACKER_LOCATION)
, opacity(1.0f, sigc::mem_fun(this, &PanelIndicatorsView::SetOpacity))
, layout_(new nux::HLayout("", NUX_TRACKER_LOCATION))
, monitor_(0)
, overlay_showing_(false)
{
  opacity.DisableNotifications();
  layout_->SetContentDistribution(nux::MAJOR_POSITION_END);
  SetLayout(layout_);

  LOG_DEBUG(logger) << "Indicators View Added: ";
}

void PanelIndicatorsView::EnableDropdownMenu(bool enable, indicator::Indicators::Ptr const& indicators)
{
  if (enable && indicators)
  {
    dropdown_ = new PanelIndicatorEntryDropdownView(GetName(), indicators);
    AddEntryView(dropdown_.GetPointer());
  }
  else
  {
    RemoveEntryView(dropdown_.GetPointer());
    dropdown_.Release();
  }
}

void PanelIndicatorsView::AddIndicator(Indicator::Ptr const& indicator)
{
  LOG_DEBUG(logger) << "IndicatorAdded: " << indicator->name();
  indicators_.push_back(indicator);

  for (auto const& entry : indicator->GetEntries())
    OnEntryAdded(entry);

  auto& conn_manager = indicators_connections_[indicator];
  conn_manager.Add(indicator->on_entry_added.connect(sigc::mem_fun(this, &PanelIndicatorsView::OnEntryAdded)));
  conn_manager.Add(indicator->on_entry_removed.connect(sigc::mem_fun(this, &PanelIndicatorsView::RemoveEntry)));
}

void PanelIndicatorsView::RemoveIndicator(Indicator::Ptr const& indicator)
{
  indicators_connections_.erase(indicator);

  for (auto const& entry : indicator->GetEntries())
    RemoveEntry(entry);

  for (auto i = indicators_.begin(); i != indicators_.end(); ++i)
  {
    if (*i == indicator)
    {
      indicators_.erase(i);
      break;
    }
  }

  LOG_DEBUG(logger) << "IndicatorRemoved: " << indicator->name();
}

PanelIndicatorsView::Indicators const& PanelIndicatorsView::GetIndicators() const
{
  return indicators_;
}

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

  GfxContext.PushClippingRectangle(geo);
  nux::GetPainter().PaintBackground(GfxContext, geo);
  GfxContext.PopClippingRectangle();
}

void PanelIndicatorsView::SetMaximumEntriesWidth(int max_width)
{
  if (!dropdown_)
    return;

  int accumolated_width = dropdown_->GetBaseWidth();
  std::vector<PanelIndicatorEntryView::Ptr> to_hide;

  for (auto* area : layout_->GetChildren())
  {
    auto en = static_cast<PanelIndicatorEntryView*>(area);
    if (en == dropdown_.GetPointer())
      continue;

    accumolated_width += en->GetBaseWidth();

    if (accumolated_width > max_width)
      to_hide.push_back(PanelIndicatorEntryView::Ptr(en));
  }

  // No need to hide an item if there's space that we considered for the dropdown
  if (!dropdown_->IsVisible() && to_hide.size() == 1)
  {
    if (accumolated_width - dropdown_->GetBaseWidth() < max_width)
      to_hide.clear();
  }

  // There's just one hidden entries, it might fit in the space we have
  if (to_hide.empty() && dropdown_->Size() == 1)
    accumolated_width -= dropdown_->GetBaseWidth();

  if (accumolated_width < max_width)
  {
    while (!dropdown_->Empty() && dropdown_->Top()->GetBaseWidth() < (max_width - accumolated_width))
      AddEntryView(dropdown_->Pop().GetPointer());
  }
  else
  {
    for (auto const& hidden : to_hide)
    {
      layout_->RemoveChildObject(hidden.GetPointer());
      RemoveChild(hidden.GetPointer());
      dropdown_->Push(hidden);
    }
  }
}

PanelIndicatorEntryView* PanelIndicatorsView::ActivateEntry(indicator::Entry::Ptr const& entry, int button)
{
  auto it = entries_.find(entry);

  if (it != entries_.end())
  {
    PanelIndicatorEntryView* view = it->second;

    if (view->IsSensitive() && view->IsVisible())
    {
      view->Activate(button);
    }
    else if (dropdown_)
    {
      dropdown_->ActivateChild(PanelIndicatorEntryView::Ptr(view));
    }

    return view;
  }

  return nullptr;
}

PanelIndicatorEntryView* PanelIndicatorsView::ActivateEntry(std::string const& entry_id, int button)
{
  for (auto const& it : entries_)
  {
    if (it.first->id() == entry_id)
      return ActivateEntry(it.first, button);
  }

  return nullptr;
}

bool PanelIndicatorsView::ActivateIfSensitive()
{
  for (auto* area : layout_->GetChildren())
  {
    auto* view = static_cast<PanelIndicatorEntryView*>(area);

    if (view->IsSensitive() && view->IsVisible() && view->IsFocused())
    {
      /* Use the 0 button, it means it's a keyboard activation */
      view->Activate(0);
      return true;
    }
  }

  return false;
}

void PanelIndicatorsView::GetGeometryForSync(EntryLocationMap& locations)
{
  for (auto* area : layout_->GetChildren())
    static_cast<PanelIndicatorEntryView*>(area)->GetGeometryForSync(locations);
}

PanelIndicatorEntryView* PanelIndicatorsView::ActivateEntryAt(int x, int y, int button)
{
  PanelIndicatorEntryView* target = nullptr;
  bool found_old_active = false;

  //
  // Change the entry active status without waiting
  // for slow inter-process communication with unity-panel-service,
  // which causes visible lag in many cases.
  //

  for (auto* area : layout_->GetChildren())
  {
    auto view = static_cast<PanelIndicatorEntryView*>(area);

    if (!view->IsVisible())
      continue;

    if (!target &&
        view->IsSensitive() &&
        view->GetAbsoluteGeometry().IsPointInside(x, y))
    {
      view->Activate(button);
      target = view;
      break;
    }
    else if (target && view->IsActive())
    {
      view->Unactivate();
      found_old_active = true;
      break;
    }
  }

  if (target && !found_old_active)
  {
    for (auto* area : layout_->GetChildren())
    {
      auto view = static_cast<PanelIndicatorEntryView*>(area);

      if (view != target && view->IsVisible() && view->IsActive())
      {
        view->Unactivate();
        break;
      }
    }
  }

  return target;
}

void PanelIndicatorsView::DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw)
{
  GfxContext.PushClippingRectangle(GetGeometry());
  layout_->ProcessDraw(GfxContext, force_draw);
  GfxContext.PopClippingRectangle();
}

void PanelIndicatorsView::AddEntryView(PanelIndicatorEntryView* view, IndicatorEntryPosition pos)
{
  if (!view)
    return;

  bool added_to_dropdown = false;
  bool known_entry = (entries_.find(view->GetEntry()) != entries_.end());
  view->SetOpacity(opacity());

  if (!known_entry && dropdown_ && !dropdown_->Empty())
  {
    if (pos == IndicatorEntryPosition::AUTO)
    {
      dropdown_->Insert(PanelIndicatorEntryView::Ptr(view));
      added_to_dropdown = true;
    }
    else if (view->GetEntryPriority() >= dropdown_->Top()->GetEntryPriority())
    {
      dropdown_->Push(PanelIndicatorEntryView::Ptr(view));
      added_to_dropdown = true;
    }
  }

  if (!added_to_dropdown)
  {
    int entry_pos = pos;
    if (entry_pos == IndicatorEntryPosition::AUTO)
    {
      entry_pos = nux::NUX_LAYOUT_BEGIN;

      if (view->GetEntryPriority() > -1)
      {
        for (auto area : layout_->GetChildren())
        {
          auto en = static_cast<PanelIndicatorEntryView*>(area);
          if (view->GetEntryPriority() <= en->GetEntryPriority())
            break;

          ++entry_pos;
        }
      }
    }

    layout_->AddView(view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL, 1.0, (nux::LayoutPosition) entry_pos);
    AddChild(view);

    QueueRelayout();
    QueueDraw();
  }

  if (!known_entry)
  {
    view->SetMonitor(monitor_);
    view->refreshed.connect(sigc::mem_fun(this, &PanelIndicatorsView::OnEntryRefreshed));
    entries_.insert({view->GetEntry(), view});
    on_indicator_updated.emit();
    entry_added.emit(view);
  }
}

PanelIndicatorEntryView *PanelIndicatorsView::AddEntry(Entry::Ptr const& entry, int padding, IndicatorEntryPosition pos, IndicatorEntryType type)
{
  auto view = new PanelIndicatorEntryView(entry, padding, type);
  AddEntryView(view, pos);

  if (overlay_showing_)
    view->OverlayShown();

  return view;
}

void PanelIndicatorsView::OnEntryAdded(Entry::Ptr const& entry)
{
  AddEntry(entry);
}

void PanelIndicatorsView::OnEntryRefreshed(PanelIndicatorEntryView* view)
{
  QueueRelayout();
  QueueDraw();
  on_indicator_updated.emit();
}

void PanelIndicatorsView::ClearEntries()
{
  for (auto it = entries_.begin(); it != entries_.end();)
  {
    auto* entry = it->second;
    ++it;

    if (entry != dropdown_.GetPointer())
      RemoveEntryView(entry);
  }

  on_indicator_updated.emit();

  QueueRelayout();
  QueueDraw();
}

void PanelIndicatorsView::RemoveEntryView(PanelIndicatorEntryView* view)
{
  if (!view)
    return;

  entry_removed.emit(view);

  if (dropdown_)
    dropdown_->Remove(PanelIndicatorEntryView::Ptr(view));

  RemoveChild(view);
  entries_.erase(view->GetEntry());
  layout_->RemoveChildObject(view);
  on_indicator_updated.emit();

  QueueRelayout();
  QueueDraw();
}

void PanelIndicatorsView::RemoveEntry(indicator::Entry::Ptr const& entry)
{
  auto it = entries_.find(entry);

  if (it != entries_.end())
    RemoveEntryView(it->second);
}

void PanelIndicatorsView::OverlayShown()
{
  overlay_showing_ = true;

  for (auto const& entry: entries_)
    entry.second->OverlayShown();
}

void PanelIndicatorsView::OverlayHidden()
{
  overlay_showing_ = false;

  for (auto const& entry: entries_)
    entry.second->OverlayHidden();
}

void PanelIndicatorsView::SetMonitor(int monitor)
{
  monitor_ = monitor;

  for (auto const& entry : entries_)
    entry.second->SetMonitor(monitor_);
}

bool PanelIndicatorsView::SetOpacity(double& target, double const& new_value)
{
  double opacity = CLAMP(new_value, 0.0f, 1.0f);

  for (auto const& entry : entries_)
    entry.second->SetOpacity(opacity);

  if (opacity != target)
  {
    target = opacity;
    QueueDraw();

    return true;
  }

  return false;
}

std::string PanelIndicatorsView::GetName() const
{
  return "Indicators";
}

void PanelIndicatorsView::AddProperties(debug::IntrospectionData& introspection)
{
  introspection
  .add(GetAbsoluteGeometry())
  .add("entries", entries_.size())
  .add("opacity", opacity);
}

} // namespace panel
} // namespace unity