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