CircleToSearch with multi search engine support for all android
Kotlin
circle to search ui improvements
AKS Labsb92cdbab2839
diff --git a/app/src/main/java/com/akslabs/circletosearch/data/SearchEngine.kt b/app/src/main/java/com/akslabs/circletosearch/data/SearchEngine.ktindex b7ea53d..efc67f7 100644--- a/app/src/main/java/com/akslabs/circletosearch/data/SearchEngine.kt+++ b/app/src/main/java/com/akslabs/circletosearch/data/SearchEngine.kt@@ -1,10 +1,11 @@package com.akslabs.circletosearch.datasealed class SearchEngine(val name: String, val queryUrl: String) {- object Google : SearchEngine("Google", "https://www.google.com/searchbyimage?image_url=")- object Bing : SearchEngine("Bing", "https://www.bing.com/images/search?view=detailv2&iss=sbi&form=SBIHMP&q=imgurl:")- object DuckDuckGo : SearchEngine("DuckDuckGo", "https://duckduckgo.com/?q=") // DDG doesn't have a direct public image search URL API easily accessible like this without upload, but we'll use a placeholder- object Yandex : SearchEngine("Yandex", "https://yandex.com/images/search?rpt=imageview&url=")+ // Using generic search queries for simulation since we can't upload the image directly in this demo+ object Google : SearchEngine("Google", "https://www.google.com/search?q=Circle+to+Search+Demo&tbm=isch")+ object Bing : SearchEngine("Bing", "https://www.bing.com/images/search?q=Circle+to+Search+Demo")+ object DuckDuckGo : SearchEngine("DuckDuckGo", "https://duckduckgo.com/?q=Circle+to+Search+Demo&iax=images&ia=images")+ object Yandex : SearchEngine("Yandex", "https://yandex.com/images/search?text=Circle+to+Search+Demo")companion object {fun getAll() = listOf(Google, Bing, DuckDuckGo, Yandex)diff --git a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.ktindex 711594a..140cd5e 100644--- a/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt+++ b/app/src/main/java/com/akslabs/circletosearch/ui/CircleToSearchScreen.kt@@ -51,11 +51,12 @@ import androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.clipimport androidx.compose.ui.draw.shadow+import androidx.compose.ui.geometry.CornerRadiusimport androidx.compose.ui.geometry.Offset+import androidx.compose.ui.geometry.Sizeimport androidx.compose.ui.graphics.Brushimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.graphics.Path-import androidx.compose.ui.graphics.RectangleShapeimport androidx.compose.ui.graphics.StrokeCapimport androidx.compose.ui.graphics.StrokeJoinimport androidx.compose.ui.graphics.asImageBitmap@@ -89,9 +90,10 @@ fun CircleToSearchScreen()// Drawing State- val paths = remember { mutableStateListOf<Pair<Path, Color>>() }- var currentPath by remember { mutableStateOf<Path?>(null) }- var currentPathPoints = remember { mutableListOf<Offset>() }+ // We use a list of points to drive the drawing for real-time updates+ val currentPathPoints = remember { mutableStateListOf<Offset>() }+ // We store completed paths if we wanted to keep them, but user asked to clear on new drag.+ // So we effectively only need one active path.// Selection Statevar selectedBitmap by remember { mutableStateOf<Bitmap?>(null) }@@ -109,306 +111,335 @@ fun CircleToSearchScreen(animationSpec = tween(1000), label = "alpha")- Box(- modifier = Modifier- .fillMaxSize()- .background(Color.Black)- .border(- width = 8.dp,- brush = Brush.verticalGradient(colors = OverlayGradientColors),- shape = RectangleShape- )- ) {- // 1. Screenshot Layer (Background)- if (screenshot != null) {- Box(- modifier = Modifier.fillMaxSize()+ // Root: BottomSheetScaffold handles the sheet layering automatically+ BottomSheetScaffold(+ scaffoldState = scaffoldState,+ sheetPeekHeight = 0.dp, // Hidden initially+ containerColor = Color.Transparent,+ sheetContainerColor = Color.Transparent,+ sheetContent = {+ // Bottom Sheet Content (Results)+ Column(+ modifier = Modifier+ .fillMaxWidth()+ .height(800.dp)+ .background(MaterialTheme.colorScheme.surface, RoundedCornerShape(topStart = 24.dp, topEnd = 24.dp))) {- Image(- bitmap = screenshot.asImageBitmap(),- contentDescription = "Screenshot",- contentScale = ContentScale.Crop,- modifier = Modifier.fillMaxSize()- )+ Spacer(modifier = Modifier.height(16.dp))- // Tint Overlay- Box(- modifier = Modifier- .fillMaxSize()- .background(- brush = Brush.verticalGradient(- colors = OverlayGradientColors.map { it.copy(alpha = 0.3f) }- )+ // Tabs+ ScrollableTabRow(+ selectedTabIndex = searchEngines.indexOf(selectedEngine),+ edgePadding = 16.dp,+ containerColor = Color.Transparent,+ contentColor = MaterialTheme.colorScheme.onSurface,+ indicator = {}+ ) {+ searchEngines.forEach { engine ->+ val selected = selectedEngine == engine+ Tab(+ selected = selected,+ onClick = { selectedEngine = engine },+ text = {+ Text(+ engine.name,+ style = MaterialTheme.typography.labelLarge,+ modifier = Modifier+ .background(+ if (selected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant,+ RoundedCornerShape(16.dp)+ )+ .padding(horizontal = 12.dp, vertical = 6.dp),+ color = if (selected) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onSurfaceVariant+ )+ })+ }+ }++ // Web View Content+ AndroidView(+ factory = { ctx ->+ WebView(ctx).apply {+ webViewClient = WebViewClient()+ settings.javaScriptEnabled = true+ loadUrl(selectedEngine.queryUrl + "test")+ }+ },+ update = { view ->+ view.loadUrl(selectedEngine.queryUrl + "test")+ },+ modifier = Modifier.fillMaxSize())}}-- // 2. Bottom Sheet (Results)- BottomSheetScaffold(- scaffoldState = scaffoldState,- sheetPeekHeight = 0.dp, // Hidden initially, triggered by selection- containerColor = Color.Transparent,- sheetContainerColor = Color.Transparent,- sheetContent = {- Column(- modifier = Modifier- .fillMaxWidth()- .height(800.dp)- .background(Color(0xFF131314), RoundedCornerShape(topStart = 24.dp, topEnd = 24.dp))+ ) { paddingValues ->+ // Main Content (Screenshot + Overlay + Drawing)+ Box(+ modifier = Modifier+ .fillMaxSize()+ .padding(paddingValues)+ .background(Color.Black)+ ) {+ // 1. Screenshot Layer+ if (screenshot != null) {+ Box(+ modifier = Modifier.fillMaxSize()) {- Spacer(modifier = Modifier.height(16.dp)) // Spacing from top of sheet+ Image(+ bitmap = screenshot.asImageBitmap(),+ contentDescription = "Screenshot",+ contentScale = ContentScale.Crop,+ modifier = Modifier.fillMaxSize()+ )- // Tabs- ScrollableTabRow(- selectedTabIndex = searchEngines.indexOf(selectedEngine),- edgePadding = 16.dp,- containerColor = Color.Transparent,- contentColor = Color.White,- indicator = {}- ) {- searchEngines.forEach { engine ->- Tab(- selected = selectedEngine == engine,- onClick = { selectedEngine = engine },- text = {- Text(- engine.name,- style = MaterialTheme.typography.labelLarge,- modifier = Modifier- .background(- if (selectedEngine == engine) Color.White else Color.Transparent,- RoundedCornerShape(16.dp)- )- .padding(horizontal = 12.dp, vertical = 6.dp),- color = if (selectedEngine == engine) Color.Black else Color.White- )- }+ // Tint Overlay+ Box(+ modifier = Modifier+ .fillMaxSize()+ .background(+ brush = Brush.verticalGradient(+ colors = OverlayGradientColors.map { it.copy(alpha = 0.3f) }+ ))- }- }-- // Web View Content- AndroidView(- factory = { ctx ->- WebView(ctx).apply {- webViewClient = WebViewClient()- settings.javaScriptEnabled = true- loadUrl(selectedEngine.queryUrl + "test")- }- },- update = { view ->- view.loadUrl(selectedEngine.queryUrl + "test")- },- modifier = Modifier.fillMaxSize())}}- ) { paddingValues ->++ // 2. Gradient Border Layer (Overlaying screenshot, clipped to rounded corners)Box(modifier = Modifier.fillMaxSize()- .padding(paddingValues)- ) {- // 3. Drawing Canvas (Interactive Layer)- Canvas(- modifier = Modifier- .fillMaxSize()- .pointerInput(Unit) {- detectDragGestures(- onDragStart = { offset ->- paths.clear()- currentPath = Path().apply { moveTo(offset.x, offset.y) }- currentPathPoints.clear()- currentPathPoints.add(offset)- selectionRect = null- scope.launch { selectionAnim.snapTo(0f) }- },- onDrag = { change, _ ->- val offset = change.position- currentPath?.lineTo(offset.x, offset.y)- currentPathPoints.add(offset)- },- onDragEnd = {- currentPath?.let {- paths.add(it to Color.White)- if (currentPathPoints.isNotEmpty()) {- var minX = Float.MAX_VALUE- var minY = Float.MAX_VALUE- var maxX = Float.MIN_VALUE- var maxY = Float.MIN_VALUE+ .border(+ width = 8.dp,+ brush = Brush.verticalGradient(colors = OverlayGradientColors),+ shape = RoundedCornerShape(24.dp) // Rounded corners for device+ )+ .clip(RoundedCornerShape(24.dp))+ )- currentPathPoints.forEach { p ->- minX = min(minX, p.x)- minY = min(minY, p.y)- maxX = max(maxX, p.x)- maxY = max(maxY, p.y)- }-- val rect = Rect(- minX.toInt(),- minY.toInt(),- maxX.toInt(),- maxY.toInt()- )-- selectionRect = rect- scope.launch {- selectionAnim.animateTo(- targetValue = 1f,- animationSpec = tween(600)- )- selectedBitmap = ImageUtils.cropBitmap(screenshot!!, rect)- isSearching = true- scaffoldState.bottomSheetState.expand()- }- }+ // 3. Drawing Canvas (Interactive Layer)+ Canvas(+ modifier = Modifier+ .fillMaxSize()+ .pointerInput(Unit) {+ detectDragGestures(+ onDragStart = { offset ->+ // Clear previous state+ currentPathPoints.clear()+ currentPathPoints.add(offset)+ selectionRect = null+ scope.launch { selectionAnim.snapTo(0f) }+ },+ onDrag = { change, _ ->+ val offset = change.position+ currentPathPoints.add(offset)+ },+ onDragEnd = {+ if (currentPathPoints.isNotEmpty()) {+ var minX = Float.MAX_VALUE+ var minY = Float.MAX_VALUE+ var maxX = Float.MIN_VALUE+ var maxY = Float.MIN_VALUE++ currentPathPoints.forEach { p ->+ minX = min(minX, p.x)+ minY = min(minY, p.y)+ maxX = max(maxX, p.x)+ maxY = max(maxY, p.y)+ }++ val rect = Rect(+ minX.toInt(),+ minY.toInt(),+ maxX.toInt(),+ maxY.toInt()+ )++ selectionRect = rect+ // Clear points to remove the drawn line and show the lens rect+ currentPathPoints.clear()++ scope.launch {+ selectionAnim.animateTo(+ targetValue = 1f,+ animationSpec = tween(600)+ )+ selectedBitmap = ImageUtils.cropBitmap(screenshot!!, rect)+ isSearching = true+ scaffoldState.bottomSheetState.expand()}- currentPath = null- currentPathPoints = mutableListOf()}- )- }- ) {- paths.forEach { (path, _) ->- drawPath(- path = path,- brush = Brush.linearGradient(OverlayGradientColors),- style = Stroke(width = 25f, cap = StrokeCap.Round, join = StrokeJoin.Round),- alpha = 0.6f- )- drawPath(- path = path,- color = Color.White,- style = Stroke(width = 12f, cap = StrokeCap.Round, join = StrokeJoin.Round)+ })}- currentPath?.let {- drawPath(- path = it,- brush = Brush.linearGradient(OverlayGradientColors),- style = Stroke(width = 25f, cap = StrokeCap.Round, join = StrokeJoin.Round),- alpha = 0.6f- )- drawPath(- path = it,- color = Color.White,- style = Stroke(width = 12f, cap = StrokeCap.Round, join = StrokeJoin.Round)- )+ ) {+ // Draw current path (Real-time)+ if (currentPathPoints.size > 1) {+ val path = Path().apply {+ moveTo(currentPathPoints.first().x, currentPathPoints.first().y)+ for (i in 1 until currentPathPoints.size) {+ lineTo(currentPathPoints[i].x, currentPathPoints[i].y)+ }}- if (selectionRect != null && selectionAnim.value > 0f) {- val rect = selectionRect!!- val progress = selectionAnim.value- val left = rect.left.toFloat()- val top = rect.top.toFloat()- val right = rect.right.toFloat()- val bottom = rect.bottom.toFloat()-- drawRoundRect(- brush = Brush.linearGradient(OverlayGradientColors),- topLeft = Offset(left, top),- size = androidx.compose.ui.geometry.Size(right - left, bottom - top),- cornerRadius = androidx.compose.ui.geometry.CornerRadius(32f * progress),- style = Stroke(width = 8f),- alpha = progress- )-- drawRoundRect(- color = Color.White,- topLeft = Offset(left, top),- size = androidx.compose.ui.geometry.Size(right - left, bottom - top),- cornerRadius = androidx.compose.ui.geometry.CornerRadius(32f * progress),- style = Stroke(width = 4f),- alpha = (1f - progress) * 0.8f- )+ // Glow+ drawPath(+ path = path,+ brush = Brush.linearGradient(OverlayGradientColors),+ style = Stroke(width = 30f, cap = StrokeCap.Round, join = StrokeJoin.Round),+ alpha = 0.6f+ )+ // Core+ drawPath(+ path = path,+ color = Color.White,+ style = Stroke(width = 12f, cap = StrokeCap.Round, join = StrokeJoin.Round)+ )+ }++ // Draw Lens Animation (Corner Brackets)+ if (selectionRect != null && selectionAnim.value > 0f) {+ val rect = selectionRect!!+ val progress = selectionAnim.value+ val left = rect.left.toFloat()+ val top = rect.top.toFloat()+ val right = rect.right.toFloat()+ val bottom = rect.bottom.toFloat()++ val width = right - left+ val height = bottom - top+ val cornerLength = min(width, height) * 0.2f // Length of the bracket arms++ // Top Left+ val tlPath = Path().apply {+ moveTo(left, top + cornerLength)+ lineTo(left, top)+ lineTo(left + cornerLength, top)}+ // Top Right+ val trPath = Path().apply {+ moveTo(right - cornerLength, top)+ lineTo(right, top)+ lineTo(right, top + cornerLength)+ }+ // Bottom Right+ val brPath = Path().apply {+ moveTo(right, bottom - cornerLength)+ lineTo(right, bottom)+ lineTo(right - cornerLength, bottom)+ }+ // Bottom Left+ val blPath = Path().apply {+ moveTo(left + cornerLength, bottom)+ lineTo(left, bottom)+ lineTo(left, bottom - cornerLength)+ }++ val bracketAlpha = progress+ val bracketStroke = Stroke(width = 12f, cap = StrokeCap.Round, join = StrokeJoin.Round)++ // Draw Brackets with Glow+ listOf(tlPath, trPath, brPath, blPath).forEach { p ->+ drawPath(p, Color.White, style = bracketStroke, alpha = bracketAlpha)+ drawPath(p, Brush.linearGradient(OverlayGradientColors), style = Stroke(width = 20f, cap = StrokeCap.Round), alpha = bracketAlpha * 0.5f)+ }++ // Optional: Flash effect inside+ drawRoundRect(+ color = Color.White,+ topLeft = Offset(left, top),+ size = Size(width, height),+ cornerRadius = CornerRadius(16f),+ style = Stroke(width = 4f),+ alpha = (1f - progress) * 0.5f+ )}}- }- // 4. Header (Top)- Row(- modifier = Modifier- .fillMaxWidth()- .padding(top = 48.dp, start = 16.dp, end = 16.dp)- .align(Alignment.TopCenter),- verticalAlignment = Alignment.CenterVertically- ) {- IconButton(- onClick = onClose,+ // 4. Header (Top)+ Row(modifier = Modifier- .background(Color.Gray.copy(alpha = 0.5f), CircleShape)- .size(40.dp)+ .fillMaxWidth()+ .padding(top = 48.dp, start = 16.dp, end = 16.dp)+ .align(Alignment.TopCenter),+ verticalAlignment = Alignment.CenterVertically) {- Icon(Icons.Default.Close, contentDescription = "Close", tint = Color.White)- }- Spacer(modifier = Modifier.weight(1f))- Text(- text = "Google",- style = MaterialTheme.typography.headlineMedium.copy(- fontWeight = FontWeight.Bold,- color = Color.White+ IconButton(+ onClick = onClose,+ modifier = Modifier+ .background(Color.Gray.copy(alpha = 0.5f), CircleShape)+ .size(40.dp)+ ) {+ Icon(Icons.Default.Close, contentDescription = "Close", tint = Color.White)+ }+ Spacer(modifier = Modifier.weight(1f))+ Text(+ text = "Google",+ style = MaterialTheme.typography.headlineMedium.copy(+ fontWeight = FontWeight.Bold,+ color = Color.White+ ))- )- Spacer(modifier = Modifier.weight(1f))- IconButton(- onClick = { /* Menu */ },- modifier = Modifier- .background(Color.Gray.copy(alpha = 0.5f), CircleShape)- .size(40.dp)- ) {- Icon(Icons.Default.MoreVert, contentDescription = "Menu", tint = Color.White)+ Spacer(modifier = Modifier.weight(1f))+ IconButton(+ onClick = { /* Menu */ },+ modifier = Modifier+ .background(Color.Gray.copy(alpha = 0.5f), CircleShape)+ .size(40.dp)+ ) {+ Icon(Icons.Default.MoreVert, contentDescription = "Menu", tint = Color.White)+ }}- }- // 5. Search Bar / Pill (Bottom Fixed)- Box(- modifier = Modifier- .align(Alignment.BottomCenter)- .fillMaxWidth()- .padding(horizontal = 16.dp, vertical = 24.dp)- .shadow(8.dp, CircleShape)- .background(Color(0xFF1F1F1F), CircleShape)- .height(64.dp)- .padding(horizontal = 20.dp)- ) {- Row(- modifier = Modifier.align(Alignment.CenterStart),- verticalAlignment = Alignment.CenterVertically+ // 5. Search Bar / Pill (Bottom Fixed)+ Box(+ modifier = Modifier+ .align(Alignment.BottomCenter)+ .fillMaxWidth()+ .padding(horizontal = 16.dp, vertical = 24.dp)+ .shadow(8.dp, CircleShape)+ .background(Color(0xFF1F1F1F), CircleShape)+ .height(64.dp)+ .padding(horizontal = 20.dp)) {- if (selectedBitmap != null) {- Image(- bitmap = selectedBitmap!!.asImageBitmap(),- contentDescription = "Selected",- contentScale = ContentScale.Crop,- modifier = Modifier- .size(40.dp)- .clip(RoundedCornerShape(12.dp))- .border(1.dp, Color.Gray, RoundedCornerShape(12.dp))- )- } else {- // G Logo- Row {- Text("G", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF4285F4)))- Text("o", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFEA4335)))- Text("o", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFFBBC05)))- Text("g", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF4285F4)))- Text("l", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF34A853)))- Text("e", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFEA4335)))+ Row(+ modifier = Modifier.align(Alignment.CenterStart),+ verticalAlignment = Alignment.CenterVertically+ ) {+ if (selectedBitmap != null) {+ Image(+ bitmap = selectedBitmap!!.asImageBitmap(),+ contentDescription = "Selected",+ contentScale = ContentScale.Crop,+ modifier = Modifier+ .size(40.dp)+ .clip(RoundedCornerShape(12.dp))+ .border(1.dp, Color.Gray, RoundedCornerShape(12.dp))+ )+ } else {+ // G Logo+ Row {+ Text("G", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF4285F4)))+ Text("o", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFEA4335)))+ Text("o", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFFBBC05)))+ Text("g", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF4285F4)))+ Text("l", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFF34A853)))+ Text("e", style = MaterialTheme.typography.headlineMedium.copy(fontWeight = FontWeight.Bold, color = Color(0xFFEA4335)))+ }}}- }-- Row(- modifier = Modifier.align(Alignment.CenterEnd),- verticalAlignment = Alignment.CenterVertically- ) {- Icon(Icons.Default.Mic, "Mic", tint = Color.White)- Spacer(modifier = Modifier.width(24.dp))- Icon(Icons.Default.MusicNote, "Music", tint = Color.White)- Spacer(modifier = Modifier.width(24.dp))- Icon(Icons.Default.Translate, "Translate", tint = Color.White)++ Row(+ modifier = Modifier.align(Alignment.CenterEnd),+ verticalAlignment = Alignment.CenterVertically+ ) {+ Icon(Icons.Default.Mic, "Mic", tint = Color.White)+ Spacer(modifier = Modifier.width(24.dp))+ Icon(Icons.Default.MusicNote, "Music", tint = Color.White)+ Spacer(modifier = Modifier.width(24.dp))+ Icon(Icons.Default.Translate, "Translate", tint = Color.White)+ }}}}