Merge lp:~azzar1/unity/fix-971332 into lp:unity

Proposed by Andrea Azzarone
Status: Merged
Approved by: Gord Allott
Approved revision: no longer in the source branch.
Merged at revision: 2230
Proposed branch: lp:~azzar1/unity/fix-971332
Merge into: lp:unity
Diff against target: 173 lines (+66/-14)
5 files modified
plugins/unityshell/src/ShortcutHint.cpp (+1/-1)
plugins/unityshell/src/ShortcutHintPrivate.cpp (+27/-0)
plugins/unityshell/src/ShortcutHintPrivate.h (+1/-0)
plugins/unityshell/src/ShortcutView.cpp (+36/-12)
plugins/unityshell/src/ShortcutView.h (+1/-1)
To merge this branch: bzr merge lp:~azzar1/unity/fix-971332
Reviewer Review Type Date Requested Status
Gord Allott (community) Approve
Tim Penhey (community) Needs Information
Review via email: mp+100673@code.launchpad.net

Commit message

Use gtk_accelerator_get_label to get a translatable key binding.

Description of the change

== The problem ==
Overlay key bindings are not fully translatable.

== The fix ==
Use gtk_accelerator_get_label to get a translatable key binding. The goal is to increase the use of GtkAccelerator in the shortcut overlay.

== Test ==
The shortcut overlay has already tests. No need to test gtk_accelerator_get_label.

To post a comment you must log in.
Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

41 + glib::String key(gtk_accelerator_get_label(accelerator_key, accelerator_mods));
42 +
43 + std::string temp(key.Str());
44 + if (temp.empty())
45 + return "";

This could just be:

std::string key(glib::String(gtk_accelerator_get_label(accelerator_key, accelerator_mods)).Str())

or... use std::string const& temp = key.Str()

Revision history for this message
Tim Penhey (thumper) wrote :

On 04/04/12 09:58, Marco Trevisan (Treviño) wrote:
> 41 + glib::String key(gtk_accelerator_get_label(accelerator_key, accelerator_mods));
> 42 +
> 43 + std::string temp(key.Str());
> 44 + if (temp.empty())
> 45 + return "";
>
> This could just be:
>
> std::string key(glib::String(gtk_accelerator_get_label(accelerator_key, accelerator_mods)).Str())
>
> or... use std::string const& temp = key.Str()

No, don't do that last one.

key.Str() creates an object.
   std::string const& temp = key.Str();
Creates a reference to a temporary. At the best, the compiler will
complain, at the worst, your program will crash when you try to access temp.

Revision history for this message
Tim Penhey (thumper) wrote :

> std::string ret(temp.begin(), temp.end() - 1);

What are you trying to do here?

> if (scut[scut.size() - 1] != '+')
> ret += scut[scut.size() - 1];

And what is happening here?

review: Needs Information
Revision history for this message
Andrea Azzarone (azzar1) wrote :

> > std::string ret(temp.begin(), temp.end() - 1);
>
> What are you trying to do here?
>
> > if (scut[scut.size() - 1] != '+')
> > ret += scut[scut.size() - 1];
>
> And what is happening here?

gtk_accelerator_get_label adds an extra '+'. Also it doesn't add spaces around the '+'.

Revision history for this message
Andrea Azzarone (azzar1) wrote :

Now it should be clearer.

