~3v1n0/unity/overlay-border-scale

« back to all changes in this revision

Viewing changes to plugins/unityshell/src/SwitcherController.cpp

  • Committer: Daniel van Vugt
  • Date: 2012-03-14 06:24:18 UTC
  • mfrom: (2108 unity)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: daniel.van.vugt@canonical.com-20120314062418-nprucpbr0m7qky5e
MergedĀ latestĀ lp:unity

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
#include <Nux/HLayout.h>
23
23
 
24
24
#include "UBusMessages.h"
25
 
#include "ubus-server.h"
26
25
#include "WindowManager.h"
27
26
 
28
27
#include "SwitcherController.h"
36
35
namespace switcher
37
36
{
38
37
 
39
 
Controller::Controller()
40
 
  :  view_window_(0)
 
38
Controller::Controller(unsigned int load_timeout)
 
39
  :  construct_timeout_(load_timeout)
 
40
  ,  view_window_(nullptr)
 
41
  ,  main_layout_(nullptr)
 
42
  ,  monitor_(0)
41
43
  ,  visible_(false)
42
44
  ,  show_timer_(0)
43
45
  ,  detail_timer_(0)
 
46
  ,  lazy_timer_(0)
 
47
  ,  view_idle_timer_(0)
 
48
  ,  bg_color_(0, 0, 0, 0.5)
44
49
{
45
 
  timeout_length = 150;
 
50
  timeout_length = 75;
46
51
  detail_on_timeout = true;
47
52
  detail_timeout_length = 1500;
48
53
 
49
 
  bg_color_ = nux::Color(0.0, 0.0, 0.0, 0.5);
 
54
  ubus_manager_.RegisterInterest(UBUS_BACKGROUND_COLOR_CHANGED, sigc::mem_fun(this, &Controller::OnBackgroundUpdate));
50
55
 
51
 
  UBusServer *ubus = ubus_server_get_default();
52
 
  bg_update_handle_ =
53
 
  ubus_server_register_interest(ubus, UBUS_BACKGROUND_COLOR_CHANGED,
54
 
                                (UBusCallback)&Controller::OnBackgroundUpdate,
55
 
                                this);
 
56
  /* Construct the view after a prefixed timeout, to improve the startup time */
 
57
  lazy_timer_ = g_timeout_add_seconds_full(G_PRIORITY_LOW, construct_timeout_, [] (gpointer data) -> gboolean {
 
58
    auto self = static_cast<Controller*>(data);
 
59
    self->lazy_timer_ = 0;
 
60
    self->ConstructWindow();
 
61
    return FALSE;
 
62
  }, this, nullptr);
56
63
}
57
64
 
58
65
Controller::~Controller()
59
66
{
60
 
  ubus_server_unregister_interest(ubus_server_get_default(), bg_update_handle_);
61
67
  if (view_window_)
62
68
    view_window_->UnReference();
 
69
 
 
70
  if (lazy_timer_)
 
71
    g_source_remove(lazy_timer_);
 
72
 
 
73
  if (view_idle_timer_)
 
74
    g_source_remove(view_idle_timer_);
63
75
}
64
76
 
65
 
void Controller::OnBackgroundUpdate(GVariant* data, Controller* self)
 
77
void Controller::OnBackgroundUpdate(GVariant* data)
66
78
{
67
79
  gdouble red, green, blue, alpha;
68
80
  g_variant_get(data, "(dddd)", &red, &green, &blue, &alpha);
69
 
  self->bg_color_ = nux::Color(red, green, blue, alpha);
70
 
 
71
 
  if (self->view_)
72
 
    self->view_->background_color = self->bg_color_;
73
 
}
74
 
 
75
 
bool IsOnOtherViewport (AbstractLauncherIcon* icon)
76
 
{
77
 
  return !icon->HasWindowOnViewport();
 
81
  bg_color_ = nux::Color(red, green, blue, alpha);
 
82
 
 
83
  if (view_)
 
84
    view_->background_color = bg_color_;
78
85
}
79
86
 
80
87
void Controller::Show(ShowMode show, SortMode sort, bool reverse,
81
 
                      std::vector<AbstractLauncherIcon*> results)
 
88
                      std::vector<AbstractLauncherIcon::Ptr> results)
