diff --git a/src/ui.cpp b/src/ui.cpp index 4ae3234..663092a 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1118,8 +1118,12 @@ LRESULT CALLBACK historyProc(HWND w, UINT msg, WPARAM wp, LPARAM lp) auto old = selected(w, v); if (old.empty()) old = v->rememberedSelection; int top=(int)SendDlgItemMessageW(w,102,LB_GETTOPINDEX,0,0); - auto topId=top>=0 && top<(int)v->items.size()?v->items[top].id:L""; - if (topId.empty()) topId = v->rememberedTop; + // Follow new entries at the top. Anchor the viewport only while browsing older rows. + std::wstring topId; + if (top > 0 && top < (int)v->items.size()) + topId = v->items[top].id; + else if (v->items.empty()) + topId = v->rememberedTop; if (v->selectFirstOnResults) { old.clear(); diff --git a/tests/input.cpp b/tests/input.cpp index c752e8e..29a4315 100644 --- a/tests/input.cpp +++ b/tests/input.cpp @@ -336,11 +336,26 @@ static void historyLayoutChecks(ks::App &app, const std::filesystem::path &temp, require(SendDlgItemMessageW(history,102,LB_GETCURSEL,0,0)==0 && SendDlgItemMessageW(history,102,LB_GETTOPINDEX,0,0)==0, "fresh history invocation selects and reveals newest screenshot instead of saved selection"); + SendDlgItemMessageW(history,102,LB_SETCURSEL,2,0); + app.history->add(ks::textClip(L"background clipboard update"),L"test",app.settings); + ks::refreshHistory(app); + require(pumpUntil(4),"background refresh loads a new item"); + require(SendDlgItemMessageW(history,102,LB_GETCURSEL,0,0)==3, + "background refresh preserves the selected record while browsing"); + SendDlgItemMessageW(history,102,LB_SETTOPINDEX,0,0); + screenshot.bgra[0] ^= 1; + app.history->add(ks::Clip{{ks::imageDib(screenshot)}},L"new screenshot",app.settings); + ks::refreshHistory(app); + require(pumpUntil(5),"open history loads the new screenshot"); + require(SendDlgItemMessageW(history,102,LB_GETTOPINDEX,0,0)==0, + "history at the top immediately shows the new screenshot"); + require(SendDlgItemMessageW(history,102,LB_GETCURSEL,0,0)==4, + "showing a new screenshot preserves the selection"); if (capturePreview) { RedrawWindow(history, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN); ks::InputRegressionAccess::finishHistoryPreviews(app); - pumpUntil(3); + pumpUntil(5); RedrawWindow(history, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN); RECT historyClient{}; GetClientRect(history, &historyClient); @@ -350,12 +365,13 @@ static void historyLayoutChecks(ks::App &app, const std::filesystem::path &temp, ks::atomicWrite(std::filesystem::path(ks::executable()).parent_path() / L"clipboard-history.png", ks::encodePng(ks::decodeImage(preview))); } - SendDlgItemMessageW(history,102,LB_SETCURSEL,2,0); - app.history->add(ks::textClip(L"background clipboard update"),L"test",app.settings); + SendDlgItemMessageW(history,102,LB_SETTOPINDEX,1,0); + require(SendDlgItemMessageW(history,102,LB_GETTOPINDEX,0,0)==1, + "history can scroll past the newest item"); + app.history->add(ks::textClip(L"another clipboard update"),L"test",app.settings); ks::refreshHistory(app); - require(pumpUntil(4),"background refresh loads a new item"); - require(SendDlgItemMessageW(history,102,LB_GETCURSEL,0,0)==3, - "background refresh preserves the selected record while browsing"); + require(pumpUntil(6) && SendDlgItemMessageW(history,102,LB_GETTOPINDEX,0,0)==2, + "history preserves the top record when scrolled down"); ShowWindow(history,SW_HIDE); } auto childRect=[&](int id) { RECT r{}; GetWindowRect(GetDlgItem(history,id),&r); MapWindowPoints(nullptr,history,(POINT*)&r,2); return r; };