~bilalakhtar/unity/software-center-integration-for-o

« back to all changes in this revision

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

  • Committer: Bilal Akhtar
  • Date: 2012-01-15 14:03:49 UTC
  • mfrom: (1346.2.488 unity)
  • Revision ID: bilalakhtar@ubuntu.com-20120115140349-mht4dke0occbsyra
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Andrea Azzarone <azzaronea@gmail.com>
 
17
 */
 
18
 
 
19
#include "ShortcutController.h"
 
20
 
 
21
#include "UBusMessages.h"
 
22
#include "ubus-server.h"
 
23
#include "WindowManager.h"
 
24
 
 
25
namespace unity
 
26
{
 
27
namespace shortcut
 
28
{
 
29
namespace
 
30
{
 
31
const unsigned int SUPER_TAP_DURATION = 650;
 
32
} // anonymouse namespace;
 
33
  
 
34
Controller::Controller(std::list<AbstractHint*>& hints)
 
35
  : view_window_(0)
 
36
  , visible_(false)
 
37
  , show_timer_(0)
 
38
  , fade_in_animator_(new Animator(100))
 
39
  , fade_out_animator_(new Animator(100))
 
40
 
 
41
{
 
42
  bg_color_ = nux::Color(0.0, 0.0, 0.0, 0.5);
 
43
 
 
44
  UBusServer *ubus = ubus_server_get_default();
 
45
  bg_update_handle_ = ubus_server_register_interest(ubus, UBUS_BACKGROUND_COLOR_CHANGED,
 
46
                                                    (UBusCallback)&Controller::OnBackgroundUpdate,
 
47
                                                    this);
 
48
 
 
49
  model_.reset(new Model(hints));
 
50
  
 
51
  model_->Fill();
 
52
  ConstructView();
 
53
 
 
54
  fade_in_animator_->animation_updated.connect(sigc::mem_fun(this, &Controller::OnFadeInUpdated));
 
55
  fade_in_animator_->animation_ended.connect(sigc::mem_fun(this, &Controller::OnFadeInEnded));
 
56
  fade_out_animator_->animation_updated.connect(sigc::mem_fun(this, &Controller::OnFadeOutUpdated));
 
57
  fade_out_animator_->animation_ended.connect(sigc::mem_fun(this, &Controller::OnFadeOutEnded));
 
58
}
 
59
 
 
60
Controller::~Controller()
 
61
{
 
62
  ubus_server_unregister_interest(ubus_server_get_default(), bg_update_handle_);
 
63
  
 
64
  if (fade_in_animator_)
 
65
    delete fade_in_animator_;
 
66
 
 
67
  if (fade_out_animator_)
 
68
    delete fade_out_animator_;
 
69
  
 
70
  if (view_window_)
 
71
    view_window_->UnReference();
 
72
    
 
73
  view_.Release();
 
74
}
 
75
 
 
76
 
 
77
void Controller::OnFadeInUpdated(double opacity)
 
78
{
 
79
  view_window_->SetOpacity(opacity);
 
80
}
 
81
 
 
82
void Controller::OnFadeInEnded()
 
83
{
 
84
  view_window_->SetOpacity(1.0);
 
85
}
 
86
 
 
87
void Controller::OnFadeOutUpdated(double progress)
 
88
{
 
89
  double opacity = CLAMP(1.0f - progress, 0.0f, 1.0f);
 
90
  view_window_->SetOpacity(opacity);
 
91
}
 
92
 
 
93
void Controller::OnFadeOutEnded()
 
94
{
 
95
  view_window_->SetOpacity(0.0);
 
96
}
 
97
 
 
98
 
 
99
void Controller::OnBackgroundUpdate(GVariant* data, Controller* self)
 
100
{
 
101
  gdouble red, green, blue, alpha;
 
102
  g_variant_get(data, "(dddd)", &red, &green, &blue, &alpha);
 
103
  self->bg_color_ = nux::Color(red, green, blue, alpha);
 
104
 
 
105
  if (self->view_)
 
106
    self->view_->background_color = self->bg_color_;
 
107
}
 
108
 
 
109
void Controller::Show()
 
110
{
 
111
  if (show_timer_)
 
112
    g_source_remove (show_timer_);
 
113
  show_timer_ = g_timeout_add(SUPER_TAP_DURATION, &Controller::OnShowTimer, this);
 
114
 
 
115
  model_->Fill();
 
116
  visible_ = true;
 
117
}
 
118
 
 
119
gboolean Controller::OnShowTimer(gpointer data)
 
120
{
 
121
  Controller* self = static_cast<Controller*>(data);
 
122
  
 
123
  ubus_server_send_message(ubus_server_get_default(),
 
124
                           UBUS_PLACE_VIEW_CLOSE_REQUEST,
 
125
                           NULL);
 
126
 
 
127
  if (self->visible_)
 
128
  {
 
129
    self->view_->SetupBackground(true);
 
130
    self->fade_out_animator_->Stop();
 
131
    self->fade_in_animator_->Start(self->view_window_->GetOpacity());
 
132
  }
 
133
 
 
134
  self->show_timer_ = 0;
 
135
  return FALSE;
 
136
}
 
137
 
 
138
void Controller::ConstructView()
 
139
{
 
140
  view_ = View::Ptr(new View());
 
141
  view_->SetModel(model_);
 
142
  view_->background_color = bg_color_;
 
143
 
 
144
  if (!view_window_)
 
145
  {
 
146
    main_layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
 
147
    main_layout_->SetVerticalExternalMargin(0);
 
148
    main_layout_->SetHorizontalExternalMargin(0);
 
149
 
 
150
    view_window_ = new nux::BaseWindow("ShortcutHint");
 
151
    view_window_->SinkReference();
 
152
    view_window_->SetLayout(main_layout_);
 
153
    view_window_->SetBackgroundColor(nux::Color(0x00000000));
 
154
  }
 
155
 
 
156
  main_layout_->AddView(view_.GetPointer(), 1);
 
157
 
 
158
  view_->SetupBackground(false);
 
159
  view_window_->SetOpacity(0.0);
 
160
  view_window_->ShowWindow(true);
 
161
}
 
162
 
 
163
void Controller::SetWorkspace(nux::Geometry const& geo)
 
164
{
 
165
  workarea_ = geo;
 
166
  view_window_->SetGeometry(workarea_);
 
167
}
 
168
 
 
169
void Controller::Hide()
 
170
{
 
171
  if (!visible_)
 
172
    return;
 
173
    
 
174
  visible_ = false;
 
175
 
 
176
  if (view_window_)
 
177
  {
 
178
    view_->SetupBackground(false);
 
179
    fade_in_animator_->Stop();
 
180
    fade_out_animator_->Start(1.0 - view_window_->GetOpacity());
 
181
  }
 
182
 
 
183
  if (show_timer_)
 
184
    g_source_remove(show_timer_);
 
185
  show_timer_ = 0;
 
186
}
 
187
 
 
188
bool Controller::Visible()
 
189
{
 
190
  return visible_;
 
191
}
 
192
 
 
193
} // namespace shortcut
 
194
} // namespace unity