82
89
{
83
90
  if (sort == SortMode::FOCUS_ORDER)
84
91
  {
85
92
    std::sort(results.begin(), results.end(), CompareSwitcherItemsPriority);
86
93
  }
87
94
  
88
 
  if (show == ShowMode::CURRENT_VIEWPORT)
89
 
  {
90
 
    results.erase(std::remove_if(results.begin(), results.end(), IsOnOtherViewport), results.end());
91
 
  }
92
 
 
93
95
  model_.reset(new SwitcherModel(results));
94
96
  AddChild(model_.get());
95
97
  model_->selection_changed.connect(sigc::mem_fun(this, &Controller::OnModelSelectionChanged));
101
103
 
102
104
  if (timeout_length > 0)
103
105
  {
 
106
    if (view_idle_timer_)
 
107
      g_source_remove(view_idle_timer_);
 
108
 
 
109
    view_idle_timer_ = g_idle_add_full(G_PRIORITY_LOW, [] (gpointer data) -> gboolean {
 
110
      auto self = static_cast<Controller*>(data);
 
111
      self->ConstructView();
 
112
      self->view_idle_timer_ = 0;
 
113
      return FALSE;
 
114
    }, this, NULL);
 
115
 
104
116
    if (show_timer_)
105
117
      g_source_remove (show_timer_);
106
 
    show_timer_ = g_timeout_add(timeout_length, &Controller::OnShowTimer, this);
 
118
 
 
119
    show_timer_ = g_timeout_add(timeout_length, [] (gpointer data) -> gboolean {
 
120
      auto self = static_cast<Controller*>(data);
 
121
      self->ShowView();
 
122
      self->show_timer_ = 0;
 
123
      return FALSE;
 
124
    }, this);
107
125
  }
108
126
  else
109
127
  {
110
 
    ConstructView();
 
128
    ShowView();
111
129
  }
112
130
 
113
131
  if (detail_on_timeout)
117
135
    detail_timer_ = g_timeout_add(detail_timeout_length, &Controller::OnDetailTimer, this);
118
136
  }
119
137
 
120
 
  ubus_server_send_message(ubus_server_get_default(),
121
 
                           UBUS_PLACE_VIEW_CLOSE_REQUEST,
122
 
                           NULL);
123
 
 
124
 
  ubus_server_send_message(ubus_server_get_default(),
125
 
                           UBUS_SWITCHER_SHOWN, g_variant_new_boolean(true));
 
138
  ubus_manager_.SendMessage(UBUS_PLACE_VIEW_CLOSE_REQUEST);
 
139
  ubus_manager_.SendMessage(UBUS_SWITCHER_SHOWN, g_variant_new_boolean(true));
126
140
}
127
141
 
128
142
void Controller::Select(int index)
131
145
    model_->Select(index);
132
146
}
133
147
 
134
 
gboolean Controller::OnShowTimer(gpointer data)
135
 
{
136
 
  Controller* self = static_cast<Controller*>(data);
137
 
 
138
 
  if (self->visible_)
139
 
    self->ConstructView();
140
 
 
141
 
  self->show_timer_ = 0;
142
 
  return FALSE;
143
 
}
144
 
 
145
148
gboolean Controller::OnDetailTimer(gpointer data)
146
149
{
147
150
  Controller* self = static_cast<Controller*>(data);
156
159
  return FALSE;
157
160
}
158
161
 
159
 
void Controller::OnModelSelectionChanged(AbstractLauncherIcon *icon)
 
162
void Controller::OnModelSelectionChanged(AbstractLauncherIcon::Ptr icon)
160
163
{
161
164
  if (detail_on_timeout)
162
165
  {
166
169
    detail_timer_ = g_timeout_add(detail_timeout_length, &Controller::OnDetailTimer, this);
167
170
  }
168
171
 
169
 
  ubus_server_send_message(ubus_server_get_default(),
170
 
                           UBUS_SWITCHER_SELECTION_CHANGED,
171
 
                           g_variant_new_string(icon->tooltip_text().c_str()));
172
 
}
173
 
 
174
 
void Controller::ConstructView()
175
 
{
176
 
  view_ = SwitcherView::Ptr(new SwitcherView());
177
 
  AddChild(view_.GetPointer());
178
 
  view_->SetModel(model_);
179
 
  view_->background_color = bg_color_;
 
172
  ubus_manager_.SendMessage(UBUS_SWITCHER_SELECTION_CHANGED,
 
173
                            g_variant_new_string(icon->tooltip_text().c_str()));
 
174
}
 
175
 
 
176
void Controller::ShowView()
 
177
{
 
178
  if (!visible_)
 
179
    return;
 
180
  
 
181
  ConstructView();
 
182
 
 
183
  if (view_window_)
 
184
    view_window_->SetOpacity(1.0f);
 
185
}
 
186
 
 
187
void Controller::ConstructWindow()
 
188
{
 
189
  if (lazy_timer_)
 
190
  {
 
191
    g_source_remove(lazy_timer_);
 
192
    lazy_timer_ = 0;
 
193
  }
180
194
 
181
195
  if (!view_window_)
182
196
  {
188
202
    view_window_->SinkReference();
189
203
    view_window_->SetLayout(main_layout_);
190
204
    view_window_->SetBackgroundColor(nux::Color(0x00000000));
191
 
  }
192
 
 
 
205
    view_window_->SetGeometry(workarea_);
 
206
  }
 
207
}
 
208
 
 
209
void Controller::ConstructView()
 
210
{
 
211
  if (view_ || !model_)
 
212
    return;
 
213
 
 
214
  if (view_idle_timer_)
 
215
  {
 
216
    g_source_remove(view_idle_timer_);
 
217
    view_idle_timer_ = 0;
 
218
  }
 
219
 
 
220
  view_ = SwitcherView::Ptr(new SwitcherView());
 
221
  AddChild(view_.GetPointer());
 
222
  view_->SetModel(model_);
 
223
  view_->background_color = bg_color_;
 
224
  view_->monitor = monitor_;
 
225
  view_->SetupBackground();
 
226
 
 
227
  ConstructWindow();
193
228
  main_layout_->AddView(view_.GetPointer(), 1);
194
 
 
195
229
  view_window_->SetGeometry(workarea_);
196
 
  view_->SetupBackground();
 
230
  view_window_->SetOpacity(0.0f);
197
231
  view_window_->ShowWindow(true);
198
232
}
199
233
 
200
 
void Controller::SetWorkspace(nux::Geometry geo)
 
234
void Controller::SetWorkspace(nux::Geometry geo, int monitor)
201
235
{
 
236
  monitor_ = monitor;
202
237
  workarea_ = geo;
 
238
 
 
239
  if (view_)
 
240
    view_->monitor = monitor_;
203
241
}
204
242
 
205
243
void Controller::Hide(bool accept_state)
209
247
 
210
248
  if (accept_state)
211
249
  {
212
 
    AbstractLauncherIcon* selection = model_->Selection();
 
250
    AbstractLauncherIcon::Ptr selection = model_->Selection();
213
251
    if (selection)
214
252
    {
215
253
      if (model_->detail_selection)
231
269
    }
232
270
  }
233
271
 
 
272
  if (view_idle_timer_)
 
273
  {
 
274
    g_source_remove(view_idle_timer_);
 
275
    view_idle_timer_ = 0;
 
276
  }
 
277
 
234
278
  model_.reset();
235
279
  visible_ = false;
236
280
 
238
282
    main_layout_->RemoveChildObject(view_.GetPointer());
239
283
 
240
284
  if (view_window_)
 
285
  {
 
286
    view_window_->SetOpacity(0.0f);
241
287
    view_window_->ShowWindow(false);
 
288
  }
242
289
 
243
290
  if (show_timer_)
244
291
    g_source_remove(show_timer_);
248
295
    g_source_remove(detail_timer_);
249
296
  detail_timer_ = 0;
250
297
 
251
 
  ubus_server_send_message(ubus_server_get_default(),
252
 
                           UBUS_SWITCHER_SHOWN, g_variant_new_boolean(false));
 
298
  ubus_manager_.SendMessage(UBUS_SWITCHER_SHOWN, g_variant_new_boolean(false));
253
299
 
254
300
  view_.Release();
255
301
}
269
315
    switch (detail_mode_)
270
316
    {
271
317
      case TAB_NEXT_WINDOW:
272
 
        if (model_->detail_selection_index < model_->Selection()->RelatedXids ().size () - 1)
 
318
        if (model_->detail_selection_index < model_->DetailXids().size () - 1)
273
319
          model_->NextDetail();
274
320
        else
275
321
          model_->Next();
324
370
 
325
371
void Controller::SetDetail(bool value, unsigned int min_windows)
326
372
{
327
 
  if (value && model_->Selection()->RelatedXids().size () >= min_windows)
 
373
  if (value && model_->DetailXids().size () >= min_windows)
328
374
  {
329
375
    model_->detail_selection = true;
330
 
    detail_mode_ = TAB_NEXT_WINDOW_LOOP;
 
376
    detail_mode_ = TAB_NEXT_WINDOW;
331
377
  }
332
378
  else
333
379
  {
378
424
  return view_->ExternalTargets();
379
425
}
380
426
 
381
 
bool Controller::CompareSwitcherItemsPriority(AbstractLauncherIcon* first,
382
 
                                              AbstractLauncherIcon* second)
 
427
bool Controller::CompareSwitcherItemsPriority(AbstractLauncherIcon::Ptr first,
 
428
                                              AbstractLauncherIcon::Ptr second)
383
429
{
384
 
  if (first->Type() == second->Type())
 
430
  if (first->GetIconType() == second->GetIconType())
385
431
    return first->SwitcherPriority() > second->SwitcherPriority();
386
 
  return first->Type() < second->Type();
 
432
  return first->GetIconType() < second->GetIconType();
387
433
}
388
434
 
389
435
void Controller::SelectFirstItem()
391
437
  if (!model_)
392
438
    return;
393
439
 
394
 
  AbstractLauncherIcon* first  = model_->at(1);
395
 
  AbstractLauncherIcon* second = model_->at(2);
 
440
  AbstractLauncherIcon::Ptr first  = model_->at(1);
 
441
  AbstractLauncherIcon::Ptr second = model_->at(2);
396
442
 
397
443
  if (!first)
398
444
  {
409
455
  unsigned int first_second = 0; // first icons second highest active
410
456
  unsigned int second_first = 0; // second icons first highest active
411
457
 
412
 
  for (guint32 xid : first->RelatedXids())
 
458
  for (guint32 xid : first->Windows())
413
459
  {
414
460
    unsigned int num = WindowManager::Default()->GetWindowActiveNumber(xid);
415
461
 
424
470
    }
425
471
  }
426
472
 
427
 
  for (guint32 xid : second->RelatedXids())
 
473
  for (guint32 xid : second->Windows())
428
474
  {
429
475
    second_first = MAX (WindowManager::Default()->GetWindowActiveNumber(xid), second_first);
430
476
  }