~3v1n0/unity/light-shortcuts

« back to all changes in this revision

Viewing changes to panel/PanelController.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-04-26 12:41:09 UTC
  • Revision ID: mail@3v1n0.net-20130426124109-t3b2shjah2omiqa2
Unity: Remove all the views, but the Shortcuts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
2
 
/*
3
 
 * Copyright (C) 2011 Canonical Ltd
4
 
 *
5
 
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License version 3 as
7
 
 * published by the Free Software Foundation.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
 *
17
 
 * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
18
 
 */
19
 
 
20
 
#include "PanelController.h"
21
 
 
22
 
#include <vector>
23
 
#include <NuxCore/Logger.h>
24
 
#include <Nux/BaseWindow.h>
25
 
#include <UnityCore/Variant.h>
26
 
 
27
 
#include "unity-shared/UScreen.h"
28
 
#include "PanelView.h"
29
 
#include "unity-shared/PanelStyle.h"
30
 
 
31
 
namespace unity
32
 
{
33
 
namespace panel
34
 
{
35
 
DECLARE_LOGGER(logger, "unity.panel.controller");
36
 
 
37
 
const char* window_title = "unity-panel";
38
 
 
39
 
class Controller::Impl
40
 
{
41
 
public:
42
 
  Impl();
43
 
 
44
 
  void FirstMenuShow();
45
 
  void QueueRedraw();
46
 
 
47
 
  std::vector<nux::View*> GetPanelViews() const;
48
 
  std::vector<nux::Geometry> GetGeometries() const;
49
 
 
50
 
  // NOTE: nux::Property maybe?
51
 
  void SetLauncherWidth(int width);
52
 
  void SetOpacity(float opacity);
53
 
  void SetOpacityMaximizedToggle(bool enabled);
54
 
 
55
 
  float opacity() const;
56
 
 
57
 
  void SetMenuShowTimings(int fadein, int fadeout, int discovery,
58
 
                          int discovery_fadein, int discovery_fadeout);
59
 
 
60
 
  void OnScreenChanged(unsigned int primary_monitor, std::vector<nux::Geometry>& monitors, Introspectable *iobj);
61
 
 
62
 
  typedef nux::ObjectPtr<nux::BaseWindow> BaseWindowPtr;
63
 
 
64
 
  unity::PanelView* ViewForWindow(BaseWindowPtr const& window) const;
65
 
 
66
 
  static void WindowConfigureCallback(int            window_width,
67
 
                                      int            window_height,
68
 
                                      nux::Geometry& geo,
69
 
                                      void*          user_data);
70
 
 
71
 
  std::vector<BaseWindowPtr> windows_;
72
 
  std::vector<Window> tray_xids_;
73
 
  float opacity_;
74
 
  bool opacity_maximized_toggle_;
75
 
  int menus_fadein_;
76
 
  int menus_fadeout_;
77
 
  int menus_discovery_;
78
 
  int menus_discovery_fadein_;
79
 
  int menus_discovery_fadeout_;
80
 
  indicator::DBusIndicators::Ptr dbus_indicators_;
81
 
};
82
 
 
83
 
 
84
 
Controller::Impl::Impl()
85
 
  : opacity_(1.0f)
86
 
  , opacity_maximized_toggle_(false)
87
 
  , menus_fadein_(0)
88
 
  , menus_fadeout_(0)
89
 
  , menus_discovery_(0)
90
 
  , menus_discovery_fadein_(0)
91
 
  , menus_discovery_fadeout_(0)
92
 
  , dbus_indicators_(std::make_shared<indicator::DBusIndicators>())
93
 
{}
94
 
 
95
 
std::vector<nux::View*> Controller::Impl::GetPanelViews() const
96
 
{
97
 
  std::vector<nux::View*> views;
98
 
  views.reserve(windows_.size());
99
 
  for (auto const& window: windows_)
100
 
    views.push_back(ViewForWindow(window));
101
 
  return views;
102
 
}
103
 
 
104
 
std::vector<nux::Geometry> Controller::Impl::GetGeometries() const
105
 
{
106
 
  std::vector<nux::Geometry> geometries;
107
 
 
108
 
  for (auto const& window : windows_)
109
 
  {
110
 
    geometries.push_back(window->GetAbsoluteGeometry());
111
 
  }
112
 
 
113
 
  return geometries;
114
 
}
115
 
 
116
 
void Controller::Impl::FirstMenuShow()
117
 
{
118
 
  for (auto const& window: windows_)
119
 
  {
120
 
    if (ViewForWindow(window)->FirstMenuShow())
121
 
      break;
122
 
  }
123
 
}
124
 
 
125
 
void Controller::Impl::SetOpacity(float opacity)
126
 
{
127
 
  opacity_ = opacity;
128
 
 
129
 
  for (auto const& window: windows_)
130
 
  {
131
 
    ViewForWindow(window)->SetOpacity(opacity_);
132
 
  }
133
 
}
134
 
 
135
 
void Controller::Impl::SetLauncherWidth(int width)
136
 
{
137
 
  for (auto const& window: windows_)
138
 
  {
139
 
    ViewForWindow(window)->SetLauncherWidth(width);
140
 
  }
141
 
}
142
 
 
143
 
void Controller::Impl::SetOpacityMaximizedToggle(bool enabled)
144
 
{
145
 
  opacity_maximized_toggle_ = enabled;
146
 
 
147
 
  for (auto const& window: windows_)
148
 
  {
149
 
    ViewForWindow(window)->SetOpacityMaximizedToggle(opacity_maximized_toggle_);
150
 
  }
151
 
}
152
 
 
153
 
void Controller::Impl::SetMenuShowTimings(int fadein, int fadeout, int discovery,
154
 
                                          int discovery_fadein, int discovery_fadeout)
155
 
{
156
 
  menus_fadein_ = fadein;
157
 
  menus_fadeout_ = fadeout;
158
 
  menus_discovery_ = discovery;
159
 
  menus_discovery_fadein_ = discovery_fadein;
160
 
  menus_discovery_fadeout_ = discovery_fadeout;
161
 
 
162
 
  for (auto const& window: windows_)
163
 
  {
164
 
    ViewForWindow(window)->SetMenuShowTimings(fadein, fadeout, discovery,
165
 
                                              discovery_fadein, discovery_fadeout);
166
 
  }
167
 
}
168
 
 
169
 
void Controller::Impl::QueueRedraw()
170
 
{
171
 
  for (auto const& window: windows_)
172
 
  {
173
 
    window->QueueDraw();
174
 
  }
175
 
}
176
 
 
177
 
PanelView* Controller::Impl::ViewForWindow(BaseWindowPtr const& window) const
178
 
{
179
 
  nux::Layout* layout = window->GetLayout();
180
 
  auto it = layout->GetChildren().begin();
181
 
 
182
 
  return static_cast<PanelView*>(*it);
183
 
}
184
 
 
185
 
// We need to put a panel on every monitor, and try and re-use the panels we already have
186
 
void Controller::Impl::OnScreenChanged(unsigned int primary_monitor,
187
 
                                       std::vector<nux::Geometry>& monitors,
188
 
                                       Introspectable *iobj)
189
 
{
190
 
  std::vector<BaseWindowPtr>::iterator it;
191
 
  unsigned n_monitors = monitors.size();
192
 
  unsigned int i = 0;
193
 
 
194
 
  tray_xids_.resize(n_monitors);
195
 
 
196
 
  for (it = windows_.begin(); it != windows_.end(); ++it)
197
 
  {
198
 
    if (i < n_monitors)
199
 
    {
200
 
      PanelView* view;
201
 
 
202
 
      (*it)->EnableInputWindow(false);
203
 
      (*it)->InputWindowEnableStruts(false);
204
 
 
205
 
      nux::Geometry geo = monitors[i];
206
 
      geo.height = panel::Style::Instance().panel_height;
207
 
      (*it)->SetGeometry(geo);
208
 
      (*it)->SetMinMaxSize(geo.width, geo.height);
209
 
 
210
 
      view = ViewForWindow(*it);
211
 
      view->SetPrimary(i == primary_monitor);
212
 
      view->SetMonitor(i);
213
 
      tray_xids_[i] = view->GetTrayXid();
214
 
 
215
 
      if (nux::GetWindowThread()->IsEmbeddedWindow())
216
 
      {
217
 
        (*it)->EnableInputWindow(true);
218
 
        (*it)->InputWindowEnableStruts(true);
219
 
      }
220
 
 
221
 
      LOG_DEBUG(logger) << "Updated Panel for Monitor " << i;
222
 
 
223
 
      i++;
224
 
    }
225
 
    else
226
 
      break;
227
 
  }
228
 
 
229
 
  // Add new ones if needed
230
 
  if (i < n_monitors)
231
 
  {
232
 
    for (i = i; i < n_monitors; i++)
233
 
    {
234
 
      nux::HLayout* layout = new nux::HLayout(NUX_TRACKER_LOCATION);
235
 
 
236
 
      PanelView* view = new PanelView(dbus_indicators_);
237
 
      view->SetMaximumHeight(panel::Style::Instance().panel_height);
238
 
      view->SetOpacity(opacity_);
239
 
      view->SetOpacityMaximizedToggle(opacity_maximized_toggle_);
240
 
      view->SetMenuShowTimings(menus_fadein_, menus_fadeout_, menus_discovery_,
241
 
                               menus_discovery_fadein_, menus_discovery_fadeout_);
242
 
      view->SetPrimary(i == primary_monitor);
243
 
      view->SetMonitor(i);
244
 
      tray_xids_[i] = view->GetTrayXid();
245
 
 
246
 
      layout->AddView(view, 1);
247
 
      layout->SetContentDistribution(nux::MAJOR_POSITION_START);
248
 
      layout->SetVerticalExternalMargin(0);
249
 
      layout->SetHorizontalExternalMargin(0);
250
 
 
251
 
      BaseWindowPtr window(new nux::BaseWindow());
252
 
      nux::Geometry geo = monitors[i];
253
 
      geo.height = panel::Style::Instance().panel_height;
254
 
 
255
 
      window->SetConfigureNotifyCallback(&Impl::WindowConfigureCallback, window.GetPointer());
256
 
      window->SetBackgroundColor(nux::Color(0.0f, 0.0f, 0.0f, 0.0f));
257
 
      window->ShowWindow(true);
258
 
 
259
 
      if (nux::GetWindowThread()->IsEmbeddedWindow())
260
 
        window->EnableInputWindow(true, panel::window_title, false, false);
261
 
 
262
 
      window->SetGeometry(geo);
263
 
      window->SetMinMaxSize(geo.width, geo.height);
264
 
      window->SetLayout(layout);
265
 
      window->InputWindowEnableStruts(true);
266
 
 
267
 
      windows_.push_back(window);
268
 
 
269
 
      // add to introspectable tree:
270
 
      iobj->AddChild(view);
271
 
 
272
 
      LOG_DEBUG(logger) << "Added Panel for Monitor " << i;
273
 
    }
274
 
  }
275
 
 
276
 
  if (windows_.size() > n_monitors)
277
 
  {
278
 
    LOG_DEBUG(logger) << "Removed extra Panels";
279
 
    windows_.erase(it, windows_.end());
280
 
  }
281
 
}
282
 
 
283
 
void Controller::Impl::WindowConfigureCallback(int window_width,
284
 
                                               int window_height,
285
 
                                               nux::Geometry& geo,
286
 
                                               void* user_data)
287
 
{
288
 
  nux::BaseWindow* window = static_cast<nux::BaseWindow*>(user_data);
289
 
  geo = window->GetGeometry();
290
 
}
291
 
 
292
 
bool Controller::IsMouseInsideIndicator(nux::Point const& mouse_position) const
293
 
{
294
 
  for (auto view : pimpl->GetPanelViews())
295
 
  {
296
 
    PanelView* panel_view = static_cast<PanelView*>(view);
297
 
 
298
 
    if (panel_view->IsMouseInsideIndicator(mouse_position))
299
 
      return true;
300
 
  }
301
 
 
302
 
  return false;
303
 
}
304
 
 
305
 
float Controller::Impl::opacity() const
306
 
{
307
 
  return opacity_;
308
 
}
309
 
 
310
 
Controller::Controller()
311
 
  : launcher_width(64)
312
 
  , pimpl(new Impl())
313
 
{
314
 
  UScreen* screen = UScreen::GetDefault();
315
 
  screen->changed.connect(sigc::mem_fun(this, &Controller::OnScreenChanged));
316
 
  OnScreenChanged(screen->GetPrimaryMonitor(), screen->GetMonitors());
317
 
 
318
 
  launcher_width.changed.connect([&] (int width)
319
 
  {
320
 
    pimpl->SetLauncherWidth(width);
321
 
  });
322
 
}
323
 
 
324
 
Controller::~Controller()
325
 
{
326
 
  delete pimpl;
327
 
}
328
 
 
329
 
void Controller::FirstMenuShow()
330
 
{
331
 
  pimpl->FirstMenuShow();
332
 
}
333
 
 
334
 
void Controller::SetOpacity(float opacity)
335
 
{
336
 
  pimpl->SetOpacity(opacity);
337
 
}
338
 
 
339
 
void Controller::SetOpacityMaximizedToggle(bool enabled)
340
 
{
341
 
  pimpl->SetOpacityMaximizedToggle(enabled);
342
 
}
343
 
 
344
 
void Controller::SetMenuShowTimings(int fadein, int fadeout, int discovery,
345
 
                                    int discovery_fadein, int discovery_fadeout)
346
 
{
347
 
  pimpl->SetMenuShowTimings(fadein, fadeout, discovery, discovery_fadein, discovery_fadeout);
348
 
}
349
 
 
350
 
void Controller::QueueRedraw()
351
 
{
352
 
  pimpl->QueueRedraw();
353
 
}
354
 
 
355
 
std::vector<Window> const& Controller::GetTrayXids() const
356
 
{
357
 
  return pimpl->tray_xids_;
358
 
}
359
 
 
360
 
std::vector<nux::View*> Controller::GetPanelViews() const
361
 
{
362
 
  return pimpl->GetPanelViews();
363
 
}
364
 
 
365
 
std::vector<nux::Geometry> Controller::GetGeometries() const
366
 
{
367
 
  return pimpl->GetGeometries();
368
 
}
369
 
 
370
 
float Controller::opacity() const
371
 
{
372
 
  return pimpl->opacity();
373
 
}
374
 
 
375
 
std::string Controller::GetName() const
376
 
{
377
 
  return "PanelController";
378
 
}
379
 
 
380
 
void Controller::AddProperties(GVariantBuilder* builder)
381
 
{
382
 
  variant::BuilderWrapper(builder)
383
 
    .add("opacity", pimpl->opacity());
384
 
}
385
 
 
386
 
void Controller::OnScreenChanged(int primary_monitor, std::vector<nux::Geometry>& monitors)
387
 
{
388
 
  pimpl->OnScreenChanged(primary_monitor, monitors, this);
389
 
}
390
 
 
391
 
} // namespace panel
392
 
} // namespace unity