Merge lp:~3v1n0/unity/dynamic-shortcut-hint into lp:~3v1n0/unity/shortcuts-hidpi

Proposed by Marco Trevisan (Treviño)
Status: Merged
Merged at revision: 3809
Proposed branch: lp:~3v1n0/unity/dynamic-shortcut-hint
Merge into: lp:~3v1n0/unity/shortcuts-hidpi
Diff against target: 268 lines (+95/-44)
3 files modified
shortcuts/ShortcutController.cpp (+1/-1)
shortcuts/ShortcutView.cpp (+85/-40)
shortcuts/ShortcutView.h (+9/-3)
To merge this branch: bzr merge lp:~3v1n0/unity/dynamic-shortcut-hint
Reviewer Review Type Date Requested Status
Andrea Azzarone (community) Approve
Review via email: mp+219447@code.launchpad.net

Commit message

ShortcutView: compute maximum columns width based on text size

In this way the text won't be truncated. The text size is computed per each column

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

LGTM. Nice!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'shortcuts/ShortcutController.cpp'
--- shortcuts/ShortcutController.cpp 2014-05-08 03:28:38 +0000
+++ shortcuts/ShortcutController.cpp 2014-05-14 01:48:25 +0000
@@ -70,11 +70,11 @@
70 if (!view_)70 if (!view_)
71 return;71 return;
7272
73 model->Fill();
73 view_->SetModel(model);74 view_->SetModel(model);
7475
75 if (Visible())76 if (Visible())
76 {77 {
77 model->Fill();
78 auto const& offset = GetOffsetPerMonitor(view_->monitor());78 auto const& offset = GetOffsetPerMonitor(view_->monitor());
7979
80 if (offset.x < 0 || offset.y < 0)80 if (offset.x < 0 || offset.y < 0)
8181
=== modified file 'shortcuts/ShortcutView.cpp'
--- shortcuts/ShortcutView.cpp 2014-05-13 19:29:40 +0000
+++ shortcuts/ShortcutView.cpp 2014-05-14 01:48:25 +0000
@@ -39,9 +39,11 @@
39 const unsigned SECTION_NAME_FONT_SIZE = 12;39 const unsigned SECTION_NAME_FONT_SIZE = 12;
40 const unsigned SHORTKEY_ENTRY_FONT_SIZE = 9;40 const unsigned SHORTKEY_ENTRY_FONT_SIZE = 9;
41 const RawPixel INTER_SPACE_SHORTKEY_DESCRIPTION = 10_em;41 const RawPixel INTER_SPACE_SHORTKEY_DESCRIPTION = 10_em;
42 const RawPixel SHORTKEY_COLUMN_WIDTH = 200_em;42 const RawPixel SHORTKEY_COLUMN_DEFAULT_WIDTH = 150_em;
43 const RawPixel DESCRIPTION_COLUMN_WIDTH = 300_em;43 const RawPixel SHORTKEY_COLUMN_MAX_WIDTH = 350_em;
44 const RawPixel LINE_SPACING = 5_em;44 const RawPixel DESCRIPTION_COLUMN_DEFAULT_WIDTH = 265_em;
45 const RawPixel DESCRIPTION_COLUMN_MAX_WIDTH = 500_em;
46 const RawPixel LINE_SPACING = 3_em;
45 const RawPixel MAIN_HORIZONTAL_PADDING = 30_em;47 const RawPixel MAIN_HORIZONTAL_PADDING = 30_em;
46 const RawPixel MAIN_VERTICAL_PADDING = 18_em;48 const RawPixel MAIN_VERTICAL_PADDING = 18_em;
47 const RawPixel MAIN_CHILDREN_SPACE = 20_em;49 const RawPixel MAIN_CHILDREN_SPACE = 20_em;
@@ -137,7 +139,7 @@
137 return layout;139 return layout;
138}140}
139141
140nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint)142nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint, StaticCairoText* shortkey_view, StaticCairoText* description_view)
141{143{
142 auto* view = new SectionView(NUX_TRACKER_LOCATION);144 auto* view = new SectionView(NUX_TRACKER_LOCATION);
143145
@@ -147,54 +149,52 @@
147 nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);149 nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
148 nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);150 nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
149151
150 glib::String shortkey(g_markup_escape_text(hint->shortkey().c_str(), -1));
151
152 std::string skey = "<b>"+shortkey.Str()+"</b>";
153 auto* shortkey_view = new StaticCairoText(skey, NUX_TRACKER_LOCATION);
154 shortkey_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
155 shortkey_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
156 shortkey_view->SetLines(-1);
157 shortkey_view->SetScale(scale);
158 shortkey_view->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
159 shortkey_view->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
160
161 glib::String es_desc(g_markup_escape_text(hint->description().c_str(), -1));
162
163 auto* description_view = new StaticCairoText(es_desc.Str(), NUX_TRACKER_LOCATION);
164 description_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
165 description_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
166 description_view->SetLines(-1);
167 description_view->SetScale(scale);
168 description_view->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
169 description_view->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
170
171 shortkey_layout->AddView(shortkey_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);152 shortkey_layout->AddView(shortkey_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
172 shortkey_layout->SetContentDistribution(nux::MAJOR_POSITION_START);153 shortkey_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
173 shortkey_layout->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
174 shortkey_layout->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
175154
176 description_layout->AddView(description_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);155 description_layout->AddView(description_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
177 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);156 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
178 description_layout->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
179 description_layout->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
180157
181 layout->AddLayout(shortkey_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);158 layout->AddLayout(shortkey_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
182 layout->AddLayout(description_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);159 layout->AddLayout(description_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
183 layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION.CP(scale));160 layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION.CP(scale));
184 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);161 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
185162
186 view->key_changed_conn_ = hint->shortkey.changed.connect([this, view, shortkey_view] (std::string const& new_key) {163 view->key_changed_conn_ = hint->shortkey.changed.connect([this, view, shortkey_view] (std::string const& key) {
187 bool enabled = !new_key.empty();164 std::string escaped = glib::String(g_markup_escape_text(key.c_str(), -1)).Str();
188 shortkey_view->SetText(enabled ? "<b>"+new_key+"</b>" : "");165
189 view->SetVisible(enabled);166 if (!escaped.empty())
167 escaped = "<b>"+escaped+"</b>";
168
169 shortkey_view->SetText(escaped);
170 shortkey_view->SetVisible(!escaped.empty());
171 view->SetVisible(shortkey_view->IsVisible());
190 QueueRelayout();172 QueueRelayout();
173 QueueDraw();
191 });174 });
192175
193 view->SetVisible(!shortkey.Str().empty());176 view->SetVisible(shortkey_view->IsVisible());
194177
195 return view;178 return view;
196}179}
197180
181StaticCairoText* View::CreateShortcutTextView(std::string const& text, bool bold)
182{
183 std::string escaped = glib::String(g_markup_escape_text(text.c_str(), -1)).Str();
184
185 if (bold && !text.empty())
186 escaped = "<b>"+escaped+"</b>";
187
188 auto* text_view = new StaticCairoText(escaped, NUX_TRACKER_LOCATION);
189 text_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
190 text_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
191 text_view->SetLines(-1);
192 text_view->SetScale(scale);
193 text_view->SetVisible(!escaped.empty());
194
195 return text_view;
196}
197
198nux::LinearLayout* View::CreateIntermediateLayout()198nux::LinearLayout* View::CreateIntermediateLayout()
199{199{
200 nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);200 nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
@@ -213,9 +213,38 @@
213 view_layout_->ProcessDraw(GfxContext, force_draw);213 view_layout_->ProcessDraw(GfxContext, force_draw);
214}214}
215215
216void View::PreLayoutManagement()
217{
218 UnityWindowView::PreLayoutManagement();
219
220 for (auto const& column : shortkeys_)
221 {
222 int min_width = SHORTKEY_COLUMN_DEFAULT_WIDTH.CP(scale);
223
224 for (auto* shortkey : column)
225 min_width = std::min(std::max(min_width, shortkey->GetTextExtents().width), shortkey->GetMaximumWidth());
226
227 for (auto* shortkey : column)
228 shortkey->SetMinimumWidth(min_width);
229 }
230
231 for (auto const& column : descriptions_)
232 {
233 int min_width = DESCRIPTION_COLUMN_DEFAULT_WIDTH.CP(scale);
234
235 for (auto* description : column)
236 min_width = std::min(std::max(min_width, description->GetTextExtents().width), description->GetMaximumWidth());
237
238 for (auto* description : column)
239 description->SetMinimumWidth(min_width);
240 }
241}
242
216void View::RenderColumns()243void View::RenderColumns()
217{244{
218 columns_layout_->Clear();245 columns_layout_->Clear();
246 shortkeys_.clear();
247 descriptions_.clear();
219248
220 if (!model_)249 if (!model_)
221 {250 {
@@ -227,13 +256,21 @@
227 int i = 0;256 int i = 0;
228 int column_idx = 0;257 int column_idx = 0;
229 auto const& columns = columns_layout_->GetChildren();258 auto const& columns = columns_layout_->GetChildren();
259 auto const& categories = model_->categories();
260 const int categories_per_column = model_->categories_per_column();
261 const int columns_number = categories.size() / categories_per_column + 1;
230 const int top_space = (23_em).CP(scale);262 const int top_space = (23_em).CP(scale);
231 const int bottom_space = (20_em).CP(scale);263 const int bottom_space = (20_em).CP(scale);
232264 const int max_shortkeys_width = SHORTKEY_COLUMN_MAX_WIDTH.CP(scale);
233 for (auto const& category : model_->categories())265 const int max_descriptions_width = DESCRIPTION_COLUMN_MAX_WIDTH.CP(scale);
266
267 shortkeys_.resize(columns_number);
268 descriptions_.resize(columns_number);
269
270 for (auto const& category : categories)
234 {271 {
235 // Computing column index based on current index272 // Computing column index based on current index
236 column_idx = i/model_->categories_per_column();273 column_idx = i/categories_per_column;
237274
238 nux::LinearLayout* section_layout = CreateSectionLayout(category);275 nux::LinearLayout* section_layout = CreateSectionLayout(category);
239 nux::LinearLayout* intermediate_layout = CreateIntermediateLayout();276 nux::LinearLayout* intermediate_layout = CreateIntermediateLayout();
@@ -241,13 +278,21 @@
241278
242 for (auto const& hint : model_->hints().at(category))279 for (auto const& hint : model_->hints().at(category))
243 {280 {
244 nux::View* view = CreateShortKeyEntryView(hint);281 StaticCairoText* shortkey = CreateShortcutTextView(hint->shortkey(), true);
282 shortkey->SetMaximumWidth(max_shortkeys_width);
283 shortkeys_[column_idx].push_back(shortkey);
284
285 StaticCairoText* description = CreateShortcutTextView(hint->description(), false);
286 description->SetMaximumWidth(max_descriptions_width);
287 descriptions_[column_idx].push_back(description);
288
289 nux::View* view = CreateShortKeyEntryView(hint, shortkey, description);
245 intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);290 intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
246 }291 }
247292
248 section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);293 section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
249294
250 if ((i + 1) % model_->categories_per_column() != 0 && category != model_->categories().back())295 if ((i + 1) % categories_per_column != 0 && category != categories.back())
251 {296 {
252 // Add a line with some padding after and before each category that is not297 // Add a line with some padding after and before each category that is not
253 // the last of the column.298 // the last of the column.
@@ -271,7 +316,7 @@
271316
272 column->AddView(section_layout, 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);317 column->AddView(section_layout, 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
273318
274 i++;319 ++i;
275 }320 }
276321
277 ComputeContentSize();322 ComputeContentSize();
278323
=== modified file 'shortcuts/ShortcutView.h'
--- shortcuts/ShortcutView.h 2013-11-16 11:44:23 +0000
+++ shortcuts/ShortcutView.h 2014-05-14 01:48:25 +0000
@@ -29,6 +29,8 @@
2929
30namespace unity30namespace unity
31{31{
32class StaticCairoText;
33
32namespace shortcut34namespace shortcut
33{35{
3436
@@ -47,8 +49,9 @@
4749
48protected:50protected:
49 // Protected methods51 // Protected methods
50 void DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry const& clip);52 void DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry const& clip) override;
51 nux::Geometry GetBackgroundGeometry();53 nux::Geometry GetBackgroundGeometry() override;
54 void PreLayoutManagement() override;
5255
53 // Introspectable methods56 // Introspectable methods
54 std::string GetName() const;57 std::string GetName() const;
@@ -56,7 +59,8 @@
56private:59private:
57 // Private methods60 // Private methods
58 nux::LinearLayout* CreateSectionLayout(std::string const& section_name);61 nux::LinearLayout* CreateSectionLayout(std::string const& section_name);
59 nux::View* CreateShortKeyEntryView(AbstractHint::Ptr const& hint);62 nux::View* CreateShortKeyEntryView(AbstractHint::Ptr const&, StaticCairoText* shortkey, StaticCairoText* description);
63 StaticCairoText* CreateShortcutTextView(std::string const& text, bool bold);
60 nux::LinearLayout* CreateIntermediateLayout();64 nux::LinearLayout* CreateIntermediateLayout();
6165
62 void RenderColumns();66 void RenderColumns();
@@ -64,6 +68,8 @@
64 // Private members68 // Private members
65 Model::Ptr model_;69 Model::Ptr model_;
66 nux::HLayout* columns_layout_;70 nux::HLayout* columns_layout_;
71 std::vector<std::vector<StaticCairoText*>> shortkeys_;
72 std::vector<std::vector<StaticCairoText*>> descriptions_;
6773
68 friend class TestShortcutView;74 friend class TestShortcutView;
69};75};

Subscribers

People subscribed via source and target branches

to all changes: