Skip to content

Commit aa4734b

Browse files
Michael Braggmagreenblatt
Michael Bragg
authored andcommitted
alloy: win: Add spelling suggestions in context menu (fixes #3055)
1 parent f72afb7 commit aa4734b

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Diff for: libcef/browser/menu_manager.cc

+41-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
#include "content/public/browser/render_widget_host_view.h"
2121
#include "third_party/blink/public/mojom/context_menu/context_menu.mojom.h"
2222

23+
#if BUILDFLAG(IS_WIN)
24+
#include "chrome/browser/spellchecker/spellcheck_factory.h"
25+
#include "chrome/browser/spellchecker/spellcheck_service.h"
26+
#include "components/spellcheck/browser/spellcheck_platform.h"
27+
#endif
28+
2329
namespace {
2430

2531
CefString GetLabel(int message_id) {
@@ -120,8 +126,8 @@ bool CefMenuManager::IsShowingContextMenu() {
120126
return web_contents()->IsShowingContextMenu();
121127
}
122128

123-
bool CefMenuManager::CreateContextMenu(
124-
const content::ContextMenuParams& params) {
129+
bool CefMenuManager::CreateContextMenu(const content::ContextMenuParams& params,
130+
bool query_spellcheck) {
125131
// The renderer may send the "show context menu" message multiple times, one
126132
// for each right click mouse event it receives. Normally, this doesn't happen
127133
// because mouse events are not forwarded once the context menu is showing.
@@ -134,6 +140,24 @@ bool CefMenuManager::CreateContextMenu(
134140
}
135141

136142
params_ = params;
143+
144+
#if BUILDFLAG(IS_WIN)
145+
// System spellcheck suggestions need to be queried asynchronously.
146+
if (query_spellcheck && !params_.misspelled_word.empty() &&
147+
params_.dictionary_suggestions.empty()) {
148+
SpellcheckService* spellcheck_service =
149+
SpellcheckServiceFactory::GetForContext(
150+
browser_->web_contents()->GetBrowserContext());
151+
if (spellcheck_service) {
152+
spellcheck_platform::GetPerLanguageSuggestions(
153+
spellcheck_service->platform_spell_checker(), params_.misspelled_word,
154+
base::BindOnce(&CefMenuManager::OnGetPlatformSuggestionsComplete,
155+
weak_ptr_factory_.GetWeakPtr()));
156+
}
157+
return true;
158+
}
159+
#endif
160+
137161
model_->Clear();
138162

139163
// Create the default menu model.
@@ -511,3 +535,18 @@ bool CefMenuManager::IsCustomContextMenuCommand(int command_id) {
511535
}
512536
return false;
513537
}
538+
539+
#if BUILDFLAG(IS_WIN)
540+
void CefMenuManager::OnGetPlatformSuggestionsComplete(
541+
const spellcheck::PerLanguageSuggestions&
542+
platform_per_language_suggestions) {
543+
std::vector<std::u16string> combined_suggestions;
544+
spellcheck::FillSuggestions(platform_per_language_suggestions,
545+
&combined_suggestions);
546+
547+
params_.dictionary_suggestions = combined_suggestions;
548+
549+
// Now that we have spelling suggestions, call CreateContextMenu again.
550+
CreateContextMenu(params_, /*query_spellcheck=*/false);
551+
}
552+
#endif

Diff for: libcef/browser/menu_manager.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "content/public/browser/context_menu_params.h"
1414
#include "content/public/browser/web_contents_observer.h"
1515

16+
#if BUILDFLAG(IS_WIN)
17+
#include "components/spellcheck/common/spellcheck_common.h"
18+
#endif
19+
1620
namespace content {
1721
class RenderFrameHost;
1822
class WebContents;
@@ -39,7 +43,8 @@ class CefMenuManager : public CefMenuModelImpl::Delegate,
3943
bool IsShowingContextMenu();
4044

4145
// Create the context menu.
42-
bool CreateContextMenu(const content::ContextMenuParams& params);
46+
bool CreateContextMenu(const content::ContextMenuParams& params,
47+
bool query_spellcheck = true);
4348
void CancelContextMenu();
4449

4550
private:
@@ -62,6 +67,12 @@ class CefMenuManager : public CefMenuModelImpl::Delegate,
6267
// Returns true if the specified id is a custom context menu command.
6368
bool IsCustomContextMenuCommand(int command_id);
6469

70+
#if BUILDFLAG(IS_WIN)
71+
void OnGetPlatformSuggestionsComplete(
72+
const spellcheck::PerLanguageSuggestions&
73+
platform_per_language_suggestions);
74+
#endif
75+
6576
// AlloyBrowserHostImpl pointer is guaranteed to outlive this object.
6677
raw_ptr<AlloyBrowserHostImpl> browser_;
6778

0 commit comments

Comments
 (0)