~sethj/ubuntu/wily/unity/fix-for-1445595

« back to all changes in this revision

Viewing changes to shutdown/SessionController.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-02-11 22:41:12 UTC
  • mto: This revision was merged to the branch mainline in revision 3200.
  • Revision ID: mail@3v1n0.net-20130211224112-k1nxtougs7xgdjj5
ShutdownView: first draft implementation

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) 2013 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: Marco Trevisan (Treviño) <marco@ubuntu.com>
 
18
*/
 
19
 
 
20
#include "SessionController.h"
 
21
 
 
22
#include "unity-shared/UBusMessages.h"
 
23
#include "unity-shared/UScreen.h"
 
24
 
 
25
namespace na = nux::animation;
 
26
 
 
27
namespace unity
 
28
{
 
29
namespace session
 
30
{
 
31
namespace
 
32
{
 
33
const unsigned int FADE_DURATION = 100;
 
34
}
 
35
 
 
36
Controller::Controller(session::Manager::Ptr const& manager)
 
37
  : manager_(manager)
 
38
  , bg_color_(0.0, 0.0, 0.0, 0.5)
 
39
  , fade_animator_(FADE_DURATION)
 
40
{
 
41
  ubus_manager_.RegisterInterest(UBUS_BACKGROUND_COLOR_CHANGED,
 
42
                                 sigc::mem_fun(this, &Controller::OnBackgroundUpdate));
 
43
 
 
44
  ubus_manager_.SendMessage(UBUS_BACKGROUND_REQUEST_COLOUR_EMIT);
 
45
 
 
46
  fade_animator_.updated.connect([this] (double opacity) {
 
47
    if (!view_window_)
 
48
      return;
 
49
 
 
50
    view_window_->SetOpacity(opacity);
 
51
 
 
52
    if (opacity == 0.0f && fade_animator_.GetFinishValue() == 0.0f)
 
53
      CloseWindow();
 
54
  });
 
55
}
 
56
 
 
57
void Controller::OnBackgroundUpdate(GVariant* data)
 
58
{
 
59
  gdouble red, green, blue, alpha;
 
60
  g_variant_get(data, "(dddd)", &red, &green, &blue, &alpha);
 
61
  bg_color_ = nux::Color(red, green, blue, alpha);
 
62
 
 
63
  if (view_)
 
64
    view_->background_color = bg_color_;
 
65
}
 
66
 
 
67
void Controller::Show()
 
68
{
 
69
  EnsureView();
 
70
 
 
71
  view_->SetupBackground(true);
 
72
 
 
73
  int monitor = UScreen::GetDefault()->GetMonitorWithMouse();
 
74
  auto const& offset = GetOffsetPerMonitor(monitor);
 
75
  view_window_->SetXY(offset.x, offset.y);
 
76
  view_window_->EnableInputWindow(true, view_window_->GetWindowName().c_str(), true, false);
 
77
  view_window_->ShowWindow(true);
 
78
  view_window_->PushToFront();
 
79
 
 
80
  if (fade_animator_.CurrentState() == na::Animation::State::Running)
 
81
  {
 
82
    if (fade_animator_.GetFinishValue() == 0.0f)
 
83
      fade_animator_.Reverse();
 
84
  }
 
85
  else
 
86
  {
 
87
    fade_animator_.SetStartValue(0.0f).SetFinishValue(1.0f).Start();
 
88
  }
 
89
}
 
90
 
 
91
nux::Point Controller::GetOffsetPerMonitor(int monitor)
 
92
{
 
93
  EnsureView();
 
94
 
 
95
  // view_window_->ComputeContentSize();
 
96
  auto const& view_geo = view_->GetAbsoluteGeometry();
 
97
  auto const& monitor_geo = UScreen::GetDefault()->GetMonitorGeometry(monitor);
 
98
 
 
99
  //TODO get adjustment from windowmanager!
 
100
  nux::Point offset(adjustment_.x + monitor_geo.x, adjustment_.y + monitor_geo.y);
 
101
  offset.x += (monitor_geo.width - view_geo.width - adjustment_.x) / 2;
 
102
  offset.y += (monitor_geo.height - view_geo.height - adjustment_.y) / 2;
 
103
 
 
104
  return offset;
 
105
}
 
106
 
 
107
void Controller::ConstructView()
 
108
{
 
109
  view_ = View::Ptr(new View(manager_));
 
110
  view_->SetupBackground(false);
 
111
  view_->background_color = bg_color_;
 
112
  AddChild(view_.GetPointer());
 
113
 
 
114
  auto layout = new nux::HLayout(NUX_TRACKER_LOCATION);
 
115
  layout->SetVerticalExternalMargin(0);
 
116
  layout->SetHorizontalExternalMargin(0);
 
117
  layout->AddView(view_.GetPointer());
 
118
 
 
119
  view_window_ = new nux::BaseWindow("SessionManager");
 
120
  view_window_->SetLayout(layout);
 
121
  view_window_->SetBackgroundColor(nux::color::Transparent);
 
122
  view_window_->SetWindowSizeMatchLayout(true);
 
123
  view_window_->ShowWindow(false);
 
124
}
 
125
 
 
126
void Controller::EnsureView()
 
127
{
 
128
  if (!view_window_)
 
129
    ConstructView();
 
130
}
 
131
 
 
132
void Controller::Hide()
 
133
{
 
134
  if (fade_animator_.CurrentState() == na::Animation::State::Running)
 
135
  {
 
136
    if (fade_animator_.GetFinishValue() == 1.0f)
 
137
      fade_animator_.Reverse();
 
138
  }
 
139
  else
 
140
  {
 
141
    fade_animator_.SetStartValue(1.0f).SetFinishValue(0.0f).Start();
 
142
  }
 
143
}
 
144
 
 
145
void Controller::CloseWindow()
 
146
{
 
147
  view_window_->PushToBack();
 
148
  view_window_->ShowWindow(false);
 
149
  view_window_->EnableInputWindow(false);
 
150
  view_->SetupBackground(false);
 
151
}
 
152
 
 
153
//
 
154
// Introspection
 
155
//
 
156
std::string Controller::GetName() const
 
157
{
 
158
  return "ShutdownController";
 
159
}
 
160
 
 
161
void Controller::AddProperties(GVariantBuilder* builder)
 
162
{
 
163
  unity::variant::BuilderWrapper(builder)
 
164
  .add("visible", (view_window_ && view_window_->GetOpacity() == 1.0f));
 
165
}
 
166
 
 
167
} // namespace session
 
168
} // namespace unity