Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45c89cba authored by Wes Okuhara's avatar Wes Okuhara
Browse files

Desktop screenshots: Clicking button captures partial screenshot

Wires up the button in the region box to the business logic to request
taking a partial region screenshot. Also renames the button component to
not be specific to screenshots.

Bug: 417534202
Test: Manual
Flag: com.android.systemui.desktop_screen_capture
Change-Id: Ia732d9cfb82015c463e90f3e9d5175a3e579a48c
parent 3e58b7e0
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -69,14 +69,17 @@ fun PreCaptureUI(viewModel: PreCaptureViewModel) {
                    )
                }
            }

            ScreenCaptureRegion.PARTIAL -> {
                // TODO(b/427541309) Set the initial width and height of the RegionBox based on the
                // viewmodel state.
                RegionBox(
                    onRegionSelected = { rect: Rect -> viewModel.updateRegionBox(rect) },
                    drawableLoaderViewModel = viewModel,
                    onRegionSelected = { rect: Rect -> viewModel.updateRegionBox(rect) },
                    onCaptureClick = { viewModel.takePartialScreenshot() },
                )
            }

            ScreenCaptureRegion.APP_WINDOW -> {}
        }
    }
+7 −3
Original line number Diff line number Diff line
@@ -223,12 +223,14 @@ private class RegionBoxState(private val minSizePx: Float, private val touchArea
 *   user finishes a drag gesture. This rectangle is used for taking a screenshot. The rectangle is
 *   of type [android.graphics.Rect] because the screenshot API requires int values.
 * @param drawableLoaderViewModel The view model that is used to load drawables.
 * @param onCaptureClick A callback function that is invoked when the capture button is clicked.
 * @param modifier The modifier to be applied to the composable.
 */
@Composable
fun RegionBox(
    onRegionSelected: (rect: IntRect) -> Unit,
    drawableLoaderViewModel: DrawableLoaderViewModel,
    onRegionSelected: (rect: IntRect) -> Unit,
    onCaptureClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val density = LocalDensity.current
@@ -298,12 +300,14 @@ fun RegionBox(
                contentAlignment = Alignment.Center,
            ) {}

            // The screenshot button that is positioned inside or outside the region box.
            RegionScreenshotButton(
            // The button which initiates capturing the specified region of the screen. It is
            // positioned inside or outside the region box depending on the size of the region box.
            RegionBoxButton(
                boxWidthDp,
                boxHeightDp,
                currentRect,
                drawableLoaderViewModel = drawableLoaderViewModel,
                onClick = onCaptureClick,
            )
        }
    }
+8 −9
Original line number Diff line number Diff line
@@ -36,21 +36,22 @@ import com.android.systemui.screencapture.common.ui.compose.loadIcon
import com.android.systemui.screencapture.common.ui.viewmodel.DrawableLoaderViewModel

/**
 * A composable that represents the screenshot button that is positioned inside or outside the
 * region box.
 * A composable that represents the button that is positioned inside or outside the region box.
 *
 * @param boxWidthDp The width of the region box in dp.
 * @param boxHeightDp The height of the region box in dp.
 * @param currentRect The current region box.
 * @param drawableLoaderViewModel The view model that is used to load drawables.
 * @param onClick A callback function that is invoked when this button is clicked.
 * @param modifier The modifier to be applied to the composable.
 */
@Composable
fun RegionScreenshotButton(
fun RegionBoxButton(
    boxWidthDp: Dp,
    boxHeightDp: Dp,
    currentRect: Rect,
    drawableLoaderViewModel: DrawableLoaderViewModel,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    val density = LocalDensity.current
@@ -59,8 +60,8 @@ fun RegionScreenshotButton(
    val buttonWidthDp = with(density) { buttonSize.width.toDp() }
    val buttonHeightDp = with(density) { buttonSize.height.toDp() }

    // Check if the box dimensions is smaller than the screenshot button. If so, the button
    // will be positioned outside the box.
    // Check if the box dimensions is smaller than the button. If so, the button will be positioned
    // outside the box.
    val isButtonOutside = boxWidthDp < buttonWidthDp || boxHeightDp < buttonHeightDp

    // The translation of the button in the X direction.
@@ -92,20 +93,18 @@ fun RegionScreenshotButton(
    PrimaryButton(
        modifier =
            modifier
                .onSizeChanged { screenshotButtonSize -> buttonSize = screenshotButtonSize }
                .onSizeChanged { size -> buttonSize = size }
                .graphicsLayer {
                    translationX = targetTranslationX
                    translationY = targetTranslationY
                },
        text = stringResource(id = R.string.screen_capture_region_selection_button),
        onClick = {
            // TODO(b/417534202): trigger a screenshot of the selected area.
        },
        icon =
            loadIcon(
                viewModel = drawableLoaderViewModel,
                resId = R.drawable.ic_screen_capture_camera,
                contentDescription = null,
            ),
        onClick = onClick,
    )
}