~unity-team/unity/trunk

1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
1
/*
2919.3.32 by Marco Trevisan (Treviño)
ShortcutView: updating copyright
2
 * Copyright (C) 2011-2013 Canonical Ltd
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
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>
1739.7.8 by Andrea Azzarone
Jay's work merged.
17
 *              Jay Taoko <jay.taoko@canonical.com>
2919.3.32 by Marco Trevisan (Treviño)
ShortcutView: updating copyright
18
 *              Marco Trevisan <marco.trevisan@canonical.com>
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
19
 */
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
20
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
21
#include "ShortcutView.h"
22
23
#include <glib/gi18n-lib.h>
1739.7.14 by Andrea Azzarone
Updates the shortcuts window if a shortcut option changes.
24
#include <UnityCore/GLibWrapper.h>
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
25
2350.2.4 by Gord Allott
shortcuts and unityshell working woo
26
#include "unity-shared/LineSeparator.h"
27
#include "unity-shared/StaticCairoText.h"
1739.7.8 by Andrea Azzarone
Jay's work merged.
28
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
29
namespace unity
30
{
31
namespace shortcut
32
{
1739.7.8 by Andrea Azzarone
Jay's work merged.
33
namespace
34
{
2919.3.27 by Marco Trevisan (Treviño)
ShortcutView: just use layouts instead of views
35
  const unsigned SECTION_NAME_FONT_SIZE = 17/1.33;
36
  const unsigned SHORTKEY_ENTRY_FONT_SIZE = 13/1.33;
37
  const unsigned INTER_SPACE_SHORTKEY_DESCRIPTION = 10;
38
  const unsigned SHORTKEY_COLUMN_WIDTH = 150;
39
  const unsigned DESCRIPTION_COLUMN_WIDTH = 265;
40
  const unsigned LINE_SPACING = 5;
2209.6.1 by Andrea Azzarone
Fix bug #971332.
41
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
42
  // We need this class because SetVisible doesn't work for layouts.
2209.6.1 by Andrea Azzarone
Fix bug #971332.
43
  class SectionView : public nux::View
44
  {
45
    public:
46
      SectionView(NUX_FILE_LINE_DECL)
47
        : nux::View(NUX_FILE_LINE_PARAM)
2919.3.27 by Marco Trevisan (Treviño)
ShortcutView: just use layouts instead of views
48
      {}
2209.6.1 by Andrea Azzarone
Fix bug #971332.
49
2919.3.30 by Marco Trevisan (Treviño)
ShortcutView: disconnect from hint signal, when a section view is destroyed
50
      ~SectionView()
51
      {
52
        key_changed_conn.disconnect();
53
      }
54
55
      sigc::connection key_changed_conn;
56
2209.6.1 by Andrea Azzarone
Fix bug #971332.
57
    protected:
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
58
      void Draw(nux::GraphicsEngine& graphics_engine, bool force_draw)
59
      {}
60
61
      void DrawContent(nux::GraphicsEngine& graphics_engine, bool force_draw)
62
      {
63
        if (GetLayout())
64
          GetLayout()->ProcessDraw(graphics_engine, force_draw);
65
      }
2209.6.1 by Andrea Azzarone
Fix bug #971332.
66
  };
67
68
} // unnamed namespace
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
69
70
NUX_IMPLEMENT_OBJECT_TYPE(View);
71
1820.1.63 by Jason Smith
make SwitcherView and ShortcutView use the same code to render their backgrounds
72
View::View()
73
  : ui::UnityWindowView()
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
74
{
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
75
  auto main_layout = new nux::VLayout();
3144.3.17 by Marco Trevisan (Treviño)
ShortcutView: remove some padding, since now is computed into the UnityWindowView
76
  main_layout->SetPadding(30, 18);
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
77
  main_layout->SetSpaceBetweenChildren(20);
78
  SetLayout(main_layout);
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
79
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
80
  std::string header = "<b>"+std::string(_("Keyboard Shortcuts"))+"</b>";
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
81
82
  nux::StaticText* header_view = new nux::StaticText(header, NUX_TRACKER_LOCATION);
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
83
  header_view->SetTextPointSize(20/1.33);
1739.7.10 by Andrea Azzarone
Add bold to the titles.
84
  header_view->SetFontName("Ubuntu");
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
85
  main_layout->AddView(header_view, 1 , nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
86
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
87
  main_layout->AddView(new HSeparator(), 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
88
89
  columns_layout_ = new nux::HLayout();
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
90
  columns_layout_->SetSpaceBetweenChildren(30);
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
91
  main_layout->AddLayout(columns_layout_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
92
}
93
94
void View::SetModel(Model::Ptr model)
95
{
96
  model_ = model;
2919.3.45 by Marco Trevisan (Treviño)
ShortcutView: avoid crashes on null model
97
98
  if (model_)
99
    model_->categories_per_column.changed.connect(sigc::hide(sigc::mem_fun(this, &View::RenderColumns)));
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
100
101
  // Fills the columns...
1739.7.8 by Andrea Azzarone
Jay's work merged.
102
  RenderColumns();
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
103
}
104
105
Model::Ptr View::GetModel()
106
{
107
  return model_;
108
}
109
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
110
nux::LinearLayout* View::CreateSectionLayout(std::string const& section_name)
1739.7.8 by Andrea Azzarone
Jay's work merged.
111
{
112
  nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
113
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
114
  std::string name("<b>"+glib::String(g_markup_escape_text(section_name.c_str(), -1)).Str()+"</b>");
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
115
  nux::StaticText* section_name_view = new nux::StaticText(name, NUX_TRACKER_LOCATION);
1739.7.8 by Andrea Azzarone
Jay's work merged.
116
  section_name_view->SetTextPointSize(SECTION_NAME_FONT_SIZE);
1739.7.10 by Andrea Azzarone
Add bold to the titles.
117
  section_name_view->SetFontName("Ubuntu");
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
118
  layout->AddView(new nux::SpaceLayout(10, 10, 10, 10), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
1739.7.8 by Andrea Azzarone
Jay's work merged.
119
  layout->AddView(section_name_view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
120
  layout->AddView(new nux::SpaceLayout(15, 15, 15, 15), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
1739.7.8 by Andrea Azzarone
Jay's work merged.
121
122
  return layout;
123
}
124
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
125
nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint)
1739.7.8 by Andrea Azzarone
Jay's work merged.
126
{
2919.3.30 by Marco Trevisan (Treviño)
ShortcutView: disconnect from hint signal, when a section view is destroyed
127
  auto* view = new SectionView(NUX_TRACKER_LOCATION);
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
128
1739.7.8 by Andrea Azzarone
Jay's work merged.
129
  nux::HLayout* layout = new nux::HLayout("EntryLayout", NUX_TRACKER_LOCATION);
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
130
  view->SetLayout(layout);
131
1739.7.8 by Andrea Azzarone
Jay's work merged.
132
  nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
133
  nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
134
1739.7.22 by Andrea Azzarone
Fixes.
135
  glib::String shortkey(g_markup_escape_text(hint->shortkey().c_str(), -1));
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
136
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
137
  std::string skey = "<b>"+shortkey.Str()+"</b>";
1739.7.14 by Andrea Azzarone
Updates the shortcuts window if a shortcut option changes.
138
  nux::StaticText* shortkey_view = new nux::StaticText(skey, NUX_TRACKER_LOCATION);
1739.7.8 by Andrea Azzarone
Jay's work merged.
139
  shortkey_view->SetTextAlignment(nux::StaticText::ALIGN_LEFT);
1739.7.10 by Andrea Azzarone
Add bold to the titles.
140
  shortkey_view->SetFontName("Ubuntu");
1739.7.8 by Andrea Azzarone
Jay's work merged.
141
  shortkey_view->SetTextPointSize(SHORTKEY_ENTRY_FONT_SIZE);
142
  shortkey_view->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH);
143
  shortkey_view->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
144
1739.7.22 by Andrea Azzarone
Fixes.
145
  glib::String es_desc(g_markup_escape_text(hint->description().c_str(), -1));
1739.7.8 by Andrea Azzarone
Jay's work merged.
146
1739.7.14 by Andrea Azzarone
Updates the shortcuts window if a shortcut option changes.
147
  nux::StaticText* description_view = new nux::StaticText(es_desc.Value(), NUX_TRACKER_LOCATION);
1739.7.8 by Andrea Azzarone
Jay's work merged.
148
  description_view->SetTextAlignment(nux::StaticText::ALIGN_LEFT);
1739.7.10 by Andrea Azzarone
Add bold to the titles.
149
  shortkey_view->SetFontName("Ubuntu");
1739.7.8 by Andrea Azzarone
Jay's work merged.
150
  description_view->SetTextPointSize(SHORTKEY_ENTRY_FONT_SIZE);
151
  description_view->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH);
152
  description_view->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH);
153
154
  shortkey_layout->AddView(shortkey_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
155
  shortkey_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
156
  shortkey_layout->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH);
157
  shortkey_layout->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH);
158
159
  description_layout->AddView(description_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
160
  description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
161
  description_layout->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH);
162
  description_layout->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH);
163
164
  layout->AddLayout(shortkey_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
165
  layout->AddLayout(description_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
166
  layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION);
167
  description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
168
2919.3.30 by Marco Trevisan (Treviño)
ShortcutView: disconnect from hint signal, when a section view is destroyed
169
  view->key_changed_conn = hint->shortkey.changed.connect([this, view, shortkey_view] (std::string const& new_key) {
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
170
    bool enabled = !new_key.empty();
171
    shortkey_view->SetText(enabled ? "<b>"+new_key+"</b>" : "");
172
    view->SetVisible(enabled);
173
    QueueRelayout();
174
  });
175
176
  view->SetVisible(!shortkey.Str().empty());
177
178
  return view;
1739.7.8 by Andrea Azzarone
Jay's work merged.
179
}
180
181
nux::LinearLayout* View::CreateIntermediateLayout()
182
{
183
  nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
184
  layout->SetSpaceBetweenChildren(LINE_SPACING);
185
186
  return layout;
187
}
188
1820.1.63 by Jason Smith
make SwitcherView and ShortcutView use the same code to render their backgrounds
189
nux::Geometry View::GetBackgroundGeometry()
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
190
{
2919.3.63 by Marco Trevisan (Treviño)
ShortcutView: compute content size on columns rendering
191
  return GetGeometry();
1820.1.63 by Jason Smith
make SwitcherView and ShortcutView use the same code to render their backgrounds
192
}
193
3144.3.9 by Marco Trevisan (Treviño)
UnityWindowView: pass the internal clip as const&
194
void View::DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry const& clip)
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
195
{
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
196
  view_layout_->ProcessDraw(GfxContext, force_draw);
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
197
}
198
1739.7.8 by Andrea Azzarone
Jay's work merged.
199
void View::RenderColumns()
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
200
{
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
201
  columns_layout_->Clear();
202
2919.3.45 by Marco Trevisan (Treviño)
ShortcutView: avoid crashes on null model
203
  if (!model_)
204
  {
2919.3.63 by Marco Trevisan (Treviño)
ShortcutView: compute content size on columns rendering
205
    ComputeContentSize();
2919.3.45 by Marco Trevisan (Treviño)
ShortcutView: avoid crashes on null model
206
    QueueRelayout();
207
    return;
208
  }
209
1739.7.8 by Andrea Azzarone
Jay's work merged.
210
  int i = 0;
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
211
  int column_idx = 0;
2919.3.26 by Marco Trevisan (Treviño)
ShortcutView: dynamically adds new columns correctly
212
  auto const& columns = columns_layout_->GetChildren();
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
213
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
214
  for (auto const& category : model_->categories())
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
215
  {
2919.3.32 by Marco Trevisan (Treviño)
ShortcutView: updating copyright
216
    // Computing column index based on current index
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
217
    column_idx = i/model_->categories_per_column();
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
218
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
219
    nux::LinearLayout* section_layout = CreateSectionLayout(category);
1739.7.8 by Andrea Azzarone
Jay's work merged.
220
    nux::LinearLayout* intermediate_layout = CreateIntermediateLayout();
221
    intermediate_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
222
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
223
    for (auto const& hint : model_->hints().at(category))
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
224
    {
2919.3.29 by Marco Trevisan (Treviño)
ShortcutView: put the dynamic hints view, relayout on hints changes
225
      nux::View* view = CreateShortKeyEntryView(hint);
226
      intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
1739.7.8 by Andrea Azzarone
Jay's work merged.
227
    }
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
228
1739.7.8 by Andrea Azzarone
Jay's work merged.
229
    section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
230
2919.3.28 by Marco Trevisan (Treviño)
ShortcutView: don't show line separator after the last category (ever!)
231
    if ((i + 1) % model_->categories_per_column() != 0 && category != model_->categories().back())
1739.7.8 by Andrea Azzarone
Jay's work merged.
232
    {
2919.3.27 by Marco Trevisan (Treviño)
ShortcutView: just use layouts instead of views
233
      // Add a line with some padding after and before each category that is not
234
      // the last of the column.
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
235
      section_layout->AddView(new nux::SpaceLayout(23, 23, 23, 23), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
1739.7.8 by Andrea Azzarone
Jay's work merged.
236
      section_layout->AddView(new HSeparator(), 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
1739.7.24 by Andrea Azzarone
Try to make it as pixel perfect as possible.
237
      section_layout->AddView(new nux::SpaceLayout(20, 20, 20, 20), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_MATCHCONTENT);
1739.7.8 by Andrea Azzarone
Jay's work merged.
238
    }
239
2919.3.26 by Marco Trevisan (Treviño)
ShortcutView: dynamically adds new columns correctly
240
    nux::VLayout* column = nullptr;
241
    auto column_it = std::next(columns.begin(), column_idx);
242
243
    if (column_it == columns.end())
244
    {
245
      column = new nux::VLayout();
246
      columns_layout_->AddLayout(column, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
247
    }
248
    else
249
    {
250
      column = static_cast<nux::VLayout*>(*column_it);
251
    }
252
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
253
    column->AddView(section_layout, 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
254
1739.7.8 by Andrea Azzarone
Jay's work merged.
255
    i++;
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
256
  }
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
257
2919.3.63 by Marco Trevisan (Treviño)
ShortcutView: compute content size on columns rendering
258
  ComputeContentSize();
2919.3.22 by Marco Trevisan (Treviño)
ShortcutView: make column rendering more dynamic, relayout on changed settings
259
  QueueRelayout();
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
260
}
2119.3.1 by Thomi Richards
Removed lots of calls to std::string.c_str() where we didn't need to call it.
261
2348.1.5 by Andrea Azzarone
Add AP test.
262
//
263
// Introspectable methods
264
//
265
std::string View::GetName() const
266
{
267
  return "ShortcutView";
268
}
269
1739.7.1 by Andrea Azzarone
First draft of a shortcuts hint overlay...
270
} // namespace shortcut
271
} // namespace unity