Merge lp:~mandel/unity/add-action-link into lp:unity
- add-action-link
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Nick Dedekind |
Approved revision: | no longer in the source branch. |
Merged at revision: | 2949 |
Proposed branch: | lp:~mandel/unity/add-action-link |
Merge into: | lp:unity |
Prerequisite: | lp:~mandel/unity/add-text-entry |
Diff against target: |
739 lines (+610/-2) 7 files modified
dash/previews/ActionLink.cpp (+248/-0) dash/previews/ActionLink.h (+100/-0) dash/previews/CMakeLists.txt (+1/-0) tests/CMakeLists.txt (+1/-0) tests/test_action_link.cpp (+188/-0) unity-shared/StaticCairoText.cpp (+62/-2) unity-shared/StaticCairoText.h (+10/-0) |
To merge this branch: | bzr merge lp:~mandel/unity/add-action-link |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Nick Dedekind (community) | Approve | ||
Marco Trevisan (Treviño) | Approve | ||
PS Jenkins bot | continuous-integration | Pending | |
Review via email: mp+130107@code.launchpad.net |
Commit message
- Provide a re-usable UI element that looks and behaves like a url link (LP: #1067705).
Description of the change
- Provide a re-usable UI element that looks and behaves like a url link (LP: #1067705).
Nick Dedekind (nick-dedekind) wrote : | # |
apon furthur inspection, looks OK to leave it as a button.
Thought there was a bit more to a abstract button than there is.
Marco Trevisan (Treviño) (3v1n0) wrote : | # |
Please, add tests too.
Marco Trevisan (Treviño) (3v1n0) wrote : | # |
56 +ActionLink:
57 +{
58 +}
Get rid of it
Marco Trevisan (Treviño) (3v1n0) wrote : | # |
Please get rid of the magic numbers in GetLinkAlpha replacing them with const values into an anonymous namespace, however that's globally nice, thanks! ;)
Nick Dedekind (nick-dedekind) wrote : | # |
50 + SetAcceptKeyNav
51 + SetAcceptKeyNav
Since you have an Init function, you should put these in there.
73 + SetMinimumHeigh
MAGIC. Is there a reason you need a minimum height and are not just using the layout's height calculated from the contents? If you do need it, use a const in anon namespace.
105 + static_
106 + static_
Since this is a general control these should be options on the action link, which are defaulted to these values. (SetTextAlignme
139 + if (cached_geometry_ != geo && geo.width > 0 && geo.height > 0)
140 + {
141 + cached_geometry_ = geo;
142 + }
There doesn't seem to be a purpose for the geometry caching that I can see, in which case you should be able to remove the ComputeContentSize override.
179 + nux::Geometry clip_geo = geo;
No need to make a copy of the geometry ref unless you're modifying it.
Nick Dedekind (nick-dedekind) wrote : | # |
LGTM.
All tests run successfully.
Preview Diff
1 | === added file 'dash/previews/ActionLink.cpp' | |||
2 | --- dash/previews/ActionLink.cpp 1970-01-01 00:00:00 +0000 | |||
3 | +++ dash/previews/ActionLink.cpp 2012-11-28 10:01:36 +0000 | |||
4 | @@ -0,0 +1,248 @@ | |||
5 | 1 | /* | ||
6 | 2 | * Copyright 2012 Canonical Ltd. | ||
7 | 3 | * | ||
8 | 4 | * This program is free software: you can redistribute it and/or modify it | ||
9 | 5 | * under the terms of the GNU Lesser General Public License version 3, as | ||
10 | 6 | * published by the Free Software Foundation. | ||
11 | 7 | * | ||
12 | 8 | * This program is distributed in the hope that it will be useful, but | ||
13 | 9 | * WITHOUT ANY WARRANTY; without even the implied warranties of | ||
14 | 10 | * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR | ||
15 | 11 | * PURPOSE. See the applicable version of the GNU Lesser General Public | ||
16 | 12 | * License for more details. | ||
17 | 13 | * | ||
18 | 14 | * You should have received a copy of both the GNU Lesser General Public | ||
19 | 15 | * License version 3 along with this program. If not, see | ||
20 | 16 | * <http://www.gnu.org/licenses/> | ||
21 | 17 | * | ||
22 | 18 | * Authored by: Manuel de la Pena <manuel.delapena@canonical.com> | ||
23 | 19 | * | ||
24 | 20 | */ | ||
25 | 21 | |||
26 | 22 | #include "ActionLink.h" | ||
27 | 23 | #include <NuxCore/Logger.h> | ||
28 | 24 | #include <Nux/VLayout.h> | ||
29 | 25 | #include <UnityCore/Variant.h> | ||
30 | 26 | #include "unity-shared/DashStyle.h" | ||
31 | 27 | #include "unity-shared/IconTexture.h" | ||
32 | 28 | #include "unity-shared/StaticCairoText.h" | ||
33 | 29 | |||
34 | 30 | namespace | ||
35 | 31 | { | ||
36 | 32 | nux::logging::Logger logger("unity.dash.actionlink"); | ||
37 | 33 | const double LINK_NORMAL_ALPHA_VALUE = 4; | ||
38 | 34 | const double LINK_HIGHLIGHTED_ALPHA_VALUE = 1; | ||
39 | 35 | } | ||
40 | 36 | |||
41 | 37 | namespace unity | ||
42 | 38 | { | ||
43 | 39 | namespace dash | ||
44 | 40 | { | ||
45 | 41 | |||
46 | 42 | ActionLink::ActionLink(std::string const& action_hint, std::string const& label, NUX_FILE_LINE_DECL) | ||
47 | 43 | : nux::AbstractButton(NUX_FILE_LINE_PARAM) | ||
48 | 44 | , action_hint_(action_hint) | ||
49 | 45 | , aligment_(nux::StaticCairoText::NUX_ALIGN_CENTRE) | ||
50 | 46 | , underline_(nux::StaticCairoText::NUX_UNDERLINE_SINGLE) | ||
51 | 47 | { | ||
52 | 48 | Init(); | ||
53 | 49 | BuildLayout(label); | ||
54 | 50 | } | ||
55 | 51 | |||
56 | 52 | std::string ActionLink::GetName() const | ||
57 | 53 | { | ||
58 | 54 | return "ActionLink"; | ||
59 | 55 | } | ||
60 | 56 | |||
61 | 57 | void ActionLink::AddProperties(GVariantBuilder* builder) | ||
62 | 58 | { | ||
63 | 59 | variant::BuilderWrapper(builder) | ||
64 | 60 | .add(GetAbsoluteGeometry()) | ||
65 | 61 | .add("action", action_hint_) | ||
66 | 62 | .add("label", label_) | ||
67 | 63 | .add("font-hint", font_hint) | ||
68 | 64 | .add("active", active_) | ||
69 | 65 | .add("text-aligment", text_aligment) | ||
70 | 66 | .add("underline-state", underline_state); | ||
71 | 67 | } | ||
72 | 68 | |||
73 | 69 | void ActionLink::Init() | ||
74 | 70 | { | ||
75 | 71 | SetAcceptKeyNavFocusOnMouseDown(false); | ||
76 | 72 | SetAcceptKeyNavFocusOnMouseEnter(true); | ||
77 | 73 | |||
78 | 74 | // set properties to ensure that we do redraw when one of them changes | ||
79 | 75 | text_aligment.SetSetterFunction(sigc::mem_fun(this, &ActionLink::set_aligment)); | ||
80 | 76 | text_aligment.SetGetterFunction(sigc::mem_fun(this, &ActionLink::get_aligment)); | ||
81 | 77 | |||
82 | 78 | underline_state.SetSetterFunction(sigc::mem_fun(this, &ActionLink::set_underline)); | ||
83 | 79 | underline_state.SetGetterFunction(sigc::mem_fun(this, &ActionLink::get_underline)); | ||
84 | 80 | |||
85 | 81 | font_hint.SetSetterFunction(sigc::mem_fun(this, &ActionLink::set_font_hint)); | ||
86 | 82 | font_hint.SetGetterFunction(sigc::mem_fun(this, &ActionLink::get_font_hint)); | ||
87 | 83 | |||
88 | 84 | key_nav_focus_change.connect([&] (nux::Area*, bool, nux::KeyNavDirection) | ||
89 | 85 | { | ||
90 | 86 | QueueDraw(); | ||
91 | 87 | }); | ||
92 | 88 | |||
93 | 89 | key_nav_focus_activate.connect([&](nux::Area*) | ||
94 | 90 | { | ||
95 | 91 | if (GetInputEventSensitivity()) | ||
96 | 92 | activate.emit(this, action_hint_); | ||
97 | 93 | }); | ||
98 | 94 | } | ||
99 | 95 | |||
100 | 96 | void ActionLink::BuildLayout(std::string const& label) | ||
101 | 97 | { | ||
102 | 98 | |||
103 | 99 | if (label != label_) | ||
104 | 100 | { | ||
105 | 101 | label_ = label; | ||
106 | 102 | if (static_text_) | ||
107 | 103 | { | ||
108 | 104 | static_text_.Release(); | ||
109 | 105 | static_text_ = NULL; | ||
110 | 106 | } | ||
111 | 107 | |||
112 | 108 | if (!label_.empty()) | ||
113 | 109 | { | ||
114 | 110 | static_text_ = new nux::StaticCairoText(label_, true, NUX_TRACKER_LOCATION); | ||
115 | 111 | if (!font_hint_.empty()) | ||
116 | 112 | static_text_->SetFont(font_hint_); | ||
117 | 113 | static_text_->SetInputEventSensitivity(false); | ||
118 | 114 | static_text_->SetTextAlignment(aligment_); | ||
119 | 115 | static_text_->SetUnderline(underline_); | ||
120 | 116 | } | ||
121 | 117 | } | ||
122 | 118 | |||
123 | 119 | RemoveLayout(); | ||
124 | 120 | |||
125 | 121 | nux::VLayout* layout = new nux::VLayout(); | ||
126 | 122 | if (static_text_) | ||
127 | 123 | { | ||
128 | 124 | layout->AddView(static_text_.GetPointer(), | ||
129 | 125 | 1, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL, | ||
130 | 126 | 100.0f, nux::NUX_LAYOUT_END); | ||
131 | 127 | } | ||
132 | 128 | SetLayout(layout); | ||
133 | 129 | |||
134 | 130 | ComputeContentSize(); | ||
135 | 131 | QueueDraw(); | ||
136 | 132 | } | ||
137 | 133 | |||
138 | 134 | int ActionLink::GetLinkAlpha(nux::ButtonVisualState state) | ||
139 | 135 | { | ||
140 | 136 | if (state == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT) | ||
141 | 137 | return LINK_HIGHLIGHTED_ALPHA_VALUE; | ||
142 | 138 | else | ||
143 | 139 | return LINK_NORMAL_ALPHA_VALUE; | ||
144 | 140 | } | ||
145 | 141 | |||
146 | 142 | void ActionLink::Draw(nux::GraphicsEngine& GfxContext, bool force_draw) | ||
147 | 143 | { | ||
148 | 144 | nux::Geometry const& geo = GetGeometry(); | ||
149 | 145 | |||
150 | 146 | gPainter.PaintBackground(GfxContext, geo); | ||
151 | 147 | // set up our texture mode | ||
152 | 148 | nux::TexCoordXForm texxform; | ||
153 | 149 | texxform.SetWrap(nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT); | ||
154 | 150 | texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_COORD); | ||
155 | 151 | |||
156 | 152 | // clear what is behind us | ||
157 | 153 | unsigned int alpha = 0, src = 0, dest = 0; | ||
158 | 154 | |||
159 | 155 | // set the alpha of the text according to its state | ||
160 | 156 | static_text_->SetTextAlpha(GetLinkAlpha(GetVisualState())); | ||
161 | 157 | |||
162 | 158 | GfxContext.GetRenderStates().GetBlend(alpha, src, dest); | ||
163 | 159 | GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | ||
164 | 160 | |||
165 | 161 | nux::Color col = nux::color::Black; | ||
166 | 162 | col.alpha = 0; | ||
167 | 163 | GfxContext.QRP_Color(geo.x, | ||
168 | 164 | geo.y, | ||
169 | 165 | geo.width, | ||
170 | 166 | geo.height, | ||
171 | 167 | col); | ||
172 | 168 | |||
173 | 169 | GfxContext.GetRenderStates().SetBlend(alpha, src, dest); | ||
174 | 170 | |||
175 | 171 | if (GetCompositionLayout()) | ||
176 | 172 | { | ||
177 | 173 | gPainter.PushPaintLayerStack(); | ||
178 | 174 | { | ||
179 | 175 | |||
180 | 176 | GfxContext.PushClippingRectangle(geo); | ||
181 | 177 | gPainter.PushPaintLayerStack(); | ||
182 | 178 | GetCompositionLayout()->ProcessDraw(GfxContext, force_draw); | ||
183 | 179 | gPainter.PopPaintLayerStack(); | ||
184 | 180 | GfxContext.PopClippingRectangle(); | ||
185 | 181 | } | ||
186 | 182 | gPainter.PopPaintLayerStack(); | ||
187 | 183 | } | ||
188 | 184 | } | ||
189 | 185 | |||
190 | 186 | void ActionLink::RecvClick(int x, int y, unsigned long button_flags, unsigned long key_flags) | ||
191 | 187 | { | ||
192 | 188 | activate.emit(this, action_hint_); | ||
193 | 189 | } | ||
194 | 190 | |||
195 | 191 | bool ActionLink::set_aligment(nux::StaticCairoText::AlignState aligment) | ||
196 | 192 | { | ||
197 | 193 | if(static_text_ && aligment_ != aligment) | ||
198 | 194 | { | ||
199 | 195 | static_text_->SetTextAlignment(aligment_); | ||
200 | 196 | aligment_ = aligment; | ||
201 | 197 | ComputeContentSize(); | ||
202 | 198 | QueueDraw(); | ||
203 | 199 | } | ||
204 | 200 | return true; | ||
205 | 201 | } | ||
206 | 202 | |||
207 | 203 | nux::StaticCairoText::AlignState ActionLink::get_aligment() | ||
208 | 204 | { | ||
209 | 205 | return aligment_; | ||
210 | 206 | } | ||
211 | 207 | |||
212 | 208 | bool ActionLink::set_underline(nux::StaticCairoText::UnderlineState underline) | ||
213 | 209 | { | ||
214 | 210 | if(static_text_ && underline_ != underline) | ||
215 | 211 | { | ||
216 | 212 | static_text_->SetUnderline(underline_); | ||
217 | 213 | underline_ = underline; | ||
218 | 214 | ComputeContentSize(); | ||
219 | 215 | QueueDraw(); | ||
220 | 216 | } | ||
221 | 217 | return true; | ||
222 | 218 | } | ||
223 | 219 | |||
224 | 220 | nux::StaticCairoText::UnderlineState ActionLink::get_underline() | ||
225 | 221 | { | ||
226 | 222 | return underline_; | ||
227 | 223 | } | ||
228 | 224 | |||
229 | 225 | bool ActionLink::set_font_hint(std::string font_hint) | ||
230 | 226 | { | ||
231 | 227 | if(static_text_ && font_hint_ != font_hint) | ||
232 | 228 | { | ||
233 | 229 | static_text_->SetFont(font_hint_); | ||
234 | 230 | font_hint_ = font_hint; | ||
235 | 231 | ComputeContentSize(); | ||
236 | 232 | QueueDraw(); | ||
237 | 233 | } | ||
238 | 234 | return true; | ||
239 | 235 | } | ||
240 | 236 | |||
241 | 237 | std::string ActionLink::get_font_hint() | ||
242 | 238 | { | ||
243 | 239 | return font_hint_; | ||
244 | 240 | } | ||
245 | 241 | |||
246 | 242 | std::string ActionLink::GetLabel() const | ||
247 | 243 | { | ||
248 | 244 | return label_; | ||
249 | 245 | } | ||
250 | 246 | |||
251 | 247 | } // namespace dash | ||
252 | 248 | } // namespace unity | ||
253 | 0 | 249 | ||
254 | === added file 'dash/previews/ActionLink.h' | |||
255 | --- dash/previews/ActionLink.h 1970-01-01 00:00:00 +0000 | |||
256 | +++ dash/previews/ActionLink.h 2012-11-28 10:01:36 +0000 | |||
257 | @@ -0,0 +1,100 @@ | |||
258 | 1 | // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- | ||
259 | 2 | /* | ||
260 | 3 | * Copyright 2012 Canonical Ltd. | ||
261 | 4 | * | ||
262 | 5 | * This program is free software: you can redistribute it and/or modify it | ||
263 | 6 | * under the terms of the GNU Lesser General Public License version 3, as | ||
264 | 7 | * published by the Free Software Foundation. | ||
265 | 8 | * | ||
266 | 9 | * This program is distributed in the hope that it will be useful, but | ||
267 | 10 | * WITHOUT ANY WARRANTY; without even the implied warranties of | ||
268 | 11 | * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR | ||
269 | 12 | * PURPOSE. See the applicable version of the GNU Lesser General Public | ||
270 | 13 | * License for more details. | ||
271 | 14 | * | ||
272 | 15 | * You should have received a copy of both the GNU Lesser General Public | ||
273 | 16 | * License version 3 along with this program. If not, see | ||
274 | 17 | * <http://www.gnu.org/licenses/> | ||
275 | 18 | * | ||
276 | 19 | * Authored by: Manuel de la Pena <manuel.delapena@canonical.com> | ||
277 | 20 | * | ||
278 | 21 | */ | ||
279 | 22 | |||
280 | 23 | #ifndef ACTIONLINK_H | ||
281 | 24 | #define ACTIONLINK_H | ||
282 | 25 | |||
283 | 26 | #include <Nux/Nux.h> | ||
284 | 27 | #include <Nux/CairoWrapper.h> | ||
285 | 28 | #include <Nux/AbstractButton.h> | ||
286 | 29 | #include "unity-shared/Introspectable.h" | ||
287 | 30 | #include "unity-shared/StaticCairoText.h" | ||
288 | 31 | |||
289 | 32 | |||
290 | 33 | namespace unity | ||
291 | 34 | { | ||
292 | 35 | class IconTexture; | ||
293 | 36 | |||
294 | 37 | namespace dash | ||
295 | 38 | { | ||
296 | 39 | |||
297 | 40 | class ActionLink : public nux::AbstractButton, public debug::Introspectable | ||
298 | 41 | { | ||
299 | 42 | public: | ||
300 | 43 | ActionLink(std::string const& action_hint, std::string const& label, NUX_FILE_LINE_PROTO); | ||
301 | 44 | |||
302 | 45 | sigc::signal<void, ActionLink*, std::string const&> activate; | ||
303 | 46 | |||
304 | 47 | nux::RWProperty<nux::StaticCairoText::AlignState> text_aligment; | ||
305 | 48 | nux::RWProperty<nux::StaticCairoText::UnderlineState> underline_state; | ||
306 | 49 | nux::RWProperty<std::string> font_hint; | ||
307 | 50 | |||
308 | 51 | void Activate() {} | ||
309 | 52 | void Deactivate() {} | ||
310 | 53 | |||
311 | 54 | virtual bool AcceptKeyNavFocus() const { return true; } | ||
312 | 55 | |||
313 | 56 | std::string GetLabel() const; | ||
314 | 57 | std::string GetExtraText() const; | ||
315 | 58 | |||
316 | 59 | protected: | ||
317 | 60 | nux::ObjectPtr<nux::StaticCairoText> static_text_; | ||
318 | 61 | |||
319 | 62 | int GetLinkAlpha(nux::ButtonVisualState state); | ||
320 | 63 | |||
321 | 64 | void Draw(nux::GraphicsEngine& GfxContext, bool force_draw); | ||
322 | 65 | void DrawContent(nux::GraphicsEngine& GfxContext, bool force_draw) {} | ||
323 | 66 | void RecvClick(int x, int y, unsigned long button_flags, unsigned long key_flags); | ||
324 | 67 | |||
325 | 68 | void Init(); | ||
326 | 69 | |||
327 | 70 | void BuildLayout(std::string const& label); | ||
328 | 71 | |||
329 | 72 | // From debug::Introspectable | ||
330 | 73 | std::string GetName() const; | ||
331 | 74 | void AddProperties(GVariantBuilder* builder); | ||
332 | 75 | |||
333 | 76 | // this methods/vars could be private but are protected to make testing | ||
334 | 77 | // easier | ||
335 | 78 | bool set_aligment(nux::StaticCairoText::AlignState aligment); | ||
336 | 79 | nux::StaticCairoText::AlignState get_aligment(); | ||
337 | 80 | |||
338 | 81 | bool set_underline(nux::StaticCairoText::UnderlineState underline); | ||
339 | 82 | nux::StaticCairoText::UnderlineState get_underline(); | ||
340 | 83 | |||
341 | 84 | bool set_font_hint(std::string font_hint); | ||
342 | 85 | std::string get_font_hint(); | ||
343 | 86 | |||
344 | 87 | std::string action_hint_; | ||
345 | 88 | std::string font_hint_; | ||
346 | 89 | nux::StaticCairoText::AlignState aligment_; | ||
347 | 90 | nux::StaticCairoText::UnderlineState underline_; | ||
348 | 91 | private: | ||
349 | 92 | typedef std::unique_ptr<nux::CairoWrapper> NuxCairoPtr; | ||
350 | 93 | |||
351 | 94 | |||
352 | 95 | }; | ||
353 | 96 | |||
354 | 97 | } // namespace dash | ||
355 | 98 | } // namespace unity | ||
356 | 99 | |||
357 | 100 | #endif // ACTIONLINK_H | ||
358 | 0 | 101 | ||
359 | === modified file 'dash/previews/CMakeLists.txt' | |||
360 | --- dash/previews/CMakeLists.txt 2012-11-26 16:09:53 +0000 | |||
361 | +++ dash/previews/CMakeLists.txt 2012-11-28 10:01:36 +0000 | |||
362 | @@ -26,6 +26,7 @@ | |||
363 | 26 | # | 26 | # |
364 | 27 | set (PREVIEWS_SOURCES | 27 | set (PREVIEWS_SOURCES |
365 | 28 | ActionButton.cpp | 28 | ActionButton.cpp |
366 | 29 | ActionLink.cpp | ||
367 | 29 | ApplicationPreview.cpp | 30 | ApplicationPreview.cpp |
368 | 30 | GenericPreview.cpp | 31 | GenericPreview.cpp |
369 | 31 | MusicPreview.cpp | 32 | MusicPreview.cpp |
370 | 32 | 33 | ||
371 | === modified file 'tests/CMakeLists.txt' | |||
372 | --- tests/CMakeLists.txt 2012-11-24 15:34:32 +0000 | |||
373 | +++ tests/CMakeLists.txt 2012-11-28 10:01:36 +0000 | |||
374 | @@ -206,6 +206,7 @@ | |||
375 | 206 | # Tests that require X | 206 | # Tests that require X |
376 | 207 | add_executable(test-gtest | 207 | add_executable(test-gtest |
377 | 208 | test_main.cpp | 208 | test_main.cpp |
378 | 209 | test_action_link.cpp | ||
379 | 209 | test_application_launcher_icon.cpp | 210 | test_application_launcher_icon.cpp |
380 | 210 | test_bfb_launcher_icon.cpp | 211 | test_bfb_launcher_icon.cpp |
381 | 211 | test_dashview_impl.cpp | 212 | test_dashview_impl.cpp |
382 | 212 | 213 | ||
383 | === added file 'tests/test_action_link.cpp' | |||
384 | --- tests/test_action_link.cpp 1970-01-01 00:00:00 +0000 | |||
385 | +++ tests/test_action_link.cpp 2012-11-28 10:01:36 +0000 | |||
386 | @@ -0,0 +1,188 @@ | |||
387 | 1 | /* | ||
388 | 2 | * Copyright 2012 Canonical Ltd. | ||
389 | 3 | * | ||
390 | 4 | * This program is free software: you can redistribute it and/or modify it | ||
391 | 5 | * under the terms of the GNU Lesser General Public License version 3, as | ||
392 | 6 | * published by the Free Software Foundation. | ||
393 | 7 | * | ||
394 | 8 | * This program is distributed in the hope that it will be useful, but | ||
395 | 9 | * WITHOUT ANY WARRANTY; without even the implied warranties of | ||
396 | 10 | * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR | ||
397 | 11 | * PURPOSE. See the applicable version of the GNU Lesser General Public | ||
398 | 12 | * License for more details. | ||
399 | 13 | * | ||
400 | 14 | * You should have received a copy of both the GNU Lesser General Public | ||
401 | 15 | * License version 3 along with this program. If not, see | ||
402 | 16 | * <http://www.gnu.org/licenses/> | ||
403 | 17 | * | ||
404 | 18 | * Authored by: Manuel de la Pena <manuel.delapena@canonical.com> | ||
405 | 19 | * | ||
406 | 20 | */ | ||
407 | 21 | |||
408 | 22 | #include <gmock/gmock.h> | ||
409 | 23 | #include <gtest/gtest.h> | ||
410 | 24 | |||
411 | 25 | #include <unity-shared/StaticCairoText.h> | ||
412 | 26 | |||
413 | 27 | #include "dash/previews/ActionLink.cpp" | ||
414 | 28 | #include "test_utils.h" | ||
415 | 29 | |||
416 | 30 | using namespace nux; | ||
417 | 31 | using namespace unity; | ||
418 | 32 | using namespace unity::dash; | ||
419 | 33 | |||
420 | 34 | namespace unity | ||
421 | 35 | { | ||
422 | 36 | |||
423 | 37 | namespace dash | ||
424 | 38 | { | ||
425 | 39 | |||
426 | 40 | class ActionLinkMock : public ActionLink | ||
427 | 41 | { | ||
428 | 42 | public: | ||
429 | 43 | MOCK_METHOD0(QueueDraw, void()); | ||
430 | 44 | MOCK_METHOD0(ComputeContentSize, long()); | ||
431 | 45 | MOCK_METHOD2(CalculateBar, void(nux::GraphicsEngine&, bool)); | ||
432 | 46 | |||
433 | 47 | ActionLinkMock(std::string const& action_hint, std::string const& label, NUX_FILE_LINE_PROTO) | ||
434 | 48 | : ActionLink(action_hint, label){} | ||
435 | 49 | ~ActionLinkMock(){} | ||
436 | 50 | |||
437 | 51 | nux::ObjectPtr<nux::StaticCairoText> GetText() { return static_text_; } | ||
438 | 52 | |||
439 | 53 | using ActionLink::GetLinkAlpha; | ||
440 | 54 | using ActionLink::Draw; | ||
441 | 55 | using ActionLink::DrawContent; | ||
442 | 56 | using ActionLink::RecvClick; | ||
443 | 57 | using ActionLink::GetName; | ||
444 | 58 | using ActionLink::AddProperties; | ||
445 | 59 | using ActionLink::set_aligment; | ||
446 | 60 | using ActionLink::get_aligment; | ||
447 | 61 | using ActionLink::set_underline; | ||
448 | 62 | using ActionLink::get_underline; | ||
449 | 63 | using ActionLink::set_font_hint; | ||
450 | 64 | using ActionLink::get_font_hint; | ||
451 | 65 | using ActionLink::action_hint_; | ||
452 | 66 | using ActionLink::font_hint_; | ||
453 | 67 | using ActionLink::aligment_; | ||
454 | 68 | using ActionLink::underline_; | ||
455 | 69 | }; | ||
456 | 70 | |||
457 | 71 | class TestActionLink : public ::testing::Test | ||
458 | 72 | { | ||
459 | 73 | protected: | ||
460 | 74 | TestActionLink() : Test() | ||
461 | 75 | { | ||
462 | 76 | action_link = new ActionLinkMock("action_id", "display_name"); | ||
463 | 77 | } | ||
464 | 78 | nux::ObjectPtr<ActionLinkMock> action_link; | ||
465 | 79 | }; | ||
466 | 80 | |||
467 | 81 | TEST_F(TestActionLink, AligmentCorrectlySetDifferent) | ||
468 | 82 | { | ||
469 | 83 | ActionLinkMock link("test", "test"); | ||
470 | 84 | |||
471 | 85 | EXPECT_CALL(link, ComputeContentSize()).Times(1); | ||
472 | 86 | EXPECT_CALL(link, QueueDraw()).Times(1); | ||
473 | 87 | |||
474 | 88 | link.text_aligment.Set(nux::StaticCairoText::NUX_ALIGN_RIGHT); | ||
475 | 89 | } | ||
476 | 90 | |||
477 | 91 | TEST_F(TestActionLink, AligmentCorrectlySetSame) | ||
478 | 92 | { | ||
479 | 93 | ActionLinkMock link("test", "test"); | ||
480 | 94 | |||
481 | 95 | EXPECT_CALL(link, ComputeContentSize()).Times(0); | ||
482 | 96 | EXPECT_CALL(link, QueueDraw()).Times(0); | ||
483 | 97 | |||
484 | 98 | link.text_aligment.Set(link.text_aligment.Get()); | ||
485 | 99 | } | ||
486 | 100 | |||
487 | 101 | TEST_F(TestActionLink, AligmentCorrectlyRetrieved) | ||
488 | 102 | { | ||
489 | 103 | nux::StaticCairoText::AlignState aligment = | ||
490 | 104 | nux::StaticCairoText::NUX_ALIGN_RIGHT; | ||
491 | 105 | action_link->aligment_ = aligment; | ||
492 | 106 | EXPECT_EQ(aligment, action_link->text_aligment.Get()); | ||
493 | 107 | } | ||
494 | 108 | |||
495 | 109 | TEST_F(TestActionLink, UnderlineCorrectlySetDifferent) | ||
496 | 110 | { | ||
497 | 111 | ActionLinkMock link("test", "test"); | ||
498 | 112 | |||
499 | 113 | EXPECT_CALL(link, ComputeContentSize()).Times(1); | ||
500 | 114 | EXPECT_CALL(link, QueueDraw()).Times(1); | ||
501 | 115 | link.underline_state.Set(nux::StaticCairoText::NUX_UNDERLINE_NONE); | ||
502 | 116 | } | ||
503 | 117 | |||
504 | 118 | TEST_F(TestActionLink, UnderlineCorrectlySetSame) | ||
505 | 119 | { | ||
506 | 120 | ActionLinkMock link("test", "test"); | ||
507 | 121 | |||
508 | 122 | EXPECT_CALL(link, ComputeContentSize()).Times(0); | ||
509 | 123 | EXPECT_CALL(link, QueueDraw()).Times(0); | ||
510 | 124 | link.underline_state.Set(link.underline_state.Get()); | ||
511 | 125 | } | ||
512 | 126 | |||
513 | 127 | TEST_F(TestActionLink, UnderlineCorrectlyRetrieved) | ||
514 | 128 | { | ||
515 | 129 | nux::StaticCairoText::UnderlineState underline = | ||
516 | 130 | nux::StaticCairoText::NUX_UNDERLINE_DOUBLE; | ||
517 | 131 | action_link->underline_ = underline; | ||
518 | 132 | EXPECT_EQ(underline, action_link->underline_state.Get()); | ||
519 | 133 | } | ||
520 | 134 | |||
521 | 135 | TEST_F(TestActionLink, FontCorrectlySetDifferent) | ||
522 | 136 | { | ||
523 | 137 | ActionLinkMock link("test", "test"); | ||
524 | 138 | link.font_hint_ = "Ubuntu 10"; | ||
525 | 139 | |||
526 | 140 | EXPECT_CALL(link, ComputeContentSize()).Times(1); | ||
527 | 141 | EXPECT_CALL(link, QueueDraw()).Times(1); | ||
528 | 142 | link.font_hint.Set("Ubuntu 11"); | ||
529 | 143 | } | ||
530 | 144 | |||
531 | 145 | TEST_F(TestActionLink, FontCorrectlySetSame) | ||
532 | 146 | { | ||
533 | 147 | ActionLinkMock link("test", "test"); | ||
534 | 148 | link.font_hint_ = "Ubuntu 10"; | ||
535 | 149 | |||
536 | 150 | EXPECT_CALL(link, ComputeContentSize()).Times(0); | ||
537 | 151 | EXPECT_CALL(link, QueueDraw()).Times(0); | ||
538 | 152 | link.font_hint.Set(link.font_hint.Get()); | ||
539 | 153 | } | ||
540 | 154 | |||
541 | 155 | TEST_F(TestActionLink, FontCorrectlyRetrieved) | ||
542 | 156 | { | ||
543 | 157 | std::string font_hint = "Ubuntu 11"; | ||
544 | 158 | action_link->font_hint_ = font_hint; | ||
545 | 159 | EXPECT_EQ(font_hint, action_link->font_hint.Get()); | ||
546 | 160 | } | ||
547 | 161 | |||
548 | 162 | TEST_F(TestActionLink, LinkAlphaOnPressed) | ||
549 | 163 | { | ||
550 | 164 | ButtonVisualState state = ButtonVisualState::VISUAL_STATE_PRESSED; | ||
551 | 165 | EXPECT_EQ(4, action_link->GetLinkAlpha(state)); | ||
552 | 166 | } | ||
553 | 167 | |||
554 | 168 | TEST_F(TestActionLink, LinkAlphaOnNormal) | ||
555 | 169 | { | ||
556 | 170 | ButtonVisualState state = ButtonVisualState::VISUAL_STATE_NORMAL; | ||
557 | 171 | EXPECT_EQ(4, action_link->GetLinkAlpha(state)); | ||
558 | 172 | } | ||
559 | 173 | |||
560 | 174 | TEST_F(TestActionLink, LinkAlphaOnPrelight) | ||
561 | 175 | { | ||
562 | 176 | ButtonVisualState state = ButtonVisualState::VISUAL_STATE_PRELIGHT; | ||
563 | 177 | EXPECT_EQ(1, action_link->GetLinkAlpha(state)); | ||
564 | 178 | } | ||
565 | 179 | |||
566 | 180 | TEST_F(TestActionLink, LinkAlphaOnDisabled) | ||
567 | 181 | { | ||
568 | 182 | ButtonVisualState state = ButtonVisualState::VISUAL_STATE_DISABLED; | ||
569 | 183 | EXPECT_EQ(4, action_link->GetLinkAlpha(state)); | ||
570 | 184 | } | ||
571 | 185 | |||
572 | 186 | } | ||
573 | 187 | |||
574 | 188 | } | ||
575 | 0 | 189 | ||
576 | === modified file 'unity-shared/StaticCairoText.cpp' | |||
577 | --- unity-shared/StaticCairoText.cpp 2012-11-22 10:46:13 +0000 | |||
578 | +++ unity-shared/StaticCairoText.cpp 2012-11-28 10:01:36 +0000 | |||
579 | @@ -54,6 +54,7 @@ | |||
580 | 54 | std::string GetEffectiveFont() const; | 54 | std::string GetEffectiveFont() const; |
581 | 55 | Size GetTextExtents() const; | 55 | Size GetTextExtents() const; |
582 | 56 | 56 | ||
583 | 57 | void SetAttributes(PangoLayout* layout); | ||
584 | 57 | void DrawText(cairo_t* cr, int width, int height, int line_spacing, Color const& color); | 58 | void DrawText(cairo_t* cr, int width, int height, int line_spacing, Color const& color); |
585 | 58 | 59 | ||
586 | 59 | void UpdateTexture(); | 60 | void UpdateTexture(); |
587 | @@ -75,6 +76,7 @@ | |||
588 | 75 | EllipsizeState ellipsize_; | 76 | EllipsizeState ellipsize_; |
589 | 76 | AlignState align_; | 77 | AlignState align_; |
590 | 77 | AlignState valign_; | 78 | AlignState valign_; |
591 | 79 | UnderlineState underline_; | ||
592 | 78 | 80 | ||
593 | 79 | std::string font_; | 81 | std::string font_; |
594 | 80 | 82 | ||
595 | @@ -97,6 +99,7 @@ | |||
596 | 97 | , ellipsize_(NUX_ELLIPSIZE_END) | 99 | , ellipsize_(NUX_ELLIPSIZE_END) |
597 | 98 | , align_(NUX_ALIGN_LEFT) | 100 | , align_(NUX_ALIGN_LEFT) |
598 | 99 | , valign_(NUX_ALIGN_TOP) | 101 | , valign_(NUX_ALIGN_TOP) |
599 | 102 | , underline_(NUX_UNDERLINE_NONE) | ||
600 | 100 | , lines_(-2) // should find out why -2... | 103 | , lines_(-2) // should find out why -2... |
601 | 101 | // the desired height of the layout in Pango units if positive, or desired | 104 | // the desired height of the layout in Pango units if positive, or desired |
602 | 102 | // number of lines if negative. | 105 | // number of lines if negative. |
603 | @@ -312,6 +315,16 @@ | |||
604 | 312 | } | 315 | } |
605 | 313 | } | 316 | } |
606 | 314 | 317 | ||
607 | 318 | void StaticCairoText::SetTextAlpha(unsigned int alpha) | ||
608 | 319 | { | ||
609 | 320 | if (pimpl->text_color_.alpha != alpha) | ||
610 | 321 | { | ||
611 | 322 | pimpl->text_color_.alpha = alpha; | ||
612 | 323 | pimpl->UpdateTexture(); | ||
613 | 324 | QueueDraw(); | ||
614 | 325 | } | ||
615 | 326 | } | ||
616 | 327 | |||
617 | 315 | void StaticCairoText::SetMaximumSize(int w, int h) | 328 | void StaticCairoText::SetMaximumSize(int w, int h) |
618 | 316 | { | 329 | { |
619 | 317 | if (w != GetMaximumWidth()) | 330 | if (w != GetMaximumWidth()) |
620 | @@ -320,7 +333,7 @@ | |||
621 | 320 | View::SetMaximumSize(w, h); | 333 | View::SetMaximumSize(w, h); |
622 | 321 | pimpl->UpdateTexture(); | 334 | pimpl->UpdateTexture(); |
623 | 322 | return; | 335 | return; |
625 | 323 | } | 336 | } |
626 | 324 | 337 | ||
627 | 325 | View::SetMaximumSize(w, h); | 338 | View::SetMaximumSize(w, h); |
628 | 326 | } | 339 | } |
629 | @@ -380,6 +393,18 @@ | |||
630 | 380 | return pimpl->font_; | 393 | return pimpl->font_; |
631 | 381 | } | 394 | } |
632 | 382 | 395 | ||
633 | 396 | void StaticCairoText::SetUnderline(UnderlineState underline) | ||
634 | 397 | { | ||
635 | 398 | if (pimpl->underline_ != underline) | ||
636 | 399 | { | ||
637 | 400 | pimpl->underline_ = underline; | ||
638 | 401 | pimpl->need_new_extent_cache_ = true; | ||
639 | 402 | Size s = GetTextExtents(); | ||
640 | 403 | SetMinimumHeight(s.height); | ||
641 | 404 | NeedRedraw(); | ||
642 | 405 | } | ||
643 | 406 | } | ||
644 | 407 | |||
645 | 383 | int StaticCairoText::GetLineCount() const | 408 | int StaticCairoText::GetLineCount() const |
646 | 384 | { | 409 | { |
647 | 385 | return pimpl->actual_lines_; | 410 | return pimpl->actual_lines_; |
648 | @@ -489,6 +514,37 @@ | |||
649 | 489 | return result; | 514 | return result; |
650 | 490 | } | 515 | } |
651 | 491 | 516 | ||
652 | 517 | void StaticCairoText::Impl::SetAttributes(PangoLayout *layout) | ||
653 | 518 | { | ||
654 | 519 | PangoAttrList* attr_list = NULL; | ||
655 | 520 | PangoAttribute* underline_attr = NULL; | ||
656 | 521 | |||
657 | 522 | attr_list = pango_layout_get_attributes(layout); | ||
658 | 523 | if(!attr_list) | ||
659 | 524 | { | ||
660 | 525 | attr_list = pango_attr_list_new(); | ||
661 | 526 | } | ||
662 | 527 | |||
663 | 528 | PangoUnderline underline_type; | ||
664 | 529 | |||
665 | 530 | switch(underline_){ | ||
666 | 531 | case(NUX_UNDERLINE_SINGLE): | ||
667 | 532 | underline_type = PANGO_UNDERLINE_SINGLE; | ||
668 | 533 | break; | ||
669 | 534 | case(NUX_UNDERLINE_DOUBLE): | ||
670 | 535 | underline_type = PANGO_UNDERLINE_DOUBLE; | ||
671 | 536 | break; | ||
672 | 537 | case(NUX_UNDERLINE_LOW): | ||
673 | 538 | underline_type = PANGO_UNDERLINE_LOW; | ||
674 | 539 | break; | ||
675 | 540 | default: | ||
676 | 541 | underline_type = PANGO_UNDERLINE_NONE; | ||
677 | 542 | } | ||
678 | 543 | underline_attr = pango_attr_underline_new(underline_type); | ||
679 | 544 | pango_attr_list_insert(attr_list, underline_attr); | ||
680 | 545 | pango_layout_set_attributes(layout, attr_list); | ||
681 | 546 | } | ||
682 | 547 | |||
683 | 492 | void StaticCairoText::Impl::DrawText(cairo_t* cr, | 548 | void StaticCairoText::Impl::DrawText(cairo_t* cr, |
684 | 493 | int width, | 549 | int width, |
685 | 494 | int height, | 550 | int height, |
686 | @@ -506,8 +562,9 @@ | |||
687 | 506 | 562 | ||
688 | 507 | cairo_set_font_options(cr, gdk_screen_get_font_options(screen)); | 563 | cairo_set_font_options(cr, gdk_screen_get_font_options(screen)); |
689 | 508 | layout = pango_cairo_create_layout(cr); | 564 | layout = pango_cairo_create_layout(cr); |
690 | 565 | |||
691 | 566 | |||
692 | 509 | desc = pango_font_description_from_string(font.c_str()); | 567 | desc = pango_font_description_from_string(font.c_str()); |
693 | 510 | |||
694 | 511 | pango_layout_set_font_description(layout, desc); | 568 | pango_layout_set_font_description(layout, desc); |
695 | 512 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); | 569 | pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); |
696 | 513 | pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode()); | 570 | pango_layout_set_ellipsize(layout, GetPangoEllipsizeMode()); |
697 | @@ -518,6 +575,9 @@ | |||
698 | 518 | pango_layout_set_spacing(layout, line_spacing * PANGO_SCALE); | 575 | pango_layout_set_spacing(layout, line_spacing * PANGO_SCALE); |
699 | 519 | 576 | ||
700 | 520 | pango_layout_set_height(layout, lines_); | 577 | pango_layout_set_height(layout, lines_); |
701 | 578 | |||
702 | 579 | SetAttributes(layout); | ||
703 | 580 | |||
704 | 521 | pangoCtx = pango_layout_get_context(layout); // is not ref'ed | 581 | pangoCtx = pango_layout_get_context(layout); // is not ref'ed |
705 | 522 | pango_cairo_context_set_font_options(pangoCtx, | 582 | pango_cairo_context_set_font_options(pangoCtx, |
706 | 523 | gdk_screen_get_font_options(screen)); | 583 | gdk_screen_get_font_options(screen)); |
707 | 524 | 584 | ||
708 | === modified file 'unity-shared/StaticCairoText.h' | |||
709 | --- unity-shared/StaticCairoText.h 2012-11-22 10:46:13 +0000 | |||
710 | +++ unity-shared/StaticCairoText.h 2012-11-28 10:01:36 +0000 | |||
711 | @@ -51,6 +51,14 @@ | |||
712 | 51 | NUX_ALIGN_BOTTOM = NUX_ALIGN_RIGHT | 51 | NUX_ALIGN_BOTTOM = NUX_ALIGN_RIGHT |
713 | 52 | }; | 52 | }; |
714 | 53 | 53 | ||
715 | 54 | enum UnderlineState | ||
716 | 55 | { | ||
717 | 56 | NUX_UNDERLINE_NONE, | ||
718 | 57 | NUX_UNDERLINE_SINGLE, | ||
719 | 58 | NUX_UNDERLINE_DOUBLE, | ||
720 | 59 | NUX_UNDERLINE_LOW | ||
721 | 60 | }; | ||
722 | 61 | |||
723 | 54 | StaticCairoText(std::string const& text, NUX_FILE_LINE_PROTO); | 62 | StaticCairoText(std::string const& text, NUX_FILE_LINE_PROTO); |
724 | 55 | StaticCairoText(std::string const& text, bool escape_text, NUX_FILE_LINE_PROTO); | 63 | StaticCairoText(std::string const& text, bool escape_text, NUX_FILE_LINE_PROTO); |
725 | 56 | ~StaticCairoText(); | 64 | ~StaticCairoText(); |
726 | @@ -67,12 +75,14 @@ | |||
727 | 67 | 75 | ||
728 | 68 | // public API | 76 | // public API |
729 | 69 | void SetText(std::string const& text, bool escape_text = false); | 77 | void SetText(std::string const& text, bool escape_text = false); |
730 | 78 | void SetTextAlpha(unsigned int alpha); | ||
731 | 70 | void SetTextColor(Color const& textColor); | 79 | void SetTextColor(Color const& textColor); |
732 | 71 | void SetTextEllipsize(EllipsizeState state); | 80 | void SetTextEllipsize(EllipsizeState state); |
733 | 72 | void SetTextAlignment(AlignState state); | 81 | void SetTextAlignment(AlignState state); |
734 | 73 | void SetTextVerticalAlignment(AlignState state); | 82 | void SetTextVerticalAlignment(AlignState state); |
735 | 74 | void SetFont(std::string const& font); | 83 | void SetFont(std::string const& font); |
736 | 75 | std::string GetFont(); | 84 | std::string GetFont(); |
737 | 85 | void SetUnderline(UnderlineState underline); | ||
738 | 76 | void SetLines(int maximum_lines); | 86 | void SetLines(int maximum_lines); |
739 | 77 | void SetLineSpacing(float line_spacing); | 87 | void SetLineSpacing(float line_spacing); |
740 | 78 | 88 |
Firstly, you need to watch out when copying classes. You should do a full code check to make sure there is nothing left over that isn't used in the new class, and anything (defs/comment) that references the old class.
I don't think this belongs to the AbstractButton scope; it's more in the StaticCairoText area, seeing as that's all a link really is as far as I'm aware (a label which you can click). You can then put the mouse/keyboard activation into the overriding class.
Other than that, here are a few code comments.
34 +namespace :Logger logger( "unity. dash.actionlink ");
35 +{
36 +
37 +
38 +nux::logging:
39 +}
remove extra white-space
99 +void ActionLink: :BuildLayout( std::string const& label, std::string const& extra_hint)
no extra_hint in an action link.
94 + cr_normal_ .reset( new nux::CairoWrapp er(geo,
95 + [](nux::Geometry const& geom, cairo_t* cr) {}));
You're not using the texture, so remove it from the class/drawing. (left over from ActionButton)
Underline: Can you not use font/markup to do the underlining instead of a fairly hacky hseparator?