Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.8 kB
6
Indexable
Never
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.input.pointer.consumeAllChanges
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.singleWindowApplication

fun main() = singleWindowApplication {
    var result by remember { mutableStateOf("") }
    val buttons = listOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "+", "-", "*", "/", "=")
    val points = remember { mutableStateListOf<Offset>() }
    var pointerOffset by remember {
        mutableStateOf(Offset(0f, 0f))
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput("dragging") {
                detectDragGestures { change, dragAmount ->
                    pointerOffset += dragAmount
                }
            }
            .onSizeChanged {
                pointerOffset = Offset(it.width / 2f, it.height / 2f)
            }
            .drawWithContent {
                drawContent()
                // draws a fully black area with a small keyhole at pointerOffset that’ll show part of the UI.
                drawRect(
                    Brush.radialGradient(
                        listOf(Color.Transparent, Color.Black),
                        center = pointerOffset,
                        radius = 100.dp.toPx(),
                    )
                )
            }
    ) {
        Row(modifier = Modifier.fillMaxSize()) {
            Column(
                modifier = Modifier.weight(1f),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(text = "Result: $result")

                Spacer(modifier = Modifier.height(16.dp))

                buttons.chunked(4).forEach { row ->
                    Row {
                        row.forEach { button ->
                            Button(onClick = {
                                when (button) {
                                    "=" -> result = try {
                                        result.split(" ").let { if (it.size < 3) "" else it[0].toInt().let { first ->
                                            it[2].toInt().let { second ->
                                                when (it[1]) {
                                                    "+" -> (first + second).toString()
                                                    "-" -> (first - second).toString()
                                                    "*" -> (first * second).toString()
                                                    "/" -> if (second != 0) (first / second).toString() else "Error"
                                                    else -> ""
                                                }
                                            }
                                        } }
                                    } catch (e: Exception) {
                                        "Error"
                                    }
                                    else -> result += if (button in listOf("+", "-", "*", "/")) " $button " else button
                                }
                            }) {
                                Text(text = button)
                            }

                            Spacer(modifier = Modifier.width(16.dp))
                        }
                    }

                    Spacer(modifier = Modifier.height(16.dp))
                }

                Button(onClick = { result = "" }) {
                    Text(text = "Clear")
                }
            }

            /*Canvas(modifier = Modifier.weight(1f).fillMaxHeight().pointerInput(Unit) {
                detectDragGestures { change, dragAmount ->
                    change.consumeAllChanges()
                    points.add(change.position)
                }
            }) {
                points.forEach { point ->
                    drawCircle(color = Color.Black, center = point, radius = 1.dp.toPx(), style = Stroke())

                }
            }*/


        }
    }

}