Revision history for this message
Gord Allott (gordallott) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/unityshell/src/ShortcutHint.cpp'
2--- plugins/unityshell/src/ShortcutHint.cpp 2012-03-14 06:24:18 +0000
3+++ plugins/unityshell/src/ShortcutHint.cpp 2012-04-04 15:51:42 +0000
4@@ -103,7 +103,7 @@
5 {
6 if (opt.name() == arg2())
7 {
8- std::string temp = impl::FixShortcutFormat(opt.value().action().keyToString());
9+ std::string temp(impl::GetTranslatableLabel(opt.value().action().keyToString()));
10 temp = impl::ProperCase(temp);
11
12 if (value() != temp)
13
14=== modified file 'plugins/unityshell/src/ShortcutHintPrivate.cpp'
15--- plugins/unityshell/src/ShortcutHintPrivate.cpp 2012-03-14 06:24:18 +0000
16+++ plugins/unityshell/src/ShortcutHintPrivate.cpp 2012-04-04 15:51:42 +0000
17@@ -17,8 +17,10 @@
18 */
19
20 #include <glib/gi18n-lib.h>
21+#include <gtk/gtk.h>
22
23 #include "ShortcutHintPrivate.h"
24+#include "UnityCore/GLibWrapper.h"
25
26 #include <boost/algorithm/string/replace.hpp>
27
28@@ -51,6 +53,31 @@
29 return ret;
30 }
31
32+std::string GetTranslatableLabel(std::string const& scut)
33+{
34+ guint accelerator_key;
35+ GdkModifierType accelerator_mods;
36+
37+ gtk_accelerator_parse(scut.c_str(),
38+ &accelerator_key,
39+ &accelerator_mods);
40+
41+ std::string temp(glib::String(gtk_accelerator_get_label(accelerator_key, accelerator_mods)).Str());
42+
43+ // gtk_accelerator_get_label adds an extra '+' at the end of the label.
44+ if (temp.length() > 0)
45+ {
46+ std::string::iterator it = temp.end() - 1;
47+ if (*it == '+')
48+ temp.erase(it);
49+ }
50+
51+ // Adds an extra space around the '+'.
52+ boost::replace_all(temp, "+", " + ");
53+
54+ return temp;
55+}
56+
57 std::string FixMouseShortcut(std::string const& scut)
58 {
59 std::string ret(scut);
60
61=== modified file 'plugins/unityshell/src/ShortcutHintPrivate.h'
62--- plugins/unityshell/src/ShortcutHintPrivate.h 2012-03-14 06:24:18 +0000
63+++ plugins/unityshell/src/ShortcutHintPrivate.h 2012-04-04 15:51:42 +0000
64@@ -30,6 +30,7 @@
65
66 std::string GetMetaKey(std::string const& scut);
67 std::string FixShortcutFormat(std::string const& scut);
68+std::string GetTranslatableLabel(std::string const& scut);
69 std::string FixMouseShortcut(std::string const& scut);
70 std::string ProperCase(std::string const& str);
71
72
73=== modified file 'plugins/unityshell/src/ShortcutView.cpp'
74--- plugins/unityshell/src/ShortcutView.cpp 2012-03-29 05:14:03 +0000
75+++ plugins/unityshell/src/ShortcutView.cpp 2012-04-04 15:51:42 +0000
76@@ -38,7 +38,29 @@
77 int SHORTKEY_COLUMN_WIDTH = 150;
78 int DESCRIPTION_COLUMN_WIDTH = 265;
79 int LINE_SPACING = 5;
80-} // namespace anonymouse
81+
82+ // We need this class because SetVisible doesn't work for layouts.
83+ class SectionView : public nux::View
84+ {
85+ public:
86+ SectionView(NUX_FILE_LINE_DECL)
87+ : nux::View(NUX_FILE_LINE_PARAM)
88+ {
89+ }
90+
91+ protected:
92+ void Draw(nux::GraphicsEngine& graphics_engine, bool force_draw)
93+ {
94+ }
95+
96+ void DrawContent(nux::GraphicsEngine& graphics_engine, bool force_draw)
97+ {
98+ if (GetLayout())
99+ GetLayout()->ProcessDraw(graphics_engine, force_draw);
100+ }
101+ };
102+
103+} // unnamed namespace
104
105 NUX_IMPLEMENT_OBJECT_TYPE(View);
106
107@@ -109,9 +131,13 @@
108 return layout;
109 }
110
111-nux::LinearLayout* View::CreateShortKeyEntryLayout(AbstractHint::Ptr const& hint)
112+nux::View* View::CreateShortKeyEntryView(AbstractHint::Ptr const& hint)
113 {
114+ nux::View* view = new SectionView(NUX_TRACKER_LOCATION);
115+
116 nux::HLayout* layout = new nux::HLayout("EntryLayout", NUX_TRACKER_LOCATION);
117+ view->SetLayout(layout);
118+
119 nux::HLayout* shortkey_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
120 nux::HLayout* description_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
121
122@@ -152,17 +178,18 @@
123 layout->SetSpaceBetweenChildren(INTER_SPACE_SHORTKEY_DESCRIPTION);
124 description_layout->SetContentDistribution(nux::MAJOR_POSITION_START);
125
126- auto on_shortkey_changed = [](std::string const& new_shortkey, nux::StaticText* view) {
127- std::string skey = "<b>";
128+ auto on_shortkey_changed = [](std::string const& new_shortkey, nux::View* main_view, nux::StaticText* view) {
129+ std::string skey("<b>");
130 skey += new_shortkey;
131 skey += "</b>";
132
133 view->SetText(skey);
134+ main_view->SetVisible(!new_shortkey.empty());
135 };
136
137- hint->shortkey.changed.connect(sigc::bind(sigc::slot<void, std::string const&, nux::StaticText*>(on_shortkey_changed), shortkey_view));
138+ hint->shortkey.changed.connect(sigc::bind(sigc::slot<void, std::string const&, nux::View*, nux::StaticText*>(on_shortkey_changed), view, shortkey_view));
139
140- return layout;
141+ return view;
142 }
143
144 nux::LinearLayout* View::CreateIntermediateLayout()
145@@ -208,12 +235,9 @@
146
147 for (auto hint : model_->hints()[category])
148 {
149- //std::string str_value = hint->prefix() + hint->value() + hint->postfix();
150- //boost::replace_all(str_value, "&", "&amp;");
151- //boost::replace_all(str_value, "<", "&lt;");
152- //boost::replace_all(str_value, ">", "&gt;");
153-
154- intermediate_layout->AddLayout(CreateShortKeyEntryLayout(hint), 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
155+ nux::View* view = CreateShortKeyEntryView(hint);
156+ view->SetVisible(!hint->shortkey().empty());
157+ intermediate_layout->AddView(view, 0, nux::MINOR_POSITION_START, nux::MINOR_SIZE_FULL);
158 }
159
160 section_layout->AddLayout(intermediate_layout, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
161
162=== modified file 'plugins/unityshell/src/ShortcutView.h'
163--- plugins/unityshell/src/ShortcutView.h 2012-03-29 05:14:03 +0000
164+++ plugins/unityshell/src/ShortcutView.h 2012-04-04 15:51:42 +0000
165@@ -59,7 +59,7 @@
166 private:
167 // Private methods
168 nux::LinearLayout* CreateSectionLayout(const char* section_name);
169- nux::LinearLayout* CreateShortKeyEntryLayout(AbstractHint::Ptr const& hint);
170+ nux::View* CreateShortKeyEntryView(AbstractHint::Ptr const& hint);
171 nux::LinearLayout* CreateIntermediateLayout();
172
173 void RenderColumns();