-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from timhuang1018/feature/sudoku-grid-crop-hint
Feature/sudoku grid crop hint window
- Loading branch information
Showing
8 changed files
with
165 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
krop-core/src/commonMain/kotlin/io/keeppro/widget/CropHintWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.keeppro.widget | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.EnterTransition | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.layout | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
@Composable | ||
fun BoxScope.CropHintWindow( | ||
gridLineColor: Color, | ||
cropWindow: Rect, | ||
windowStateFlow: MutableStateFlow<Boolean>, | ||
) { | ||
var cropWindowVisible by remember { mutableStateOf(false) } | ||
var delayJob by remember { mutableStateOf<Job?>(null) } | ||
val scope = rememberCoroutineScope() | ||
|
||
LaunchedEffect(Unit) { | ||
windowStateFlow.collectLatest { | ||
cropWindowVisible = it | ||
if (it) { | ||
delayJob?.cancel() | ||
delayJob = scope.launch { | ||
delay(1500) | ||
withContext(Dispatchers.Main) { | ||
windowStateFlow.value = false | ||
} | ||
} | ||
} else { | ||
delayJob?.cancel() | ||
delayJob = null | ||
} | ||
} | ||
} | ||
|
||
|
||
AnimatedVisibility( | ||
visible = cropWindowVisible, | ||
enter = EnterTransition.None, | ||
exit = fadeOut() | ||
) { | ||
|
||
SudokuGrid( | ||
gridLineColor = gridLineColor, | ||
modifier = Modifier.layout { measurable, constraints -> | ||
val placeable = measurable.measure( | ||
constraints = constraints.copy( | ||
maxWidth = cropWindow.width.toInt(), | ||
maxHeight = cropWindow.height.toInt(), | ||
minWidth = cropWindow.width.toInt(), | ||
minHeight = cropWindow.height.toInt() | ||
) | ||
) | ||
layout( | ||
width = cropWindow.width.toInt(), | ||
height = cropWindow.height.toInt() | ||
) { | ||
placeable.place( | ||
(-cropWindow.topLeft.x.toInt()), | ||
(-cropWindow.topLeft.y.toInt()) | ||
) | ||
} | ||
}) | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
krop-core/src/commonMain/kotlin/io/keeppro/widget/SudokuGrid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.keeppro.widget | ||
|
||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun BoxScope.SudokuGrid(gridLineColor: Color, modifier: Modifier = Modifier) { | ||
Canvas(modifier = modifier) { | ||
val gridSize = 3 // For a 3x3 grid | ||
val lineThickness = 2.dp.toPx() | ||
val cellSizeX = size.width / gridSize | ||
val cellSizeY = size.height / gridSize | ||
|
||
// Draw the grid lines | ||
for (i in 1 until gridSize) { | ||
val x = i * cellSizeX | ||
val y = i * cellSizeY | ||
if (i < gridSize) { | ||
drawLine( | ||
color = gridLineColor, | ||
start = Offset(x, 0f), | ||
end = Offset(x, size.height), | ||
strokeWidth = lineThickness | ||
) | ||
} | ||
drawLine( | ||
color = gridLineColor, | ||
start = Offset(0f, y), | ||
end = Offset(size.width, y), | ||
strokeWidth = lineThickness | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters