Fix 05: Remove g_lastLoadedText poisoning bug; add live session archiving via FinalizeSession

This commit is contained in:
Win Dictation Dev
2026-06-11 19:08:31 +12:00
parent a87a12ae7e
commit e04b003bf8
2 changed files with 347 additions and 14 deletions
+30 -14
View File
@@ -144,6 +144,7 @@ DWORD g_insStart = 0, g_insEnd = 0;
REAL g_statsTopY = 0;
std::vector<HistoryEntry> g_history;
std::wstring g_lastLoadedText;
std::wstring g_sessionPath;
#define ID_SEL_HISTORY 1019
enum class View { Main, Settings };
View g_view = View::Main;
@@ -1130,6 +1131,24 @@ static RectF SelectsRowFullWidthAnchor() {
return RectF(a.X, b.Y, (b.X + b.Width) - a.X, b.Height);
}
static bool HasInk(const std::wstring& s) {
for (wchar_t c : s) if (!iswspace(c)) return true;
return false;
}
static bool FinalizeSession(HWND hWnd) {
std::wstring cur = GetEditText(hWnd);
bool saved = false;
if (!g_sessionPath.empty()) {
if (HasInk(cur)) WriteFileUtf8(g_sessionPath, cur);
saved = true;
} else if (HasInk(cur) && cur != g_lastLoadedText) {
saved = !ArchiveSession(cur).empty();
}
g_sessionPath.clear();
return saved;
}
void OnClick(HWND hWnd, WK kind) {
if (!g_modelLoaded.load() && kind != WK::RecordHero && kind != WK::Clear
&& kind != WK::Copy && kind != WK::Paste && kind != WK::Pin
@@ -1161,15 +1180,13 @@ void OnClick(HWND hWnd, WK kind) {
}
break;
case WK::Clear: {
std::wstring cur = GetEditText(hWnd);
if (!cur.empty() && cur != g_lastLoadedText)
ArchiveSession(cur);
bool saved = FinalizeSession(hWnd);
g_history = LoadHistoryIndex();
g_lastLoadedText.clear();
g_editDirty = false;
SetDlgItemText(hWnd, ID_EDIT_TEXT, L"");
UpdatePlaceholder(hWnd);
SetStatus(hWnd, L"Saved to history");
SetStatus(hWnd, saved ? L"Saved to history" : L"Cleared");
break;
}
case WK::Pin:
@@ -1667,7 +1684,11 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
SendMessageW(hEdit, EM_REPLACESEL, TRUE, (LPARAM)ins.c_str());
SendMessageW(hEdit, EM_SCROLLCARET, 0, 0);
UpdatePlaceholder(hWnd);
g_lastLoadedText = GetEditText(hWnd);
{
std::wstring all = GetEditText(hWnd);
if (g_sessionPath.empty()) g_sessionPath = ArchiveSession(all);
else WriteFileUtf8(g_sessionPath, all);
}
SetClipboardTextUtf8(hWnd, *res);
@@ -1736,9 +1757,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
InvalidateRect(GetDlgItem(hWnd, ID_SEL_MODEL), nullptr, FALSE);
PersistNow();
} else if (ctrlId == ID_SEL_HISTORY && idx >= 0 && idx < (int)g_history.size()) {
std::wstring cur = GetEditText(hWnd);
if (!cur.empty() && cur != g_lastLoadedText)
ArchiveSession(cur);
FinalizeSession(hWnd);
std::wstring text = ReadFileUtf8(g_history[idx].path);
SetWindowTextW(GetDlgItem(hWnd, ID_EDIT_TEXT), text.c_str());
g_lastLoadedText = text;
@@ -1782,15 +1801,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
PostMessage(hWnd, WM_HOTKEY, HK_TOGGLE, 0);
break;
case ID_BTN_CLEAR: {
std::wstring cur = GetEditText(hWnd);
if (!cur.empty() && cur != g_lastLoadedText)
ArchiveSession(cur);
bool saved = FinalizeSession(hWnd);
g_history = LoadHistoryIndex();
g_lastLoadedText.clear();
g_editDirty = false;
SetDlgItemText(hWnd, ID_EDIT_TEXT, L"");
UpdatePlaceholder(hWnd);
SetStatus(hWnd, L"Saved to history");
SetStatus(hWnd, saved ? L"Saved to history" : L"Cleared");
break;
}
case ID_BTN_PIN:
@@ -1844,8 +1861,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
return 0;
case WM_DESTROY: {
std::wstring cur = GetEditText(hWnd);
if (!cur.empty() && cur != g_lastLoadedText) ArchiveSession(cur);
FinalizeSession(hWnd);
PersistNow();
PostQuitMessage(0);
break;