OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/renderer_context_menu/spelling_options_submenu_observer .h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "chrome/app/chrome_command_ids.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "chrome/grit/generated_resources.h" | |
14 #include "components/prefs/pref_member.h" | |
15 #include "components/prefs/pref_service.h" | |
16 #include "components/renderer_context_menu/render_view_context_menu_proxy.h" | |
17 #include "content/public/browser/browser_context.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 #include "ui/base/models/menu_separator_types.h" | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 SpellingOptionsSubMenuObserver::SpellingOptionsSubMenuObserver( | |
25 RenderViewContextMenuProxy* proxy, | |
26 ui::SimpleMenuModel::Delegate* delegate, | |
27 int group_id) | |
28 : proxy_(proxy), | |
29 submenu_model_(delegate), | |
30 language_group_id_(group_id), | |
31 num_selected_languages_(0) { | |
32 DCHECK(proxy_); | |
33 } | |
34 | |
35 SpellingOptionsSubMenuObserver::~SpellingOptionsSubMenuObserver() {} | |
36 | |
37 void SpellingOptionsSubMenuObserver::InitMenu( | |
38 const content::ContextMenuParams& params) { | |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
40 | |
41 // Add available spell-checker languages to the sub menu. | |
42 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | |
43 DCHECK(browser_context); | |
44 num_selected_languages_ = | |
45 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | |
46 DCHECK(languages_.size() < | |
47 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
48 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
49 | |
50 if (languages_.size() > 1) { | |
51 submenu_model_.AddRadioItemWithStringId( | |
52 IDC_SPELLCHECK_MULTI_LINGUAL, | |
53 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_id_); | |
54 } | |
55 | |
56 const size_t kMaxLanguages = static_cast<size_t>( | |
57 IDC_SPELLCHECK_LANGUAGES_FIRST - IDC_SPELLCHECK_LANGUAGES_LAST); | |
58 for (size_t i = 0; i < languages_.size() && i < kMaxLanguages; ++i) { | |
groby-ooo-7-16
2016/02/04 18:59:10
We've DCHECKED above that i will be below kMaxLang
please use gerrit instead
2016/02/05 00:14:13
I need "i" to calculate "IDC_SPELLCHECK_LANGUAGES_
| |
59 submenu_model_.AddRadioItem( | |
60 IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
61 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), | |
62 language_group_id_); | |
63 } | |
64 | |
65 // Add an item that opens the 'fonts and languages options' page. | |
66 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
67 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | |
68 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | |
69 | |
70 if (num_selected_languages_ > 0) { | |
71 // Add a 'Check spelling while typing' item in the sub menu. | |
groby-ooo-7-16
2016/02/04 18:59:10
This should be greyed out if no languages are sele
please use gerrit instead
2016/02/05 00:14:13
========
1) If the user sees the following menu,
| |
72 submenu_model_.AddCheckItem( | |
73 IDC_CHECK_SPELLING_WHILE_TYPING, | |
74 l10n_util::GetStringUTF16( | |
75 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | |
76 } | |
77 | |
78 // Add a check item "Ask Google for spelling suggestions" item. (This class | |
79 // does not handle this item because the SpellingMenuObserver class handles it | |
80 // on behalf of this class.) | |
groby-ooo-7-16
2016/02/04 18:59:10
Would it make sense to handle it here instead?
please use gerrit instead
2016/02/05 00:14:13
This class is not used on Mac, which also has "Ask
| |
81 submenu_model_.AddCheckItem( | |
82 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
83 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | |
84 | |
85 proxy_->AddSubMenu( | |
86 IDC_SPELLCHECK_MENU, | |
87 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | |
88 &submenu_model_); | |
89 } | |
90 | |
91 bool SpellingOptionsSubMenuObserver::IsCommandIdSupported(int command_id) { | |
92 // Allow Spell Check language items on sub menu for text area context menu. | |
93 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
94 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
groby-ooo-7-16
2016/02/04 18:59:10
Should this gate on the number of languages in |la
please use gerrit instead
2016/02/05 00:14:13
I would like to avoid the extra calculation of "ID
| |
95 return true; | |
96 } | |
97 | |
98 switch (command_id) { | |
99 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: | |
100 // Return false so RenderViewContextMenu can handle this item because it | |
101 // is hard for this class to handle it. | |
groby-ooo-7-16
2016/02/04 18:59:10
No excuses! ;) Why is it hard?
please use gerrit instead
2016/02/05 00:14:13
Agreed that this is worded badly. This is similar
| |
102 return false; | |
103 | |
104 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
105 case IDC_SPELLCHECK_MENU: | |
106 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
107 return true; | |
108 } | |
109 | |
110 return false; | |
111 } | |
112 | |
113 bool SpellingOptionsSubMenuObserver::IsCommandIdChecked(int command_id) { | |
114 DCHECK(IsCommandIdSupported(command_id)); | |
115 | |
116 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) | |
117 return num_selected_languages_ == languages_.size(); | |
118 | |
119 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
120 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
121 if (languages_.size() == 1 && num_selected_languages_ == 1) | |
122 return true; | |
123 | |
124 if (languages_.size() == num_selected_languages_) | |
125 return false; | |
groby-ooo-7-16
2016/02/04 18:59:10
You might combine with previous if...
if (size ==
please use gerrit instead
2016/02/05 00:14:13
Done.
| |
126 | |
127 size_t language_index = | |
128 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
129 // The first |num_selected_languages_| are used for spellchecking. | |
130 return num_selected_languages_ > language_index; | |
131 } | |
132 | |
133 // Check box for 'Check Spelling while typing'. | |
134 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
135 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
136 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
137 } | |
138 | |
139 return false; | |
140 } | |
141 | |
142 bool SpellingOptionsSubMenuObserver::IsCommandIdEnabled(int command_id) { | |
143 DCHECK(IsCommandIdSupported(command_id)); | |
144 | |
145 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
146 DCHECK(profile); | |
147 const PrefService* pref = profile->GetPrefs(); | |
148 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
149 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || | |
150 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
151 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
152 } | |
153 | |
154 switch (command_id) { | |
155 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
156 case IDC_SPELLCHECK_MENU: | |
157 return true; | |
158 } | |
159 | |
160 return false; | |
161 } | |
162 | |
163 void SpellingOptionsSubMenuObserver::ExecuteCommand(int command_id) { | |
164 DCHECK(IsCommandIdSupported(command_id)); | |
165 | |
166 // Check to see if one of the spell check language ids have been clicked. | |
167 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
168 DCHECK(profile); | |
169 | |
170 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
171 static_cast<size_t>(command_id) < | |
172 IDC_SPELLCHECK_LANGUAGES_FIRST + languages_.size()) { | |
173 size_t language_index = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | |
174 StringListPrefMember dictionaries_pref; | |
175 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
176 dictionaries_pref.SetValue({languages_[language_index]}); | |
177 return; | |
178 } | |
179 | |
180 switch (command_id) { | |
181 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
182 profile->GetPrefs()->SetBoolean( | |
183 prefs::kEnableContinuousSpellcheck, | |
184 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
185 break; | |
186 | |
187 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
188 StringListPrefMember dictionaries_pref; | |
189 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, | |
190 profile->GetPrefs()); | |
191 dictionaries_pref.SetValue(languages_); | |
192 break; | |
193 } | |
194 } | |
OLD | NEW |