Back to index

unity  6.0.0
ShortcutController.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Andrea Azzarone <azzaronea@gmail.com>
00017  */
00018 
00019 #include "ShortcutController.h"
00020 
00021 #include "unity-shared/UBusMessages.h"
00022 #include "unity-shared/WindowManager.h"
00023 
00024 namespace unity
00025 {
00026 namespace shortcut
00027 {
00028 namespace
00029 {
00030 const unsigned int SUPER_TAP_DURATION = 650;
00031 const unsigned int FADE_DURATION = 100;
00032 } // anonymouse namespace;
00033 
00034 Controller::Controller(std::list<AbstractHint::Ptr>& hints)
00035   : visible_(false)
00036   , enabled_(true)
00037   , fade_in_animator_(FADE_DURATION)
00038   , fade_out_animator_(FADE_DURATION)
00039 {
00040   bg_color_ = nux::Color(0.0, 0.0, 0.0, 0.5);
00041 
00042   ubus_manager_.RegisterInterest(UBUS_BACKGROUND_COLOR_CHANGED,
00043                                  sigc::mem_fun(this, &Controller::OnBackgroundUpdate));
00044 
00045   ubus_manager_.RegisterInterest(UBUS_LAUNCHER_START_KEY_SWTICHER, [&] (GVariant*) {
00046                                    enabled_ = false;
00047                                  });
00048 
00049   ubus_manager_.RegisterInterest(UBUS_LAUNCHER_END_KEY_SWTICHER, [&] (GVariant*) {
00050                                    enabled_ = true;
00051                                  });
00052 
00053   ubus_manager_.RegisterInterest(UBUS_OVERLAY_SHOWN, [&] (GVariant*) {
00054                                    Hide();
00055                                  });
00056 
00057   ubus_manager_.SendMessage(UBUS_BACKGROUND_REQUEST_COLOUR_EMIT);
00058 
00059   model_.reset(new Model(hints));
00060 
00061   model_->Fill();
00062 
00063   fade_in_animator_.animation_updated.connect(sigc::mem_fun(this, &Controller::OnFadeInUpdated));
00064   fade_in_animator_.animation_ended.connect(sigc::mem_fun(this, &Controller::OnFadeInEnded));
00065   fade_out_animator_.animation_updated.connect(sigc::mem_fun(this, &Controller::OnFadeOutUpdated));
00066   fade_out_animator_.animation_ended.connect(sigc::mem_fun(this, &Controller::OnFadeOutEnded));
00067 }
00068 
00069 void Controller::OnFadeInUpdated(double opacity)
00070 {
00071   view_window_->SetOpacity(opacity);
00072 }
00073 
00074 void Controller::OnFadeInEnded()
00075 {
00076   view_window_->SetOpacity(1.0);
00077 }
00078 
00079 void Controller::OnFadeOutUpdated(double progress)
00080 {
00081   double opacity = CLAMP(1.0f - progress, 0.0f, 1.0f);
00082   view_window_->SetOpacity(opacity);
00083 }
00084 
00085 void Controller::OnFadeOutEnded()
00086 {
00087   view_window_->SetOpacity(0.0);
00088 }
00089 
00090 
00091 void Controller::OnBackgroundUpdate(GVariant* data)
00092 {
00093   gdouble red, green, blue, alpha;
00094   g_variant_get(data, "(dddd)", &red, &green, &blue, &alpha);
00095   bg_color_ = nux::Color(red, green, blue, alpha);
00096 
00097   if (view_)
00098     view_->background_color = bg_color_;
00099 }
00100 
00101 bool Controller::Show()
00102 {
00103   if (enabled_)
00104   {
00105     show_timer_.reset(new glib::Timeout(SUPER_TAP_DURATION, sigc::mem_fun(this, &Controller::OnShowTimer)));
00106     model_->Fill();
00107     visible_ = true;
00108 
00109     return true;
00110   }
00111 
00112   return false;
00113 }
00114 
00115 bool Controller::OnShowTimer()
00116 {
00117   if (!enabled_)
00118     return false;
00119 
00120   EnsureView();
00121 
00122   nux::Geometry geo;
00123   if (!view_->GetBaseGeometry(geo))
00124     return false;
00125 
00126   view_window_->SetGeometry(geo);
00127 
00128   if (visible_)
00129   {
00130     view_->SetupBackground(true);
00131     fade_out_animator_.Stop();
00132     fade_in_animator_.Start(view_window_->GetOpacity());
00133   }
00134 
00135   return false;
00136 }
00137 
00138 void Controller::ConstructView()
00139 {
00140   view_ = View::Ptr(new View());
00141   AddChild(view_.GetPointer());
00142   view_->SetModel(model_);
00143   view_->background_color = bg_color_;
00144 
00145   if (!view_window_)
00146   {
00147     main_layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
00148     main_layout_->SetVerticalExternalMargin(0);
00149     main_layout_->SetHorizontalExternalMargin(0);
00150 
00151     view_window_ = new nux::BaseWindow("ShortcutHint");
00152     view_window_->SetLayout(main_layout_);
00153     view_window_->SetBackgroundColor(nux::Color(0x00000000));
00154   }
00155 
00156   main_layout_->AddView(view_.GetPointer());
00157 
00158   view_->SetupBackground(false);
00159   view_window_->SetOpacity(0.0);
00160   view_window_->ShowWindow(true);
00161 }
00162 
00163 void Controller::EnsureView()
00164 {
00165   if (!view_window_)
00166     ConstructView();
00167 }
00168 
00169 void Controller::SetAdjustment(int x, int y)
00170 {
00171   EnsureView();
00172 
00173   view_->SetAdjustment(x, y);
00174 }
00175 
00176 
00177 void Controller::Hide()
00178 {
00179   if (!visible_)
00180     return;
00181 
00182   visible_ = false;
00183   show_timer_.reset();
00184 
00185   if (view_window_)
00186   {
00187     view_->SetupBackground(false);
00188     fade_in_animator_.Stop();
00189     fade_out_animator_.Start(1.0 - view_window_->GetOpacity());
00190   }
00191 }
00192 
00193 bool Controller::Visible()
00194 {
00195   return visible_;
00196 }
00197 
00198 bool Controller::IsEnabled()
00199 {
00200   return enabled_;
00201 }
00202 
00203 void Controller::SetEnabled(bool enabled)
00204 {
00205   enabled_ = enabled;
00206 }
00207 
00208 //
00209 // Introspection
00210 //
00211 std::string Controller::GetName() const
00212 {
00213   return "ShortcutController";
00214 }
00215 
00216 void Controller::AddProperties(GVariantBuilder* builder)
00217 {
00218   unity::variant::BuilderWrapper(builder)
00219   .add("timeout_duration", SUPER_TAP_DURATION + FADE_DURATION)
00220   .add("enabled", IsEnabled())
00221   .add("about_to_show", (Visible() && !fade_out_animator_.IsRunning() && view_window_ && view_window_->GetOpacity() != 1.0f))
00222   .add("about_to_hide", (Visible() && !fade_in_animator_.IsRunning() && view_window_ && view_window_->GetOpacity() != 1.0f))
00223   .add("visible", (Visible() && view_window_ && view_window_->GetOpacity() == 1.0f));
00224 }
00225 
00226 } // namespace shortcut
00227 } // namespace unity