Chromium Code Reviews| 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 <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/prefs/pref_member.h" | |
| 11 #include "base/prefs/pref_service.h" | |
| 12 #include "chrome/app/chrome_command_ids.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | |
| 15 #include "chrome/browser/spellchecker/spellcheck_service.h" | |
| 16 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 #include "chrome/common/spellcheck_common.h" | |
| 19 #include "chrome/grit/generated_resources.h" | |
| 20 #include "content/public/browser/render_view_host.h" | |
| 21 #include "content/public/browser/render_widget_host_view.h" | |
| 22 #include "extensions/browser/view_type_utils.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/models/simple_menu_model.h" | |
| 25 | |
| 26 using content::BrowserThread; | |
| 27 | |
| 28 SpellingOptionsSubMenuObserver::SpellingOptionsSubMenuObserver( | |
| 29 RenderViewContextMenuProxy* proxy, | |
| 30 ui::SimpleMenuModel::Delegate* delegate, | |
| 31 int group_id) | |
| 32 : proxy_(proxy), | |
|
groby-ooo-7-16
2016/01/28 22:49:28
Hm. I wonder if it's a better idea to
a) pass thi
please use gerrit instead
2016/01/29 20:09:34
Both good ideas, but implementing the RenderViewCo
| |
| 33 submenu_model_(delegate), | |
| 34 language_group_id_(group_id), | |
| 35 num_selected_languages_(0) { | |
| 36 DCHECK(proxy_); | |
| 37 } | |
| 38 | |
| 39 SpellingOptionsSubMenuObserver::~SpellingOptionsSubMenuObserver() {} | |
| 40 | |
| 41 void SpellingOptionsSubMenuObserver::InitMenu( | |
| 42 const content::ContextMenuParams& params) { | |
| 43 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 44 | |
| 45 // Add available spell-checker languages to the sub menu. | |
| 46 content::BrowserContext* browser_context = proxy_->GetBrowserContext(); | |
| 47 DCHECK(browser_context); | |
| 48 num_selected_languages_ = | |
| 49 SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); | |
| 50 DCHECK(languages_.size() < | |
| 51 IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
| 52 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
| 53 | |
| 54 if (languages_.size() > 1) { | |
| 55 submenu_model_.AddRadioItemWithStringId( | |
| 56 IDC_SPELLCHECK_MULTI_LINGUAL, | |
| 57 IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_id_); | |
| 58 } | |
| 59 | |
| 60 const int kMaxLanguages = 100; | |
| 61 for (size_t i = 0; i < languages_.size() && i < kMaxLanguages; ++i) { | |
| 62 submenu_model_.AddRadioItem( | |
| 63 IDC_SPELLCHECK_LANGUAGES_FIRST + i, | |
| 64 l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), | |
| 65 language_group_id_); | |
| 66 } | |
| 67 | |
| 68 // Add an item that opens the 'fonts and languages options' page. | |
| 69 submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | |
| 70 submenu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, | |
| 71 IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); | |
| 72 | |
| 73 if (num_selected_languages_ > 0) { | |
| 74 // Add a 'Check spelling while typing' item in the sub menu. | |
| 75 submenu_model_.AddCheckItem( | |
| 76 IDC_CHECK_SPELLING_WHILE_TYPING, | |
| 77 l10n_util::GetStringUTF16( | |
| 78 IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); | |
| 79 } | |
| 80 | |
| 81 // Add a check item "Ask Google for spelling suggestions" item. (This class | |
| 82 // does not handle this item because the SpellingMenuObserver class handles it | |
| 83 // on behalf of this class.) | |
| 84 submenu_model_.AddCheckItem( | |
| 85 IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, | |
| 86 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); | |
| 87 | |
| 88 proxy_->AddSubMenu( | |
| 89 IDC_SPELLCHECK_MENU, | |
| 90 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), | |
| 91 &submenu_model_); | |
| 92 } | |
| 93 | |
| 94 bool SpellingOptionsSubMenuObserver::IsCommandIdSupported(int command_id) { | |
| 95 // Allow Spell Check language items on sub menu for text area context menu. | |
| 96 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
| 97 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 switch (command_id) { | |
| 102 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: | |
| 103 // Return false so RenderViewContextMenu can handle this item because it | |
| 104 // is hard for this class to handle it. | |
| 105 return false; | |
| 106 | |
| 107 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
| 108 case IDC_SPELLCHECK_MENU: | |
| 109 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 bool SpellingOptionsSubMenuObserver::IsCommandIdChecked(int command_id) { | |
| 117 DCHECK(IsCommandIdSupported(command_id)); | |
| 118 | |
| 119 if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) | |
| 120 return num_selected_languages_ == languages_.size(); | |
| 121 | |
| 122 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
| 123 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
| 124 if (languages_.size() == 1 && num_selected_languages_ == 1) | |
| 125 return true; | |
| 126 | |
| 127 if (languages_.size() == num_selected_languages_) | |
| 128 return false; | |
| 129 | |
| 130 return num_selected_languages_ > | |
|
groby-ooo-7-16
2016/01/28 22:49:28
What is this test supposed to achieve? Comment, pl
please use gerrit instead
2016/01/29 20:09:34
Commented and pulled out "command_id - IDC_SPELLCH
| |
| 131 static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); | |
| 132 } | |
| 133 | |
| 134 // Check box for 'Check Spelling while typing'. | |
| 135 if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { | |
| 136 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
| 137 return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
| 138 } | |
| 139 | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 bool SpellingOptionsSubMenuObserver::IsCommandIdEnabled(int command_id) { | |
| 144 DCHECK(IsCommandIdSupported(command_id)); | |
| 145 | |
| 146 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
| 147 DCHECK(profile); | |
| 148 const PrefService* pref = profile->GetPrefs(); | |
| 149 if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
| 150 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || | |
| 151 command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { | |
| 152 return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); | |
| 153 } | |
| 154 | |
| 155 switch (command_id) { | |
| 156 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
| 157 case IDC_SPELLCHECK_MENU: | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 void SpellingOptionsSubMenuObserver::ExecuteCommand(int command_id) { | |
| 165 DCHECK(IsCommandIdSupported(command_id)); | |
| 166 | |
| 167 // Check to see if one of the spell check language ids have been clicked. | |
| 168 Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); | |
| 169 DCHECK(profile); | |
| 170 | |
| 171 if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && | |
| 172 command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { | |
|
groby-ooo-7-16
2016/01/28 22:49:28
Do you want to dynamically adjust to LANGUAGES_FIR
please use gerrit instead
2016/01/29 20:09:34
Done.
| |
| 173 size_t language_index = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; | |
| 174 DCHECK_LT(language_index, languages_.size()); | |
| 175 StringListPrefMember dictionaries_pref; | |
| 176 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); | |
| 177 | |
| 178 dictionaries_pref.SetValue( | |
| 179 std::vector<std::string>(1, languages_[language_index])); | |
|
groby-ooo-7-16
2016/01/28 22:49:28
Sneaky! ;) (The use of the initializer, that is)
please use gerrit instead
2016/01/29 20:09:34
Now that you mention it, we can start using initia
| |
| 180 | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 switch (command_id) { | |
| 185 case IDC_CHECK_SPELLING_WHILE_TYPING: | |
| 186 profile->GetPrefs()->SetBoolean( | |
| 187 prefs::kEnableContinuousSpellcheck, | |
| 188 !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); | |
| 189 break; | |
| 190 | |
| 191 case IDC_SPELLCHECK_MULTI_LINGUAL: | |
| 192 StringListPrefMember dictionaries_pref; | |
| 193 dictionaries_pref.Init(prefs::kSpellCheckDictionaries, | |
| 194 profile->GetPrefs()); | |
| 195 dictionaries_pref.SetValue(languages_); | |
| 196 break; | |
| 197 } | |
| 198 } | |
| OLD | NEW |