Show KSwitcher snippet suggestions in WebView2 text fields

Refs #209
This commit is contained in:
agrynco committed 2026-11-27 16:06:50 +02:00
1 parent f7620d164f
commit 187334aed2
5 files changed
+158 -26

No files matched your search

+46 -16
View File
@@ -45,7 +45,7 @@ GUITHREADINFO snippetGuiInfo(HWND target)
return info;
}
std::optional<RECT> accessibleSnippetCaret(HWND target)
std::optional<SnippetAnchor> accessibleSnippetCaret(HWND target)
{
using Microsoft::WRL::ComPtr;
ComPtr<IUIAutomation2> automation;
@@ -57,14 +57,28 @@ std::optional<RECT> accessibleSnippetCaret(HWND target)
ComPtr<IUIAutomationElement> focused;
if (FAILED(automation->GetFocusedElement(&focused)) || !focused)
return {};
DWORD process{};
GetWindowThreadProcessId(target, &process);
int focusedProcess{};
BOOL password = TRUE;
if (!process || FAILED(focused->get_CurrentProcessId(&focusedProcess)) ||
process != static_cast<DWORD>(focusedProcess) ||
FAILED(focused->get_CurrentIsPassword(&password)) || password)
if (FAILED(focused->get_CurrentIsPassword(&password)) || password ||
!focusedTextBelongsToWindow(automation.Get(), focused.Get(), target))
return {};
// The xterm.js input follows the terminal cursor for IME. It doesn't
// expose the shell's selection, so the popup can only display triggers.
BSTR className{};
const bool terminalInput = SUCCEEDED(focused->get_CurrentClassName(&className)) && className &&
std::wstring_view(className) == L"xterm-helper-textarea";
SysFreeString(className);
if (terminalInput)
{
RECT cell{};
BOOL offscreen = TRUE;
if (SUCCEEDED(focused->get_CurrentIsOffscreen(&offscreen)) && !offscreen &&
SUCCEEDED(focused->get_CurrentBoundingRectangle(&cell)) && cell.right > cell.left &&
cell.bottom > cell.top)
return SnippetAnchor{{cell.left, cell.top, cell.left, cell.bottom}, false};
return {};
}
ComPtr<IUIAutomationTextPattern2> pattern;
ComPtr<IUIAutomationTextRange> range;
BOOL active{};
@@ -121,7 +135,7 @@ std::optional<RECT> accessibleSnippetCaret(HWND target)
}
}
SafeArrayDestroy(rectangles);
if (anchor && anchor->bottom > anchor->top) return anchor;
if (anchor && anchor->bottom > anchor->top) return SnippetAnchor{*anchor};
}
return {};
}
@@ -1485,6 +1499,12 @@ void App::matchSnippets(HWND fg, bool expandExact)
!snippetWordCharacter(layout->at(physicalLayout().code(previous)));
}))
{
if (!snippetPopupAnchor.canInsert && snippetTarget == fg && snippetPopup &&
IsWindowVisible(snippetPopup) && snippetFocus == snippetGuiInfo(fg).hwndFocus)
{
hideSnippetPopup();
return;
}
queueSnippet(fg, i, sn.trigger.size());
return;
}
@@ -1559,11 +1579,11 @@ void App::showSnippetPopup()
if ((info.rcCaret.left || info.rcCaret.top || anchor.bottom - anchor.top >= 4) &&
snippetCaretInside(anchor, bounds))
{
placeSnippetPopup(anchor);
placeSnippetPopup({anchor});
return;
}
}
if (snippetPopup && IsWindowVisible(snippetPopup) && snippetCaretInside(snippetPopupAnchor, bounds))
if (snippetPopup && IsWindowVisible(snippetPopup) && snippetCaretInside(snippetPopupAnchor.bounds, bounds))
placeSnippetPopup(snippetPopupAnchor);
if (snippetCaretPending) return;
snippetCaretPending = true;
@@ -1572,6 +1592,14 @@ void App::showSnippetPopup()
const auto receiver = window;
enqueue([this, version, target, receiver] {
auto caret = GetForegroundWindow() == target ? accessibleSnippetCaret(target) : std::nullopt;
if (!caret && inputVersion == version && GetForegroundWindow() == target)
{
// A WebView can expose the old, empty field while the keystroke
// that requested suggestions is still reaching its renderer.
Sleep(45);
if (inputVersion == version && GetForegroundWindow() == target)
caret = accessibleSnippetCaret(target);
}
{
std::lock_guard lock(stateMutex);
snippetCaret = caret;
@@ -1579,9 +1607,10 @@ void App::showSnippetPopup()
PostMessageW(receiver, SnippetCaretMessage, static_cast<WPARAM>(version), 0);
});
}
void App::placeSnippetPopup(RECT anchor)
void App::placeSnippetPopup(SnippetAnchor placement)
{
snippetPopupAnchor = anchor;
snippetPopupAnchor = placement;
const auto &anchor = placement.bounds;
if (!snippetPopup)
{
WNDCLASSW cls{};
@@ -1632,7 +1661,7 @@ void App::placeSnippetPopup(RECT anchor)
}
void App::selectSnippet(size_t row)
{
if (row >= snippetMatches.size() || !snippetPopupCurrent()) return;
if (!snippetPopupAnchor.canInsert || row >= snippetMatches.size() || !snippetPopupCurrent()) return;
const auto index = snippetMatches[row];
const auto length = snippetPrefixLength;
hideSnippetPopup();
@@ -1642,6 +1671,7 @@ void App::selectSnippet(size_t row)
}
bool App::handleSnippetKey(DWORD vk, UINT mods)
{
if (!snippetPopupAnchor.canInsert && vk != VK_ESCAPE) return false;
const bool accept = vk == VK_TAB ||
(vk == VK_RETURN && (snippetNavigated || snippetMatches.size() == 1));
if (mods || (!accept && vk != VK_UP && vk != VK_DOWN && vk != VK_ESCAPE) ||
@@ -1690,7 +1720,7 @@ LRESULT CALLBACK App::snippetPopupProc(HWND w, UINT msg, WPARAM wp, LPARAM lp)
{
const LONG top = static_cast<LONG>(i - first) * app->snippetRowHeight;
RECT row{0, top, bounds.right, top + app->snippetRowHeight};
const bool selected = i == app->snippetSelection;
const bool selected = app->snippetPopupAnchor.canInsert && i == app->snippetSelection;
if (selected) FillRect(dc, &row, GetSysColorBrush(COLOR_HIGHLIGHT));
SetTextColor(dc, GetSysColor(selected ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT));
row.left += MulDiv(8, dpi, 96);
@@ -3603,13 +3633,13 @@ LRESULT CALLBACK App::windowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
app->showSnippetPopup();
return 0;
}
std::optional<RECT> caret;
std::optional<SnippetAnchor> caret;
{
std::lock_guard lock(app->stateMutex);
caret = app->snippetCaret;
}
RECT bounds{};
if (caret && GetWindowRect(app->snippetTarget, &bounds) && snippetCaretInside(*caret, bounds))
if (caret && GetWindowRect(app->snippetTarget, &bounds) && snippetCaretInside(caret->bounds, bounds))
{
app->placeSnippetPopup(*caret);
return 0;