Back to index

unity  6.0.0
LensBar.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 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: Neil Jagdish Patel <neil.patel@canonical.com>
00017  */
00018 
00019 #include <NuxCore/Logger.h>
00020 #include "config.h"
00021 
00022 #include "unity-shared/CairoTexture.h"
00023 #include "LensBar.h"
00024 
00025 namespace unity
00026 {
00027 namespace dash
00028 {
00029 namespace
00030 {
00031 
00032 nux::logging::Logger logger("unity.dash.lensbar");
00033 
00034 const int LENSBAR_HEIGHT = 44;
00035 
00036 }
00037 
00038 NUX_IMPLEMENT_OBJECT_TYPE(LensBar);
00039 
00040 LensBar::LensBar()
00041   : nux::View(NUX_TRACKER_LOCATION)
00042 {
00043   SetupBackground();
00044   SetupLayout();
00045   SetupHomeLens();
00046 }
00047 
00048 void LensBar::SetupBackground()
00049 {
00050   nux::ROPConfig rop;
00051   rop.Blend = true;
00052   rop.SrcBlend = GL_ONE;
00053   rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;
00054   bg_layer_.reset(new nux::ColorLayer(nux::Color(0.0f, 0.0f, 0.0f, 0.2f), true, rop));
00055 }
00056 
00057 void LensBar::SetupLayout()
00058 {
00059   layout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
00060   layout_->SetContentDistribution(nux::MAJOR_POSITION_CENTER);
00061   SetLayout(layout_);
00062 
00063   SetMinimumHeight(LENSBAR_HEIGHT);
00064   SetMaximumHeight(LENSBAR_HEIGHT);
00065 }
00066 
00067 void LensBar::SetupHomeLens()
00068 {
00069   LensBarIcon* icon = new LensBarIcon("home.lens", PKGDATADIR"/lens-nav-home.svg");
00070   icon->SetVisible(true);
00071   icon->active = true;
00072   icons_.push_back(icon);
00073   layout_->AddView(icon, 0, nux::eCenter, nux::MINOR_SIZE_FULL);
00074   AddChild(icon);
00075 
00076   icon->mouse_click.connect([&, icon] (int x, int y, unsigned long button, unsigned long keyboard) { SetActive(icon); });
00077   icon->key_nav_focus_activate.connect([&, icon](nux::Area*){ SetActive(icon); });
00078 }
00079 
00080 void LensBar::AddLens(Lens::Ptr& lens)
00081 {
00082   LensBarIcon* icon = new LensBarIcon(lens->id, lens->icon_hint);
00083   icon->SetVisible(lens->visible);
00084   lens->visible.changed.connect([icon](bool visible) { icon->SetVisible(visible); } );
00085   icons_.push_back(icon);
00086   layout_->AddView(icon, 0, nux::eCenter, nux::eFix);
00087   AddChild(icon);
00088 
00089   icon->mouse_click.connect([&, icon] (int x, int y, unsigned long button, unsigned long keyboard) { SetActive(icon); });
00090   icon->key_nav_focus_activate.connect([&, icon](nux::Area*){ SetActive(icon); });
00091 }
00092 
00093 void LensBar::Activate(std::string id)
00094 {
00095   for (auto icon: icons_)
00096   {
00097     if (icon->id == id)
00098     {
00099       SetActive(icon);
00100       break;
00101     }
00102   }
00103 }
00104 
00105 void LensBar::Draw(nux::GraphicsEngine& gfx_context, bool force_draw)
00106 {
00107   nux::Geometry const& base = GetGeometry();
00108 
00109   gfx_context.PushClippingRectangle(base);
00110   nux::GetPainter().PaintBackground(gfx_context, base);
00111 
00112   bg_layer_->SetGeometry(base);
00113   nux::GetPainter().RenderSinglePaintLayer(gfx_context, base, bg_layer_.get());
00114 
00115   gfx_context.PopClippingRectangle();
00116 }
00117 
00118 void LensBar::DrawContent(nux::GraphicsEngine& gfx_context, bool force_draw)
00119 {
00120   nux::Geometry const& base = GetGeometry();
00121 
00122   gfx_context.PushClippingRectangle(base);
00123 
00124   if (!IsFullRedraw())
00125     nux::GetPainter().PushLayer(gfx_context, bg_layer_->GetGeometry(), bg_layer_.get());
00126 
00127   layout_->ProcessDraw(gfx_context, force_draw);
00128 
00129   if (!IsFullRedraw())
00130     nux::GetPainter().PopBackground();
00131 
00132   for (auto icon: icons_)
00133   {
00134     if (icon->active)
00135     {
00136       nux::Geometry const& geo = icon->GetGeometry();
00137       int middle = geo.x + geo.width/2;
00138       int size = 5;
00139       // Nux doesn't draw too well the small triangles, so let's draw a
00140       // bigger one and clip part of them using the "-1".
00141       int y = base.y - 1;
00142 
00143       nux::GetPainter().Draw2DTriangleColor(gfx_context,
00144                                             middle - size, y,
00145                                             middle, y + size,
00146                                             middle + size, y,
00147                                             nux::color::White);
00148 
00149       break;
00150 
00151     }
00152   }
00153 
00154   gfx_context.PopClippingRectangle();
00155 }
00156 
00157 void LensBar::SetActive(LensBarIcon* activated)
00158 {
00159   bool state_changed = false;
00160 
00161   for (auto icon: icons_)
00162   {
00163     bool state = icon == activated;
00164 
00165     if (icon->active != state)
00166       state_changed = true;
00167 
00168     icon->active = state;
00169   }
00170 
00171   if (state_changed)
00172     lens_activated.emit(activated->id);
00173 }
00174 
00175 void LensBar::ActivateNext()
00176 {
00177   // Special case when switching from the command lens.
00178   if (GetActiveLensId() == "commands.lens")
00179   {
00180     SetActive(icons_[0]);
00181     return;
00182   }
00183 
00184   bool activate_next = false;
00185   for (auto it = icons_.begin();
00186        it < icons_.end();
00187        it++)
00188   {
00189     LensBarIcon *icon = *it;
00190 
00191     if (activate_next && icon->IsVisible())
00192     {
00193       SetActive(icon);
00194       return;
00195     }
00196     if (icon->active)
00197       activate_next = true;
00198   }
00199   SetActive(icons_[0]);
00200 
00201 }
00202 
00203 void LensBar::ActivatePrevious()
00204 {
00205   // Special case when switching from the command lens.
00206   if (GetActiveLensId() == "commands.lens")
00207   {
00208     SetActive(icons_.back());
00209     return;
00210   }
00211 
00212   bool activate_previous = false;
00213   for (auto it = icons_.rbegin();
00214        it < icons_.rend();
00215        ++it)
00216   {
00217     LensBarIcon *icon = *it;
00218 
00219     if (activate_previous && icon->IsVisible())
00220     {
00221        SetActive(icon);
00222        return;
00223     }
00224     if (icon->active)
00225       activate_previous = true;
00226   }
00227   SetActive(icons_.back());
00228 
00229 }
00230 
00231 // Keyboard navigation
00232 bool LensBar::AcceptKeyNavFocus()
00233 {
00234   return false;
00235 }
00236 
00237 // Introspectable
00238 std::string LensBar::GetName() const
00239 {
00240   return "LensBar";
00241 }
00242 
00243 void LensBar::AddProperties(GVariantBuilder* builder)
00244 {
00245   unity::variant::BuilderWrapper wrapper(builder);
00246 
00247   wrapper.add("focused-lens-icon", "");
00248 
00249   for( auto icon : icons_)
00250   {
00251     if (icon->active)
00252       wrapper.add("active-lens", icon->id.Get());
00253 
00254     if (icon->HasKeyFocus())
00255       wrapper.add("focused-lens-icon", icon->id.Get());
00256   }
00257 }
00258 
00259 std::string LensBar::GetActiveLensId() const
00260 {
00261   for (auto icon : icons_)
00262   {
00263     if (icon->active)
00264       return icon->id;
00265   }
00266   return "";
00267 }
00268 
00269 }
00270 }