Find in page: WebView2 counts its active match from 1; the shell's index is 0-based

Seen live on the shared app: "domain" on example.com showed "2/2" for the first of two matches, and "2 matches" after Next. `ICoreWebView2Find::ActiveMatchIndex` starts at 1 (-1: none); `FindState::from_webview2` converts it (tested), so the bar reads 1/2, then 2/2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
CRTkafa 2026-10-01 03:47:23 +07:00
parent 3450eac09a
commit 5211995694
2 changed files with 18 additions and 1 deletions

View file

@ -48,6 +48,16 @@ pub struct FindState {
pub index: i32,
}
impl FindState {
/// From WebView2's counts: its `ActiveMatchIndex` starts at 1 (and is -1 without an active match);
/// the shell's is 0-based. A negative count is none.
pub fn from_webview2(id: &str, count: i32, active_one_based: i32) -> Self {
let count = count.max(0);
let index = if (1..=count).contains(&active_one_based) { active_one_based - 1 } else { -1 };
Self { id: id.to_owned(), count, index }
}
}
/// A request as native code runs it: `start` carries the query, the others none.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FindRequest {
@ -139,5 +149,12 @@ mod tests {
fn counts_are_numbers_about_the_tab() {
let state = FindState { id: "t1".into(), count: 17, index: 2 };
assert_eq!(serde_json::to_string(&state).unwrap(), r#"{"id":"t1","count":17,"index":2}"#);
// WebView2 counts its active match from 1 (seen live: "2/2" for the first of two).
assert_eq!(FindState::from_webview2("t1", 2, 1), FindState { id: "t1".into(), count: 2, index: 0 });
assert_eq!(FindState::from_webview2("t1", 2, 2).index, 1);
assert_eq!(FindState::from_webview2("t1", 2, -1).index, -1, "no active match");
assert_eq!(FindState::from_webview2("t1", 2, 3).index, -1, "out of range");
assert_eq!(FindState::from_webview2("t1", 0, 0), FindState { id: "t1".into(), count: 0, index: -1 });
assert_eq!(FindState::from_webview2("t1", -5, 1).count, 0);
}
}