improve nativ find in page handling

This commit is contained in:
Fabian Freund
2025-05-05 14:33:33 +02:00
parent 98191f38d3
commit 8e0325f6b5
3 changed files with 35 additions and 21 deletions
@@ -2,6 +2,7 @@ package eu.lensai.flutter_mozilla_components.api
import eu.lensai.flutter_mozilla_components.GlobalComponents
import eu.lensai.flutter_mozilla_components.pigeons.GeckoFindApi
import mozilla.components.browser.state.action.ContentAction
import mozilla.components.browser.state.selector.findTabOrCustomTab
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.concept.engine.EngineSession
@@ -34,7 +35,15 @@ class GeckoFindApiImpl : GeckoFindApi {
}
override fun clearMatches(tabId: String?) {
val engineSession = sessionByTabId(tabId)
val loadSessionId = tabId
?: components.core.store.state.selectedTabId
if (loadSessionId == null) {
return
}
val engineSession = sessionByTabId(loadSessionId)
engineSession?.clearFindMatches()
components.core.store.dispatch(ContentAction.ClearFindResultsAction(loadSessionId))
}
}
@@ -169,25 +169,5 @@ class Events(
) { _ -> }
}
}
stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs }
.filterChanged {
it.content.findResults
}
.distinctUntilChanged()
.collect { tab ->
tab.content.findResults
flutterEvents.onFindResults(
System.currentTimeMillis(),
tab.id,
tab.content.findResults.map { result -> FindResultState(
activeMatchOrdinal = result.activeMatchOrdinal.toLong(),
numberOfMatches = result.numberOfMatches.toLong(),
isDoneCounting = result.isDoneCounting,
) }
) { _ -> }
}
}
}
}
@@ -10,6 +10,7 @@ import eu.lensai.flutter_mozilla_components.ext.resize
import eu.lensai.flutter_mozilla_components.ext.toWebPBytes
import eu.lensai.flutter_mozilla_components.pigeons.AudioHitResult
import eu.lensai.flutter_mozilla_components.pigeons.EmailHitResult
import eu.lensai.flutter_mozilla_components.pigeons.FindResultState
import eu.lensai.flutter_mozilla_components.pigeons.GeckoStateEvents
import eu.lensai.flutter_mozilla_components.pigeons.GeoHitResult
import eu.lensai.flutter_mozilla_components.pigeons.ImageHitResult
@@ -108,6 +109,30 @@ class FlutterEventMiddleware(private val flutterEvents: GeckoStateEvents) : Midd
) { _ -> }
}
}
is ContentAction.AddFindResultAction -> {
runOnUiThread {
flutterEvents.onFindResults(
System.currentTimeMillis(),
action.sessionId,
listOf(
FindResultState(
activeMatchOrdinal = action.findResult.activeMatchOrdinal.toLong(),
numberOfMatches = action.findResult.numberOfMatches.toLong(),
isDoneCounting = action.findResult.isDoneCounting,
)
)
) { _ -> }
}
}
is ContentAction.ClearFindResultsAction -> {
runOnUiThread {
flutterEvents.onFindResults(
System.currentTimeMillis(),
action.sessionId,
listOf()
) { _ -> }
}
}
else -> {
//logger.debug("Event fired: " + action.javaClass.name)
}