#include "app.h" #include #include "../vendor/json.hpp" #include "../resources/resource.h" #include "ocr.h" #include "lan_clipboard_image.h" #include "lan_files.h" #include "configuration.h" #include "google_drive.h" #include "diagnostics.h" #include "paste_probe.h" #include "utilities.h" #include "screenshot_notifications.h" #include #include #include #include #include namespace ks { #include "app_sync.inl" App *App::current = nullptr; #include "app_history.inl" namespace { constexpr UINT SnippetPopupMessage = WM_APP + 81; constexpr UINT SnippetCaretMessage = WM_APP + 82; constexpr UINT CheckAutostartMessage = WM_APP + 83; constexpr UINT_PTR SnippetPopupTimer = 21; constexpr size_t SnippetVisibleRows = 7; constexpr wchar_t SnippetPopupClass[] = L"KSwitcher.SnippetSuggestions"; HFONT createSnippetFont(UINT dpi) { return CreateFontW(-MulDiv(13, dpi, 96), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0, 0, 0, L"Segoe UI"); } GUITHREADINFO snippetGuiInfo(HWND target) { GUITHREADINFO info{sizeof(info)}; if (const auto thread = GetWindowThreadProcessId(target, nullptr)) GetGUIThreadInfo(thread, &info); return info; } using Microsoft::WRL::ComPtr; constexpr std::wstring_view terminalSnippetTicketPrefix = L"dirlume-terminal-snippet-v1:"; bool snippetElementClass(IUIAutomationElement *element, std::wstring_view name) { BSTR className{}; const bool matches = SUCCEEDED(element->get_CurrentClassName(&className)) && className && className == name; SysFreeString(className); return matches; } ComPtr focusedSnippetElement(IUIAutomation2 *automation, HWND target) { ComPtr focused; if (FAILED(automation->GetFocusedElement(&focused)) || !focused) return {}; BOOL password = TRUE; if (FAILED(focused->get_CurrentIsPassword(&password)) || password || !focusedTextBelongsToWindow(automation, focused.Get(), target)) return {}; return focused; } ComPtr snippetAutomation() { ComPtr automation; if (FAILED(CoCreateInstance(CLSID_CUIAutomation8, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&automation)))) return {}; automation->put_ConnectionTimeout(150); automation->put_TransactionTimeout(150); return automation; } ComPtr terminalSnippetInput(IUIAutomation *automation, IUIAutomationElement *focused) { // The receiver is a sibling of this terminal's focused xterm input. ComPtr walker; ComPtr parent, input; ComPtr condition; if (FAILED(automation->get_RawViewWalker(&walker)) || !walker || FAILED(walker->GetParentElement(focused, &parent)) || !parent) return {}; VARIANT className{}; className.vt = VT_BSTR; className.bstrVal = SysAllocString(L"dirlume-terminal-snippet-input"); const auto result = automation->CreatePropertyCondition(UIA_ClassNamePropertyId, className, &condition); VariantClear(&className); if (FAILED(result) || !condition || FAILED(parent->FindFirst(TreeScope_Children, condition.Get(), &input)) || !input) return {}; ComPtr value; input->GetCurrentPatternAs(UIA_ValuePatternId, IID_PPV_ARGS(&value)); return value; } std::wstring terminalSnippetTicket(IUIAutomationValuePattern *value) { if (!value) return {}; BSTR ticket{}; const auto result = value->get_CurrentValue(&ticket); const std::wstring_view text(ticket ? ticket : L"", SysStringLen(ticket)); auto accepted = SUCCEEDED(result) && text.size() == terminalSnippetTicketPrefix.size() + 36 && text.starts_with(terminalSnippetTicketPrefix) ? std::wstring(text) : std::wstring(); SysFreeString(ticket); return accepted; } std::optional