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
1=== modified file 'shortcuts/ShortcutController.cpp'
2--- shortcuts/ShortcutController.cpp 2014-05-08 03:28:38 +0000
3+++ shortcuts/ShortcutController.cpp 2014-05-14 01:48:25 +0000
4@@ -70,11 +70,11 @@
5 if (!view_)
6 return;
7
8+ model->Fill();
9 view_->SetModel(model);
10
11 if (Visible())
12 {
13- model->Fill();
14 auto const& offset = GetOffsetPerMonitor(view_->monitor());
15
16 if (offset.x < 0 || offset.y < 0)
17
18=== modified file 'shortcuts/ShortcutView.cpp'
19--- shortcuts/ShortcutView.cpp 2014-05-13 19:29:40 +0000
20+++ shortcuts/ShortcutView.cpp 2014-05-14 01:48:25 +0000
21@@ -39,9 +39,11 @@
22 const unsigned SECTION_NAME_FONT_SIZE = 12;
23 const unsigned SHORTKEY_ENTRY_FONT_SIZE = 9;
24 const RawPixel INTER_SPACE_SHORTKEY_DESCRIPTION = 10_em;
25- const RawPixel SHORTKEY_COLUMN_WIDTH = 200_em;
26- const RawPixel DESCRIPTION_COLUMN_WIDTH = 300_em;
27- const RawPixel LINE_SPACING = 5_em;
28+ const RawPixel SHORTKEY_COLUMN_DEFAULT_WIDTH = 150_em;
29+ const RawPixel SHORTKEY_COLUMN_MAX_WIDTH = 350_em;
30+ const RawPixel DESCRIPTION_COLUMN_DEFAULT_WIDTH = 265_em;
31+ const RawPixel DESCRIPTION_COLUMN_MAX_WIDTH = 500_em;
32+ const RawPixel LINE_SPACING = 3_em;
33 const RawPixel MAIN_HORIZONTAL_PADDING = 30_em;
34 const RawPixel MAIN_VERTICAL_PADDING = 18_em;
35 const RawPixel MAIN_CHILDREN_SPACE = 20_em;
36@@ -137,7 +139,7 @@
37 return layout;
38 }
39
40-nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint)
41+nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint, StaticCairoText* shortkey_view, StaticCairoText* description_view)
42 {
43 auto* view = new SectionView(NUX_TRACKER_LOCATION);
44
45@@ -147,54 +149,52 @@
46 nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
47 nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
48
49- glib::String shortkey(g_markup_escape_text(hint->shortkey().c_str(), -1));
50-
51- std::string skey = "<b>"+shortkey.Str()+"</b>";
52- auto* shortkey_view = new StaticCairoText(skey, NUX_TRACKER_LOCATION);
53- shortkey_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
54- shortkey_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
55- shortkey_view->SetLines(-1);
56- shortkey_view->SetScale(scale);
57- shortkey_view->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
58- shortkey_view->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
59-
60- glib::String es_desc(g_markup_escape_text(hint->description().c_str(), -1));
61-
62- auto* description_view = new StaticCairoText(es_desc.Str(), NUX_TRACKER_LOCATION);
63- description_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
64- description_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
65- description_view->SetLines(-1);
66- description_view->SetScale(scale);
67- description_view->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
68- description_view->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
69-
70 shortkey_layout->AddView(shortkey_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
71 shortkey_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
72- shortkey_layout->SetMinimumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
73- shortkey_layout->SetMaximumWidth(SHORTKEY_COLUMN_WIDTH.CP(scale));
74
75 description_layout->AddView(description_view, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
76 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
77- description_layout->SetMinimumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
78- description_layout->SetMaximumWidth(DESCRIPTION_COLUMN_WIDTH.CP(scale));
79
80 layout->AddLayout(shortkey_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
81 layout->AddLayout(description_layout, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_MATCHCONTENT);
82 layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION.CP(scale));
83 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
84
85- view->key_changed_conn_ = hint->shortkey.changed.connect([this, view, shortkey_view] (std::string const& new_key) {
86- bool enabled = !new_key.empty();
87- shortkey_view->SetText(enabled ? "<b>"+new_key+"</b>" : "");
88- view->SetVisible(enabled);
89+ view->key_changed_conn_ = hint->shortkey.changed.connect([this, view, shortkey_view] (std::string const& key) {
90+ std::string escaped = glib::String(g_markup_escape_text(key.c_str(), -1)).Str();
91+
92+ if (!escaped.empty())
93+ escaped = "<b>"+escaped+"</b>";
94+
95+ shortkey_view->SetText(escaped);
96+ shortkey_view->SetVisible(!escaped.empty());
97+ view->SetVisible(shortkey_view->IsVisible());
98 QueueRelayout();
99+ QueueDraw();
100 });
101
102- view->SetVisible(!shortkey.Str().empty());
103+ view->SetVisible(shortkey_view->IsVisible());
104
105 return view;
106 }
107
108+StaticCairoText* View::CreateShortcutTextView(std::string const& text, bool bold)
109+{
110+ std::string escaped = glib::String(g_markup_escape_text(text.c_str(), -1)).Str();
111+
112+ if (bold && !text.empty())
113+ escaped = "<b>"+escaped+"</b>";
114+
115+ auto* text_view = new StaticCairoText(escaped, NUX_TRACKER_LOCATION);
116+ text_view->SetTextAlignment(StaticCairoText::AlignState::NUX_ALIGN_LEFT);
117+ text_view->SetFont(FONT_NAME+" "+std::to_string(SHORTKEY_ENTRY_FONT_SIZE));
118+ text_view->SetLines(-1);
119+ text_view->SetScale(scale);
120+ text_view->SetVisible(!escaped.empty());
121+
122+ return text_view;
123+}
124+
125 nux::LinearLayout* View::CreateIntermediateLayout()
126 {
127 nux::VLayout* layout = new nux::VLayout(NUX_TRACKER_LOCATION);
128@@ -213,9 +213,38 @@
129 view_layout_->ProcessDraw(GfxContext, force_draw);
130 }
131
132+void View::PreLayoutManagement()
133+{
134+ UnityWindowView::PreLayoutManagement();
135+
136+ for (auto const& column : shortkeys_)
137+ {
138+ int min_width = SHORTKEY_COLUMN_DEFAULT_WIDTH.CP(scale);
139+
140+ for (auto* shortkey : column)
141+ min_width = std::min(std::max(min_width, shortkey->GetTextExtents().width), shortkey->GetMaximumWidth());
142+
143+ for (auto* shortkey : column)
144+ shortkey->SetMinimumWidth(min_width);
145+ }
146+
147+ for (auto const& column : descriptions_)
148+ {
149+ int min_width = DESCRIPTION_COLUMN_DEFAULT_WIDTH.CP(scale);
150+
151+ for (auto* description : column)
152+ min_width = std::min(std::max(min_width, description->GetTextExtents().width), description->GetMaximumWidth());
153+
154+ for (auto* description : column)
155+ description->SetMinimumWidth(min_width);
156+ }
157+}
158+
159 void View::RenderColumns()
160 {
161 columns_layout_->Clear();
162+ shortkeys_.clear();
163+ descriptions_.clear();
164
165 if (!model_)
166 {
167@@ -227,13 +256,21 @@
168 int i = 0;
169 int column_idx = 0;
170 auto const& columns = columns_layout_->GetChildren();
171+ auto const& categories = model_->categories();
172+ const int categories_per_column = model_->categories_per_column();
173+ const int columns_number = categories.size() / categories_per_column + 1;
174 const int top_space = (23_em).CP(scale);
175 const int bottom_space = (20_em).CP(scale);
176-
177- for (auto const& category : model_->categories())
178+ const int max_shortkeys_width = SHORTKEY_COLUMN_MAX_WIDTH.CP(scale);
179+ const int max_descriptions_width = DESCRIPTION_COLUMN_MAX_WIDTH.CP(scale);
180+
181+ shortkeys_.resize(columns_number);
182+ descriptions_.resize(columns_number);
183+
184+ for (auto const& category : categories)
185 {
186 // Computing column index based on current index
187- column_idx = i/model_->categories_per_column();
188+ column_idx = i/categories_per_column;
189
190 nux::LinearLayout* section_layout = CreateSectionLayout(category);
191 nux::LinearLayout* intermediate_layout = CreateIntermediateLayout();
192@@ -241,13 +278,21 @@
193
194 for (auto const& hint : model_->hints().at(category))
195 {
196- nux::View* view = CreateShortKeyEntryView(hint);
197+ StaticCairoText* shortkey = CreateShortcutTextView(hint->shortkey(), true);
198+ shortkey->SetMaximumWidth(max_shortkeys_width);
199+ shortkeys_[column_idx].push_back(shortkey);
200+
201+ StaticCairoText* description = CreateShortcutTextView(hint->description(), false);
202+ description->SetMaximumWidth(max_descriptions_width);
203+ descriptions_[column_idx].push_back(description);
204+
205+ nux::View* view = CreateShortKeyEntryView(hint, shortkey, description);
206 intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
207 }
208
209 section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
210
211- if ((i + 1) % model_->categories_per_column() != 0 && category != model_->categories().back())
212+ if ((i + 1) % categories_per_column != 0 && category != categories.back())
213 {
214 // Add a line with some padding after and before each category that is not
215 // the last of the column.
216@@ -271,7 +316,7 @@
217
218 column->AddView(section_layout, 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
219
220- i++;
221+ ++i;
222 }
223
224 ComputeContentSize();
225
226=== modified file 'shortcuts/ShortcutView.h'
227--- shortcuts/ShortcutView.h 2013-11-16 11:44:23 +0000
228+++ shortcuts/ShortcutView.h 2014-05-14 01:48:25 +0000
229@@ -29,6 +29,8 @@
230
231 namespace unity
232 {
233+class StaticCairoText;
234+
235 namespace shortcut
236 {
237
238@@ -47,8 +49,9 @@
239
240 protected:
241 // Protected methods
242- void DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry const& clip);
243- nux::Geometry GetBackgroundGeometry();
244+ void DrawOverlay(nux::GraphicsEngine& GfxContext, bool force_draw, nux::Geometry const& clip) override;
245+ nux::Geometry GetBackgroundGeometry() override;
246+ void PreLayoutManagement() override;
247
248 // Introspectable methods
249 std::string GetName() const;
250@@ -56,7 +59,8 @@
251 private:
252 // Private methods
253 nux::LinearLayout* CreateSectionLayout(std::string const& section_name);
254- nux::View* CreateShortKeyEntryView(AbstractHint::Ptr const& hint);
255+ nux::View* CreateShortKeyEntryView(AbstractHint::Ptr const&, StaticCairoText* shortkey, StaticCairoText* description);
256+ StaticCairoText* CreateShortcutTextView(std::string const& text, bool bold);
257 nux::LinearLayout* CreateIntermediateLayout();
258
259 void RenderColumns();
260@@ -64,6 +68,8 @@
261 // Private members
262 Model::Ptr model_;
263 nux::HLayout* columns_layout_;
264+ std::vector<std::vector<StaticCairoText*>> shortkeys_;
265+ std::vector<std::vector<StaticCairoText*>> descriptions_;
266
267 friend class TestShortcutView;
268 };

Subscribers

People subscribed via source and target branches

to all changes: