From 8e0325f6b5a01b02276d68204a4ad2a3b50ddb93 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Mon, 5 May 2025 14:33:33 +0200 Subject: [PATCH] improve nativ find in page handling --- .../api/GeckoFindApiImpl.kt | 11 +++++++- .../components/Events.kt | 20 --------------- .../middleware/FlutterEventMiddleware.kt | 25 +++++++++++++++++++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/api/GeckoFindApiImpl.kt b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/api/GeckoFindApiImpl.kt index 36afe55f..158a5bf8 100644 --- a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/api/GeckoFindApiImpl.kt +++ b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/api/GeckoFindApiImpl.kt @@ -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)) } } \ No newline at end of file diff --git a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/components/Events.kt b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/components/Events.kt index ab2b909b..e2e9888a 100644 --- a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/components/Events.kt +++ b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/components/Events.kt @@ -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, - ) } - ) { _ -> } - } - } } } \ No newline at end of file diff --git a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/middleware/FlutterEventMiddleware.kt b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/middleware/FlutterEventMiddleware.kt index 4a82da8a..62604058 100644 --- a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/middleware/FlutterEventMiddleware.kt +++ b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/lensai/flutter_mozilla_components/middleware/FlutterEventMiddleware.kt @@ -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) }