diff --git a/SampleApps/WebView2APISample/SettingsComponent.cpp b/SampleApps/WebView2APISample/SettingsComponent.cpp index 90b0402..eefc1a4 100644 --- a/SampleApps/WebView2APISample/SettingsComponent.cpp +++ b/SampleApps/WebView2APISample/SettingsComponent.cpp @@ -601,6 +601,11 @@ bool SettingsComponent::HandleWindowMessage( wil::com_ptr target; CHECK_FAILURE(args->get_ContextMenuTarget(&target)); + if (TryShowSpellCheckContextMenu(args, items, target)) + { + return S_OK; + } + COREWEBVIEW2_CONTEXT_MENU_TARGET_KIND targetKind; CHECK_FAILURE(target->get_Kind(&targetKind)); // Custom UI Context Menu rendered if invoked on selected text @@ -610,29 +615,30 @@ bool SettingsComponent::HandleWindowMessage( auto showMenu = [this, args, itemsCount, items, target] { CHECK_FAILURE(args->put_Handled(true)); - HMENU hPopupMenu = CreatePopupMenu(); - AddMenuItems(hPopupMenu, items); + wil::unique_hmenu hPopupMenu(CreatePopupMenu()); + AddMenuItems(hPopupMenu.get(), items); HWND hWnd; - m_controller->get_ParentWindow(&hWnd); + CHECK_FAILURE(m_controller->get_ParentWindow(&hWnd)); SetForegroundWindow(hWnd); RECT rct; GetClientRect(hWnd, &rct); - POINT topLeft; - topLeft.x = rct.left; - topLeft.y = rct.top; + POINT topLeft = {rct.left, rct.top}; ClientToScreen(hWnd, &topLeft); - POINT p; - CHECK_FAILURE(args->get_Location(&p)); + POINT location; + CHECK_FAILURE(args->get_Location(&location)); RECT bounds; CHECK_FAILURE(m_controller->get_Bounds(&bounds)); double scale; - m_controller3->get_RasterizationScale(&scale); + CHECK_FAILURE( + m_controller3->get_RasterizationScale(&scale)); INT32 selectedCommandId = TrackPopupMenu( - hPopupMenu, + hPopupMenu.get(), TPM_TOPALIGN | TPM_LEFTALIGN | TPM_RETURNCMD, - bounds.left + topLeft.x + ((int)(p.x * scale)), - bounds.top + topLeft.y + ((int)(p.y * scale)), 0, hWnd, - NULL); + bounds.left + topLeft.x + + static_cast(location.x * scale), + bounds.top + topLeft.y + + static_cast(location.y * scale), + 0, hWnd, nullptr); CHECK_FAILURE( args->put_SelectedCommandId(selectedCommandId)); }; @@ -1882,6 +1888,146 @@ void SettingsComponent::SetEnhancedSecurityModeState( } //! [SetEnhancedSecurityModeState] +//! [SpellCheckSuggestions] +bool SettingsComponent::TryShowSpellCheckContextMenu( + wil::com_ptr args, + wil::com_ptr items, + wil::com_ptr target) +{ + auto spellCheckTarget = target.try_query(); + if (!spellCheckTarget) + { + return false; + } + BOOL hasSpellingError = FALSE; + CHECK_FAILURE(spellCheckTarget->get_HasSpellingError(&hasSpellingError)); + if (!hasSpellingError) + { + return false; + } + + wil::com_ptr deferral; + CHECK_FAILURE(args->GetDeferral(&deferral)); + CHECK_FAILURE(args->put_Handled(TRUE)); + CHECK_FAILURE(spellCheckTarget->GetSpellCheckSuggestions( + Callback( + [appWindow = m_appWindow, webView = m_webView, args, items, deferral]( + HRESULT errorCode, + ICoreWebView2ExperimentalSpellCheckSuggestionCollectionView* suggestions) + -> HRESULT + { + if (FAILED(errorCode)) + { + CHECK_FAILURE(deferral->Complete()); + if (errorCode != E_ABORT) + { + CHECK_FAILURE(errorCode); + } + return S_OK; + } + wil::com_ptr + suggestionItems = suggestions; + appWindow->RunAsync( + [appWindow, webView, args, items, suggestionItems, deferral]() + { + auto completeDeferral = wil::scope_exit( + [deferral]() { CHECK_FAILURE(deferral->Complete()); }); + auto settings = appWindow->GetComponent(); + if (!settings || settings->m_webView.get() != webView.get()) + { + return; + } + wil::unique_hmenu hPopupMenu(CreatePopupMenu()); + + UINT32 suggestionCount = 0; + if (suggestionItems) + { + CHECK_FAILURE(suggestionItems->get_Count(&suggestionCount)); + } + for (UINT32 i = 0; i < suggestionCount; i++) + { + wil::com_ptr + suggestion; + CHECK_FAILURE(suggestionItems->GetValueAtIndex(i, &suggestion)); + wil::unique_cotaskmem_string label; + CHECK_FAILURE(suggestion->get_SuggestionText(&label)); + INT32 commandId = 0; + CHECK_FAILURE(suggestion->get_CommandId(&commandId)); + AppendMenu( + hPopupMenu.get(), MF_STRING, static_cast(commandId), + label.get()); + } + if (suggestionCount > 0) + { + AppendMenu(hPopupMenu.get(), MF_SEPARATOR, 0, nullptr); + } + + UINT32 defaultItemsCount = 0; + CHECK_FAILURE(items->get_Count(&defaultItemsCount)); + for (UINT32 i = defaultItemsCount; i > 0; --i) + { + wil::com_ptr item; + CHECK_FAILURE(items->GetValueAtIndex(i - 1, &item)); + wil::unique_cotaskmem_string name; + CHECK_FAILURE(item->get_Name(&name)); + if (wcscmp(name.get(), L"spellcheck") == 0) + { + CHECK_FAILURE(items->RemoveValueAtIndex(i - 1)); + --defaultItemsCount; + } + } + while (defaultItemsCount > 0) + { + wil::com_ptr item; + CHECK_FAILURE(items->GetValueAtIndex(0, &item)); + COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND kind; + CHECK_FAILURE(item->get_Kind(&kind)); + if (kind != COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_SEPARATOR) + { + break; + } + CHECK_FAILURE(items->RemoveValueAtIndex(0)); + --defaultItemsCount; + } + settings->AddMenuItems(hPopupMenu.get(), items); + + INT32 selectedCommandId = + settings->ShowCustomContextMenu(hPopupMenu.get(), args); + if (selectedCommandId != 0) + { + CHECK_FAILURE(args->put_SelectedCommandId(selectedCommandId)); + } + }); + return S_OK; + }) + .Get())); + return true; +} + +//! [SpellCheckSuggestions] + +INT32 SettingsComponent::ShowCustomContextMenu( + HMENU hPopupMenu, wil::com_ptr args) +{ + HWND hWnd; + CHECK_FAILURE(m_controller->get_ParentWindow(&hWnd)); + SetForegroundWindow(hWnd); + RECT rct; + GetClientRect(hWnd, &rct); + POINT topLeft = {rct.left, rct.top}; + ClientToScreen(hWnd, &topLeft); + POINT location; + CHECK_FAILURE(args->get_Location(&location)); + RECT bounds; + CHECK_FAILURE(m_controller->get_Bounds(&bounds)); + double scale; + CHECK_FAILURE(m_controller3->get_RasterizationScale(&scale)); + return TrackPopupMenu( + hPopupMenu, TPM_TOPALIGN | TPM_LEFTALIGN | TPM_RETURNCMD, + bounds.left + topLeft.x + static_cast(location.x * scale), + bounds.top + topLeft.y + static_cast(location.y * scale), 0, hWnd, nullptr); +} + SettingsComponent::~SettingsComponent() { m_webView->remove_NavigationStarting(m_navigationStartingToken); diff --git a/SampleApps/WebView2APISample/SettingsComponent.h b/SampleApps/WebView2APISample/SettingsComponent.h index c3662d1..bdfbf5f 100644 --- a/SampleApps/WebView2APISample/SettingsComponent.h +++ b/SampleApps/WebView2APISample/SettingsComponent.h @@ -51,6 +51,13 @@ class SettingsComponent : public ComponentBase ~SettingsComponent() override; private: + bool TryShowSpellCheckContextMenu( + wil::com_ptr args, + wil::com_ptr items, + wil::com_ptr target); + INT32 ShowCustomContextMenu( + HMENU hPopupMenu, wil::com_ptr args); + HRESULT OnPermissionRequested( ICoreWebView2* sender, ICoreWebView2PermissionRequestedEventArgs* args); AppWindow* m_appWindow = nullptr; diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index 41d01a2..bad670a 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -540,13 +540,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index e3b576b..6729cbe 100644 --- a/SampleApps/WebView2APISample/packages.config +++ b/SampleApps/WebView2APISample/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index 05e57e2..e5b5832 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index e2b849c..fa943e7 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -1396,6 +1396,11 @@ void WebView_ContextMenuRequested( CoreWebView2ContextMenuRequestedEventArgs args) { IList menuList = args.MenuItems; + if (TryShowSpellCheckContextMenu(args, menuList)) + { + return; + } + CoreWebView2ContextMenuTargetKind context = args.ContextMenuTarget.Kind; // Using custom context menu UI if (context == CoreWebView2ContextMenuTargetKind.SelectedText) @@ -1444,6 +1449,94 @@ void WebView_ContextMenuRequested( } // + bool TryShowSpellCheckContextMenu( + CoreWebView2ContextMenuRequestedEventArgs args, + IList menuList) + { +#if USE_WEBVIEW2_EXPERIMENTAL + if (args.ContextMenuTarget.HasSpellingError) + { + async void ShowMenuAsync() + { + await ShowSpellCheckContextMenuAsync(args, menuList); + } + + ShowMenuAsync(); + return true; + } +#endif + return false; + } + +#if USE_WEBVIEW2_EXPERIMENTAL + // + async Task ShowSpellCheckContextMenuAsync( + CoreWebView2ContextMenuRequestedEventArgs args, + IList menuList) + { + CoreWebView2Deferral deferral = args.GetDeferral(); + args.Handled = true; + + bool menuOpened = false; + try + { + IReadOnlyList suggestions = + await args.ContextMenuTarget.GetSpellCheckSuggestionsAsync(); + + ContextMenu cm = new ContextMenu(); + foreach (CoreWebView2SpellCheckSuggestion suggestion in suggestions) + { + MenuItem suggestionItem = new MenuItem(); + suggestionItem.Header = suggestion.SuggestionText; + int commandId = suggestion.CommandId; + suggestionItem.Click += + (s, ex) => { args.SelectedCommandId = commandId; }; + cm.Items.Add(suggestionItem); + } + + for (int i = menuList.Count - 1; i >= 0; i--) + { + if (menuList[i].Name == "spellcheck") + { + menuList.RemoveAt(i); + } + } + while (menuList.Count > 0 && + menuList[0].Kind == CoreWebView2ContextMenuItemKind.Separator) + { + menuList.RemoveAt(0); + } + if (suggestions.Count > 0 && menuList.Count > 0) + { + cm.Items.Add(new Separator()); + } + + PopulateContextMenu(args, menuList, cm); + cm.Closed += (s, ex) => deferral.Complete(); + cm.IsOpen = true; + menuOpened = true; + } + catch (COMException exception) when ( + exception.HResult == unchecked((int)0x80004004)) // E_ABORT + { + // The context-menu request was superseded or its WebView was closed. + return; + } + catch (COMException exception) + { + MessageBox.Show(exception.Message, "Custom context menus"); + } + finally + { + if (!menuOpened) + { + deferral.Complete(); + } + } + } + // +#endif + void PopulateContextMenu(CoreWebView2ContextMenuRequestedEventArgs args, IList menuList, ItemsControl cm) diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index 220ef09..8abaa80 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - +