Skip to content

CircleToSearch with multi search engine support for all android

Kotlin

feat: translates captured screen

AKS Labsd3c80d0e36b1
diff --git a/OCR-translation/src/main/java/dev/davidv/translator/accessibilityOverlay/OverlayUI.kt b/OCR-translation/src/main/java/dev/davidv/translator/accessibilityOverlay/OverlayUI.kt
index 0cf5adf..c548c48 100644
--- a/OCR-translation/src/main/java/dev/davidv/translator/accessibilityOverlay/OverlayUI.kt
+++ b/OCR-translation/src/main/java/dev/davidv/translator/accessibilityOverlay/OverlayUI.kt
@@ -44,6 +44,7 @@ class OverlayUI(
private val handler = Handler(Looper.getMainLooper())
private var launcherBubble: FloatingBubble? = null
private var toolbarView: View? = null
+ private var toolbarParams: WindowManager.LayoutParams? = null
private var sourceLabelView: TextView? = null
private var targetLabelView: TextView? = null
private var readingOrderButtonView: View? = null
@@ -187,12 +188,14 @@ class OverlayUI(
windowManager.addView(toolbar, params)
toolbarView = toolbar
+ toolbarParams = params
}
fun removeToolbar() {
toolbarView?.let {
windowManager.removeView(it)
toolbarView = null
+ toolbarParams = null
sourceLabelView = null
targetLabelView = null
readingOrderButtonView = null
@@ -291,6 +294,7 @@ class OverlayUI(
windowManager.addView(imageView, params)
translationOverlays.add(imageView)
+ raiseToolbar()
ensureTouchWatcher()
}
@@ -336,6 +340,7 @@ class OverlayUI(
dialog.show()
selectDialog = dialog
selectHost = host
+ raiseToolbar()
}
fun removeSelectSurface() {
@@ -469,6 +474,17 @@ class OverlayUI(
fun hasToolbar(): Boolean = toolbarView != null
+ /** Re-inserts the toolbar so it stacks above windows added after it (result bitmap,
+ * select surface) — windows stack in insertion order. */
+ private fun raiseToolbar() {
+ val tv = toolbarView ?: return
+ val tp = toolbarParams ?: return
+ runCatching {
+ windowManager.removeView(tv)
+ windowManager.addView(tv, tp)
+ }
+ }
+
fun removeTranslationOverlays() {
for (view in translationOverlays) {
try {
diff --git a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
index e45b44d..225dc1b 100644
--- a/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/CircleToSearchAccessibilityService.kt
@@ -1182,26 +1182,18 @@ class CircleToSearchAccessibilityService : AccessibilityService() {
/**
* Translates the screenshot captured when Circle to Search launched using the
* translator module's overlay (same path as its floating bubble).
- * Returns false if the overlay bridge is not running.
+ * [onClosed] is invoked when the overlay is dismissed (toolbar close / back press)
+ * so the caller can restore its UI. Returns false if the overlay bridge is not running.
*/
- fun translateCapturedScreen(screenshot: Bitmap): Boolean {
+ fun translateCapturedScreen(screenshot: Bitmap, onClosed: (() -> Unit)? = null): Boolean {
val bridge = translatorBridge ?: return false
- bridge.activateWithScreenshot(screenshot)
+ bridge.activateWithScreenshot(screenshot, onClosed)
return true
}
- /**
- * Translates [screenshot] in place via the translator module's pipeline without
- * drawing overlay windows; [onDone] receives the translated bitmap (null on
- * failure). Invoked on the main thread.
- */
- fun translateCapturedScreenInPlace(screenshot: Bitmap, onDone: (Bitmap?) -> Unit) {
- val bridge = translatorBridge
- if (bridge == null) {
- onDone(null)
- return
- }
- bridge.translateInPlace(screenshot, onDone)
+ /** Dismisses the translator overlay if active, notifying its return callback. */
+ fun closeTranslateOverlay() {
+ translatorBridge?.deactivate()
}
companion object {
diff --git a/app/src/main/java/com/akslabs/circletosearch/TranslatorOverlayBridge.kt b/app/src/main/java/com/akslabs/circletosearch/TranslatorOverlayBridge.kt
index 3ee02ac..1f06b22 100644
--- a/app/src/main/java/com/akslabs/circletosearch/TranslatorOverlayBridge.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/TranslatorOverlayBridge.kt
@@ -47,6 +47,8 @@ class TranslatorOverlayBridge(
private val tag = "TranslatorBridge"
private var active = false
private var selectMode = false
+ private var returnToCaller: (() -> Unit)? = null
+ private var providedScreenshot: Bitmap? = null
private var forcedSourceLanguage: Language? = null
private var forcedTargetLanguage: Language? = null
private var isAutoSource: Boolean = true
@@ -192,8 +194,12 @@ class TranslatorOverlayBridge(
/**
* Same activation as the module's floating bubble ([activate]), but translates the
* screenshot captured when Circle to Search launched instead of capturing a new one.
+ * [returnToCaller] is invoked when the overlay is deactivated (toolbar close, back
+ * press) so the caller can restore its UI.
*/
- fun activateWithScreenshot(screenshot: Bitmap) {
+ fun activateWithScreenshot(screenshot: Bitmap, returnToCaller: (() -> Unit)? = null) {
+ this.returnToCaller = returnToCaller
+ providedScreenshot = screenshot
if (active) {
if (selectMode) exitSelectMode()
} else {
@@ -222,6 +228,11 @@ class TranslatorOverlayBridge(
ui.removeTranslationOverlays()
ui.dismissMenu()
ui.restoreFloatingButton()
+ providedScreenshot = null
+ returnToCaller?.let { cb ->
+ returnToCaller = null
+ cb()
+ }
}
override fun toggleSelectMode() {
@@ -356,6 +367,11 @@ class TranslatorOverlayBridge(
)
}
+ /** Full content area — for a provided capture the result also fills the zone behind
+ * the translucent toolbar, so translated UI shows through it. */
+ private fun fullCaptureRegion(): Rect =
+ dev.davidv.translator.overlayChrome.OverlayInsets.contentBounds(windowManager)
+
/** OCR + translate an already-captured screenshot, skipping the accessibility capture. */
private fun translateProvidedScreen(screenshot: Bitmap) {
ui.removeTranslationOverlays()
@@ -366,7 +382,7 @@ class TranslatorOverlayBridge(
return
}
- val region = currentCaptureRegion()
+ val region = fullCaptureRegion()
ui.dismissMenu()
val cropped = cropToRegion(screenshot, region) ?: return
@@ -387,6 +403,17 @@ class TranslatorOverlayBridge(
}
private fun captureFullScreen(region: Rect) {
+ // Entered from Circle to Search: toolbar refresh / language change re-translates the
+ // captured screenshot instead of re-capturing the live screen.
+ val provided = providedScreenshot
+ if (provided != null) {
+ val full = fullCaptureRegion()
+ val cropped = cropToRegion(provided, full) ?: return
+ surfaceState.regions.value = null
+ ui.showScanOverlay(surfaceState, full)
+ serviceScope.launch { translateRegionBitmap(cropped, full) }
+ return
+ }
host.takeScreenshot(
Display.DEFAULT_DISPLAY,
host.mainExecutor,
@@ -469,40 +496,6 @@ class TranslatorOverlayBridge(
}
}
- /**
- * Runs the module's OCR + translate pipeline on [screenshot] without drawing any
- * overlay windows, reporting the corrected bitmap (original with translated text
- * painted in) for in-place display. [onDone] is invoked on the main thread with
- * null on failure.
- */
- fun translateInPlace(screenshot: Bitmap, onDone: (Bitmap?) -> Unit) {
- serviceScope.launch {
- val coordinator = translationCoordinator
- val targetLang =
- forcedTargetLanguage
- ?: langStateManager.languageByCode(settingsManager.settings.value.defaultTargetLanguageCode)
- if (coordinator == null || targetLang == null) {
- onDone(null)
- return@launch
- }
- val sourceLang = ocrSourceLanguage()
- val result =
- withContext(Dispatchers.IO) {
- coordinator.translateImageWithOverlay(
- sourceLang ?: targetLang,
- targetLang,
- screenshot,
- onMessage = {},
- readingOrder = currentReadingOrderFor(sourceLang),
- isAutoSource = isAutoSource,
- onOcrUnavailable = {},
- onDetectedRegions = { _, _, _ -> },
- )
- }
- onDone(result?.correctedBitmap)
- }
- }
-
private fun ocrSourceLanguage(): Language? =
forcedSourceLanguage ?: settingsManager.settings.value.defaultSourceLanguageCode?.let {
langStateManager.languageByCode(it)
diff --git a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
index 8bf4c10..eab48f6 100644
--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt
@@ -47,6 +47,8 @@ import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
+import androidx.compose.foundation.gestures.awaitEachGesture
+import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.interaction.MutableInteractionSource
@@ -189,6 +191,9 @@ import androidx.compose.material3.Surface
/** Lets the Circle to Search UI finish closing before the translator overlay draws. */
private const val TRANSLATE_TRIGGER_DELAY_MS = 400L
+/** Lets the Circle to Search chrome hide before the translator overlay draws. */
+private const val TRANSLATE_HIDE_DELAY_MS = 150L
+
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CircleToSearchScreen(
@@ -415,9 +420,9 @@ fun CircleToSearchScreen(
var selectionRect by remember { mutableStateOf<Rect?>(null) }
val selectionAnim = remember { androidx.compose.animation.core.Animatable(0f) }
- // In-place translation of the captured screenshot (Translate button, short press)
- var translatedScreenshot by remember { mutableStateOf<Bitmap?>(null) }
- var isTranslating by remember { mutableStateOf(false) }
+ // Translator overlay mode: Circle to Search chrome hidden, the frozen screenshot
+ // stays visible as the backdrop under the module's scan/result overlays.
+ var isTranslatorMode by remember { mutableStateOf(false) }
// Lifecycle reset: When screenshot changes, reset selection and modes
LaunchedEffect(screenshot) {
@@ -428,8 +433,9 @@ fun CircleToSearchScreen(
isSearching = false
currentPathPoints.clear()
selectionAnim.snapTo(0f)
- translatedScreenshot = null
- isTranslating = false
+ if (isTranslatorMode) {
+ CircleToSearchAccessibilityService.instance?.closeTranslateOverlay()
+ }
}
}
@@ -550,6 +556,10 @@ fun CircleToSearchScreen(
// Back Handler Logic
BackHandler(enabled = true) {
+ if (isTranslatorMode) {
+ CircleToSearchAccessibilityService.instance?.closeTranslateOverlay()
+ return@BackHandler
+ }
if (isCopyMode) {
isCopyMode = false
onExitCopyMode()
@@ -881,7 +891,7 @@ fun CircleToSearchScreen(
// Close button for Copy Mode (Top Left)
// Friendly Message Overlay (Top Center)
- if (!isCopyMode) {
+ if (!isCopyMode && !isTranslatorMode) {
Box(
modifier = Modifier
.fillMaxSize()
@@ -913,19 +923,12 @@ fun CircleToSearchScreen(
modifier = Modifier.fillMaxSize()
)
- // In-place translated view of the same capture (toggle via Translate button)
- translatedScreenshot?.let { translated ->
- Image(
- bitmap = translated.asImageBitmap(),
- contentDescription = null,
- contentScale = ContentScale.FillBounds,
- modifier = Modifier.fillMaxSize()
- )
- }
-
- // Tint Overlay (Punch-out style)
- Canvas(modifier = Modifier.fillMaxSize()) {
+ // Tint Overlay (Punch-out style) — dimmed during normal CTS use,
+ // hidden in translator mode so the frozen backdrop stays undimmed
+ Canvas(modifier = Modifier.fillMaxSize().graphicsLayer {
+ alpha = if (isTranslatorMode) 0f else 1f
+ }) {
val strokeWidth = 0f
val dimAlpha = 0.15f
@@ -958,16 +961,11 @@ fun CircleToSearchScreen(
)
}
}
- if (isTranslating) {
- Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
- androidx.compose.material3.CircularProgressIndicator(color = Color.White)
- }
- }
}
}
// 2. Gradient Border Layer (Overlaying screenshot, clipped to rounded corners)
- if (showGradientBorder && !isCopyMode) {
+ if (showGradientBorder && !isCopyMode && !isTranslatorMode) {
androidx.compose.animation.AnimatedVisibility(
visible = isUIVisible,
enter = androidx.compose.animation.fadeIn(animationSpec = tween(700))
@@ -988,7 +986,7 @@ fun CircleToSearchScreen(
}
// 3. Drawing Canvas (Interactive Layer)
- if (!isCopyMode) { Canvas(
+ if (!isCopyMode && !isTranslatorMode) { Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(Unit) {
@@ -1193,7 +1191,7 @@ fun CircleToSearchScreen(
// 4. Header (Top)
androidx.compose.animation.AnimatedVisibility(
- visible = isUIVisible && !isCopyMode && !isEntityExtractMode,
+ visible = isUIVisible && !isCopyMode && !isEntityExtractMode && !isTranslatorMode,
enter = androidx.compose.animation.slideInVertically(
initialOffsetY = { -it }, // Commence au-dessus de l'écran (-100%)
animationSpec = tween(500, easing = androidx.compose.animation.core.FastOutSlowInEasing)
@@ -1381,7 +1379,7 @@ fun CircleToSearchScreen(
var isCopyTextTriggered by remember { mutableStateOf(false) }
androidx.compose.animation.AnimatedVisibility(
- visible = isUIVisible && !isCopyMode && !isEntityExtractMode,
+ visible = isUIVisible && !isCopyMode && !isEntityExtractMode && !isTranslatorMode,
enter = slideInVertically(
initialOffsetY = { it }, // slides up from below
animationSpec = tween(300, easing = androidx.compose.animation.core.CubicBezierEasing(0f, 0f, 0.2f, 1f))
@@ -1500,8 +1498,9 @@ fun CircleToSearchScreen(
}
// Circular Button: Translate
- // Short press: translate in place over the captured screenshot (stays open, tap again for original)
- // Long press: close and draw the translator's overlay on the screen (bubble behavior)
+ // Short press: freeze CTS (chrome hides, screenshot stays) and run the module's
+ // translator overlay on the capture; close/back returns to Circle to Search.
+ // Long press: close CTS and run the same overlay on the real screen (bubble behavior).
Box(
modifier = Modifier
.size(60.dp)
@@ -1512,22 +1511,22 @@ fun CircleToSearchScreen(
haptic.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)
val bmp = screenshot
val translatorService = CircleToSearchAccessibilityService.instance
- when {
- bmp == null || translatorService == null ->
- android.widget.Toast.makeText(context, context.getString(R.string.toast_translate_unavailable), android.widget.Toast.LENGTH_SHORT).show()
- isTranslating -> Unit
- translatedScreenshot != null -> translatedScreenshot = null
- else -> {
- isTranslating = true
- translatorService.translateCapturedScreenInPlace(bmp) { translated ->
- isTranslating = false
- if (translated != null) {
- translatedScreenshot = translated
- } else {
- android.widget.Toast.makeText(context, context.getString(R.string.toast_translate_unavailable), android.widget.Toast.LENGTH_SHORT).show()
+ if (bmp == null || translatorService == null) {
+ android.widget.Toast.makeText(context, context.getString(R.string.toast_translate_unavailable), android.widget.Toast.LENGTH_SHORT).show()
+ } else {
+ selectionRect = null
+ selectedBitmap = null
+ isSearching = false
+ currentPathPoints.clear()
+ scope.launch { selectionAnim.snapTo(0f) }
+ isTranslatorMode = true
+ android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
+ if (isTranslatorMode) {
+ translatorService.translateCapturedScreen(bmp) {
+ isTranslatorMode = false
}
}
- }
+ }, TRANSLATE_HIDE_DELAY_MS)
}
},
onLongClick = {
@@ -1734,7 +1733,7 @@ fun CircleToSearchScreen(
}
// 4. Selection Actions (Share) — Positioned at the very end for absolute top-layer rendering
- if (selectionRect != null && selectionAnim.value == 1f && !isCopyMode) {
+ if (selectionRect != null && selectionAnim.value == 1f && !isCopyMode && !isTranslatorMode) {
val rect = selectionRect!!
val density = androidx.compose.ui.platform.LocalDensity.current
val leftPx = rect.left.toFloat()
@@ -1895,7 +1894,7 @@ fun CircleToSearchScreen(
}
// --- NEW: QR Overlay Chips (High Layer) ---
- if (screenshot != null && !isCopyMode) {
+ if (screenshot != null && !isCopyMode && !isTranslatorMode) {
BoxWithConstraints(modifier = Modifier.fillMaxSize().zIndex(2500f)) {
val screenWidth = maxWidth
val screenHeight = maxHeight
@@ -2060,6 +2059,25 @@ fun CircleToSearchScreen(
}
}
+ // Input shield while the translator overlay owns the screen: consumes touches so
+ // the hidden chrome underneath can't be tapped (module windows sit above this).
+ if (isTranslatorMode) {
+ Box(
+ modifier = Modifier
+ .fillMaxSize()
+ .zIndex(4000f)
+ .pointerInput(Unit) {
+ awaitEachGesture {
+ awaitFirstDown(requireUnconsumed = false)
+ while (true) {
+ val event = awaitPointerEvent()
+ event.changes.forEach { it.consume() }
+ }
+ }
+ }
+ )
+ }
+
// QR Code Result Sheet
if (showQrSheet) {
// Full-screen invisible overlay to catch "click outside"