Files
hrynco-kswitcher/src/core.h
T

106 lines
3.8 KiB
C++

#pragma once
#include <cstdint>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include <windows.h>
namespace ks
{
struct Layout
{
HKL handle{};
std::wstring id, name, normal, shifted;
bool convertible = true;
int code(wchar_t ch) const;
wchar_t at(int code) const;
bool hasLetter(wchar_t ch) const;
};
bool letter(wchar_t ch);
std::wstring lower(std::wstring text);
std::wstring upper(std::wstring text);
std::wstring convert(const std::wstring &, const Layout &, const Layout &, bool force = false);
int detect(const std::wstring &, const std::vector<Layout> &, int preferred = -1);
std::wstring convertTo(const std::wstring &, const std::vector<Layout> &, int target, int hint);
int nextConversionLayout(const std::wstring &, const std::vector<Layout> &,
const std::vector<int> &cycle, int hint);
std::wstring cycleCase(const std::wstring &);
std::wstring transliterate(const std::wstring &);
struct Snippet
{
std::wstring trigger, text;
std::wstring id;
};
std::wstring snippetId(const Snippet &);
std::wstring newSnippetId();
struct Template
{
std::wstring text, suffix;
int cursors = 0;
int enters = 0;
};
Template parseTemplate(const std::wstring &);
struct Hotkey
{
UINT modifiers{}, key{};
bool operator==(const Hotkey &) const = default;
static std::optional<Hotkey> parse(std::wstring);
std::wstring format() const;
};
struct Settings
{
bool recent = true, switchLayout = true, history = true, autostart = false, awake = false;
bool elevatedAutostart = false;
bool autoCorrect = false;
bool autoReplace = false, autoTypos = false;
std::map<std::wstring, std::wstring> autoReplacements;
bool writeLog = true;
bool screenshotAutoOpenEditor = false;
bool useTesseract = false;
std::wstring tesseractPath;
std::wstring tesseractLanguages = L"ukr+eng+rus";
int autoMaxRank = 0;
int autoMinLength = 3;
std::wstring autoPrefixChars = L"([{«“‘\"";
std::wstring autoSuffixChars = L")]}»”’\".,!?:;";
std::vector<std::wstring> autoLanguages{L"en", L"uk"};
std::vector<std::wstring> autoNever;
std::map<std::wstring, std::vector<std::wstring>> autoWords;
std::vector<std::wstring> autoExcluded{
L"Code", L"rider64", L"devenv", L"WindowsTerminal", L"cmd", L"powershell", L"pwsh",
L"Far", L"ConEmu64", L"putty", L"KeePass", L"KeePassXC", L"1Password", L"Bitwarden"};
int maxItems = 500, maxMiB = 200, maxImageMiB = 20;
std::wstring toggle = L"Ctrl+Alt+Shift+Space", casing = L"Ctrl+Alt+Shift+C",
translit = L"Ctrl+Alt+Shift+T", undo = L"Ctrl+Alt+Shift+Z", clipboard = L"Win+V",
screenshot = L"Win+Shift+S";
std::vector<std::wstring> cycle, direct, directIds;
std::vector<std::wstring> excluded{L"KeePass", L"KeePassXC", L"1Password", L"Bitwarden"};
std::vector<Snippet> snippets;
std::vector<std::wstring> snippetTombstones;
};
// Consume repeats and key-up for intercepted chords, even if settings change.
struct HotkeyHook
{
bool pressed[256]{};
// 0: pass through, -1: consume only, positive: dispatch that action once.
int update(UINT key, bool down, UINT modifiers, const std::vector<std::pair<int, Hotkey>> &bindings);
};
std::string utf8(const std::wstring &);
std::wstring wide(const std::string &);
struct Run
{
HWND window{};
HKL layout{};
ULONGLONG at{};
std::wstring keys;
bool overflow{};
void reset();
void record(wchar_t, HWND, HKL, ULONGLONG);
void backspace(HWND, ULONGLONG);
int match(const std::wstring &, const std::vector<Layout> &, const Layout &physical) const;
int matchSuffix(const std::wstring &, const std::vector<Layout> &, const Layout &physical) const;
};
} // namespace ks