Chromium Code Reviews| Index: chrome/browser/renderer_context_menu/spellchecker_submenu_observer.cc |
| diff --git a/chrome/browser/renderer_context_menu/spellchecker_submenu_observer.cc b/chrome/browser/renderer_context_menu/spellchecker_submenu_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a5df333ccc2adb867d22e909ae2c4c378af8c07f |
| --- /dev/null |
| +++ b/chrome/browser/renderer_context_menu/spellchecker_submenu_observer.cc |
| @@ -0,0 +1,193 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/logging.h" |
| +#include "base/prefs/pref_member.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
| +#include "chrome/browser/spellchecker/spellcheck_service.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/common/spellcheck_common.h" |
| +#include "chrome/grit/generated_resources.h" |
| +#include "content/public/browser/render_view_host.h" |
| +#include "content/public/browser/render_widget_host_view.h" |
| +#include "extensions/browser/view_type_utils.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/models/simple_menu_model.h" |
| + |
| +using content::BrowserThread; |
| + |
| +SpellCheckerSubMenuObserver::SpellCheckerSubMenuObserver( |
| + RenderViewContextMenuProxy* proxy, |
| + ui::SimpleMenuModel::Delegate* delegate, |
| + int group) |
| + : proxy_(proxy), |
| + submenu_model_(delegate), |
| + language_group_(group), |
| + num_selected_languages_(0) { |
| + DCHECK(proxy_); |
| + DCHECK(!chrome::spellcheck_common::IsMultilingualSpellcheckEnabled()); |
| +} |
| + |
| +SpellCheckerSubMenuObserver::~SpellCheckerSubMenuObserver() { |
| +} |
| + |
| +void SpellCheckerSubMenuObserver::InitMenu( |
| + const content::ContextMenuParams& params) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + submenu_model_.AddRadioItemWithStringId( |
|
lazyboy
2016/01/28 20:36:24
Do we not want to guard this with >= 2 languages?
please use gerrit instead
2016/01/28 21:41:59
Done in patch 2.
|
| + IDC_SPELLCHECK_MULTI_LINGUAL, |
| + IDS_CONTENT_CONTEXT_SPELLCHECK_MULTI_LINGUAL, language_group_); |
| + |
| + // Add available spell-checker languages to the sub menu. |
| + content::BrowserContext* browser_context = proxy_->GetBrowserContext(); |
| + DCHECK(browser_context); |
| + num_selected_languages_ = |
| + SpellcheckService::GetSpellCheckLanguages(browser_context, &languages_); |
| + DCHECK(languages_.size() < |
| + IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| + const std::string app_locale = g_browser_process->GetApplicationLocale(); |
| + |
| + for (size_t i = 0; i < languages_.size(); ++i) { |
|
lazyboy
2016/01/28 20:36:25
Should we skip items beyond 100 if languages_.size
please use gerrit instead
2016/01/28 21:41:59
Done.
|
| + submenu_model_.AddRadioItem( |
| + IDC_SPELLCHECK_LANGUAGES_FIRST + i, |
| + l10n_util::GetDisplayNameForLocale(languages_[i], app_locale, true), |
| + language_group_); |
| + } |
| + |
| + // Add an item that opens the 'fonts and languages options' page. |
| + submenu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| + submenu_model_.AddItemWithStringId( |
| + IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS, |
| + IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS); |
| + |
| + if (num_selected_languages_ > 0) { |
| + // Add a 'Check spelling while typing' item in the sub menu. |
| + submenu_model_.AddCheckItem( |
| + IDC_CHECK_SPELLING_WHILE_TYPING, |
| + l10n_util::GetStringUTF16( |
| + IDS_CONTENT_CONTEXT_CHECK_SPELLING_WHILE_TYPING)); |
| + } |
| + |
| + // Add a check item "Ask Google for spelling suggestions" item. (This class |
| + // does not handle this item because the SpellingMenuObserver class handles it |
| + // on behalf of this class.) |
| + submenu_model_.AddCheckItem( |
| + IDC_CONTENT_CONTEXT_SPELLING_TOGGLE, |
| + l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLING_ASK_GOOGLE)); |
| + |
| + proxy_->AddSubMenu( |
| + IDC_SPELLCHECK_MENU, |
| + l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU), |
| + &submenu_model_); |
| +} |
| + |
| +bool SpellCheckerSubMenuObserver::IsCommandIdSupported(int command_id) { |
| + // Allow Spell Check language items on sub menu for text area context menu. |
| + if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| + command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| + return true; |
| + } |
| + |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS: |
| + // Return false so RenderViewContextMenu can handle this item because it |
| + // is hard for this class to handle it. |
| + return false; |
| + |
| + case IDC_CHECK_SPELLING_WHILE_TYPING: |
| + case IDC_SPELLCHECK_MENU: |
| + case IDC_SPELLCHECK_MULTI_LINGUAL: |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool SpellCheckerSubMenuObserver::IsCommandIdChecked(int command_id) { |
| + DCHECK(IsCommandIdSupported(command_id)); |
| + |
| + if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) |
| + return num_selected_languages_ == languages_.size(); |
| + |
| + if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| + command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| + return num_selected_languages_ != languages_.size() && |
| + num_selected_languages_ > |
| + static_cast<size_t>(command_id - IDC_SPELLCHECK_LANGUAGES_FIRST); |
| + } |
| + |
| + // Check box for 'Check Spelling while typing'. |
| + if (command_id == IDC_CHECK_SPELLING_WHILE_TYPING) { |
| + Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| + DCHECK(profile); |
|
lazyboy
2016/01/28 20:36:25
This is probably not necessary, we'll crash on the
please use gerrit instead
2016/01/28 21:41:59
Done.
|
| + return profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool SpellCheckerSubMenuObserver::IsCommandIdEnabled(int command_id) { |
| + DCHECK(IsCommandIdSupported(command_id)); |
| + |
| + Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| + DCHECK(profile); |
| + const PrefService* pref = profile->GetPrefs(); |
| + if ((command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| + command_id < IDC_SPELLCHECK_LANGUAGES_LAST) || |
| + command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { |
| + return pref->GetBoolean(prefs::kEnableContinuousSpellcheck); |
| + } |
| + |
| + switch (command_id) { |
| + case IDC_CHECK_SPELLING_WHILE_TYPING: |
| + case IDC_SPELLCHECK_MENU: |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void SpellCheckerSubMenuObserver::ExecuteCommand(int command_id) { |
| + DCHECK(IsCommandIdSupported(command_id)); |
| + |
| + // Check to see if one of the spell check language ids have been clicked. |
| + Profile* profile = Profile::FromBrowserContext(proxy_->GetBrowserContext()); |
| + DCHECK(profile); |
| + |
| + if (command_id == IDC_SPELLCHECK_MULTI_LINGUAL) { |
|
lazyboy
2016/01/28 20:36:25
Can we fold this into the switch below on line 186
please use gerrit instead
2016/01/28 21:41:59
Done.
|
| + StringListPrefMember dictionaries_pref; |
| + dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); |
| + dictionaries_pref.SetValue(languages_); |
|
lazyboy
2016/01/28 20:36:25
return?
please use gerrit instead
2016/01/28 21:41:59
using "break" inside of the switch.
|
| + } |
| + |
| + if (command_id >= IDC_SPELLCHECK_LANGUAGES_FIRST && |
| + command_id < IDC_SPELLCHECK_LANGUAGES_LAST) { |
| + size_t language = command_id - IDC_SPELLCHECK_LANGUAGES_FIRST; |
|
lazyboy
2016/01/28 20:36:25
language_index
please use gerrit instead
2016/01/28 21:41:59
Done.
|
| + DCHECK_LT(language, languages_.size()); |
| + StringListPrefMember dictionaries_pref; |
| + dictionaries_pref.Init(prefs::kSpellCheckDictionaries, profile->GetPrefs()); |
| + |
| + dictionaries_pref.SetValue( |
| + std::vector<std::string>(1, languages_[language])); |
| + |
| + return; |
| + } |
| + |
| + switch (command_id) { |
| + case IDC_CHECK_SPELLING_WHILE_TYPING: |
| + profile->GetPrefs()->SetBoolean( |
| + prefs::kEnableContinuousSpellcheck, |
| + !profile->GetPrefs()->GetBoolean(prefs::kEnableContinuousSpellcheck)); |
| + break; |
| + } |
| +} |